refactor: improved transacion service somewhat (closes #22) #24

Merged
inga-lovinde merged 1 commits from refactor-22-simplify-transaction-service into main 11 months ago
  1. 34
      src/shared/reactive.ts
  2. 47
      src/transaction/transaction.controller.ts

@ -0,0 +1,34 @@
import type { Observable } from 'rxjs';
export const promisifyObservable =
<TArgs extends any[], TOutput>(f: (...args: TArgs) => Observable<TOutput>) =>
(...args: TArgs) =>
new Promise<TOutput>((_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);
}
});

@ -1,12 +1,11 @@
import { Controller, Post, UsePipes, Body } from '@nestjs/common'; import { Controller, Post, UsePipes, Body } from '@nestjs/common';
import { import _ from 'lodash';
ExchangeRateInput, import { BodyValidationPipe } from '../pipes/body.validation.pipe';
ExchangeRateResponse, import { ExchangeRateService } from '../exchange-rate/exchange-rate.service';
} from 'src/exchange-rate/exchange-rate.dto'; import { promisifyObservable } from '../shared/reactive';
import { ExchangeRateService } from 'src/exchange-rate/exchange-rate.service'; import { getMinimumRuleOutput } from '../shared/rules';
import { TransactionService } from './transaction.service'; import { TransactionService } from './transaction.service';
import { transactionBodySchema } from './transaction.validation'; import { transactionBodySchema } from './transaction.validation';
import { BodyValidationPipe } from '../pipes/body.validation.pipe';
import { import {
Currency, Currency,
TransactionInput, TransactionInput,
@ -16,7 +15,6 @@ import {
HighTurnoverDiscount, HighTurnoverDiscount,
} from './transaction.dto'; } from './transaction.dto';
import { Transaction } from './transaction.entity'; import { Transaction } from './transaction.entity';
import { getMinimumRuleOutput } from 'src/shared/rules';
type TransactionRuleData = Pick<Transaction, 'base_amount' | 'client_id'>; type TransactionRuleData = Pick<Transaction, 'base_amount' | 'client_id'>;
@ -40,25 +38,15 @@ export class TransactionController {
getClientDeposit = async (transactionInput: TransactionRuleData) => { getClientDeposit = async (transactionInput: TransactionRuleData) => {
try { try {
const deposit = const existingTransactions = await this.transactionService.findByClientIdWithinActualMonth(
await this.transactionService.findByClientIdWithinActualMonth( transactionInput.client_id,
transactionInput.client_id, );
);
return _.sum(existingTransactions.map(t => t.base_amount));
if (deposit) {
const initialDeposit = 0;
const totalDeposit = (await deposit).reduce(
(prevAmmount, transactionAmmount) =>
prevAmmount + transactionAmmount.base_amount,
initialDeposit,
);
return totalDeposit;
}
} catch (error) { } catch (error) {
console.log(error); console.log(error);
return 0;
} }
return 0;
}; };
turnoverRule = async (transactionInput: TransactionRuleData) => { turnoverRule = async (transactionInput: TransactionRuleData) => {
@ -94,18 +82,7 @@ export class TransactionController {
); );
} }
getExchangeRateResponse(exchangeRateInput: ExchangeRateInput) { getExchangeRateResponse = promisifyObservable(this.exchangeRateService.convertCurrency.bind(this.exchangeRateService));
return new Promise<ExchangeRateResponse>((resolve, reject) => {
try {
this.exchangeRateService.convertCurrency(exchangeRateInput).subscribe({
next: resolve,
error: reject,
});
} catch (error) {
reject(error);
}
});
}
async getExchangeRateIfNeeded(transactionInput: TransactionInput) { async getExchangeRateIfNeeded(transactionInput: TransactionInput) {
if (transactionInput.currency === Currency.EUR) { if (transactionInput.currency === Currency.EUR) {

Loading…
Cancel
Save