diff --git a/bfx-report-ui b/bfx-report-ui index fe31d5250..d7919ff0b 160000 --- a/bfx-report-ui +++ b/bfx-report-ui @@ -1 +1 @@ -Subproject commit fe31d52501fb436660120f738ddba00cded1de0a +Subproject commit d7919ff0bfbed9f827c3b74aec83e899d6acfd47 diff --git a/package.json b/package.json index a0494bbca..30cb5a9c9 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "bfx-reports-framework", - "version": "4.19.0", + "version": "4.20.0", "description": "Bitfinex reports framework", "main": "worker.js", "license": "Apache-2.0", diff --git a/test/test-cases/api-sync-mode-sqlite-test-cases.js b/test/test-cases/api-sync-mode-sqlite-test-cases.js index 0e4f7affc..35599ddd6 100644 --- a/test/test-cases/api-sync-mode-sqlite-test-cases.js +++ b/test/test-cases/api-sync-mode-sqlite-test-cases.js @@ -612,6 +612,26 @@ module.exports = ( assert.isOk(Number.isInteger(res.body.result.lastSyncMts)) }) + it('it should be successfully performed by the getLastFinishedSyncMts method, lastSyncMts is integer', async function () { + this.timeout(5000) + + const res = await agent + .post(`${basePath}/json-rpc`) + .type('json') + .send({ + auth, + method: 'getLastFinishedSyncMts', + id: 5 + }) + .expect('Content-Type', /json/) + .expect(200) + + assert.isObject(res.body) + assert.propertyVal(res.body, 'id', 5) + assert.isObject(res.body.result) + assert.isOk(Number.isInteger(res.body.result.lastSyncMts)) + }) + it('it should be successfully performed by the haveCollsBeenSyncedAtLeastOnce method, returns true', async function () { this.timeout(60000) diff --git a/workers/loc.api/service.report.framework.js b/workers/loc.api/service.report.framework.js index 1fb69e74a..fb7713256 100644 --- a/workers/loc.api/service.report.framework.js +++ b/workers/loc.api/service.report.framework.js @@ -104,6 +104,17 @@ class FrameworkReportService extends ReportService { }, 'isStagingBfxApi', args, cb) } + getLastFinishedSyncMts (space, args, cb) { + return this._privResponder(async () => { + const lastFinishedSyncQueueJob = await this._dao + .getLastFinishedSyncQueueJob(args?.auth?._id) + + return { + lastSyncMts: lastFinishedSyncQueueJob?.updatedAt ?? null + } + }, 'getLastFinishedSyncMts', args, cb) + } + signUp (space, args, cb) { return this._responder(() => { return this._authenticator.signUp(args) diff --git a/workers/loc.api/sync/dao/dao.better.sqlite.js b/workers/loc.api/sync/dao/dao.better.sqlite.js index eaa1ef2a9..d54cd35b3 100644 --- a/workers/loc.api/sync/dao/dao.better.sqlite.js +++ b/workers/loc.api/sync/dao/dao.better.sqlite.js @@ -40,12 +40,7 @@ const { RemoveElemsLeaveLastNRecordsError } = require('../../errors') -const { - CONSTR_FIELD_NAME, - TRIGGER_FIELD_NAME, - INDEX_FIELD_NAME, - UNIQUE_INDEX_FIELD_NAME -} = require('../schema/models/model/db.service.field.names') +const Model = require('../schema/models/model') const ALLOWED_COLLS = require('../schema/allowed.colls') const SYNC_QUEUE_STATES = require('../sync.queue/sync.queue.states') const DB_WORKER_ACTIONS = require( @@ -221,14 +216,7 @@ class BetterSqliteDAO extends DAO { } _createTablesIfNotExists (opts = {}) { - const models = this._getModelsMap({ - models: opts?.models, - omittedFields: [ - TRIGGER_FIELD_NAME, - INDEX_FIELD_NAME, - UNIQUE_INDEX_FIELD_NAME - ] - }) + const models = opts?.models ?? this._getModelsMap() const sql = getTableCreationQuery(models, opts) return this.query({ @@ -239,10 +227,7 @@ class BetterSqliteDAO extends DAO { } _createTriggerIfNotExists (opts = {}) { - const models = this._getModelsMap({ - models: opts?.models, - omittedFields: [] - }) + const models = opts?.models ?? this._getModelsMap() const sql = getTriggerCreationQuery(models, opts) return this.query({ @@ -253,10 +238,7 @@ class BetterSqliteDAO extends DAO { } _createIndexisIfNotExists (opts = {}) { - const models = this._getModelsMap({ - models: opts?.models, - omittedFields: [] - }) + const models = opts?.models ?? this._getModelsMap() const sql = getIndexCreationQuery(models, opts) return this.query({ @@ -488,16 +470,6 @@ class BetterSqliteDAO extends DAO { return false } - const modelsMap = this._getModelsMap({ - omittedFields: [ - '_id', - CONSTR_FIELD_NAME, - TRIGGER_FIELD_NAME, - INDEX_FIELD_NAME, - UNIQUE_INDEX_FIELD_NAME - ] - }) - await this._beginTrans(async () => { const tableNames = await this.getTablesNames({ doNotQueueQuery }) const filteredTempTableNames = tableNames.filter((name) => ( @@ -508,12 +480,16 @@ class BetterSqliteDAO extends DAO { for (const tempName of filteredTempTableNames) { const name = tempName.replace(namePrefix, '') - const model = modelsMap.get(name) - const projection = Object.keys(model).join(', ') + const model = this._getModelOf(name) if (!model) { continue } + + const projection = model.getModelFieldKeys() + .filter((fieldName) => fieldName !== Model.UID_FIELD_NAME) + .join(', ') + if (isStrictEqual) { sqlArr.push(`DELETE FROM ${name}`) } diff --git a/workers/loc.api/sync/dao/dao.js b/workers/loc.api/sync/dao/dao.js index 41af7e4f8..9c236d6a1 100644 --- a/workers/loc.api/sync/dao/dao.js +++ b/workers/loc.api/sync/dao/dao.js @@ -25,8 +25,12 @@ class DAO { this.processMessageManagerFactory = processMessageManagerFactory } - _getModelsMap (params) { - return this.syncSchema.getModelsMap(params) + _getModelsMap () { + return this.syncSchema.getModelsMap() + } + + _getModelOf (tableName) { + return this.syncSchema.getModelOf(tableName) } _getMethodCollMap (params) { diff --git a/workers/loc.api/sync/dao/helpers/find-in-coll-by/get-query.js b/workers/loc.api/sync/dao/helpers/find-in-coll-by/get-query.js index 08d11a503..db774107f 100644 --- a/workers/loc.api/sync/dao/helpers/find-in-coll-by/get-query.js +++ b/workers/loc.api/sync/dao/helpers/find-in-coll-by/get-query.js @@ -25,7 +25,10 @@ module.exports = (args, methodColl, opts) => { filter } = getFilterParams(args, methodColl, { isPublic }) - const _model = { ...model, ...additionalModel } + const modelFields = { + ...model.getModelFields(), + ...additionalModel + } const exclude = isPublic ? ['_id', 'user_id'] : ['_id'] const { @@ -46,7 +49,7 @@ module.exports = (args, methodColl, opts) => { } = getGroupQuery(methodColl) const { subQuery, subQueryValues } = getSubQuery(methodColl) const projection = getProjectionQuery( - _model, + modelFields, exclude, isExcludePrivate ) diff --git a/workers/loc.api/sync/dao/helpers/get-index-creation-query.js b/workers/loc.api/sync/dao/helpers/get-index-creation-query.js index 87119b6ab..0f9d495f7 100644 --- a/workers/loc.api/sync/dao/helpers/get-index-creation-query.js +++ b/workers/loc.api/sync/dao/helpers/get-index-creation-query.js @@ -1,10 +1,5 @@ 'use strict' -const { - INDEX_FIELD_NAME, - UNIQUE_INDEX_FIELD_NAME -} = require('../../schema/models/model/db.service.field.names') - const _getIndexQuery = ( name, fields = [], @@ -67,18 +62,20 @@ const _getIndexQueryFromModel = ( shouldNotAddIfNotExistsStm } = opts ?? {} + const modelUniqueIndexies = model.getUniqueIndexies() + const modelIndexies = model.getIndexies() const uniqueIndexFields = ( - model[UNIQUE_INDEX_FIELD_NAME] && - typeof model[UNIQUE_INDEX_FIELD_NAME] === 'string' + modelUniqueIndexies && + typeof modelUniqueIndexies === 'string' ) - ? model[UNIQUE_INDEX_FIELD_NAME].split(' ') - : model[UNIQUE_INDEX_FIELD_NAME] + ? modelUniqueIndexies.split(' ') + : modelUniqueIndexies const indexFields = ( - model[INDEX_FIELD_NAME] && - typeof model[INDEX_FIELD_NAME] === 'string' + modelIndexies && + typeof modelIndexies === 'string' ) - ? model[INDEX_FIELD_NAME].split(' ') - : model[INDEX_FIELD_NAME] + ? modelIndexies.split(' ') + : modelIndexies const uniqueIndexiesArr = _getIndexQuery( name, diff --git a/workers/loc.api/sync/dao/helpers/get-insertable-array-objects-filter.js b/workers/loc.api/sync/dao/helpers/get-insertable-array-objects-filter.js index ed13a3611..58d4e945c 100644 --- a/workers/loc.api/sync/dao/helpers/get-insertable-array-objects-filter.js +++ b/workers/loc.api/sync/dao/helpers/get-insertable-array-objects-filter.js @@ -16,10 +16,7 @@ const getFieldsFilters = ( const _params = { ...params } const field = _params[fieldName] - if ( - Object.keys(model) - .some(key => key === modelFieldName) - ) { + if (model.hasModelFieldName(modelFieldName)) { if (typeof field === 'boolean') { return { ...accum, diff --git a/workers/loc.api/sync/dao/helpers/get-projection-query.js b/workers/loc.api/sync/dao/helpers/get-projection-query.js index f014fbc13..37177aaca 100644 --- a/workers/loc.api/sync/dao/helpers/get-projection-query.js +++ b/workers/loc.api/sync/dao/helpers/get-projection-query.js @@ -1,9 +1,14 @@ 'use strict' +const Model = require('../../schema/models/model') + const _getProjArr = (model) => { if (Array.isArray(model)) { return model } + if (model instanceof Model) { + return model.getModelFieldKeys() + } if ( model && typeof model === 'object' diff --git a/workers/loc.api/sync/dao/helpers/get-table-creation-query.js b/workers/loc.api/sync/dao/helpers/get-table-creation-query.js index 83f9f5f36..50315d164 100644 --- a/workers/loc.api/sync/dao/helpers/get-table-creation-query.js +++ b/workers/loc.api/sync/dao/helpers/get-table-creation-query.js @@ -1,13 +1,10 @@ 'use strict' -const { - CONSTR_FIELD_NAME -} = require('../../schema/models/model/db.service.field.names') - const _getConstraintsQuery = (name, model) => { - const constraintsArr = Array.isArray(model[CONSTR_FIELD_NAME]) - ? model[CONSTR_FIELD_NAME] - : [model[CONSTR_FIELD_NAME]] + const modelConstraints = model.getConstraints() + const constraintsArr = Array.isArray(modelConstraints) + ? modelConstraints + : [modelConstraints] return constraintsArr.reduce((accum, item) => { const _constraints = ( @@ -43,8 +40,7 @@ module.exports = (models = [], opts = {}) => { const _name = `${prefix}${name}` const constraints = _getConstraintsQuery(_name, model) - const keys = Object.keys(model) - .filter((field) => field !== CONSTR_FIELD_NAME) + const keys = model.getModelFieldKeys() const columnDefs = keys.reduce((accum, field, i, arr) => { const isLast = arr.length === (i + 1) const type = model[field] diff --git a/workers/loc.api/sync/dao/helpers/get-trigger-creation-query.js b/workers/loc.api/sync/dao/helpers/get-trigger-creation-query.js index c619d05ca..bacccca51 100644 --- a/workers/loc.api/sync/dao/helpers/get-trigger-creation-query.js +++ b/workers/loc.api/sync/dao/helpers/get-trigger-creation-query.js @@ -1,9 +1,5 @@ 'use strict' -const { - TRIGGER_FIELD_NAME -} = require('../../schema/models/model/db.service.field.names') - const _getTriggersQuery = ( name, model, @@ -13,9 +9,10 @@ const _getTriggersQuery = ( shouldNotAddIfNotExistsStm } = opts ?? {} - const triggersArr = Array.isArray(model[TRIGGER_FIELD_NAME]) - ? model[TRIGGER_FIELD_NAME] - : [model[TRIGGER_FIELD_NAME]] + const modelTriggers = model.getTriggers() + const triggersArr = Array.isArray(modelTriggers) + ? modelTriggers + : [modelTriggers] return triggersArr.reduce((accum, item) => { if ( diff --git a/workers/loc.api/sync/data.inserter/helpers/utils.js b/workers/loc.api/sync/data.inserter/helpers/utils.js index 79ba34414..4778a1009 100644 --- a/workers/loc.api/sync/data.inserter/helpers/utils.js +++ b/workers/loc.api/sync/data.inserter/helpers/utils.js @@ -16,7 +16,7 @@ const normalizeApiData = ( return data } - const modelKeys = Object.keys(model) + const modelKeys = model.getModelFieldKeys() if (modelKeys.length === 0) { return data diff --git a/workers/loc.api/sync/data.inserter/index.js b/workers/loc.api/sync/data.inserter/index.js index a67a04a52..815e1bd6f 100644 --- a/workers/loc.api/sync/data.inserter/index.js +++ b/workers/loc.api/sync/data.inserter/index.js @@ -539,11 +539,7 @@ class DataInserter extends EventEmitter { _args.params.notThrowError = true const currIterationArgs = cloneDeep(_args) - const { subUserId } = model ?? {} - const hasNotSubUserField = ( - !subUserId || - typeof subUserId !== 'string' - ) + const hasNotSubUserField = !model.hasModelFieldName('subUserId') const { auth: _auth } = _args ?? {} const { session } = _auth ?? {} const sessionAuth = isPublic || hasNotSubUserField diff --git a/workers/loc.api/sync/data.inserter/sync.user.step.manager/index.js b/workers/loc.api/sync/data.inserter/sync.user.step.manager/index.js index 91b42c015..d3a385657 100644 --- a/workers/loc.api/sync/data.inserter/sync.user.step.manager/index.js +++ b/workers/loc.api/sync/data.inserter/sync.user.step.manager/index.js @@ -213,24 +213,22 @@ class SyncUserStepManager { const hasUserIdField = ( Number.isInteger(userId) ) - const hasUserIdFieldInModel = ( - typeof model?.user_id === 'string' - ) + const hasUserIdFieldInModel = model + .hasModelFieldName('user_id') const hasSubUserIdField = ( Number.isInteger(subUserId) ) - const hasSubUserIdFieldInModel = ( - typeof model?.subUserId === 'string' - ) + const hasSubUserIdFieldInModel = model + .hasModelFieldName('subUserId') const hasSymbolField = ( symbolFieldName && - typeof model?.[symbolFieldName] === 'string' && + model.hasModelFieldName(symbolFieldName) && symbol && typeof symbol === 'string' ) const hasTimeframeField = ( timeframeFieldName && - typeof model?.[timeframeFieldName] === 'string' && + model.hasModelFieldName(timeframeFieldName) && timeframe && typeof timeframe === 'string' ) diff --git a/workers/loc.api/sync/movements/index.js b/workers/loc.api/sync/movements/index.js index 2040c91f2..95e3b0a5e 100644 --- a/workers/loc.api/sync/movements/index.js +++ b/workers/loc.api/sync/movements/index.js @@ -25,10 +25,12 @@ class Movements { this.ALLOWED_COLLS = ALLOWED_COLLS this.authenticator = authenticator - this.movementsModel = this.syncSchema.getModelsMap() - .get(this.ALLOWED_COLLS.MOVEMENTS) - this.ledgersModel = this.syncSchema.getModelsMap() - .get(this.ALLOWED_COLLS.LEDGERS) + this.movementsModelFields = this.syncSchema + .getModelOf(this.ALLOWED_COLLS.MOVEMENTS) + .getModelFields() + this.ledgersModelFields = this.syncSchema + .getModelOf(this.ALLOWED_COLLS.LEDGERS) + .getModelFields() } async getMovements (params = {}) { @@ -177,7 +179,7 @@ class Movements { end = Date.now(), filter: _filter, sort = [['mts', -1], ['id', -1]], - projection = this.ledgersModel, + projection = this.ledgersModelFields, exclude = ['user_id'], isExcludePrivate = true, isWithdrawals = false, @@ -227,7 +229,7 @@ class Movements { end = Date.now(), filter: _filter, sort = [['mts', -1], ['id', -1]], - projection = this.ledgersModel, + projection = this.ledgersModelFields, exclude = ['user_id'], isExcludePrivate = true, isWithdrawals = false, diff --git a/workers/loc.api/sync/performing.loan/index.js b/workers/loc.api/sync/performing.loan/index.js index 758c8dc79..50c04d73c 100644 --- a/workers/loc.api/sync/performing.loan/index.js +++ b/workers/loc.api/sync/performing.loan/index.js @@ -35,8 +35,9 @@ class PerformingLoan { this.ledgersMethodColl = this.syncSchema.getMethodCollMap() .get(this.SYNC_API_METHODS.LEDGERS) - this.ledgersModel = this.syncSchema.getModelsMap() - .get(this.ALLOWED_COLLS.LEDGERS) + this.ledgersModelFields = this.syncSchema + .getModelOf(this.ALLOWED_COLLS.LEDGERS) + .getModelFields() } async _getLedgers ({ @@ -47,7 +48,7 @@ class PerformingLoan { filter = { $eq: { _isMarginFundingPayment: 1 } }, - projection = this.ledgersModel + projection = this.ledgersModelFields }) { const user = await this.authenticator .verifyRequestUser({ auth }) diff --git a/workers/loc.api/sync/positions.snapshot/index.js b/workers/loc.api/sync/positions.snapshot/index.js index 151d08d45..965afaa83 100644 --- a/workers/loc.api/sync/positions.snapshot/index.js +++ b/workers/loc.api/sync/positions.snapshot/index.js @@ -45,10 +45,12 @@ class PositionsSnapshot { this.currencyConverter = currencyConverter this.authenticator = authenticator - this.positionsHistoryModel = this.syncSchema.getModelsMap() - .get(this.ALLOWED_COLLS.POSITIONS_HISTORY) - this.positionsSnapshotModel = this.syncSchema.getModelsMap() - .get(this.ALLOWED_COLLS.POSITIONS_SNAPSHOT) + this.positionsHistoryModelFields = this.syncSchema + .getModelOf(this.ALLOWED_COLLS.POSITIONS_HISTORY) + .getModelFields() + this.positionsSnapshotModelFields = this.syncSchema + .getModelOf(this.ALLOWED_COLLS.POSITIONS_SNAPSHOT) + .getModelFields() this.positionsSnapshotMethodColl = this.syncSchema.getMethodCollMap() .get(this.SYNC_API_METHODS.POSITIONS_SNAPSHOT) this.positionsSnapshotSymbolFieldName = this.positionsSnapshotMethodColl.symbolFieldName @@ -67,7 +69,7 @@ class PositionsSnapshot { $gte: { mtsUpdate: endMts } }, sort: [['mtsUpdate', -1]], - projection: this.positionsHistoryModel, + projection: this.positionsHistoryModelFields, exclude: ['user_id'], isExcludePrivate: true } @@ -105,7 +107,7 @@ class PositionsSnapshot { ...lteFilter }, sort: _sort, - projection: this.positionsSnapshotModel, + projection: this.positionsSnapshotModelFields, exclude: ['user_id'], isExcludePrivate: true } @@ -662,7 +664,7 @@ class PositionsSnapshot { $lte: { mtsUpdate: end } }, sort: [['mtsUpdate', -1], ['id', -1]], - projection: this.positionsHistoryModel, + projection: this.positionsHistoryModelFields, exclude: ['user_id'], isExcludePrivate: true } diff --git a/workers/loc.api/sync/schema/helpers/index.js b/workers/loc.api/sync/schema/helpers/index.js index a38bd6c0c..6179ea6cc 100644 --- a/workers/loc.api/sync/schema/helpers/index.js +++ b/workers/loc.api/sync/schema/helpers/index.js @@ -1,25 +1,24 @@ 'use strict' const { omit, cloneDeep } = require('lib-js-util-base') - -const { - CONSTR_FIELD_NAME, - TRIGGER_FIELD_NAME, - INDEX_FIELD_NAME, - UNIQUE_INDEX_FIELD_NAME -} = require('../models/model/db.service.field.names') +const Model = require('../models/model') const cloneSchema = (map, omittedFields = []) => { const arr = [...map].map(([key, schema]) => { const normalizedSchema = omit(schema, omittedFields) - const clonedSchema = cloneDeep(normalizedSchema) + const clonedSchema = {} + + for (const [propName, value] of Object.entries(normalizedSchema)) { + if ( + typeof value === 'function' || + value instanceof Model + ) { + clonedSchema[propName] = value - for (const [propName, value] of Object.entries(schema)) { - if (typeof value !== 'function') { continue } - clonedSchema[propName] = value + clonedSchema[propName] = cloneDeep(value) } return [key, clonedSchema] @@ -28,26 +27,6 @@ const cloneSchema = (map, omittedFields = []) => { return new Map(arr) } -const getModelsMap = (params = {}) => { - const { - models, - omittedFields = [ - CONSTR_FIELD_NAME, - TRIGGER_FIELD_NAME, - INDEX_FIELD_NAME, - UNIQUE_INDEX_FIELD_NAME - ] - } = { ...params } - - return cloneSchema(models, omittedFields) -} - -const getModelOf = (tableName, models) => { - return { ...getModelsMap({ models }).get(tableName) } -} - module.exports = { - cloneSchema, - getModelsMap, - getModelOf + cloneSchema } diff --git a/workers/loc.api/sync/schema/index.js b/workers/loc.api/sync/schema/index.js index 34ca41774..ad9cd4992 100644 --- a/workers/loc.api/sync/schema/index.js +++ b/workers/loc.api/sync/schema/index.js @@ -2,7 +2,8 @@ const { SUPPORTED_DB_VERSION, - getModelsMap + getModelsMap, + getModelOf } = require('./models') const { getMethodCollMap @@ -11,5 +12,6 @@ const { module.exports = { SUPPORTED_DB_VERSION, getMethodCollMap, - getModelsMap + getModelsMap, + getModelOf } diff --git a/workers/loc.api/sync/schema/models/index.js b/workers/loc.api/sync/schema/models/index.js index 31e2994ac..0a6f14be2 100644 --- a/workers/loc.api/sync/schema/models/index.js +++ b/workers/loc.api/sync/schema/models/index.js @@ -44,7 +44,7 @@ const progress = require('./progress') const syncQueue = require('./sync-queue') const syncUserSteps = require('./sync-user-steps') -const _models = new Map([ +const models = new Map([ [TABLES_NAMES.USERS, users], [TABLES_NAMES.SUB_ACCOUNTS, subAccounts], [TABLES_NAMES.LEDGERS, ledgers], @@ -79,20 +79,12 @@ const _models = new Map([ [TABLES_NAMES.SYNC_USER_STEPS, syncUserSteps] ]) -const { - getModelsMap: _getModelsMap, - getModelOf: _getModelOf -} = require('../helpers') - -const getModelsMap = (params = {}) => { - return _getModelsMap({ - ...params, - models: params?.models ?? _models - }) +const getModelsMap = () => { + return new Map(models) } const getModelOf = (tableName) => { - return _getModelOf(tableName, _models) + return models.get(tableName) } module.exports = { diff --git a/workers/loc.api/sync/schema/models/model/index.js b/workers/loc.api/sync/schema/models/model/index.js index 28742297d..f2abeff05 100644 --- a/workers/loc.api/sync/schema/models/model/index.js +++ b/workers/loc.api/sync/schema/models/model/index.js @@ -13,6 +13,7 @@ const BaseModel = require('./base.model') class Model extends BaseModel { #modelFields = {} + #modelFieldKeys = [] #opts = { hasNoUID: false, hasCreateUpdateMtsTriggers: false @@ -102,6 +103,16 @@ class Model extends BaseModel { : this.#modelFields } + getModelFieldKeys (opts) { + return opts?.isCloned + ? cloneDeep(this.#modelFieldKeys) + : this.#modelFieldKeys + } + + hasModelFieldName (fieldName) { + return !!this.#modelFields[fieldName] + } + getTriggers (opts) { return this.#getServiceFields( BaseModel.TRIGGER_FIELD_NAME, @@ -198,6 +209,7 @@ class Model extends BaseModel { for (const [name, value] of Object.entries(this)) { if (this.#isNotDbServiceField(name)) { this.#modelFields[name] = value + this.#modelFieldKeys.push(name) } } @@ -206,6 +218,7 @@ class Model extends BaseModel { } freezeAndSealObjectDeeply(this.#modelFields) + freezeAndSealObjectDeeply(this.#modelFieldKeys) } #isNotDbServiceField (fieldName) { diff --git a/workers/loc.api/sync/summary.by.asset/index.js b/workers/loc.api/sync/summary.by.asset/index.js index 283fdb493..fa23c704e 100644 --- a/workers/loc.api/sync/summary.by.asset/index.js +++ b/workers/loc.api/sync/summary.by.asset/index.js @@ -34,8 +34,9 @@ class SummaryByAsset { this.ledgersMethodColl = this.syncSchema.getMethodCollMap() .get(this.SYNC_API_METHODS.LEDGERS) - this.ledgersModel = this.syncSchema.getModelsMap() - .get(this.ALLOWED_COLLS.LEDGERS) + this.ledgersModelFields = this.syncSchema + .getModelOf(this.ALLOWED_COLLS.LEDGERS) + .getModelFields() this.ledgersSymbolFieldName = this.ledgersMethodColl.symbolFieldName } @@ -269,7 +270,7 @@ class SummaryByAsset { user_id: auth._id }, sort: [['mts', 1], ['id', 1]], - projection: this.ledgersModel + projection: this.ledgersModelFields } ) } diff --git a/workers/loc.api/sync/total.fees.report/index.js b/workers/loc.api/sync/total.fees.report/index.js index f580d44cf..d1e8b22be 100644 --- a/workers/loc.api/sync/total.fees.report/index.js +++ b/workers/loc.api/sync/total.fees.report/index.js @@ -38,8 +38,9 @@ class TotalFeesReport { this.ledgersMethodColl = this.syncSchema.getMethodCollMap() .get(this.SYNC_API_METHODS.LEDGERS) - this.ledgersModel = this.syncSchema.getModelsMap() - .get(this.ALLOWED_COLLS.LEDGERS) + this.ledgersModelFields = this.syncSchema + .getModelOf(this.ALLOWED_COLLS.LEDGERS) + .getModelFields() } async getTotalFeesReport (args = {}) { @@ -100,7 +101,7 @@ class TotalFeesReport { end, symbol, filter, - projection = this.ledgersModel + projection = this.ledgersModelFields }) { const user = await this.authenticator .verifyRequestUser({ auth }) diff --git a/workers/loc.api/sync/trades/index.js b/workers/loc.api/sync/trades/index.js index fc5bdac03..4d355d68f 100644 --- a/workers/loc.api/sync/trades/index.js +++ b/workers/loc.api/sync/trades/index.js @@ -41,8 +41,9 @@ class Trades { this.tradesMethodColl = this.syncSchema.getMethodCollMap() .get(this.SYNC_API_METHODS.TRADES) - this.tradesModel = this.syncSchema.getModelsMap() - .get(this.ALLOWED_COLLS.TRADES) + this.tradesModelFields = this.syncSchema + .getModelOf(this.ALLOWED_COLLS.TRADES) + .getModelFields() } async _getTrades ({ @@ -71,7 +72,7 @@ class Trades { ...symbFilter }, sort: [['mtsCreate', -1]], - projection: this.tradesModel, + projection: this.tradesModelFields, exclude: ['user_id'], isExcludePrivate: true } diff --git a/workers/loc.api/sync/transaction.tax.report/index.js b/workers/loc.api/sync/transaction.tax.report/index.js index 31e3e44bb..6817c7d29 100644 --- a/workers/loc.api/sync/transaction.tax.report/index.js +++ b/workers/loc.api/sync/transaction.tax.report/index.js @@ -70,8 +70,9 @@ class TransactionTaxReport { this.currencyConverter = currencyConverter this.processMessageManager = processMessageManager - this.tradesModel = this.syncSchema.getModelsMap() - .get(this.ALLOWED_COLLS.TRADES) + this.tradesModelFields = this.syncSchema + .getModelOf(this.ALLOWED_COLLS.TRADES) + .getModelFields() } async makeTrxTaxReportInBackground (args = {}) { @@ -187,9 +188,8 @@ class TransactionTaxReport { } ) - interrupter.emitInterrupted() - if (interrupter.hasInterrupted()) { + interrupter.emitInterrupted() await this.#emitProgress( user, { progress: null, state: PROGRESS_STATES.GENERATION_INTERRUPTED } @@ -198,6 +198,7 @@ class TransactionTaxReport { return [] } + interrupter.emitInterrupted() await this.#emitProgress( user, { progress: 100, state: PROGRESS_STATES.GENERATION_COMPLETED } @@ -321,6 +322,10 @@ class TransactionTaxReport { !synonymous || trxPriceCalculator.kindOfCcyForTriangulation ) { + if (interrupter.hasInterrupted()) { + return + } + throw new PubTradeFindForTrxTaxError({ symbol, pubTradeStart, @@ -330,6 +335,10 @@ class TransactionTaxReport { } for (const [symbol, conversion] of synonymous) { + if (interrupter.hasInterrupted()) { + return + } + const res = await this.#getPublicTrades( { symbol: getCcyPairForConversion(symbol, trxPriceCalculator), @@ -369,6 +378,10 @@ class TransactionTaxReport { pubTradeStart > trx.mtsCreate || pubTradeEnd < trx.mtsCreate ) { + if (interrupter.hasInterrupted()) { + return + } + throw new PubTradeFindForTrxTaxError({ symbol, pubTradeStart, @@ -430,7 +443,7 @@ class TransactionTaxReport { ...symbFilter }, sort: [['mtsCreate', -1]], - projection: this.tradesModel, + projection: this.tradesModelFields, exclude: ['user_id'], isExcludePrivate: false } @@ -462,8 +475,23 @@ class TransactionTaxReport { const getDataFn = this.rService[this.SYNC_API_METHODS.PUBLIC_TRADES] .bind(this.rService) + let promiseResolve = () => {} + const onceInterruptPromise = new Promise((resolve) => { + promiseResolve = () => resolve({ res: [] }) + interrupter.onceInterrupt(promiseResolve) + }) + const getResponse = (res) => { + interrupter.offInterrupt(promiseResolve) + + return res ?? [] + } + for (let i = 0; i < 6; i += 1) { - const { res } = await this.getDataFromApi({ + if (interrupter.hasInterrupted()) { + return getResponse() + } + + const pubTradesPromise = this.getDataFromApi({ getData: (s, args) => getDataFn(args), args, callerName: 'TRANSACTION_TAX_REPORT', @@ -472,21 +500,26 @@ class TransactionTaxReport { interrupter }) + const { res } = await Promise.race([ + pubTradesPromise, + onceInterruptPromise + ]) + if (isTestEnv) { /* * Need to reverse pub-trades array for test env * as mocked test server return data in desc order */ - return res.reverse() + return getResponse(res.reverse()) } if (Array.isArray(res)) { - return res + return getResponse(res) } await setTimeout(10000) } - return [] + return getResponse() } async #updateExactUsdValueInColls (trxs) { diff --git a/workers/loc.api/sync/weighted.averages.report/index.js b/workers/loc.api/sync/weighted.averages.report/index.js index a7b303658..5fe109209 100644 --- a/workers/loc.api/sync/weighted.averages.report/index.js +++ b/workers/loc.api/sync/weighted.averages.report/index.js @@ -33,8 +33,9 @@ class WeightedAveragesReport extends BaseWeightedAveragesReport { this.syncSchema = syncSchema this.ALLOWED_COLLS = ALLOWED_COLLS - this.tradesModel = this.syncSchema.getModelsMap() - .get(this.ALLOWED_COLLS.TRADES) + this.tradesModelFields = this.syncSchema + .getModelOf(this.ALLOWED_COLLS.TRADES) + .getModelFields() // Used to switch data fetching from DB for framework mode this._isNotCalcTakenFromBfxApi = true @@ -63,7 +64,7 @@ class WeightedAveragesReport extends BaseWeightedAveragesReport { ...symbFilter }, sort: [['mtsCreate', -1]], - projection: this.tradesModel, + projection: this.tradesModelFields, exclude: ['user_id'], isExcludePrivate: true }