fix: use base_amount to calculate commission

pull/8/head
Inga 🏳‍🌈 10 months ago
parent dcf34b6791
commit 296fdb4945
  1. 2
      src/transaction/transaction.controller.spec.ts
  2. 48
      src/transaction/transaction.controller.ts

@ -72,7 +72,7 @@ describe('TransactionController Unit Tests', () => {
it('calling applyRules method minimum result of applied rules should be got', async () => { it('calling applyRules method minimum result of applied rules should be got', async () => {
const mockTransactionInput = { const mockTransactionInput = {
date: '2021-01-05', date: '2021-01-05',
amount: 1000.0, base_amount: 1000.0,
currency: 'EUR', currency: 'EUR',
client_id: 1, client_id: 1,
}; };

@ -14,7 +14,7 @@ import {
} from './transaction.dto'; } from './transaction.dto';
import { Transaction } from './transaction.entity'; import { Transaction } from './transaction.entity';
type TransactionRuleData = Pick<Transaction, 'amount' | 'client_id'>; type TransactionRuleData = Pick<Transaction, 'base_amount' | 'client_id'>;
@Controller('transaction') @Controller('transaction')
export class TransactionController { export class TransactionController {
@ -83,7 +83,8 @@ export class TransactionController {
defaultRule(transactionInput: TransactionRuleData) { defaultRule(transactionInput: TransactionRuleData) {
const commissionAmount = const commissionAmount =
(transactionInput.amount * DefaultCommissionPercentage.percentage) / 100; (transactionInput.base_amount * DefaultCommissionPercentage.percentage) /
100;
return commissionAmount < DefaultCommissionAmount.amount return commissionAmount < DefaultCommissionAmount.amount
? DefaultCommissionAmount.amount ? DefaultCommissionAmount.amount
: commissionAmount; : commissionAmount;
@ -123,12 +124,20 @@ export class TransactionController {
}); });
} }
getAmountWithExchange(transactionInput: TransactionInput) { async getAmountWithExchange(transactionInput: TransactionInput) {
const exchangeRateResponse = await this.getExchangeRate({
date: transactionInput.date,
});
const transactionAmount = parseInt(transactionInput.amount);
const transactionData = { const transactionData = {
date: transactionInput.date, date: transactionInput.date,
amount: parseInt(transactionInput.amount), amount: transactionAmount,
currency: transactionInput.currency, currency: transactionInput.currency,
client_id: transactionInput.client_id, client_id: transactionInput.client_id,
base_currency: Currency.EUR,
base_amount:
transactionAmount * exchangeRateResponse[transactionInput.currency],
}; };
const commissionAmount = this.applyRules( const commissionAmount = this.applyRules(
@ -136,22 +145,12 @@ export class TransactionController {
transactionData, transactionData,
); );
const exhangeRateInput = { commissionAmount
date: transactionInput.date, .then((commission) =>
}; this.transactionService.insertOne({
...transactionData,
this.getExchangeRate(exhangeRateInput) commission,
.then((exchangeRateResponse) => }),
commissionAmount.then((commission) =>
this.transactionService.insertOne({
...transactionData,
commission,
base_currency: Currency.EUR,
base_amount:
parseInt(transactionInput.amount) *
exchangeRateResponse[transactionInput.currency],
}),
),
) )
.catch((error) => console.log(error)); .catch((error) => console.log(error));
@ -164,6 +163,8 @@ export class TransactionController {
amount: parseInt(transactionInput.amount), amount: parseInt(transactionInput.amount),
currency: transactionInput.currency, currency: transactionInput.currency,
client_id: transactionInput.client_id, client_id: transactionInput.client_id,
base_currency: Currency.EUR,
base_amount: parseInt(transactionInput.amount),
}; };
const commissionAmount = await this.applyRules( const commissionAmount = await this.applyRules(
@ -173,13 +174,8 @@ export class TransactionController {
try { try {
this.transactionService this.transactionService
.insertOne({ .insertOne({
date: transactionInput.date, ...transactionData,
amount: parseInt(transactionInput.amount),
currency: transactionInput.currency,
client_id: transactionInput.client_id,
commission: commissionAmount, commission: commissionAmount,
base_currency: Currency.EUR,
base_amount: parseInt(transactionInput.amount),
}) })
.catch((error) => console.log(error)); .catch((error) => console.log(error));
} catch (error) { } catch (error) {

Loading…
Cancel
Save