From aea5cd13a0d7c4cd7eeb6dd611f960713d0724d5 Mon Sep 17 00:00:00 2001 From: Redume Date: Tue, 20 Feb 2024 20:37:48 +0300 Subject: [PATCH] =?UTF-8?q?=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=20=D0=BD=D0=BE=D0=B2=D1=8B=D0=B9=20query=20=D0=BF=D0=B0?= =?UTF-8?q?=D1=80=D0=B0=D0=BC=D0=B5=D1=82=D1=80,=20=D1=82=D0=B5=D0=BF?= =?UTF-8?q?=D0=B5=D1=80=D1=8C=20=D1=87=D0=B5=D1=82=D0=BA=D0=BE=20=D0=B2?= =?UTF-8?q?=D1=8B=D0=B2=D0=BE=D0=B4=D0=B8=D1=82=D1=81=D1=8F=20=D0=B4=D0=B0?= =?UTF-8?q?=D0=BD=D0=BD=D1=8B=D0=B5=20=D0=BF=D0=BE=20=D0=BE=D1=82=D0=B4?= =?UTF-8?q?=D0=B5=D0=BB=D1=8C=D0=BD=D0=BE=D0=B9=20=D0=BF=D0=B0=D1=80=D0=B5?= =?UTF-8?q?,=20=D0=B0=20=D0=BD=D0=B5=20=D0=BF=D0=BE=20=D0=B2=D1=81=D0=B5?= =?UTF-8?q?=D0=BC=20=D0=B2=D0=BE=D0=B7=D0=BC=D0=BE=D0=B6=D0=BD=D1=8B=D0=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.js | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/main.js b/main.js index b7656e3..3d51a71 100644 --- a/main.js +++ b/main.js @@ -8,16 +8,17 @@ const config = yaml.parse(fs.readFileSync("./config.yaml", "utf-8")); const saveRate = require('./utils/saveRate.js'); const response = require('./utils/errorResponse'); +saveRate() schedule.scheduleJob('30 8 * * *', async function () { console.log('I save the currency data'); await saveRate(); }); fastify.get('/api/getRate/', async function (req, reply) { - if (!req['query']?.['codeCurrency']) return response( + if (!req['query']?.['fromCurrency'] || !req['query']?.['convCurrency']) return response( 400, 'error', - 'codeCurrency parameter is required' + 'fromCurrency and convCurrency parameter is required' ); if (!req['query']?.['periodStart']) return response( @@ -26,8 +27,9 @@ fastify.get('/api/getRate/', async function (req, reply) { 'periodStart parameter is required' ); - let data = await pool.query('SELECT * FROM currency WHERE from_currency = $1 AND date = $2', [ - req['query']['codeCurrency'], + 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'], req['query']['periodStart'], ]).then(response('error', 500, 'Internal Server Error')); @@ -38,8 +40,9 @@ fastify.get('/api/getRate/', async function (req, reply) { ); if (req['query']?.['periodEnd']) { - let data = await pool.query('SELECT * FROM currency WHERE (date BETWEEN $2 AND $3) AND from_currency = $1', [ - req['query']['codeCurrency'], + 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'], req['query']['periodStart'], req['query']['periodEnd'], ]); @@ -47,7 +50,7 @@ fastify.get('/api/getRate/', async function (req, reply) { return data['rows']; } - return data['rows']; + return data['rows'][0]; }); fastify.listen({