perf: optimize portfolio queries and workflows
This commit is contained in:
1 parent
da98d02661
commit
a7e0a0fe58
45 files changed
+4446
-637
No files matched your search
@@ -0,0 +1,23 @@
|
||||
import Decimal from 'decimal.js';
|
||||
import { BadRequestException } from '@nestjs/common';
|
||||
export type Movement = 'transfer' | 'borrow' | 'lend' | 'collect' | 'repay';
|
||||
export function movementDeltas(operation: Movement, amount: string, received: string, fee: string) {
|
||||
const incoming = operation === 'borrow' || operation === 'collect';
|
||||
const principal = new Decimal(amount),
|
||||
charge = new Decimal(fee),
|
||||
debt = new Decimal(received);
|
||||
if (operation === 'transfer')
|
||||
return {
|
||||
source: principal.plus(charge).neg(),
|
||||
target: debt,
|
||||
sourceReason: 'transfer_out',
|
||||
targetReason: 'transfer_in',
|
||||
};
|
||||
if (incoming && charge.gt(principal)) throw new BadRequestException('收款手续费不能超过收款金额');
|
||||
return {
|
||||
source: incoming ? principal.minus(charge) : principal.plus(charge).neg(),
|
||||
target: operation === 'borrow' || operation === 'lend' ? debt : debt.neg(),
|
||||
sourceReason: 'loan_' + operation,
|
||||
targetReason: 'loan_' + operation,
|
||||
};
|
||||
}
|
||||
Reference in new issue
Block a user