101 lines
4.2 KiB
JavaScript
101 lines
4.2 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 constants_1 = require("../config/constants");
|
||
const assert_1 = tslib_1.__importDefault(require("assert"));
|
||
const triggers = [
|
||
{
|
||
name: '当account帐户的值发生变化时,向订阅者推送',
|
||
entity: 'account',
|
||
action: ['deposit', 'withdraw', 'withdrawBack', 'consume', 'loan', 'repay'],
|
||
check(operation) {
|
||
return operation.data.hasOwnProperty('total') || operation.data.hasOwnProperty('avail');
|
||
},
|
||
when: 'after',
|
||
fn: async ({ operation }, context, option) => {
|
||
const { id, filter } = operation;
|
||
(0, assert_1.default)(typeof filter?.id === 'string');
|
||
context.saveOperationToEvent(id, `${constants_1.DATA_SUBSCRIBER_KEYS.accountNumberChanged}-${filter.id}`);
|
||
return 1;
|
||
},
|
||
},
|
||
{
|
||
name: '当account帐户的avail增加时,如果有等待中的pay,则继续完成支付',
|
||
entity: 'account',
|
||
action: ['deposit', 'withdrawBack', 'repay'],
|
||
when: 'after',
|
||
fn: async ({ operation }, context, option) => {
|
||
const { id, filter, data } = operation;
|
||
(0, assert_1.default)(typeof filter?.id === 'string');
|
||
if (data.avail && data.avail > 0) {
|
||
const [account] = await context.select('account', {
|
||
data: {
|
||
id: 1,
|
||
avail: 1,
|
||
pay$entity: {
|
||
$entity: 'pay',
|
||
data: {
|
||
id: 1,
|
||
paid: 1,
|
||
price: 1,
|
||
},
|
||
filter: {
|
||
iState: 'unpaid',
|
||
orderId: {
|
||
$exists: true,
|
||
}
|
||
}
|
||
}
|
||
},
|
||
filter: {
|
||
id: filter.id,
|
||
}
|
||
}, {});
|
||
const { avail, pay$entity: pays } = account;
|
||
let rest = avail;
|
||
let count = 0;
|
||
if (pays && pays.length > 0) {
|
||
for (const pay of pays) {
|
||
if (rest === 0) {
|
||
break;
|
||
}
|
||
const { price, paid } = pay;
|
||
const paid2 = Math.min(price - paid, rest);
|
||
if (paid2 > 0) {
|
||
await context.operate('pay', {
|
||
id: await (0, uuid_1.generateNewIdAsync)(),
|
||
action: 'continuePaying',
|
||
data: {
|
||
paid: paid + paid2,
|
||
accountOper$entity: [
|
||
{
|
||
id: await (0, uuid_1.generateNewIdAsync)(),
|
||
action: 'create',
|
||
data: {
|
||
id: await (0, uuid_1.generateNewIdAsync)(),
|
||
type: 'consume',
|
||
availPlus: -paid2,
|
||
totalPlus: -paid2,
|
||
accountId: filter.id,
|
||
}
|
||
}
|
||
]
|
||
},
|
||
filter: {
|
||
id: pay.id,
|
||
}
|
||
}, {});
|
||
rest = rest - paid2;
|
||
count++;
|
||
}
|
||
}
|
||
}
|
||
return 1;
|
||
}
|
||
return 0;
|
||
},
|
||
},
|
||
];
|
||
exports.default = triggers;
|