98 lines
3.7 KiB
JavaScript
98 lines
3.7 KiB
JavaScript
"use strict";
|
||
Object.defineProperty(exports, "__esModule", { value: true });
|
||
const tslib_1 = require("tslib");
|
||
const pay_1 = require("../utils/pay");
|
||
const payClazz_1 = require("../utils/payClazz");
|
||
const assert_1 = tslib_1.__importDefault(require("assert"));
|
||
const uuid_1 = require("oak-domain/lib/utils/uuid");
|
||
const operationResult_1 = require("oak-domain/lib/utils/operationResult");
|
||
const QUERY_PAYING_STATE_GAP = process.env.NODE_ENV === 'production' ? 3600 * 1000 : 60 * 1000;
|
||
const watchers = [
|
||
{
|
||
name: '对paying状态的订单,同步其真实支付状态',
|
||
entity: 'pay',
|
||
filter: async () => {
|
||
const now = Date.now();
|
||
return {
|
||
iState: 'paying',
|
||
$$updateAt$$: {
|
||
$lte: now - QUERY_PAYING_STATE_GAP,
|
||
},
|
||
externalId: {
|
||
$exists: true,
|
||
}
|
||
};
|
||
},
|
||
projection: pay_1.fullPayProjection,
|
||
fn: async (context, data) => {
|
||
const results = [];
|
||
for (const pay of data) {
|
||
const { applicationId, entity, entityId, timeoutAt, price } = pay;
|
||
const clazz = await (0, payClazz_1.getPayClazz)(applicationId, entity, entityId, context);
|
||
const [iState, updateData] = await clazz.getState(pay);
|
||
if (iState !== pay.iState) {
|
||
let action = 'close';
|
||
switch (iState) {
|
||
case 'closed': {
|
||
// action = 'close';
|
||
break;
|
||
}
|
||
case 'paid': {
|
||
action = 'succeedPaying';
|
||
updateData.paid = price;
|
||
break;
|
||
}
|
||
default: {
|
||
(0, assert_1.default)(false);
|
||
}
|
||
}
|
||
const result = await context.operate('pay', {
|
||
id: await (0, uuid_1.generateNewIdAsync)(),
|
||
action,
|
||
data: updateData,
|
||
filter: {
|
||
id: pay.id,
|
||
}
|
||
}, {});
|
||
results.push(result);
|
||
}
|
||
else if (iState === 'paying' && timeoutAt < Date.now()) {
|
||
// 尝试关闭订单
|
||
// 理论上用户可能在上一个getState和这个close之间支付完成,此时关闭失败,下一个Watcher到来时就能处理成功了
|
||
const result = await context.operate('pay', {
|
||
id: await (0, uuid_1.generateNewIdAsync)(),
|
||
action: 'close',
|
||
data: updateData,
|
||
filter: {
|
||
id: pay.id,
|
||
}
|
||
}, {});
|
||
results.push(result);
|
||
}
|
||
}
|
||
if (results.length === 0) {
|
||
return {};
|
||
}
|
||
return results.reduce((prev, cur) => (0, operationResult_1.mergeOperationResult)(prev, cur));
|
||
}
|
||
},
|
||
{
|
||
name: '当pay达到禁止退款期限时,关闭退款允许',
|
||
entity: 'pay',
|
||
filter: () => {
|
||
const now = Date.now();
|
||
return {
|
||
refundable: true,
|
||
forbidRefundAt: {
|
||
$lte: now,
|
||
},
|
||
};
|
||
},
|
||
action: 'closeRefund',
|
||
actionData: {
|
||
refundable: false,
|
||
}
|
||
}
|
||
];
|
||
exports.default = watchers;
|