|
|
|
@ -1,5 +1,5 @@ |
|
|
|
|
import { Controller, Post, UsePipes, Body } from '@nestjs/common'; |
|
|
|
|
import _ from 'lodash'; |
|
|
|
|
import _, { endsWith } from 'lodash'; |
|
|
|
|
import { BodyValidationPipe } from '../pipes/body.validation.pipe'; |
|
|
|
|
import { ExchangeRateService } from '../exchange-rate/exchange-rate.service'; |
|
|
|
|
import { promisifyObservable } from '../shared/reactive'; |
|
|
|
@ -9,14 +9,14 @@ import { transactionBodySchema } from './transaction.validation'; |
|
|
|
|
import { |
|
|
|
|
Currency, |
|
|
|
|
TransactionInput, |
|
|
|
|
DiscountRuleForClientById, |
|
|
|
|
DefaultCommissionPercentage, |
|
|
|
|
DefaultCommissionAmount, |
|
|
|
|
HighTurnoverDiscount, |
|
|
|
|
DISCOUNTED_COMMISSIONS, |
|
|
|
|
} from './transaction.dto'; |
|
|
|
|
import { Transaction } from './transaction.entity'; |
|
|
|
|
|
|
|
|
|
type TransactionRuleData = Pick<Transaction, 'base_amount' | 'client_id'>; |
|
|
|
|
type TransactionRuleData = Pick<Transaction, 'base_amount' | 'client_id' | 'date'>; |
|
|
|
|
|
|
|
|
|
@Controller('transaction') |
|
|
|
|
export class TransactionController { |
|
|
|
@ -38,11 +38,12 @@ export class TransactionController { |
|
|
|
|
|
|
|
|
|
getClientDeposit = async (transactionInput: TransactionRuleData) => { |
|
|
|
|
try { |
|
|
|
|
const existingTransactions = await this.transactionService.findByClientIdWithinActualMonth( |
|
|
|
|
transactionInput.client_id, |
|
|
|
|
); |
|
|
|
|
const existingTransactions = |
|
|
|
|
await this.transactionService.findByClientIdWithinActualMonth( |
|
|
|
|
transactionInput.client_id, |
|
|
|
|
); |
|
|
|
|
|
|
|
|
|
return _.sum(existingTransactions.map(t => t.base_amount)); |
|
|
|
|
return _.sum(existingTransactions.map((t) => t.base_amount)); |
|
|
|
|
} catch (error) { |
|
|
|
|
console.log(error); |
|
|
|
|
return 0; |
|
|
|
@ -61,8 +62,8 @@ export class TransactionController { |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
discountRule(transactionInput: TransactionRuleData) { |
|
|
|
|
return transactionInput.client_id === 42 |
|
|
|
|
? DiscountRuleForClientById.client_42 |
|
|
|
|
return DISCOUNTED_COMMISSIONS.has(transactionInput.client_id) |
|
|
|
|
? DISCOUNTED_COMMISSIONS.get(transactionInput.client_id) |
|
|
|
|
: false; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -75,14 +76,24 @@ export class TransactionController { |
|
|
|
|
: commissionAmount; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
firstDayOfMonthFreeRule(transactionInput: TransactionRuleData) { |
|
|
|
|
if (transactionInput.date.endsWith('-01')) { |
|
|
|
|
return 0; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return undefined; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
async applyRules(transactionInput: TransactionRuleData) { |
|
|
|
|
return getMinimumRuleOutput( |
|
|
|
|
[this.turnoverRule, this.discountRule, this.defaultRule], |
|
|
|
|
[this.turnoverRule, this.discountRule, this.firstDayOfMonthFreeRule, this.defaultRule], |
|
|
|
|
transactionInput, |
|
|
|
|
); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
getExchangeRateResponse = promisifyObservable(this.exchangeRateService.convertCurrency.bind(this.exchangeRateService)); |
|
|
|
|
getExchangeRateResponse = promisifyObservable( |
|
|
|
|
this.exchangeRateService.convertCurrency.bind(this.exchangeRateService), |
|
|
|
|
); |
|
|
|
|
|
|
|
|
|
async getExchangeRateIfNeeded(transactionInput: TransactionInput) { |
|
|
|
|
if (transactionInput.currency === Currency.EUR) { |
|
|
|
|