|
|
@ -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'>; |
|
|
|
|
|
|
|
|
|
|
@ -33,10 +33,7 @@ export class TransactionController { |
|
|
|
@Body() transactionInput: TransactionInput, |
|
|
|
@Body() transactionInput: TransactionInput, |
|
|
|
): Promise<string> { |
|
|
|
): Promise<string> { |
|
|
|
return JSON.stringify({ |
|
|
|
return JSON.stringify({ |
|
|
|
amount: |
|
|
|
amount: (await this.getCommissionAmount(transactionInput)).toFixed(2), |
|
|
|
transactionInput.currency !== Currency.EUR |
|
|
|
|
|
|
|
? (await this.getAmountWithExchange(transactionInput)).toFixed(2) |
|
|
|
|
|
|
|
: (await this.getAmountWithoutExchange(transactionInput)).toFixed(2), |
|
|
|
|
|
|
|
currency: Currency.EUR, |
|
|
|
currency: Currency.EUR, |
|
|
|
}); |
|
|
|
}); |
|
|
|
} |
|
|
|
} |
|
|
@ -90,19 +87,14 @@ 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) { |
|
|
|
getExchangeRateResponse(exchangeRateInput: ExchangeRateInput) { |
|
|
|
return new Promise<ExchangeRateResponse>((resolve, reject) => { |
|
|
|
return new Promise<ExchangeRateResponse>((resolve, reject) => { |
|
|
|
try { |
|
|
|
try { |
|
|
|
this.exchangeRateService.convertCurrency(exchangeRateInput).subscribe({ |
|
|
|
this.exchangeRateService.convertCurrency(exchangeRateInput).subscribe({ |
|
|
@ -115,11 +107,21 @@ export class TransactionController { |
|
|
|
}); |
|
|
|
}); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
async getAmountWithExchange(transactionInput: TransactionInput) { |
|
|
|
async getExchangeRateIfNeeded(transactionInput: TransactionInput) { |
|
|
|
const exchangeRateResponse = await this.getExchangeRate({ |
|
|
|
if (transactionInput.currency === Currency.EUR) { |
|
|
|
|
|
|
|
return 1; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const exchangeRateResponse = await this.getExchangeRateResponse({ |
|
|
|
date: transactionInput.date, |
|
|
|
date: transactionInput.date, |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return exchangeRateResponse[transactionInput.currency]; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async getCommissionAmount(transactionInput: TransactionInput) { |
|
|
|
|
|
|
|
const exchangeRate = await this.getExchangeRateIfNeeded(transactionInput); |
|
|
|
|
|
|
|
|
|
|
|
const transactionAmount = parseInt(transactionInput.amount); |
|
|
|
const transactionAmount = parseInt(transactionInput.amount); |
|
|
|
const transactionData = { |
|
|
|
const transactionData = { |
|
|
|
date: transactionInput.date, |
|
|
|
date: transactionInput.date, |
|
|
@ -127,53 +129,18 @@ export class TransactionController { |
|
|
|
currency: transactionInput.currency, |
|
|
|
currency: transactionInput.currency, |
|
|
|
client_id: transactionInput.client_id, |
|
|
|
client_id: transactionInput.client_id, |
|
|
|
base_currency: Currency.EUR, |
|
|
|
base_currency: Currency.EUR, |
|
|
|
base_amount: |
|
|
|
base_amount: transactionAmount / exchangeRate, |
|
|
|
transactionAmount / exchangeRateResponse[transactionInput.currency], |
|
|
|
|
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
const commissionAmount = this.applyRules( |
|
|
|
const commission = await this.applyRules(transactionData); |
|
|
|
[this.turnoverRule, this.discountRule], |
|
|
|
|
|
|
|
transactionData, |
|
|
|
|
|
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
commissionAmount |
|
|
|
this.transactionService |
|
|
|
.then((commission) => |
|
|
|
.insertOne({ |
|
|
|
this.transactionService.insertOne({ |
|
|
|
...transactionData, |
|
|
|
...transactionData, |
|
|
|
commission, |
|
|
|
commission, |
|
|
|
}) |
|
|
|
}), |
|
|
|
|
|
|
|
) |
|
|
|
|
|
|
|
.catch((error) => console.log(error)); |
|
|
|
.catch((error) => console.log(error)); |
|
|
|
|
|
|
|
|
|
|
|
return commissionAmount; |
|
|
|
return commission; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async getAmountWithoutExchange(transactionInput: TransactionInput) { |
|
|
|
|
|
|
|
const transactionData = { |
|
|
|
|
|
|
|
date: transactionInput.date, |
|
|
|
|
|
|
|
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( |
|
|
|
|
|
|
|
[this.turnoverRule, this.discountRule], |
|
|
|
|
|
|
|
transactionData, |
|
|
|
|
|
|
|
); |
|
|
|
|
|
|
|
try { |
|
|
|
|
|
|
|
this.transactionService |
|
|
|
|
|
|
|
.insertOne({ |
|
|
|
|
|
|
|
...transactionData, |
|
|
|
|
|
|
|
commission: commissionAmount, |
|
|
|
|
|
|
|
}) |
|
|
|
|
|
|
|
.catch((error) => console.log(error)); |
|
|
|
|
|
|
|
} catch (error) { |
|
|
|
|
|
|
|
console.log(error); |
|
|
|
|
|
|
|
throw error; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return commissionAmount; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|