"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.updateWithdrawState = updateWithdrawState; const tslib_1 = require("tslib"); const uuid_1 = require("oak-domain/lib/utils/uuid"); const assert_1 = tslib_1.__importDefault(require("assert")); /** * 当refund/transfer完成或失败时,如果关联有提现,去更新提现的状态 * @param context * @param refunds */ async function updateWithdrawState(context, id) { const withdraws = await context.select('withdraw', { data: { id: 1, iState: 1, price: 1, refund$withdraw: { $entity: 'refund', data: { id: 1, price: 1, iState: 1, loss: 1, }, filter: { iState: { $in: ['successful', 'refunding'] } } }, withdrawTransfer$withdraw: { $entity: 'withdrawTransfer', data: { id: 1, price: 1, iState: 1, loss: 1, }, filter: { iState: { $in: ['successful', 'transferring'] } } } }, filter: { id, } }, { forUpdate: true }); const [withdraw] = withdraws; const { price, iState, refund$withdraw: refunds, withdrawTransfer$withdraw: transfers } = withdraw; (0, assert_1.default)(iState === 'withdrawing'); let dealPrice = 0; let dealLoss = 0; let allRefundsOver = true; if (refunds) { for (const refund2 of refunds) { if (refund2.iState === 'refunding') { allRefundsOver = false; break; } else { dealPrice += refund2.price; dealLoss += refund2.loss; } } } if (!allRefundsOver) { return 0; } if (transfers) { for (const transfer of transfers) { if (transfer.iState === 'transferring') { allRefundsOver = false; break; } else { dealPrice += transfer.price; dealLoss += transfer.loss; } } } if (!allRefundsOver) { return 0; } const action = dealPrice === 0 ? 'fail' : (dealPrice === price ? 'succeed' : 'succeedPartially'); await context.operate('withdraw', { id: await (0, uuid_1.generateNewIdAsync)(), action, data: { dealPrice, dealLoss, }, filter: { id: withdraw.id, } }, {}); return 1; } const triggers = [ { name: '当withdraw创建时,如果是走退款渠道,则创建出相应的refunds,同时更改account中的数值', entity: 'withdraw', action: 'create', when: 'before', fn: async ({ operation }, context, option) => { const { data } = operation; (0, assert_1.default)(!(data instanceof Array)); const { accountId, price, refund$withdraw: refunds, withdrawTransfer$withdraw: transfers } = data; let refundAmount = 0; refunds?.forEach(({ data }) => { const { price } = data; (0, assert_1.default)(price); refundAmount += price; }); let transferAmount = 0; transfers?.forEach(({ data }) => { const { price } = data; (0, assert_1.default)(price); transferAmount += price; }); (0, assert_1.default)(refundAmount + transferAmount === price); data.dealPrice = data.dealLoss = 0; data.creatorId = context.getCurrentUserId(); data.accountOper$entity = [ { id: await (0, uuid_1.generateNewIdAsync)(), action: 'create', data: { id: await (0, uuid_1.generateNewIdAsync)(), accountId, type: 'withdraw', totalPlus: -price, availPlus: -price, refundablePlus: -refundAmount, }, } ]; return 1; }, }, { name: '当withdraw失败时,将total和avail还回到帐户', entity: 'withdraw', action: 'fail', when: 'after', fn: async ({ operation }, context, option) => { const { filter, data } = operation; const withdraws = await context.select('withdraw', { data: { id: 1, accountId: 1, iState: 1, price: 1, dealPrice: 1, }, filter, }, {}); for (const withdraw of withdraws) { const { accountId, price, dealPrice } = withdraw; (0, assert_1.default)(dealPrice === 0); await context.operate('accountOper', { id: await (0, uuid_1.generateNewIdAsync)(), action: 'create', data: { id: await (0, uuid_1.generateNewIdAsync)(), accountId, type: 'withdrawBack', totalPlus: price, availPlus: price, entity: 'withdraw', entityId: withdraw.id, }, }, {}); } return withdraws.length; }, }, { name: '当withdraw部分成功时,将差价部分还回到帐户中', entity: 'withdraw', action: 'succeedPartially', when: 'after', fn: async ({ operation }, context) => { const { filter, data } = operation; const withdraws = await context.select('withdraw', { data: { id: 1, accountId: 1, iState: 1, price: 1, dealPrice: 1, }, filter, }, {}); for (const withdraw of withdraws) { const { accountId, price, dealPrice } = withdraw; (0, assert_1.default)(price > dealPrice && dealPrice > 0); await context.operate('accountOper', { id: await (0, uuid_1.generateNewIdAsync)(), action: 'create', data: { id: await (0, uuid_1.generateNewIdAsync)(), accountId, type: 'withdrawBack', totalPlus: price - dealPrice, availPlus: price - dealPrice, entity: 'withdraw', entityId: withdraw.id, }, }, {}); } return withdraws.length; } } ]; exports.default = triggers;