Kekkai/shared/database/src/main.js

47 lines
1.5 KiB
JavaScript
Raw Normal View History

const pool = require('./postgresql.js');
const logger = require('../../logger/src/main.js');
async function getDay(from_currency, conv_currency, date) {
2024-08-17 16:40:20 +03:00
if (!from_currency || !conv_currency)
return new Error('fromCurrency and convCurrency are required');
2024-08-14 21:20:08 +03:00
else if (!date) return new Error('date is required');
2024-08-17 16:40:20 +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],
);
if (data?.['rows'].length <= 0) return 'Missing data';
2024-08-14 21:20:08 +03:00
logger.debug(data['rows'][0]);
2024-08-11 17:22:13 +03:00
return data['rows'][0];
}
async function getPeriod(from_currency, conv_currency, start_date, end_date) {
2024-08-17 16:40:20 +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-08-17 16:40:20 +03:00
const data = await pool.query(
'SELECT * FROM currency WHERE ' +
'(date BETWEEN $3 AND $4) AND from_currency = $1 AND conv_currency = $2 ORDER BY date',
[
from_currency.toUpperCase(),
conv_currency.toUpperCase(),
start_date,
2024-08-17 16:40:20 +03:00
end_date,
],
);
if (data?.['rows'].length <= 0) return 'Missing data';
2024-08-14 21:20:08 +03:00
logger.debug(data['rows']);
2024-08-11 17:22:13 +03:00
return data['rows'];
}
2024-08-17 16:40:20 +03:00
module.exports = { getDay, getPeriod };