"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const tslib_1 = require("tslib"); 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' ? 600 * 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 (0, payClazz_1.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: { (0, assert_1.default)(false); } } const result = await context.operate('refund', { id: await (0, uuid_1.generateNewIdAsync)(), action, data: updateData || {}, filter: { id: refund.id, } }, {}); results.push(result); } } if (results.length === 0) { return {}; } return results.reduce((prev, cur) => (0, operationResult_1.mergeOperationResult)(prev, cur)); } }, ]; exports.default = watchers;