refactor: improved transacion service somewhat (closes #22)

pull/24/head
Inga 🏳‍🌈 11 months ago
parent 62aa1a7d20
commit 90979e0631
  1. 34
      src/shared/reactive.ts
  2. 41
      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 {
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<Transaction, 'base_amount' | 'client_id'>;
@ -40,25 +38,15 @@ export class TransactionController {
getClientDeposit = async (transactionInput: TransactionRuleData) => {
try {
const deposit =
await this.transactionService.findByClientIdWithinActualMonth(
const existingTransactions = 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;
}
return _.sum(existingTransactions.map(t => t.base_amount));
} catch (error) {
console.log(error);
}
return 0;
}
};
turnoverRule = async (transactionInput: TransactionRuleData) => {
@ -94,18 +82,7 @@ export class TransactionController {
);
}
getExchangeRateResponse(exchangeRateInput: ExchangeRateInput) {
return new Promise<ExchangeRateResponse>((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) {

Loading…
Cancel
Save