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