2024-02-14 15:11:24 +03:00
|
|
|
const fastify = require('fastify')({ logger: true });
|
2024-02-18 22:59:54 +03:00
|
|
|
const schedule = require('node-schedule');
|
|
|
|
const pool = require("./postgresql.js");
|
2024-02-19 17:18:32 +03:00
|
|
|
const yaml = require("yaml")
|
|
|
|
const fs = require("fs");
|
|
|
|
const config = yaml.parse(fs.readFileSync("./config.yaml", "utf-8"));
|
|
|
|
|
2024-02-18 22:59:54 +03:00
|
|
|
const saveRate = require('./utils/saveRate.js');
|
|
|
|
const response = require('./utils/errorResponse');
|
2024-02-14 15:11:24 +03:00
|
|
|
|
2024-03-14 11:18:28 +03:00
|
|
|
saveRate();
|
2024-02-18 22:59:54 +03:00
|
|
|
schedule.scheduleJob('30 8 * * *', async function () {
|
|
|
|
console.log('I save the currency data');
|
|
|
|
await saveRate();
|
|
|
|
});
|
|
|
|
|
2024-02-21 21:20:37 +03:00
|
|
|
fastify.get('/api/getRate/', async function (req) {
|
2024-02-20 20:37:48 +03:00
|
|
|
if (!req['query']?.['fromCurrency'] || !req['query']?.['convCurrency']) return response(
|
2024-02-18 22:59:54 +03:00
|
|
|
'error',
|
2024-02-21 20:26:49 +03:00
|
|
|
400,
|
2024-02-20 20:37:48 +03:00
|
|
|
'fromCurrency and convCurrency parameter is required'
|
2024-02-18 22:59:54 +03:00
|
|
|
);
|
|
|
|
|
2024-02-19 17:18:32 +03:00
|
|
|
if (!req['query']?.['periodStart']) return response(
|
|
|
|
'error',
|
2024-02-21 20:26:49 +03:00
|
|
|
400,
|
2024-02-19 17:18:32 +03:00
|
|
|
'periodStart parameter is required'
|
|
|
|
);
|
2024-02-18 22:59:54 +03:00
|
|
|
|
2024-02-20 20:37:48 +03:00
|
|
|
let data = await pool.query('SELECT * FROM currency WHERE from_currency = $1 AND conv_currency = $2 AND date = $3', [
|
|
|
|
req['query']['fromCurrency'],
|
|
|
|
req['query']['convCurrency'],
|
2024-02-19 17:18:32 +03:00
|
|
|
req['query']['periodStart'],
|
2024-02-18 22:59:54 +03:00
|
|
|
]).then(response('error', 500, 'Internal Server Error'));
|
|
|
|
|
2024-02-19 17:18:32 +03:00
|
|
|
if (!data['rows']?.[0]) return response(
|
|
|
|
'error',
|
|
|
|
204,
|
|
|
|
'There is no data for this time'
|
|
|
|
);
|
2024-02-18 22:59:54 +03:00
|
|
|
|
2024-02-19 19:05:06 +03:00
|
|
|
if (req['query']?.['periodEnd']) {
|
2024-02-20 20:37:48 +03:00
|
|
|
let data = await pool.query('SELECT * FROM currency WHERE (date BETWEEN $3 AND $4) AND from_currency = $1 AND conv_currency = $2', [
|
|
|
|
req['query']['fromCurrency'],
|
|
|
|
req['query']['convCurrency'],
|
2024-02-19 19:05:06 +03:00
|
|
|
req['query']['periodStart'],
|
|
|
|
req['query']['periodEnd'],
|
|
|
|
]);
|
|
|
|
|
|
|
|
return data['rows'];
|
|
|
|
}
|
|
|
|
|
2024-02-20 20:37:48 +03:00
|
|
|
return data['rows'][0];
|
2024-02-18 22:59:54 +03:00
|
|
|
});
|
|
|
|
|
2024-02-19 17:18:32 +03:00
|
|
|
fastify.listen({
|
|
|
|
host: config['server']['host'],
|
2024-02-21 20:26:49 +03:00
|
|
|
port: config['server']['port'],
|
2024-02-19 17:18:32 +03:00
|
|
|
}, err => {
|
2024-02-18 22:59:54 +03:00
|
|
|
if (err) throw err
|
|
|
|
console.log(`server listening on ${fastify.server.address().port}`)
|
2024-02-19 17:18:32 +03:00
|
|
|
})
|