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

219 lines
8.6 KiB
JavaScript
Raw Permalink 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 payClazz_1 = require("../utils/payClazz");
const constants_1 = require("../config/constants");
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: 'before',
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,
shipId: 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,
},
}
}
},
filter,
}, {});
(0, assert_1.default)(deposits.length === 1);
const [deposit] = deposits;
const { pay$deposit: pays, price, loss, shipId } = deposit;
let cnt = 1;
let accountOpers = [];
if (shipId) {
(0, assert_1.default)(pays);
for (const pay of pays) {
const { price: payPrice, paid, application, entity, entityId, wpProduct } = pay;
//受发货限制的微信支付充值
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,
},
}, {});
}
accountOpers.push({
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++;
}
}
}
if (accountOpers && accountOpers.length > 0) {
data.accountOper$entity = accountOpers;
}
return cnt;
}
},
{
entity: 'deposit',
name: '当deposit状态发生变化时尝试向关联的account订阅者推送',
action: ['succeed', 'fail', 'ship'],
when: 'after',
fn: async ({ operation }, context, option) => {
const { filter, id } = operation;
const { id: depositId } = filter;
const [deposit] = await context.select('deposit', {
data: {
id: 1,
accountId: 1,
iState: 1,
pay$deposit: {
$entity: 'pay',
data: {
id: 1,
iState: 1,
}
}
},
filter: {
id: depositId,
}
}, {});
const { accountId, pay$deposit } = deposit;
const payId = pay$deposit?.[0].id;
const selectionId = await (0, uuid_1.generateNewIdAsync)();
await context.select('deposit', {
id: selectionId,
data: {
id: 1,
accountId: 1,
account: {
id: 1,
avail: 1,
total: 1,
},
iState: 1,
shipId: 1,
ship: {
id: 1,
iState: 1,
},
pay$deposit: {
$entity: 'pay',
data: {
id: 1,
iState: 1,
}
}
},
filter: {
id: depositId,
}
}, {});
if (payId) {
context.saveOperationToEvent(selectionId, `${constants_1.DATA_SUBSCRIBER_KEYS.depositStateChanged}-${payId}`);
}
if (accountId) {
context.saveOperationToEvent(selectionId, `${constants_1.DATA_SUBSCRIBER_KEYS.depositStateChanged}-${accountId}`);
}
return 1;
}
},
];
exports.default = triggers;