"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const tslib_1 = require("tslib"); const uuid_1 = require("oak-domain/lib/utils/uuid"); const assert_1 = tslib_1.__importDefault(require("assert")); const lodash_1 = require("oak-domain/lib/utils/lodash"); const triggers = [ { name: '当settlePlan创建时更新关联order的settlePlanned', entity: 'settlePlan', action: 'create', when: 'before', asRoot: true, priority: 99, fn: async ({ operation }, context, option) => { const { data } = operation; let count = 0; if (data instanceof Array) { const planArr = (0, lodash_1.groupBy)(data, 'orderId'); const orderIds = Object.keys(planArr); for (const orderId of orderIds) { const [order] = await context.select('order', { data: { id: 1, paid: 1, refunded: 1, settlePlanned: 1, }, filter: { id: orderId, } }, { forUpdate: true }); const { id, paid, refunded, settlePlanned } = order; let planPrice = 0; const plans = planArr[orderId]; plans.forEach((plan) => planPrice += plan.price); const newSettlePlanned = planPrice + settlePlanned; await context.operate('order', { id: await (0, uuid_1.generateNewIdAsync)(), action: 'update', data: { settlePlanned: newSettlePlanned, }, filter: { id, } }, option); } count += data.length; } else { const [order] = await context.select('order', { data: { id: 1, paid: 1, refunded: 1, settlePlanned: 1, }, filter: { id: data.orderId, } }, { forUpdate: true }); const { id, paid, refunded, settlePlanned } = order; const newSettlePlanned = data.price + settlePlanned; await context.operate('order', { id: await (0, uuid_1.generateNewIdAsync)(), action: 'update', data: { settlePlanned: newSettlePlanned, }, filter: { id, } }, option); count++; } return count; }, }, { name: '当settlePlan执行settle时将关联的settlement执行settle,并更新order的settled', entity: 'settlePlan', action: 'settle', when: 'before', asRoot: true, priority: 99, fn: async ({ operation }, context, option) => { const { filter } = operation; const settlePlans = await context.select('settlePlan', { data: { id: 1, price: 1, settlement$plan: { $entity: 'settlement', data: { id: 1, price: 1, accountId: 1, iState: 1, }, filter: { iState: 'unsettled', } }, orderId: 1, order: { id: 1, paid: 1, refunded: 1, settled: 1, } }, filter, }, { forUpdate: true }); const now = Date.now(); const planArr = (0, lodash_1.groupBy)(settlePlans, 'orderId'); const orderIds = Object.keys(planArr); for (const orderId of orderIds) { const plans = planArr[orderId]; const order = planArr[orderId][0].order; let planPrice = 0; for (const plan of plans) { const { id, price, settlement$plan: settlements, } = plan; (0, assert_1.default)(settlements && settlements.length > 0); //关联的settlement均执行settle并生成accountOper for (const settlement of settlements) { const { id: settlementId, price: settlementPrice, accountId, } = settlement; await context.operate('settlement', { id: await (0, uuid_1.generateNewIdAsync)(), action: 'settle', data: { settledAt: now, accountOper$entity: [{ id: await (0, uuid_1.generateNewIdAsync)(), action: 'create', data: { id: await (0, uuid_1.generateNewIdAsync)(), totalPlus: settlementPrice, availPlus: settlementPrice, accountId: accountId, type: 'settle', } }] }, filter: { id: settlementId, } }, option); } planPrice += price; } //更新order的settled const newSettledPrice = order?.settled + planPrice; await context.operate('order', { id: await (0, uuid_1.generateNewIdAsync)(), action: 'update', data: { settled: newSettledPrice, }, filter: { id: orderId, } }, option); } return settlePlans.length; }, }, { name: '当settlePlan执行close时将关联的settlement执行close,并更新order的settlePlanned', entity: 'settlePlan', action: 'close', when: 'before', asRoot: true, priority: 99, fn: async ({ operation }, context, option) => { const { filter } = operation; const settlePlans = await context.select('settlePlan', { data: { id: 1, price: 1, settlement$plan: { $entity: 'settlement', data: { id: 1, price: 1, accountId: 1, iState: 1, }, filter: { iState: 'unsettled', } }, orderId: 1, order: { id: 1, settlePlanned: 1, } }, filter, }, { forUpdate: true }); const now = Date.now(); const planArr = (0, lodash_1.groupBy)(settlePlans, 'orderId'); const orderIds = Object.keys(planArr); for (const orderId of orderIds) { const plans = planArr[orderId]; const order = planArr[orderId][0].order; let planPrice = 0; for (const plan of plans) { const { id, price, settlement$plan: settlements, } = plan; (0, assert_1.default)(settlements && settlements.length > 0); //关联的settlement均执行close const settlementIds = settlements.map((ele) => ele.id); await context.operate('settlement', { id: await (0, uuid_1.generateNewIdAsync)(), action: 'close', data: { closedAt: now, }, filter: { id: { $in: settlementIds, }, iState: 'unsettled' } }, option); planPrice += price; } //更新order的settlePlanned const newSettlePlanned = order?.settlePlanned - planPrice; await context.operate('order', { id: await (0, uuid_1.generateNewIdAsync)(), action: 'update', data: { settlePlanned: newSettlePlanned, }, filter: { id: orderId, } }, option); } return settlePlans.length; }, }, ]; exports.default = triggers;