152 lines
6.7 KiB
JavaScript
152 lines
6.7 KiB
JavaScript
"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 payClazz_1 = require("../utils/payClazz");
|
||
const triggers = [
|
||
/* {
|
||
entity: 'deposit',
|
||
name: '当充值完成后,生成相应的accountOper',
|
||
action: 'after',
|
||
when: 'before',
|
||
fn: async ({ operation }, context) => {
|
||
// 先放到pay succeedPaying的trigger里去一并处理了
|
||
return 0;
|
||
}
|
||
} as UpdateTriggerInTxn<EntityDict, 'deposit', BRC> */
|
||
{
|
||
entity: 'deposit',
|
||
name: '当受发货限制的deposit充值成功后,计算相应的account以及system account中的余额变化',
|
||
action: 'succeed',
|
||
when: 'after',
|
||
asRoot: true,
|
||
fn: async ({ operation }, context) => {
|
||
const { data, filter } = operation;
|
||
(0, assert_1.default)(typeof filter.id === 'string');
|
||
const deposits = await context.select('deposit', {
|
||
data: {
|
||
id: 1,
|
||
price: 1,
|
||
loss: 1,
|
||
accountId: 1,
|
||
pay$deposit: {
|
||
$entity: 'pay',
|
||
data: {
|
||
id: 1,
|
||
paid: 1,
|
||
price: 1,
|
||
entity: 1,
|
||
entityId: 1,
|
||
iState: 1,
|
||
orderId: 1,
|
||
application: {
|
||
systemId: 1,
|
||
},
|
||
refundable: 1,
|
||
wpProduct: {
|
||
id: 1,
|
||
type: 1,
|
||
shipping: 1,
|
||
}
|
||
}
|
||
}
|
||
},
|
||
filter,
|
||
}, {});
|
||
(0, assert_1.default)(deposits.length === 1);
|
||
const [deposit] = deposits;
|
||
const { pay$deposit: pays, price, loss } = deposit;
|
||
let cnt = 1;
|
||
if (pays && pays.length > 0) {
|
||
for (const pay of pays) {
|
||
const { price: payPrice, paid, application, entity, entityId, wpProduct } = pay;
|
||
if (entity === 'wpProduct' && wpProduct?.shipping && pay.iState === 'paid') {
|
||
//受发货限制的微信支付充值
|
||
const clazz = await (0, payClazz_1.getPayClazz)(entity, entityId, context);
|
||
const [tax, sysAccountEntity, sysAccountEntityId] = clazz.calcPayTax(paid);
|
||
await context.operate('sysAccountOper', {
|
||
id: await (0, uuid_1.generateNewIdAsync)(),
|
||
action: 'create',
|
||
data: {
|
||
id: await (0, uuid_1.generateNewIdAsync)(),
|
||
delta: paid - tax,
|
||
entity: sysAccountEntity,
|
||
entityId: sysAccountEntityId,
|
||
payId: pay.id,
|
||
type: 'pay',
|
||
}
|
||
}, {});
|
||
cnt++;
|
||
if (tax !== 0) {
|
||
// tax产生的损失由sys account来承担
|
||
const [account] = await context.select('account', {
|
||
data: {
|
||
id: 1,
|
||
},
|
||
filter: {
|
||
entity: 'system',
|
||
entityId: application.systemId,
|
||
}
|
||
}, { dontCollect: true });
|
||
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: 'pay',
|
||
entityId: pay.id,
|
||
},
|
||
}, {});
|
||
}
|
||
const accountOpers = [
|
||
{
|
||
id: await (0, uuid_1.generateNewIdAsync)(),
|
||
data: {
|
||
id: await (0, uuid_1.generateNewIdAsync)(),
|
||
totalPlus: payPrice - loss,
|
||
availPlus: payPrice - loss,
|
||
refundablePlus: pay.refundable ? price : 0,
|
||
accountId: deposit.accountId,
|
||
type: 'deposit',
|
||
},
|
||
action: 'create',
|
||
}
|
||
];
|
||
if (loss > 0) {
|
||
// 如果有loss就充入system的account账户
|
||
const [account] = await context.select('account', {
|
||
data: {
|
||
id: 1,
|
||
},
|
||
filter: {
|
||
entity: 'system',
|
||
entityId: application?.systemId,
|
||
},
|
||
}, { dontCollect: true });
|
||
accountOpers.push({
|
||
id: await (0, uuid_1.generateNewIdAsync)(),
|
||
data: {
|
||
id: await (0, uuid_1.generateNewIdAsync)(),
|
||
totalPlus: loss,
|
||
availPlus: loss,
|
||
accountId: account.id,
|
||
type: 'earn',
|
||
},
|
||
action: 'create',
|
||
});
|
||
cnt++;
|
||
}
|
||
}
|
||
}
|
||
}
|
||
return 0;
|
||
}
|
||
}
|
||
];
|
||
exports.default = triggers;
|