148 lines
5.6 KiB
JavaScript
148 lines
5.6 KiB
JavaScript
import { generateNewId, generateNewIdAsync } from 'oak-domain/lib/utils/uuid';
|
||
import assert from 'assert';
|
||
import { getAccountPayRefunds } from '../utils/pay';
|
||
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 { withdrawAccountId, accountId, price } = data;
|
||
if (!withdrawAccountId) {
|
||
// 没有指定渠道则走退款,前台通过getAccountPayRefunds的aspect去获得refund数据并挂载
|
||
if (!data.refund$entity) {
|
||
const refundData = await getAccountPayRefunds(context, accountId, price);
|
||
data.refund$entity = refundData.map((data) => ({
|
||
id: generateNewId(),
|
||
action: 'create',
|
||
data,
|
||
}));
|
||
let loss = 0;
|
||
data.refund$entity.forEach((ele) => {
|
||
const { data } = ele;
|
||
loss += data.loss || 0;
|
||
});
|
||
data.loss = loss;
|
||
}
|
||
data.iState = 'withdrawing';
|
||
}
|
||
else {
|
||
// 否则走渠道提现,暂时假设为人工操作
|
||
const [withdrawAccount] = await context.select('withdrawAccount', {
|
||
data: {
|
||
id: 1,
|
||
channel: {
|
||
id: 1,
|
||
lossRatio: 1,
|
||
},
|
||
},
|
||
filter: {
|
||
id: withdrawAccountId,
|
||
}
|
||
}, { dontCollect: true });
|
||
const { channel } = withdrawAccount;
|
||
const { lossRatio } = channel;
|
||
data.loss = lossRatio ? Math.ceil(data.price * lossRatio / 100) : 0;
|
||
data.iState = 'applying';
|
||
}
|
||
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: withdrawAccountId ? 0 : -price,
|
||
},
|
||
}
|
||
];
|
||
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,
|
||
withdrawAccountId: 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,
|
||
withdrawAccountId: 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;
|