60 lines
2.4 KiB
JavaScript
60 lines
2.4 KiB
JavaScript
"use strict";
|
||
Object.defineProperty(exports, "__esModule", { value: true });
|
||
const tslib_1 = require("tslib");
|
||
const uuid_1 = require("oak-domain/lib/utils/uuid");
|
||
const assert_1 = tslib_1.__importDefault(require("assert"));
|
||
const constants_1 = require("../config/constants");
|
||
const triggers = [
|
||
{
|
||
name: '当生成accountOper时,修改account中的值,并向订阅者推送',
|
||
entity: 'accountOper',
|
||
action: 'create',
|
||
when: 'before',
|
||
asRoot: true,
|
||
fn: async ({ operation }, context, option) => {
|
||
const { id, data } = operation;
|
||
(0, assert_1.default)(!(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 (0, uuid_1.generateNewIdAsync)(),
|
||
action: type,
|
||
data: {
|
||
total: total2,
|
||
avail: avail2,
|
||
refundable: refundable2,
|
||
},
|
||
};
|
||
context.saveOperationToEvent(id, `${constants_1.DATA_SUBSCRIBER_KEYS.accountNumberChanged}-${accountId}`);
|
||
return 1;
|
||
},
|
||
},
|
||
];
|
||
exports.default = triggers;
|