57 lines
2.2 KiB
JavaScript
57 lines
2.2 KiB
JavaScript
import { generateNewIdAsync } from 'oak-domain/lib/utils/uuid';
|
||
import assert from 'assert';
|
||
import { DATA_SUBSCRIBER_KEYS } from '../config/constants';
|
||
const triggers = [
|
||
{
|
||
name: '当生成accountOper时,修改account中的值,并向订阅者推送',
|
||
entity: 'accountOper',
|
||
action: 'create',
|
||
when: 'before',
|
||
asRoot: true,
|
||
fn: async ({ operation }, context, option) => {
|
||
const { id, data } = operation;
|
||
assert(!(data instanceof Array));
|
||
const { accountId, totalPlus, availPlus, type, refundablePlus } = data;
|
||
const [account] = await context.select('account', {
|
||
data: {
|
||
id: 1,
|
||
total: 1,
|
||
avail: 1,
|
||
refundable: 1,
|
||
},
|
||
filter: {
|
||
id: accountId,
|
||
}
|
||
}, { forUpdate: true });
|
||
const { total, avail, refundable } = account;
|
||
// 提现和退款不能把账户提成负数
|
||
// if (['withdraw', 'refund'].includes(type!)) {
|
||
// if (avail! + availPlus! < 0) {
|
||
// throw new OakInputIllegalException('accountOper', ['availPlus'], 'withdraw::availOverflow');
|
||
// }
|
||
// if (refundablePlus && refundable ! + refundablePlus < 0) {
|
||
// throw new OakInputIllegalException('accountOper', ['availPlus'], 'withdraw::refundableOverflow');
|
||
// }
|
||
// }
|
||
const total2 = total + totalPlus;
|
||
const avail2 = avail + availPlus;
|
||
const refundable2 = refundable + (refundablePlus || 0);
|
||
data.total = total2;
|
||
data.avail = avail2;
|
||
data.refundable = refundable2;
|
||
data.account = {
|
||
id: await generateNewIdAsync(),
|
||
action: type,
|
||
data: {
|
||
total: total2,
|
||
avail: avail2,
|
||
refundable: refundable2,
|
||
},
|
||
};
|
||
context.saveOperationToEvent(id, `${DATA_SUBSCRIBER_KEYS.accountNumberChanged}-${accountId}`);
|
||
return 1;
|
||
},
|
||
},
|
||
];
|
||
export default triggers;
|