2024-07-30 18:01:35 +03:00
|
|
|
const pool = require('./postgresql.js');
|
2024-08-11 17:22:13 +03:00
|
|
|
const logger = require('../logger/main.js');
|
2024-08-04 13:55:37 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Getting the currency exchange rate for a specific day
|
|
|
|
* @param from_currency {String}
|
|
|
|
* @param conv_currency {String}
|
|
|
|
* @param date
|
|
|
|
* @returns {Promise<*|Error>}
|
|
|
|
*/
|
|
|
|
|
2024-07-30 18:01:35 +03:00
|
|
|
async function getDay(from_currency, conv_currency, date) {
|
2024-07-31 14:45:12 +03:00
|
|
|
if (!from_currency || !conv_currency) return new Error('fromCurrency and convCurrency are required');
|
|
|
|
else if (!date) return new Error('date is required')
|
2024-07-30 18:01:35 +03:00
|
|
|
|
|
|
|
const data = await pool.query('SELECT from_currency, conv_currency, date, rate FROM currency ' +
|
|
|
|
'WHERE from_currency = $1 AND conv_currency = $2 AND date = $3', [
|
|
|
|
from_currency.toUpperCase(),
|
|
|
|
conv_currency.toUpperCase(),
|
|
|
|
date
|
|
|
|
]);
|
|
|
|
|
2024-07-31 14:45:12 +03:00
|
|
|
if (!data) return new Error('Missing data');
|
2024-07-30 18:01:35 +03:00
|
|
|
|
2024-08-07 00:50:50 +03:00
|
|
|
let set_date = data['rows'][0]['date']
|
|
|
|
set_date = new Date(set_date.setDate(set_date.getDate() + 1));
|
|
|
|
|
2024-08-11 17:22:13 +03:00
|
|
|
logger.debug(data['rows'][0])
|
|
|
|
|
2024-07-30 18:01:35 +03:00
|
|
|
return data['rows'][0];
|
|
|
|
}
|
|
|
|
|
2024-08-04 13:55:37 +03:00
|
|
|
/**
|
|
|
|
* Getting the exchange rate for a certain period
|
|
|
|
* @param {String} from_currency - The currency that is being converted
|
|
|
|
* @param {String} conv_currency - The currency to be converted into
|
|
|
|
* @param {String} start_date - Start date of the period
|
|
|
|
* @param {String} end_date - End date of the period
|
|
|
|
* @returns {Promise<*|Error>}
|
|
|
|
*/
|
|
|
|
|
2024-07-30 18:01:35 +03:00
|
|
|
async function getPeriod(from_currency, conv_currency, start_date, end_date) {
|
2024-07-31 14:45:12 +03:00
|
|
|
if (!from_currency || !conv_currency) return new Error('from_currency and conv_currency are required');
|
|
|
|
else if(!start_date || !end_date) return new Error('start_date and end_date are required')
|
2024-07-30 18:01:35 +03:00
|
|
|
|
|
|
|
const data = await pool.query('SELECT * FROM currency WHERE ' +
|
2024-08-07 00:50:50 +03:00
|
|
|
'(date BETWEEN $3 AND $4) AND from_currency = $1 AND conv_currency = $2 ORDER BY date', [
|
2024-07-31 14:45:12 +03:00
|
|
|
from_currency.toUpperCase(),
|
|
|
|
conv_currency.toUpperCase(),
|
2024-07-30 18:01:35 +03:00
|
|
|
start_date,
|
|
|
|
end_date
|
|
|
|
]);
|
|
|
|
|
2024-07-31 14:45:12 +03:00
|
|
|
if (!data) return new Error('Missing data');
|
2024-07-30 18:01:35 +03:00
|
|
|
|
2024-08-07 00:50:50 +03:00
|
|
|
for (let i = 0; i < data['rows'].length; i++) {
|
|
|
|
let date = data['rows'][i]['date']
|
|
|
|
date = new Date(date.setDate(date.getDate() + 1));
|
|
|
|
}
|
|
|
|
|
2024-08-11 17:22:13 +03:00
|
|
|
logger.debug(data['rows'])
|
|
|
|
|
2024-07-30 18:01:35 +03:00
|
|
|
return data['rows'];
|
|
|
|
}
|
|
|
|
|
2024-07-31 14:45:12 +03:00
|
|
|
module.exports = { getDay, getPeriod };
|