refactor: rules now accept partial transaction object instead of TransactionInput

pull/8/head
Inga 🏳‍🌈 10 months ago
parent 279e5230e5
commit ac7aabb7c4
  1. 4
      src/transaction/transaction.controller.spec.ts
  2. 43
      src/transaction/transaction.controller.ts

@ -70,9 +70,9 @@ 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: TransactionInput = { const mockTransactionInput = {
date: '2021-01-05', date: '2021-01-05',
amount: '1000.00', amount: 1000.0,
currency: 'EUR', currency: 'EUR',
client_id: 1, client_id: 1,
}; };

@ -12,6 +12,7 @@ import {
DefaultCommissionAmount, DefaultCommissionAmount,
HighTurnoverDiscount, HighTurnoverDiscount,
} from './transaction.dto'; } from './transaction.dto';
import { Transaction } from './transaction.entity';
@Controller('transaction') @Controller('transaction')
export class TransactionController { export class TransactionController {
@ -38,7 +39,9 @@ export class TransactionController {
}); });
} }
getClientDeposit = async (transactionInput: TransactionInput) => { getClientDeposit = async (
transactionInput: Pick<Transaction, 'client_id'>,
) => {
try { try {
const deposit = const deposit =
await this.transactionService.findByClientIdWithinActualMonth( await this.transactionService.findByClientIdWithinActualMonth(
@ -61,7 +64,7 @@ export class TransactionController {
return 0; return 0;
}; };
turnoverRule = async (transactionInput: TransactionInput) => { turnoverRule = async (transactionInput: Pick<Transaction, 'client_id'>) => {
try { try {
const clientDeposit = await this.getClientDeposit(transactionInput); const clientDeposit = await this.getClientDeposit(transactionInput);
if (clientDeposit) { if (clientDeposit) {
@ -72,24 +75,25 @@ export class TransactionController {
} }
}; };
discountRule(transactionInput: TransactionInput) { discountRule(transactionInput: Pick<Transaction, 'client_id'>) {
return transactionInput.client_id === 42 return transactionInput.client_id === 42
? DiscountRuleForClientById.client_42 ? DiscountRuleForClientById.client_42
: false; : false;
} }
defaultRule(transactionInput: TransactionInput) { defaultRule(transactionInput: Pick<Transaction, 'amount'>) {
const commissionAmount = const commissionAmount =
(parseInt(transactionInput.amount) / 100) * (transactionInput.amount * DefaultCommissionPercentage.percentage) / 100;
DefaultCommissionPercentage.percentage;
return commissionAmount < DefaultCommissionAmount.amount return commissionAmount < DefaultCommissionAmount.amount
? DefaultCommissionAmount.amount ? DefaultCommissionAmount.amount
: commissionAmount; : commissionAmount;
} }
async applyRules( async applyRules(
rules: ((transactionInput: TransactionInput) => any)[], rules: ((
transactionInput: TransactionInput, transactionInput: Pick<Transaction, 'client_id' | 'amount'>,
) => any)[],
transactionInput: Pick<Transaction, 'client_id' | 'amount'>,
) { ) {
let commissionAmount; let commissionAmount;
for (let i = 0; i < rules.length; i++) { for (let i = 0; i < rules.length; i++) {
@ -120,9 +124,16 @@ export class TransactionController {
} }
getAmountWithExchange(transactionInput: TransactionInput) { getAmountWithExchange(transactionInput: TransactionInput) {
const transactionData = {
date: transactionInput.date,
amount: parseInt(transactionInput.amount),
currency: transactionInput.currency,
client_id: transactionInput.client_id,
};
const commissionAmount = this.applyRules( const commissionAmount = this.applyRules(
[this.turnoverRule, this.discountRule], [this.turnoverRule, this.discountRule],
transactionInput, transactionData,
); );
const exhangeRateInput = { const exhangeRateInput = {
@ -133,10 +144,7 @@ export class TransactionController {
.then((exchangeRateResponse) => .then((exchangeRateResponse) =>
commissionAmount.then((commission) => commissionAmount.then((commission) =>
this.transactionService.insertOne({ this.transactionService.insertOne({
date: transactionInput.date, ...transactionData,
amount: parseInt(transactionInput.amount),
currency: transactionInput.currency,
client_id: transactionInput.client_id,
commission, commission,
base_currency: Currency.EUR, base_currency: Currency.EUR,
base_amount: base_amount:
@ -151,9 +159,16 @@ export class TransactionController {
} }
async getAmountWithoutExchange(transactionInput: TransactionInput) { async getAmountWithoutExchange(transactionInput: TransactionInput) {
const transactionData = {
date: transactionInput.date,
amount: parseInt(transactionInput.amount),
currency: transactionInput.currency,
client_id: transactionInput.client_id,
};
const commissionAmount = await this.applyRules( const commissionAmount = await this.applyRules(
[this.turnoverRule, this.discountRule], [this.turnoverRule, this.discountRule],
transactionInput, transactionData,
); );
try { try {
this.transactionService this.transactionService

Loading…
Cancel
Save