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

101 lines
3.6 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.

"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 pay_1 = require("../utils/pay");
const triggers = [
{
name: '当生成accountOper时修改account中的值并向订阅者推送',
entity: 'accountOper',
action: 'create',
when: 'after',
fn: async ({ operation }, context, option) => {
const { id, data } = operation;
(0, assert_1.default)(!(data instanceof Array));
const { accountId, totalPlus, availPlus, type } = data;
const [account] = await context.select('account', {
data: {
id: 1,
total: 1,
avail: 1,
},
filter: {
id: accountId,
}
}, { forUpdate: true });
const { total, avail } = account;
await context.operate('account', {
id: await (0, uuid_1.generateNewIdAsync)(),
action: type,
data: {
total: total + totalPlus,
avail: avail + availPlus,
},
filter: {
id: accountId,
}
}, option);
context.saveOperationToEvent(id, `${constants_1.DATA_SUBSCRIBER_KEYS.accountNumberChanged}-${accountId}`);
return 1;
},
},
{
name: '当account depoist时根据系统配置扣除掉损耗',
entity: 'accountOper',
action: 'create',
when: 'after',
check(operation) {
const { data } = operation;
(0, assert_1.default)(!(data instanceof Array));
return data.type === 'deposit';
},
fn: async ({ operation }, context, option) => {
const { data } = operation;
(0, assert_1.default)(!(data instanceof Array));
const { entity, entityId } = data;
(0, assert_1.default)(entity === 'pay' && entityId);
const [pay] = await context.select('pay', {
data: {
id: 1,
price: 1,
channel: 1,
application: {
id: 1,
payConfig: 1,
system: {
id: 1,
payConfig: 1,
},
}
},
filter: {
id: entityId,
},
}, {});
const { price, application, channel } = pay;
const ratio = (0, pay_1.getDepositRatio)(channel, application);
if (ratio > 0) {
const loss = Math.ceil(price * ratio / 100);
await context.operate('accountOper', {
id: await (0, uuid_1.generateNewIdAsync)(),
action: 'create',
data: {
id: await (0, uuid_1.generateNewIdAsync)(),
type: 'loss',
totalPlus: -loss,
availPlus: -loss,
entity,
entityId,
accountId: data.accountId,
}
}, {});
return 1;
}
return 0;
}
},
];
exports.default = triggers;