288 lines
10 KiB
JavaScript
288 lines
10 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 lodash_1 = require("oak-domain/lib/utils/lodash");
|
||
const triggers = [
|
||
{
|
||
name: '当settlePlan创建后,更新关联order的settlePlanned',
|
||
entity: 'settlePlan',
|
||
action: 'create',
|
||
when: 'after',
|
||
asRoot: true,
|
||
// priority: 99,
|
||
fn: async ({ operation }, context, option) => {
|
||
const { data } = operation;
|
||
let ids = [];
|
||
if (data instanceof Array) {
|
||
ids = data.map((ele) => ele.id);
|
||
}
|
||
else {
|
||
ids = [data.id];
|
||
}
|
||
const settlePlans = await context.select('settlePlan', {
|
||
data: {
|
||
id: 1,
|
||
orderId: 1,
|
||
price: 1,
|
||
},
|
||
filter: {
|
||
id: {
|
||
$in: ids,
|
||
}
|
||
},
|
||
}, { forUpdate: true });
|
||
const planArr = (0, lodash_1.groupBy)(settlePlans, 'orderId');
|
||
const orderIds = Object.keys(planArr);
|
||
for (const orderId of orderIds) {
|
||
const [order] = await context.select('order', {
|
||
data: {
|
||
id: 1,
|
||
settlePlanned: 1,
|
||
},
|
||
filter: {
|
||
id: orderId,
|
||
}
|
||
}, { forUpdate: true });
|
||
const { id, 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);
|
||
}
|
||
return ids.length;
|
||
},
|
||
},
|
||
{
|
||
name: '当settlePlan执行settle时将关联的settlement执行settle',
|
||
entity: 'settlePlan',
|
||
action: 'settle',
|
||
when: 'before',
|
||
asRoot: true,
|
||
priority: 99,
|
||
fn: async ({ operation }, context, option) => {
|
||
const { filter, data } = 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',
|
||
}
|
||
},
|
||
},
|
||
filter,
|
||
}, { forUpdate: true });
|
||
const now = Date.now();
|
||
for (const plan of settlePlans) {
|
||
const { 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);
|
||
}
|
||
}
|
||
//为settlePlan赋上settledAt
|
||
if (data instanceof Array) {
|
||
for (const d of data) {
|
||
d.settledAt = now;
|
||
}
|
||
}
|
||
else {
|
||
data.settledAt = now;
|
||
}
|
||
return settlePlans.length;
|
||
},
|
||
},
|
||
{
|
||
name: '当settlePlan执行settle后,更新order的settled',
|
||
entity: 'settlePlan',
|
||
action: 'settle',
|
||
when: 'after',
|
||
asRoot: true,
|
||
fn: async ({ operation }, context, option) => {
|
||
const { filter } = operation;
|
||
const settlePlans = await context.select('settlePlan', {
|
||
data: {
|
||
id: 1,
|
||
price: 1,
|
||
orderId: 1,
|
||
order: {
|
||
id: 1,
|
||
paid: 1,
|
||
refunded: 1,
|
||
settled: 1,
|
||
}
|
||
},
|
||
filter,
|
||
}, { forUpdate: true });
|
||
(0, assert_1.default)(settlePlans.length > 0, '未查询到settlePlan,请检查filter');
|
||
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;
|
||
plans.forEach((plan) => planPrice += plan.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 orderIds.length;
|
||
},
|
||
},
|
||
{
|
||
name: '当settlePlan执行close时将关联的settlement执行close',
|
||
entity: 'settlePlan',
|
||
action: 'close',
|
||
when: 'before',
|
||
asRoot: true,
|
||
priority: 99,
|
||
fn: async ({ operation }, context, option) => {
|
||
const { filter, data } = 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',
|
||
}
|
||
},
|
||
},
|
||
filter,
|
||
}, { forUpdate: true });
|
||
const now = Date.now();
|
||
for (const plan of settlePlans) {
|
||
const { 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,
|
||
},
|
||
}
|
||
}, option);
|
||
}
|
||
//为settlePlan赋上closedAt
|
||
if (data instanceof Array) {
|
||
for (const d of data) {
|
||
d.closedAt = now;
|
||
}
|
||
}
|
||
else {
|
||
data.closedAt = now;
|
||
}
|
||
return settlePlans.length;
|
||
},
|
||
},
|
||
{
|
||
name: '当settlePlan执行close后,并更新order的settlePlanned',
|
||
entity: 'settlePlan',
|
||
action: 'close',
|
||
when: 'after',
|
||
asRoot: true,
|
||
fn: async ({ operation }, context, option) => {
|
||
const { filter } = operation;
|
||
const settlePlans = await context.select('settlePlan', {
|
||
data: {
|
||
id: 1,
|
||
price: 1,
|
||
orderId: 1,
|
||
order: {
|
||
id: 1,
|
||
settlePlanned: 1,
|
||
}
|
||
},
|
||
filter,
|
||
}, { forUpdate: true });
|
||
(0, assert_1.default)(settlePlans.length > 0, '未查询到settlePlan,请检查filter');
|
||
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;
|
||
plans.forEach((plan) => planPrice += plan.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 orderIds.length;
|
||
},
|
||
},
|
||
];
|
||
exports.default = triggers;
|