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

106 lines
3.9 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 PayConfig_1 = require("../types/PayConfig");
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, 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;
await context.operate('account', {
id: await (0, uuid_1.generateNewIdAsync)(),
action: type,
data: {
total: total + totalPlus,
avail: avail + availPlus,
refundable: refundable + (refundablePlus || 0),
},
filter: {
id: accountId,
}
}, option);
context.saveOperationToEvent(id, `${constants_1.DATA_SUBSCRIBER_KEYS.accountNumberChanged}-${accountId}`);
return 1;
},
},
{
name: '当account depoist时根据系统配置扣除掉损耗如果有没支付完整的account类型的pay则继续支付',
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;
(0, assert_1.default)(PayConfig_1.PAY_ORG_CHANNELS.includes(channel));
const ratio = (0, pay_1.getDepositRatio)(channel, application);
let count = 0;
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,
}
}, {});
count++;
}
return count;
}
},
];
exports.default = triggers;