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

73 lines
2.4 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.

import { getPayClazz } from '../utils/payClazz';
import assert from 'assert';
import { generateNewIdAsync } from 'oak-domain/lib/utils/uuid';
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: '对refund状态的退款同步其真实退款状态',
entity: 'refund',
filter: async () => {
const now = Date.now();
return {
iState: 'refunding',
$$updateAt$$: {
$lte: now - QUERY_PAYING_STATE_GAP,
},
externalId: {
$exists: true,
}
};
},
projection: {
id: 1,
price: 1,
iState: 1,
loss: 1,
pay: {
id: 1,
entity: 1,
entityId: 1,
applicationId: 1,
}
},
fn: async (context, data) => {
const results = [];
for (const refund of data) {
const { id, pay } = refund;
const clazz = await getPayClazz(pay.entity, pay.entityId, context);
const [iState, updateData] = await clazz.getRefundState(refund);
if (iState !== refund.iState) {
let action = 'succeed';
switch (iState) {
case 'successful': {
break;
}
case 'failed': {
action = 'fail';
break;
}
default: {
assert(false);
}
}
const result = await context.operate('refund', {
id: await generateNewIdAsync(),
action,
data: updateData || {},
filter: {
id: refund.id,
}
}, {});
results.push(result);
}
}
if (results.length === 0) {
return {};
}
return results.reduce((prev, cur) => mergeOperationResult(prev, cur));
}
},
];
export default watchers;