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

57 lines
2.2 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';
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;