|
|
@ -1,5 +1,4 @@ |
|
|
|
import { Controller, Post, UsePipes, Body } from '@nestjs/common'; |
|
|
|
import { Controller, Post, UsePipes, Body } from '@nestjs/common'; |
|
|
|
import _ from 'lodash'; |
|
|
|
|
|
|
|
import { |
|
|
|
import { |
|
|
|
ExchangeRateInput, |
|
|
|
ExchangeRateInput, |
|
|
|
ExchangeRateResponse, |
|
|
|
ExchangeRateResponse, |
|
|
@ -17,6 +16,7 @@ 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'>; |
|
|
|
|
|
|
|
|
|
|
@ -90,16 +90,11 @@ export class TransactionController { |
|
|
|
: commissionAmount; |
|
|
|
: commissionAmount; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
async applyRules( |
|
|
|
async applyRules(transactionInput: TransactionRuleData) { |
|
|
|
rules: (( |
|
|
|
return getMinimumRuleOutput( |
|
|
|
transactionInput: TransactionRuleData, |
|
|
|
[this.turnoverRule, this.discountRule, this.defaultRule], |
|
|
|
) => number | false | undefined | Promise<number | false | undefined>)[], |
|
|
|
transactionInput, |
|
|
|
transactionInput: TransactionRuleData, |
|
|
|
|
|
|
|
) { |
|
|
|
|
|
|
|
const commissions = await Promise.all( |
|
|
|
|
|
|
|
[...rules, this.defaultRule].map((rule) => rule(transactionInput)), |
|
|
|
|
|
|
|
); |
|
|
|
); |
|
|
|
return Math.min(..._.compact(commissions)); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
getExchangeRate(exchangeRateInput: ExchangeRateInput) { |
|
|
|
getExchangeRate(exchangeRateInput: ExchangeRateInput) { |
|
|
@ -131,10 +126,7 @@ export class TransactionController { |
|
|
|
transactionAmount / exchangeRateResponse[transactionInput.currency], |
|
|
|
transactionAmount / exchangeRateResponse[transactionInput.currency], |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
const commissionAmount = this.applyRules( |
|
|
|
const commissionAmount = this.applyRules(transactionData); |
|
|
|
[this.turnoverRule, this.discountRule], |
|
|
|
|
|
|
|
transactionData, |
|
|
|
|
|
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
commissionAmount |
|
|
|
commissionAmount |
|
|
|
.then((commission) => |
|
|
|
.then((commission) => |
|
|
@ -158,10 +150,7 @@ export class TransactionController { |
|
|
|
base_amount: parseInt(transactionInput.amount), |
|
|
|
base_amount: parseInt(transactionInput.amount), |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
const commissionAmount = await this.applyRules( |
|
|
|
const commissionAmount = await this.applyRules(transactionData); |
|
|
|
[this.turnoverRule, this.discountRule], |
|
|
|
|
|
|
|
transactionData, |
|
|
|
|
|
|
|
); |
|
|
|
|
|
|
|
try { |
|
|
|
try { |
|
|
|
this.transactionService |
|
|
|
this.transactionService |
|
|
|
.insertOne({ |
|
|
|
.insertOne({ |
|
|
|