diff --git a/src/shared/reactive.ts b/src/shared/reactive.ts new file mode 100644 index 0000000..e3a68a4 --- /dev/null +++ b/src/shared/reactive.ts @@ -0,0 +1,34 @@ +import type { Observable } from 'rxjs'; + +export const promisifyObservable = + (f: (...args: TArgs) => Observable) => + (...args: TArgs) => + new Promise((_resolve, _reject) => { + let isCalled = false; + const resolve = (result: TOutput) => { + if (isCalled) { + console.log('observable emits more than once'); + return; + } + + _resolve(result); + }; + + const reject = (error: unknown) => { + if (isCalled) { + console.log('observable emits more than once'); + return; + } + + _reject(error); + }; + + try { + f(...args).subscribe({ + next: resolve, + error: reject, + }); + } catch (error) { + reject(error); + } + }); diff --git a/src/transaction/transaction.controller.ts b/src/transaction/transaction.controller.ts index 9bbd985..959d914 100644 --- a/src/transaction/transaction.controller.ts +++ b/src/transaction/transaction.controller.ts @@ -1,12 +1,11 @@ import { Controller, Post, UsePipes, Body } from '@nestjs/common'; -import { - ExchangeRateInput, - ExchangeRateResponse, -} from 'src/exchange-rate/exchange-rate.dto'; -import { ExchangeRateService } from 'src/exchange-rate/exchange-rate.service'; +import _ from 'lodash'; +import { BodyValidationPipe } from '../pipes/body.validation.pipe'; +import { ExchangeRateService } from '../exchange-rate/exchange-rate.service'; +import { promisifyObservable } from '../shared/reactive'; +import { getMinimumRuleOutput } from '../shared/rules'; import { TransactionService } from './transaction.service'; import { transactionBodySchema } from './transaction.validation'; -import { BodyValidationPipe } from '../pipes/body.validation.pipe'; import { Currency, TransactionInput, @@ -16,7 +15,6 @@ import { HighTurnoverDiscount, } from './transaction.dto'; import { Transaction } from './transaction.entity'; -import { getMinimumRuleOutput } from 'src/shared/rules'; type TransactionRuleData = Pick; @@ -40,25 +38,15 @@ export class TransactionController { getClientDeposit = async (transactionInput: TransactionRuleData) => { try { - const deposit = - await this.transactionService.findByClientIdWithinActualMonth( - transactionInput.client_id, - ); - - if (deposit) { - const initialDeposit = 0; - const totalDeposit = (await deposit).reduce( - (prevAmmount, transactionAmmount) => - prevAmmount + transactionAmmount.base_amount, - initialDeposit, - ); - - return totalDeposit; - } + const existingTransactions = await this.transactionService.findByClientIdWithinActualMonth( + transactionInput.client_id, + ); + + return _.sum(existingTransactions.map(t => t.base_amount)); } catch (error) { console.log(error); + return 0; } - return 0; }; turnoverRule = async (transactionInput: TransactionRuleData) => { @@ -94,18 +82,7 @@ export class TransactionController { ); } - getExchangeRateResponse(exchangeRateInput: ExchangeRateInput) { - return new Promise((resolve, reject) => { - try { - this.exchangeRateService.convertCurrency(exchangeRateInput).subscribe({ - next: resolve, - error: reject, - }); - } catch (error) { - reject(error); - } - }); - } + getExchangeRateResponse = promisifyObservable(this.exchangeRateService.convertCurrency.bind(this.exchangeRateService)); async getExchangeRateIfNeeded(transactionInput: TransactionInput) { if (transactionInput.currency === Currency.EUR) {