oak-pay-business/es/watchers/pay.js

71 lines
2.0 KiB
JavaScript
Raw Permalink 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.

import { fullPayProjection, refreshPayState } from '../utils/pay';
import { mergeOperationResult } from 'oak-domain/lib/utils/operationResult';
const QUERY_PAYING_STATE_GAP = process.env.NODE_ENV === 'production' ? 600 * 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: fullPayProjection,
fn: async (context, data) => {
const results = [];
for (const pay of data) {
const result = await refreshPayState(pay, context);
if (result) {
results.push(result);
}
}
if (results.length === 0) {
return {};
}
return results.reduce((prev, cur) => mergeOperationResult(prev, cur));
}
},
{
name: '当pay达到禁止退款期限时关闭退款允许',
entity: 'pay',
filter: () => {
const now = Date.now();
return {
refundable: true,
forbidRefundAt: {
$lte: now,
},
};
},
action: 'closeRefund',
actionData: {
refundable: false,
}
},
{
name: '当pay达到过期期限时关闭pay',
entity: 'pay',
filter: () => {
const now = Date.now();
return {
iState: {
$in: ['unpaid', 'paying'],
},
timeoutAt: {
$lt: now,
},
};
},
action: 'close',
actionData: {},
}
];
export default watchers;