From cf813568d49b3eab5ab8b90f50dee496da4ab6fd Mon Sep 17 00:00:00 2001 From: Vladimir Voronkov Date: Thu, 7 Aug 2025 15:09:26 +0300 Subject: [PATCH 01/11] Optimize data validator init flow for fw-reports integration --- workers/loc.api/data-validator/index.js | 58 +++++++++++++++---------- workers/loc.api/di/core.deps.js | 2 +- 2 files changed, 36 insertions(+), 24 deletions(-) diff --git a/workers/loc.api/data-validator/index.js b/workers/loc.api/data-validator/index.js index 2ee6ea52..2fc61b5a 100644 --- a/workers/loc.api/data-validator/index.js +++ b/workers/loc.api/data-validator/index.js @@ -22,27 +22,33 @@ const FILTER_SCHEMA_IDS = require('./filter.schema.ids') const schemas = require('./schemas') const filterSchemas = require('./filter-schemas') -const ajv = new Ajv({ - // Compile schema on initialization - schemas: [ - ...Object.values(schemas), - ...Object.values(filterSchemas) - ], - - // Strict mode - strict: true, - strictRequired: true, - allowMatchingProperties: true, - allowUnionTypes: true, - - $data: true, - ownProperties: true, - allErrors: true, - messages: true, - formats: { reserved: true }, - verbose: isDevEnv -}) -addFormats(ajv) +let ajv + +const init = () => { + ajv = new Ajv({ + // Compile schema on initialization + schemas: [ + ...Object.values(schemas), + ...Object.values(filterSchemas) + ], + + // Strict mode + strict: true, + strictRequired: true, + allowMatchingProperties: true, + allowUnionTypes: true, + + $data: true, + ownProperties: true, + allErrors: true, + messages: true, + formats: { reserved: true }, + verbose: isDevEnv + }) + addFormats(ajv) + + return module.exports +} const addSchemas = (schemas = []) => { const _schemas = Array.isArray(schemas) @@ -104,7 +110,7 @@ const reinit = (args) => { schemaIds, filterSchemaNames, filterSchemaIds, - schemas = [] + schemas = {} } = args ?? {} Object.assign(SCHEMA_NAMES, schemaNames) @@ -112,7 +118,9 @@ const reinit = (args) => { Object.assign(FILTER_SCHEMA_NAMES, filterSchemaNames) Object.assign(FILTER_SCHEMA_IDS, filterSchemaIds) - addSchemas(schemas) + addSchemas(Object.values(schemas)) + + return module.exports } module.exports = { @@ -122,6 +130,10 @@ module.exports = { FILTER_SCHEMA_NAMES, FILTER_SCHEMA_IDS, + schemas, + filterSchemas, + + init, reinit, addSchemas, validate diff --git a/workers/loc.api/di/core.deps.js b/workers/loc.api/di/core.deps.js index db01e483..eaa52ffb 100644 --- a/workers/loc.api/di/core.deps.js +++ b/workers/loc.api/di/core.deps.js @@ -17,6 +17,6 @@ module.exports = () => { [TYPES.CONF] ) }).inSingletonScope() - bind(TYPES.DataValidator).toConstantValue(dataValidator) + bind(TYPES.DataValidator).toConstantValue(dataValidator.init()) }) } From 6028ac7474360b85f020c426f384c0f8d1fe9b59 Mon Sep 17 00:00:00 2001 From: Vladimir Voronkov Date: Fri, 8 Aug 2025 10:02:24 +0300 Subject: [PATCH 02/11] Simplify filter-schemas import --- workers/loc.api/data-validator/filter-schemas/index.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/workers/loc.api/data-validator/filter-schemas/index.js b/workers/loc.api/data-validator/filter-schemas/index.js index 4f82d9bc..bd3c352f 100644 --- a/workers/loc.api/data-validator/filter-schemas/index.js +++ b/workers/loc.api/data-validator/filter-schemas/index.js @@ -1,11 +1,9 @@ 'use strict' -const path = require('node:path') - const FILTER_SCHEMA_NAMES = require('../filter.schema.names') const { requireSchemas } = require('../helpers') module.exports = requireSchemas( FILTER_SCHEMA_NAMES, - path.join(__dirname, '../filter-schemas') + __dirname ) From 02bda8c79bde8698d0404f537d97a727aa38b6f6 Mon Sep 17 00:00:00 2001 From: Vladimir Voronkov Date: Fri, 8 Aug 2025 10:40:19 +0300 Subject: [PATCH 03/11] Add isLimitUnused flag to getReportFileArgs helper --- workers/loc.api/helpers/limit-param.helpers.js | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/workers/loc.api/helpers/limit-param.helpers.js b/workers/loc.api/helpers/limit-param.helpers.js index a540b9b5..e1f4b5fd 100644 --- a/workers/loc.api/helpers/limit-param.helpers.js +++ b/workers/loc.api/helpers/limit-param.helpers.js @@ -60,20 +60,29 @@ const getMethodLimit = (sendLimit, method, methodsLimits = {}) => { return getLimitNotMoreThan(base, max) } -const getReportFileArgs = (args, method, extraParams = {}) => { +const getReportFileArgs = (args, opts) => { + const { + method, + extraParams = {}, + isLimitUnused + } = opts ?? {} + const reportFileArgs = { ...args, params: { - ...args.params, + ...args?.params, ...extraParams } } - if (method === 'getWeightedAverages') { + if (isLimitUnused) { return reportFileArgs } - reportFileArgs.params.limit = getMethodLimit({ isMax: true }, method) + reportFileArgs.params.limit = getMethodLimit( + { isMax: true }, + method + ) return reportFileArgs } From 82fe09a10f357d7a3e557a2095ddc095bbd676d3 Mon Sep 17 00:00:00 2001 From: Vladimir Voronkov Date: Fri, 8 Aug 2025 10:40:39 +0300 Subject: [PATCH 04/11] Adapt report-file-job-data to new helper signature --- .../report.file.job.data.js | 88 +++++++++++++++---- 1 file changed, 71 insertions(+), 17 deletions(-) diff --git a/workers/loc.api/generate-report-file/report.file.job.data.js b/workers/loc.api/generate-report-file/report.file.job.data.js index 466cfd5f..c50cc470 100644 --- a/workers/loc.api/generate-report-file/report.file.job.data.js +++ b/workers/loc.api/generate-report-file/report.file.job.data.js @@ -50,7 +50,10 @@ class ReportFileJobData { uInfo ) - const reportFileArgs = getReportFileArgs(args, 'trades') + const reportFileArgs = getReportFileArgs( + args, + { method: 'trades' } + ) const jobData = { userInfo, @@ -97,7 +100,10 @@ class ReportFileJobData { uInfo ) - const reportFileArgs = getReportFileArgs(args, 'fundingTrades') + const reportFileArgs = getReportFileArgs( + args, + { method: 'fundingTrades' } + ) const jobData = { userInfo, @@ -143,7 +149,10 @@ class ReportFileJobData { uInfo ) - const reportFileArgs = getReportFileArgs(args, 'tickersHistory') + const reportFileArgs = getReportFileArgs( + args, + { method: 'tickersHistory' } + ) const symb = Array.isArray(args.params.symbol) ? args.params.symbol : [args.params.symbol] @@ -248,7 +257,10 @@ class ReportFileJobData { uInfo ) - const reportFileArgs = getReportFileArgs(args, 'positionsHistory') + const reportFileArgs = getReportFileArgs( + args, + { method: 'positionsHistory' } + ) const jobData = { userInfo, @@ -352,7 +364,10 @@ class ReportFileJobData { uInfo ) - const reportFileArgs = getReportFileArgs(_args, 'positionsAudit') + const reportFileArgs = getReportFileArgs( + _args, + { method: 'positionsAudit' } + ) const jobData = { userInfo, @@ -409,7 +424,13 @@ class ReportFileJobData { const isTradingPair = Array.isArray(params.symbol) ? params.symbol[0].startsWith('t') : params.symbol.startsWith('t') - const reportFileArgs = getReportFileArgs(args, 'publicTrades', { isTradingPair }) + const reportFileArgs = getReportFileArgs( + args, + { + method: 'publicTrades', + extraParams: { isTradingPair } + } + ) const columnsCsv = (isTradingPair) ? { id: '#', @@ -549,7 +570,10 @@ class ReportFileJobData { uInfo ) - const reportFileArgs = getReportFileArgs(args, 'ledgers') + const reportFileArgs = getReportFileArgs( + args, + { method: 'ledgers' } + ) const jobData = { userInfo, @@ -597,7 +621,10 @@ class ReportFileJobData { uInfo ) - const reportFileArgs = getReportFileArgs(args, 'payInvoiceList') + const reportFileArgs = getReportFileArgs( + args, + { method: 'payInvoiceList' } + ) const jobData = { userInfo, @@ -643,7 +670,10 @@ class ReportFileJobData { uInfo ) - const reportFileArgs = getReportFileArgs(args, 'orderTrades') + const reportFileArgs = getReportFileArgs( + args, + { method: 'orderTrades' } + ) const jobData = { userInfo, @@ -689,7 +719,10 @@ class ReportFileJobData { uInfo ) - const reportFileArgs = getReportFileArgs(args, 'orders') + const reportFileArgs = getReportFileArgs( + args, + { method: 'orders' } + ) const jobData = { userInfo, @@ -790,7 +823,10 @@ class ReportFileJobData { uInfo ) - const reportFileArgs = getReportFileArgs(args, 'movements') + const reportFileArgs = getReportFileArgs( + args, + { method: 'movements' } + ) const jobData = { userInfo, @@ -836,7 +872,10 @@ class ReportFileJobData { uInfo ) - const reportFileArgs = getReportFileArgs(args, 'fundingOfferHistory') + const reportFileArgs = getReportFileArgs( + args, + { method: 'fundingOfferHistory' } + ) const jobData = { userInfo, @@ -885,7 +924,10 @@ class ReportFileJobData { uInfo ) - const reportFileArgs = getReportFileArgs(args, 'fundingLoanHistory') + const reportFileArgs = getReportFileArgs( + args, + { method: 'fundingLoanHistory' } + ) const jobData = { userInfo, @@ -936,7 +978,10 @@ class ReportFileJobData { uInfo ) - const reportFileArgs = getReportFileArgs(args, 'fundingCreditHistory') + const reportFileArgs = getReportFileArgs( + args, + { method: 'fundingCreditHistory' } + ) const jobData = { userInfo, @@ -988,7 +1033,10 @@ class ReportFileJobData { uInfo ) - const reportFileArgs = getReportFileArgs(args, 'logins') + const reportFileArgs = getReportFileArgs( + args, + { method: 'logins' } + ) const jobData = { userInfo, @@ -1028,7 +1076,10 @@ class ReportFileJobData { uInfo ) - const reportFileArgs = getReportFileArgs(args, 'changeLogs') + const reportFileArgs = getReportFileArgs( + args, + { method: 'changeLogs' } + ) const jobData = { userInfo, @@ -1138,7 +1189,10 @@ class ReportFileJobData { uInfo ) - const reportFileArgs = getReportFileArgs(args, 'getWeightedAverages') + const reportFileArgs = getReportFileArgs( + args, + { isLimitUnused: true } + ) const jobData = { userInfo, From daf20ae2de03977eed932eaef57b060f05a7babd Mon Sep 17 00:00:00 2001 From: Vladimir Voronkov Date: Mon, 11 Aug 2025 15:17:21 +0300 Subject: [PATCH 05/11] Fix currencies getter interrupter --- workers/loc.api/service.report.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/workers/loc.api/service.report.js b/workers/loc.api/service.report.js index 665a4762..99a4b154 100644 --- a/workers/loc.api/service.report.js +++ b/workers/loc.api/service.report.js @@ -73,8 +73,10 @@ class ReportService extends Api { return rest.futures() } - _getCurrencies () { - const rest = this._getREST({}) + _getCurrencies (args) { + const rest = this._getREST({}, { + interrupter: args?.interrupter + }) return rest.currencies() } From 425e1121e5b7b51a5dc1d247180dff57767be0cc Mon Sep 17 00:00:00 2001 From: Vladimir Voronkov Date: Wed, 13 Aug 2025 10:28:14 +0300 Subject: [PATCH 06/11] Add candle timeframe validation schema def --- workers/loc.api/data-validator/schemas/defs.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/workers/loc.api/data-validator/schemas/defs.js b/workers/loc.api/data-validator/schemas/defs.js index 4d9b4539..d23518ed 100644 --- a/workers/loc.api/data-validator/schemas/defs.js +++ b/workers/loc.api/data-validator/schemas/defs.js @@ -151,6 +151,10 @@ module.exports = { }, method: { type: 'string' + }, + candleTimeframe: { + type: 'string', + minLength: 2 } } } From 63647dde2079d4f3ee9dcc2633e50756fa4d1d8f Mon Sep 17 00:00:00 2001 From: Vladimir Voronkov Date: Wed, 13 Aug 2025 10:28:36 +0300 Subject: [PATCH 07/11] Use candle timeframe validation schema def --- workers/loc.api/data-validator/schemas/getCandlesFileReq.js | 3 +-- workers/loc.api/data-validator/schemas/getCandlesReq.js | 3 +-- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/workers/loc.api/data-validator/schemas/getCandlesFileReq.js b/workers/loc.api/data-validator/schemas/getCandlesFileReq.js index c1552202..1f7f7a78 100644 --- a/workers/loc.api/data-validator/schemas/getCandlesFileReq.js +++ b/workers/loc.api/data-validator/schemas/getCandlesFileReq.js @@ -24,8 +24,7 @@ module.exports = { $ref: 'defs#/definitions/sort' }, timeframe: { - type: 'string', - minLength: 2 + $ref: 'defs#/definitions/candleTimeframe' }, section: { type: 'string', diff --git a/workers/loc.api/data-validator/schemas/getCandlesReq.js b/workers/loc.api/data-validator/schemas/getCandlesReq.js index c79ccf8f..18d7b80e 100644 --- a/workers/loc.api/data-validator/schemas/getCandlesReq.js +++ b/workers/loc.api/data-validator/schemas/getCandlesReq.js @@ -24,8 +24,7 @@ module.exports = { $ref: 'defs#/definitions/sort' }, timeframe: { - type: 'string', - minLength: 2 + $ref: 'defs#/definitions/candleTimeframe' }, section: { type: 'string', From 4d0291fb3967c03a7691137c542febc408093093 Mon Sep 17 00:00:00 2001 From: Vladimir Voronkov Date: Thu, 14 Aug 2025 13:26:44 +0300 Subject: [PATCH 08/11] Improve notThrowError param setting --- workers/loc.api/generate-report-file/index.js | 4 +- .../report.file.job.data.js | 106 ++++++++++++++---- .../queue/write-data-to-stream/helpers.js | 15 +-- 3 files changed, 93 insertions(+), 32 deletions(-) diff --git a/workers/loc.api/generate-report-file/index.js b/workers/loc.api/generate-report-file/index.js index 0cda2e22..3fdda07b 100644 --- a/workers/loc.api/generate-report-file/index.js +++ b/workers/loc.api/generate-report-file/index.js @@ -86,7 +86,7 @@ const _truncateFileNameEnding = (name) => { return `${cleanedName[0].toLowerCase()}${cleanedName.slice(1)}` } -const _getfilterApiMethodNamesAndArgs = ( +const _getFilterApiMethodNamesAndArgs = ( name, reqArgs ) => { @@ -141,7 +141,7 @@ module.exports = ( rootPath, conf }) - const checkingDataArr = _getfilterApiMethodNamesAndArgs( + const checkingDataArr = _getFilterApiMethodNamesAndArgs( name, args ) diff --git a/workers/loc.api/generate-report-file/report.file.job.data.js b/workers/loc.api/generate-report-file/report.file.job.data.js index c50cc470..5616d156 100644 --- a/workers/loc.api/generate-report-file/report.file.job.data.js +++ b/workers/loc.api/generate-report-file/report.file.job.data.js @@ -52,7 +52,10 @@ class ReportFileJobData { const reportFileArgs = getReportFileArgs( args, - { method: 'trades' } + { + method: 'trades', + extraParams: { notThrowError: true } + } ) const jobData = { @@ -102,7 +105,10 @@ class ReportFileJobData { const reportFileArgs = getReportFileArgs( args, - { method: 'fundingTrades' } + { + method: 'fundingTrades', + extraParams: { notThrowError: true } + } ) const jobData = { @@ -151,7 +157,10 @@ class ReportFileJobData { const reportFileArgs = getReportFileArgs( args, - { method: 'tickersHistory' } + { + method: 'tickersHistory', + extraParams: { notThrowError: true } + } ) const symb = Array.isArray(args.params.symbol) ? args.params.symbol @@ -259,7 +268,10 @@ class ReportFileJobData { const reportFileArgs = getReportFileArgs( args, - { method: 'positionsHistory' } + { + method: 'positionsHistory', + extraParams: { notThrowError: true } + } ) const jobData = { @@ -314,7 +326,13 @@ class ReportFileJobData { userInfo, userId, name: 'getActivePositions', - args, + args: { + ...args, + params: { + ...args?.params, + notThrowError: true + } + }, propNameForPagination: 'mtsUpdate', columnsCsv: { id: '#', @@ -366,7 +384,10 @@ class ReportFileJobData { const reportFileArgs = getReportFileArgs( _args, - { method: 'positionsAudit' } + { + method: 'positionsAudit', + extraParams: { notThrowError: true } + } ) const jobData = { @@ -428,7 +449,7 @@ class ReportFileJobData { args, { method: 'publicTrades', - extraParams: { isTradingPair } + extraParams: { isTradingPair, notThrowError: true } } ) const columnsCsv = (isTradingPair) @@ -531,7 +552,13 @@ class ReportFileJobData { userInfo, userId, name: 'getCandles', - args, + args: { + ...args, + params: { + ...args?.params, + notThrowError: true + } + }, propNameForPagination: 'mts', columnsCsv: { mts: 'TIME', @@ -572,7 +599,10 @@ class ReportFileJobData { const reportFileArgs = getReportFileArgs( args, - { method: 'ledgers' } + { + method: 'ledgers', + extraParams: { notThrowError: true } + } ) const jobData = { @@ -623,7 +653,10 @@ class ReportFileJobData { const reportFileArgs = getReportFileArgs( args, - { method: 'payInvoiceList' } + { + method: 'payInvoiceList', + extraParams: { notThrowError: true } + } ) const jobData = { @@ -672,7 +705,10 @@ class ReportFileJobData { const reportFileArgs = getReportFileArgs( args, - { method: 'orderTrades' } + { + method: 'orderTrades', + extraParams: { notThrowError: true } + } ) const jobData = { @@ -721,7 +757,10 @@ class ReportFileJobData { const reportFileArgs = getReportFileArgs( args, - { method: 'orders' } + { + method: 'orders', + extraParams: { notThrowError: true } + } ) const jobData = { @@ -778,7 +817,13 @@ class ReportFileJobData { userInfo, userId, name: 'getActiveOrders', - args, + args: { + ...args, + params: { + ...args?.params, + notThrowError: true + } + }, propNameForPagination: null, columnsCsv: { id: '#', @@ -825,7 +870,10 @@ class ReportFileJobData { const reportFileArgs = getReportFileArgs( args, - { method: 'movements' } + { + method: 'movements', + extraParams: { notThrowError: true } + } ) const jobData = { @@ -874,7 +922,10 @@ class ReportFileJobData { const reportFileArgs = getReportFileArgs( args, - { method: 'fundingOfferHistory' } + { + method: 'fundingOfferHistory', + extraParams: { notThrowError: true } + } ) const jobData = { @@ -926,7 +977,10 @@ class ReportFileJobData { const reportFileArgs = getReportFileArgs( args, - { method: 'fundingLoanHistory' } + { + method: 'fundingLoanHistory', + extraParams: { notThrowError: true } + } ) const jobData = { @@ -980,7 +1034,10 @@ class ReportFileJobData { const reportFileArgs = getReportFileArgs( args, - { method: 'fundingCreditHistory' } + { + method: 'fundingCreditHistory', + extraParams: { notThrowError: true } + } ) const jobData = { @@ -1035,7 +1092,10 @@ class ReportFileJobData { const reportFileArgs = getReportFileArgs( args, - { method: 'logins' } + { + method: 'logins', + extraParams: { notThrowError: true } + } ) const jobData = { @@ -1078,7 +1138,10 @@ class ReportFileJobData { const reportFileArgs = getReportFileArgs( args, - { method: 'changeLogs' } + { + method: 'changeLogs', + extraParams: { notThrowError: true } + } ) const jobData = { @@ -1191,7 +1254,10 @@ class ReportFileJobData { const reportFileArgs = getReportFileArgs( args, - { isLimitUnused: true } + { + isLimitUnused: true, + extraParams: { notThrowError: true } + } ) const jobData = { diff --git a/workers/loc.api/queue/write-data-to-stream/helpers.js b/workers/loc.api/queue/write-data-to-stream/helpers.js index 218e1527..0a88ace0 100644 --- a/workers/loc.api/queue/write-data-to-stream/helpers.js +++ b/workers/loc.api/queue/write-data-to-stream/helpers.js @@ -170,16 +170,6 @@ const writeMessageToStream = ( } const setDefaultParams = (args, method) => { - if (method === 'getWallets') { - args.params.end = args.params.end - ? Math.min(args.params.end, Date.now()) - : Date.now() - - return - } - - args.params.notThrowError = true - if (method === 'getStatusMessages') { return } @@ -187,6 +177,11 @@ const setDefaultParams = (args, method) => { args.params.end = args.params.end ? Math.min(args.params.end, Date.now()) : Date.now() + + if (method === 'getWallets') { + return + } + args.params.start = args.params.start ? args.params.start : 0 From abb462e9f61cce4570c11c2cb94788b82a394dee Mon Sep 17 00:00:00 2001 From: Vladimir Voronkov Date: Fri, 15 Aug 2025 12:09:08 +0300 Subject: [PATCH 09/11] Adjust default params setting for positions snapshot file gen --- workers/loc.api/queue/write-data-to-stream/helpers.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/workers/loc.api/queue/write-data-to-stream/helpers.js b/workers/loc.api/queue/write-data-to-stream/helpers.js index 0a88ace0..947d4153 100644 --- a/workers/loc.api/queue/write-data-to-stream/helpers.js +++ b/workers/loc.api/queue/write-data-to-stream/helpers.js @@ -178,7 +178,10 @@ const setDefaultParams = (args, method) => { ? Math.min(args.params.end, Date.now()) : Date.now() - if (method === 'getWallets') { + if ( + method === 'getWallets' || + method === 'getPositionsSnapshot' + ) { return } From 5311d52f7b34eac4be7da6ac09be38d7f1619520 Mon Sep 17 00:00:00 2001 From: Vladimir Voronkov Date: Fri, 15 Aug 2025 13:08:08 +0300 Subject: [PATCH 10/11] Adjust default params setting for full snapshot report file gen --- workers/loc.api/queue/write-data-to-stream/helpers.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/workers/loc.api/queue/write-data-to-stream/helpers.js b/workers/loc.api/queue/write-data-to-stream/helpers.js index 947d4153..80db494e 100644 --- a/workers/loc.api/queue/write-data-to-stream/helpers.js +++ b/workers/loc.api/queue/write-data-to-stream/helpers.js @@ -180,7 +180,8 @@ const setDefaultParams = (args, method) => { if ( method === 'getWallets' || - method === 'getPositionsSnapshot' + method === 'getPositionsSnapshot' || + method === 'getFullSnapshotReport' ) { return } From c23c35b91c89c9591e1a58b2142ef7d592389e9a Mon Sep 17 00:00:00 2001 From: Vladimir Voronkov Date: Thu, 21 Aug 2025 13:10:20 +0300 Subject: [PATCH 11/11] Clean up old param validation --- workers/loc.api/helpers/check-params.js | 70 ----- workers/loc.api/helpers/index.js | 2 - workers/loc.api/helpers/schema.js | 378 ------------------------ 3 files changed, 450 deletions(-) delete mode 100644 workers/loc.api/helpers/check-params.js delete mode 100644 workers/loc.api/helpers/schema.js diff --git a/workers/loc.api/helpers/check-params.js b/workers/loc.api/helpers/check-params.js deleted file mode 100644 index 22de7e8d..00000000 --- a/workers/loc.api/helpers/check-params.js +++ /dev/null @@ -1,70 +0,0 @@ -'use strict' - -const { cloneDeep } = require('lib-js-util-base') -const Ajv = require('ajv') - -const schema = require('./schema') -const { - UnprocessableEntityError, - ArgsParamsError, - ParamsValidSchemaFindingError -} = require('../errors') - -module.exports = ( - args, - schemaName = 'paramsSchemaForFile', - requireFields = [], - checkParamsField = false, - additionalSchema = {} -) => { - const ajv = new Ajv({ allowUnionTypes: true }) - const extendedSchema = { ...schema, ...additionalSchema } - - if (!extendedSchema[schemaName]) { - throw new ParamsValidSchemaFindingError() - } - - const _schema = cloneDeep(extendedSchema[schemaName]) - - if ( - Array.isArray(requireFields) && - requireFields.length > 0 - ) { - if (!args.params) { - throw new ArgsParamsError() - } - - if (!Array.isArray(_schema.required)) { - _schema.required = [] - } - - requireFields.forEach(field => { - _schema.required.push(field) - }) - } - - if ( - (checkParamsField || args.params) && - !ajv.validate(_schema, args.params) - ) { - throw new ArgsParamsError({ data: ajv.errors }) - } - if ( - args.params && - typeof args.params === 'object' && - Number.isFinite(args.params.start) && - Number.isFinite(args.params.end) && - args.params.start >= args.params.end - ) { - // Use same error format like Ajv for consistency - throw new UnprocessableEntityError({ - data: [{ - instancePath: '/end', - schemaPath: '#/properties/end/type', - keyword: 'type', - params: { type: 'integer' }, - message: 'must be start < end' - }] - }) - } -} diff --git a/workers/loc.api/helpers/index.js b/workers/loc.api/helpers/index.js index e038c79a..499cdaf9 100644 --- a/workers/loc.api/helpers/index.js +++ b/workers/loc.api/helpers/index.js @@ -6,7 +6,6 @@ const { prepareApiResponse, prepareSymbolResponse } = require('./prepare-response') -const checkParams = require('./check-params') const { getMethodLimit, getReportFileArgs, @@ -73,7 +72,6 @@ module.exports = { MIN_START_MTS, getDateNotMoreNow, getDateNotLessMinStart, - checkParams, hasJobInQueueWithStatusBy, isAuthError, isRateLimitError, diff --git a/workers/loc.api/helpers/schema.js b/workers/loc.api/helpers/schema.js deleted file mode 100644 index 93ea76a9..00000000 --- a/workers/loc.api/helpers/schema.js +++ /dev/null @@ -1,378 +0,0 @@ -'use strict' - -const { cloneDeep } = require('lib-js-util-base') - -const _publicTradesSymbol = { - type: ['string', 'array'], - if: { - type: 'array' - }, - then: { - maxItems: 1, - items: { - type: 'string' - } - } -} - -const paramsSchemaForApi = { - type: 'object', - properties: { - limit: { - type: 'integer' - }, - start: { - type: 'integer' - }, - end: { - type: 'integer' - }, - symbol: { - type: ['string', 'array'], - if: { - type: 'array' - }, - then: { - minItems: 1, - items: { - type: 'string' - } - } - } - } -} - -const timezone = { - type: ['number', 'string'] -} -const dateFormat = { - type: 'string', - enum: [ - 'DD-MM-YY', - 'DD-MM-YYYY', - 'MM-DD-YY', - 'MM-DD-YYYY', - 'YY-MM-DD', - 'YYYY-MM-DD' - ] -} -const language = { type: 'string' } - -const paramsSchemaForPayInvoiceList = { - ...paramsSchemaForApi, - properties: { - ...paramsSchemaForApi.properties, - id: { - type: 'string' - } - } -} - -const paramsSchemaForPayInvoiceListFile = { - type: 'object', - properties: { - ...paramsSchemaForPayInvoiceList.properties, - timezone, - dateFormat, - language - } -} - -const paramsSchemaForCandlesApi = { - type: 'object', - required: ['symbol'], - properties: { - timeframe: { - type: 'string' - }, - symbol: { - type: ['string', 'array'], - if: { - type: 'array' - }, - then: { - maxItems: 1, - items: { - type: 'string' - } - } - }, - section: { - type: 'string' - }, - limit: { - type: 'integer' - }, - start: { - type: 'integer' - }, - end: { - type: 'integer' - }, - sort: { - type: 'integer' - } - } -} - -const paramsSchemaForWeightedAveragesReportApi = { - type: 'object', - properties: { - start: { - type: 'integer' - }, - end: { - type: 'integer' - }, - symbol: { - type: ['string', 'array'], - if: { - type: 'array' - }, - then: { - maxItems: 1, - items: { - type: 'string' - } - } - } - } -} - -const paramsSchemaForMovementInfo = { - type: 'object', - properties: { - id: { - type: 'integer' - } - } -} - -const paramsSchemaForCandlesFile = { - type: 'object', - properties: { - ...paramsSchemaForCandlesApi.properties, - timezone, - dateFormat, - language - } -} - -const paramsSchemaForStatusMessagesApi = { - type: 'object', - properties: { - type: { - type: 'string' - }, - symbol: { - type: ['string', 'array'], - if: { - type: 'array' - }, - then: { - minItems: 1, - items: { - type: 'string' - } - } - } - } -} - -const paramsSchemaForStatusMessagesFile = { - type: 'object', - properties: { - ...paramsSchemaForStatusMessagesApi.properties, - timezone, - dateFormat, - language - } -} - -const paramsSchemaForFile = { - ...paramsSchemaForApi, - properties: { - ...paramsSchemaForApi.properties, - timezone, - dateFormat, - language - } -} - -const paramsSchemaForPublicTradesFile = { - ...paramsSchemaForFile, - properties: { - ...paramsSchemaForFile.properties, - symbol: _publicTradesSymbol - } -} - -const paramsSchemaForPublicTrades = { - ...paramsSchemaForApi, - properties: { - ...paramsSchemaForApi.properties, - symbol: _publicTradesSymbol - } -} - -const paramsSchemaForPositionsAudit = { - ...paramsSchemaForApi, - required: [ - ...(Array.isArray(paramsSchemaForApi.required) - ? paramsSchemaForApi.required - : [] - ), - 'id' - ], - properties: { - ...paramsSchemaForApi.properties, - id: { - type: 'array', - minItems: 1, - items: { - type: 'integer' - } - } - } -} - -const paramsSchemaForPositionsAuditFile = { - ...paramsSchemaForFile, - properties: { - ...paramsSchemaForFile.properties, - ...paramsSchemaForPositionsAudit.properties - } -} - -const paramsSchemaForWallets = { - type: 'object', - properties: { - end: { - type: 'integer' - } - } -} - -const paramsSchemaForWalletsFile = { - type: 'object', - properties: { - end: { - type: 'integer' - }, - timezone, - dateFormat, - language - } -} - -const paramsSchemaForActivePositionsFile = { - type: 'object', - properties: { - timezone, - dateFormat, - language - } -} - -const paramsSchemaForMultipleFile = { - type: 'object', - required: ['multiExport'], - properties: { - isPDFRequired: { - type: 'boolean' - }, - email: { - type: 'string' - }, - language, - multiExport: { - type: 'array', - minItems: 1, - items: { - type: 'object', - required: ['method'], - properties: { - method: { - type: 'string' - } - } - } - } - } -} - -const paramsSchemaForOrderTradesApi = { - type: 'object', - required: ['id', 'symbol'], - properties: { - id: { - type: 'integer' - }, - limit: { - type: 'integer' - }, - start: { - type: 'integer' - }, - end: { - type: 'integer' - }, - symbol: { - type: ['string', 'array'], - if: { - type: 'array' - }, - then: { - minItems: 1, - maxItems: 1, - items: { - type: 'string' - } - } - } - } -} - -const paramsSchemaForOrderTradesFile = { - ...paramsSchemaForOrderTradesApi, - properties: { - ...paramsSchemaForOrderTradesApi.properties, - timezone, - dateFormat, - language - } -} - -const paramsSchemaForWeightedAveragesReportFile = { - type: 'object', - properties: { - ...cloneDeep(paramsSchemaForWeightedAveragesReportApi.properties), - timezone, - dateFormat, - language - } -} - -module.exports = { - paramsSchemaForApi, - paramsSchemaForFile, - paramsSchemaForPayInvoiceList, - paramsSchemaForPayInvoiceListFile, - paramsSchemaForPublicTradesFile, - paramsSchemaForPublicTrades, - paramsSchemaForPositionsAudit, - paramsSchemaForPositionsAuditFile, - paramsSchemaForWallets, - paramsSchemaForWalletsFile, - paramsSchemaForMultipleFile, - paramsSchemaForActivePositionsFile, - paramsSchemaForOrderTradesApi, - paramsSchemaForOrderTradesFile, - paramsSchemaForStatusMessagesApi, - paramsSchemaForStatusMessagesFile, - paramsSchemaForCandlesApi, - paramsSchemaForCandlesFile, - paramsSchemaForWeightedAveragesReportApi, - paramsSchemaForWeightedAveragesReportFile, - paramsSchemaForMovementInfo -}