54 lines
1.6 KiB
JavaScript
54 lines
1.6 KiB
JavaScript
import { fullPayProjection, refreshPayState } from '../utils/pay';
|
||
import { mergeOperationResult } from '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: 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,
|
||
}
|
||
}
|
||
];
|
||
export default watchers;
|