oak-pay-business/es/triggers/withdraw.js

121 lines
4.3 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { generateNewIdAsync } from 'oak-domain/lib/utils/uuid';
import assert from 'assert';
const triggers = [
{
name: '当withdraw创建时如果是走退款渠道则创建出相应的refunds同时更改account中的数值',
entity: 'withdraw',
action: 'create',
when: 'before',
fn: async ({ operation }, context, option) => {
const { data } = operation;
assert(!(data instanceof Array));
const { accountId, price, refund$withdraw: refunds, withdrawTransfer$withdraw: transfers } = data;
let refundAmount = 0;
refunds?.forEach(({ data }) => {
const { price } = data;
assert(price);
refundAmount += price;
});
let transferAmount = 0;
transfers?.forEach(({ data }) => {
const { price } = data;
assert(price);
transferAmount += price;
});
assert(refundAmount + transferAmount === price);
data.dealPrice = data.dealLoss = 0;
data.creatorId = context.getCurrentUserId();
data.accountOper$entity = [
{
id: await generateNewIdAsync(),
action: 'create',
data: {
id: await generateNewIdAsync(),
accountId,
type: 'withdraw',
totalPlus: -price,
availPlus: -price,
refundablePlus: -refundAmount,
},
}
];
return 1;
},
},
{
name: '当withdraw失败时将total和avail还回到帐户',
entity: 'withdraw',
action: 'fail',
when: 'after',
fn: async ({ operation }, context, option) => {
const { filter, data } = operation;
const withdraws = await context.select('withdraw', {
data: {
id: 1,
accountId: 1,
iState: 1,
price: 1,
dealPrice: 1,
},
filter,
}, {});
for (const withdraw of withdraws) {
const { accountId, price, dealPrice } = withdraw;
assert(dealPrice === 0);
await context.operate('accountOper', {
id: await generateNewIdAsync(),
action: 'create',
data: {
id: await generateNewIdAsync(),
accountId,
type: 'withdrawBack',
totalPlus: price,
availPlus: price,
entity: 'withdraw',
entityId: withdraw.id,
},
}, {});
}
return withdraws.length;
},
},
{
name: '当withdraw部分成功时将差价部分还回到帐户中',
entity: 'withdraw',
action: 'succeedPartially',
when: 'after',
fn: async ({ operation }, context) => {
const { filter, data } = operation;
const withdraws = await context.select('withdraw', {
data: {
id: 1,
accountId: 1,
iState: 1,
price: 1,
dealPrice: 1,
},
filter,
}, {});
for (const withdraw of withdraws) {
const { accountId, price, dealPrice } = withdraw;
assert(price > dealPrice && dealPrice > 0);
await context.operate('accountOper', {
id: await generateNewIdAsync(),
action: 'create',
data: {
id: await generateNewIdAsync(),
accountId,
type: 'withdrawBack',
totalPlus: price - dealPrice,
availPlus: price - dealPrice,
entity: 'withdraw',
entityId: withdraw.id,
},
}, {});
}
return withdraws.length;
}
}
];
export default triggers;