diff --git a/workers/loc.api/sync/transaction.tax.report/helpers/get-trx-map-by-ccy.js b/workers/loc.api/sync/transaction.tax.report/helpers/get-trx-map-by-ccy.js index 45a0eee9e..f2a2fe68f 100644 --- a/workers/loc.api/sync/transaction.tax.report/helpers/get-trx-map-by-ccy.js +++ b/workers/loc.api/sync/transaction.tax.report/helpers/get-trx-map-by-ccy.js @@ -15,18 +15,32 @@ const setCcyCalculator = (map, symb, trxPriceCalculator) => { map.get(symb).push(trxPriceCalculator) } +const calcTotalTrxAmount = (trxMapArray) => { + return trxMapArray.reduce((accum, [s, calcs]) => { + return accum + calcs.length + }, 0) +} + const placeTriangulationCcyAtStart = (map) => { if (!map.has(TrxPriceCalculator.CRYPTO_CCY_FOR_TRIANGULATION)) { - return map + return { + trxMapByCcy: map, + totalTrxAmount: calcTotalTrxAmount([...map]) + } } const triangulationCcyCalculators = map.get(TrxPriceCalculator.CRYPTO_CCY_FOR_TRIANGULATION) map.delete(TrxPriceCalculator.CRYPTO_CCY_FOR_TRIANGULATION) - return new Map([ + const trxMapArray = [ [TrxPriceCalculator.CRYPTO_CCY_FOR_TRIANGULATION, triangulationCcyCalculators], ...map - ]) + ] + + return { + trxMapByCcy: new Map(trxMapArray), + totalTrxAmount: calcTotalTrxAmount(trxMapArray) + } } module.exports = (trxs) => { diff --git a/workers/loc.api/sync/transaction.tax.report/helpers/index.js b/workers/loc.api/sync/transaction.tax.report/helpers/index.js index dc9b8137f..680903d1e 100644 --- a/workers/loc.api/sync/transaction.tax.report/helpers/index.js +++ b/workers/loc.api/sync/transaction.tax.report/helpers/index.js @@ -3,6 +3,7 @@ const TRX_TAX_TYPES = require('./trx.tax.types') const TRX_TAX_STRATEGIES = require('./trx.tax.strategies') const PRIORITY_CURRENCY_LIST = require('./priority.currency.list') +const PROGRESS_STATES = require('./progress.states') const remapTrades = require('./remap-trades') const remapMovements = require('./remap-movements') const lookUpTrades = require('./look-up-trades') @@ -16,6 +17,7 @@ module.exports = { TRX_TAX_TYPES, TRX_TAX_STRATEGIES, PRIORITY_CURRENCY_LIST, + PROGRESS_STATES, remapTrades, remapMovements, lookUpTrades, diff --git a/workers/loc.api/sync/transaction.tax.report/helpers/progress.states.js b/workers/loc.api/sync/transaction.tax.report/helpers/progress.states.js new file mode 100644 index 000000000..654a39b39 --- /dev/null +++ b/workers/loc.api/sync/transaction.tax.report/helpers/progress.states.js @@ -0,0 +1,12 @@ +'use strict' + +const PROGRESS_STATES = { + GENERATION_STARTED: 'GENERATION_STARTED', + OBTAINING_CURRENCY_PRICES: 'OBTAINING_CURRENCY_PRICES', + TRANSACTION_HISTORY_GENERATION: 'TRANSACTION_HISTORY_GENERATION', + GENERATION_COMPLETED: 'GENERATION_COMPLETED', + + GENERATION_INTERRUPTED: 'GENERATION_INTERRUPTED' +} + +module.exports = PROGRESS_STATES diff --git a/workers/loc.api/sync/transaction.tax.report/index.js b/workers/loc.api/sync/transaction.tax.report/index.js index caff388ae..0c5435d61 100644 --- a/workers/loc.api/sync/transaction.tax.report/index.js +++ b/workers/loc.api/sync/transaction.tax.report/index.js @@ -10,6 +10,7 @@ const { PubTradeFindForTrxTaxError } = require('../../errors') const { TRX_TAX_STRATEGIES, + PROGRESS_STATES, remapTrades, remapMovements, lookUpTrades, @@ -96,6 +97,10 @@ class TransactionTaxReport { user, name: INTERRUPTER_NAMES.TRX_TAX_REPORT_INTERRUPTER }) + await this.#emitProgress( + user, + { progress: 0, state: PROGRESS_STATES.GENERATION_STARTED } + ) const isFIFO = strategy === TRX_TAX_STRATEGIES.FIFO const isLIFO = strategy === TRX_TAX_STRATEGIES.LIFO @@ -114,6 +119,10 @@ class TransactionTaxReport { trxsForCurrPeriod.length === 0 ) { interrupter.emitInterrupted() + await this.#emitProgress( + user, + { progress: 100, state: PROGRESS_STATES.GENERATION_COMPLETED } + ) return [] } @@ -151,7 +160,10 @@ class TransactionTaxReport { !Number.isFinite(trx?.lastSymbPriceUsd) )) ) - await this.#convertCurrencies(trxsForConvToUsd, { interrupter }) + await this.#convertCurrencies( + trxsForConvToUsd, + { interrupter, user } + ) const { saleTradesWithRealizedProfit } = await lookUpTrades( trxsForCurrPeriod, @@ -165,9 +177,19 @@ class TransactionTaxReport { interrupter.emitInterrupted() if (interrupter.hasInterrupted()) { + await this.#emitProgress( + user, + { progress: null, state: PROGRESS_STATES.GENERATION_INTERRUPTED } + ) + return [] } + await this.#emitProgress( + user, + { progress: 100, state: PROGRESS_STATES.GENERATION_COMPLETED } + ) + return saleTradesWithRealizedProfit } @@ -231,8 +253,13 @@ class TransactionTaxReport { } async #convertCurrencies (trxs, opts) { - const { interrupter } = opts - const trxMapByCcy = getTrxMapByCcy(trxs) + const { interrupter, user } = opts + const { + trxMapByCcy, + totalTrxAmount + } = getTrxMapByCcy(trxs) + let count = 0 + let progress = 0 for (const [symbol, trxPriceCalculators] of trxMapByCcy.entries()) { if (interrupter.hasInterrupted()) { @@ -246,6 +273,8 @@ class TransactionTaxReport { let pubTradeEnd = pubTrades[pubTrades.length - 1]?.mts for (const trxPriceCalculator of trxPriceCalculatorIterator) { + count += 1 + if (interrupter.hasInterrupted()) { return } @@ -337,10 +366,28 @@ class TransactionTaxReport { const pubTrade = findPublicTrade(pubTrades, trx.mtsCreate) trxPriceCalculator.calcPrice(pubTrade?.price) + const _progress = (count / totalTrxAmount) * 100 + + if ( + _progress <= 0 || + _progress >= 100 + ) { + continue + } + + progress = _progress + await this.#emitProgress( + user, + { progress, state: PROGRESS_STATES.OBTAINING_CURRENCY_PRICES } + ) } } await this.#updateExactUsdValueInColls(trxs) + await this.#emitProgress( + user, + { progress, state: PROGRESS_STATES.TRANSACTION_HISTORY_GENERATION } + ) } async #getTrades ({ @@ -481,6 +528,24 @@ class TransactionTaxReport { } } } + + async #emitProgress (user, params) { + const { + progress = null, + state = null + } = params ?? {} + + await this.wsEventEmitterFactory() + .emitTrxTaxReportGenerationProgressToOne( + { + progress: Number.isFinite(progress) + ? Math.floor(progress) + : progress, + state + }, + user + ) + } } decorateInjectable(TransactionTaxReport, depsTypes) diff --git a/workers/loc.api/ws-transport/ws.event.emitter.js b/workers/loc.api/ws-transport/ws.event.emitter.js index 4d0f2350c..ff4ab8213 100644 --- a/workers/loc.api/ws-transport/ws.event.emitter.js +++ b/workers/loc.api/ws-transport/ws.event.emitter.js @@ -153,6 +153,21 @@ class WSEventEmitter extends AbstractWSEventEmitter { }, 'emitTrxTaxReportGenerationInBackgroundToOne') } + emitTrxTaxReportGenerationProgressToOne ( + handler = () => {}, + auth = {} + ) { + return this.emit(async (user, ...args) => { + if (this.isNotTargetUser(auth, user)) { + return { isNotEmitted: true } + } + + return typeof handler === 'function' + ? await handler(user, ...args) + : handler + }, 'emitTrxTaxReportGenerationProgressToOne') + } + async emitRedirectingRequestsStatusToApi ( handler = () => {} ) {