oak-pay-business/lib/triggers/withdrawTransfer.js

110 lines
4.6 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.

"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 withdraw_1 = require("./withdraw");
const payClazz_1 = require("../utils/payClazz");
const triggers = [
{
entity: 'withdrawTransfer',
name: '当转账完成或失败后更新withdraw的状态',
action: ['succeed', 'fail'],
when: 'after',
fn: async ({ operation }, context) => {
const { filter, action } = operation;
(0, assert_1.default)(typeof filter?.id === 'string');
const [transfer] = await context.select('withdrawTransfer', {
data: {
id: 1,
price: 1,
loss: 1,
withdrawId: 1,
withdrawAccount: {
channel: {
entity: 1,
entityId: 1,
},
ofSystemId: 1,
},
},
filter: {
id: filter.id,
}
}, { dontCollect: true });
let cnt = await (0, withdraw_1.updateWithdrawState)(context, transfer.withdrawId);
if (action === 'succeed') {
// 如果转账成功对应的sysAccount中的余额应减少
const { withdrawAccount, price, loss } = transfer;
const { entity, entityId } = withdrawAccount.channel;
const actualPrice = price - loss;
// wpAccount不可能成为转账账户offline这样取应当是安全的
const payClazz = await (0, payClazz_1.getPayClazz)(entity, entityId, context);
const [tax] = payClazz.calcTransferTax(actualPrice);
await context.operate('sysAccountOper', {
id: await (0, uuid_1.generateNewIdAsync)(),
action: 'create',
data: {
id: await (0, uuid_1.generateNewIdAsync)(),
delta: -actualPrice - tax,
entity,
entityId,
withdrawTransferId: transfer.id,
type: 'withdrawTransfer',
}
}, {});
cnt++;
if (tax || loss) {
const systemId = withdrawAccount.ofSystemId;
const [account] = await context.select('account', {
data: {
id: 1,
},
filter: {
entity: 'system',
entityId: systemId,
}
}, { dontCollect: true });
if (loss) {
// loss也进system account的账户
await context.operate('accountOper', {
id: await (0, uuid_1.generateNewIdAsync)(),
action: 'create',
data: {
id: await (0, uuid_1.generateNewIdAsync)(),
accountId: account.id,
type: 'earn',
totalPlus: loss,
availPlus: loss,
entity: 'withdrawTransfer',
entityId: transfer.id,
},
}, {});
cnt++;
}
if (tax) {
// 如果转账有手续费由system的account来承受
(0, assert_1.default)(tax > 0);
await context.operate('accountOper', {
id: await (0, uuid_1.generateNewIdAsync)(),
action: 'create',
data: {
id: await (0, uuid_1.generateNewIdAsync)(),
accountId: account.id,
type: 'tax',
totalPlus: -tax,
availPlus: -tax,
entity: 'withdrawTransfer',
entityId: transfer.id,
},
}, {});
cnt++;
}
}
}
return cnt;
}
}
];
exports.default = triggers;