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. 40
      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 () => {
const mockTransactionInput = {
date: '2021-01-05',
amount: 1000.0,
base_amount: 1000.0,
currency: 'EUR',
client_id: 1,
};

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

Loading…
Cancel
Save