74 lines
2.5 KiB
JavaScript
74 lines
2.5 KiB
JavaScript
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,
|
||
},
|
||
externalId: 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;
|