oak-pay-business/es/triggers/settlePlan.js

243 lines
9.3 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.

import { generateNewIdAsync } from 'oak-domain/lib/utils/uuid';
import assert from 'assert';
import { groupBy } from 'oak-domain/lib/utils/lodash';
const triggers = [
{
name: '当settlePlan创建时更新关联order的settlePlanned',
entity: 'settlePlan',
action: 'create',
when: 'before',
asRoot: true,
priority: 99,
fn: async ({ operation }, context, option) => {
const { data } = operation;
let count = 0;
if (data instanceof Array) {
const planArr = groupBy(data, 'orderId');
const orderIds = Object.keys(planArr);
for (const orderId of orderIds) {
const [order] = await context.select('order', {
data: {
id: 1,
paid: 1,
refunded: 1,
settlePlanned: 1,
},
filter: {
id: orderId,
}
}, { forUpdate: true });
const { id, paid, refunded, 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 generateNewIdAsync(),
action: 'update',
data: {
settlePlanned: newSettlePlanned,
},
filter: {
id,
}
}, option);
}
count += data.length;
}
else {
const [order] = await context.select('order', {
data: {
id: 1,
paid: 1,
refunded: 1,
settlePlanned: 1,
},
filter: {
id: data.orderId,
}
}, { forUpdate: true });
const { id, paid, refunded, settlePlanned } = order;
const newSettlePlanned = data.price + settlePlanned;
await context.operate('order', {
id: await generateNewIdAsync(),
action: 'update',
data: {
settlePlanned: newSettlePlanned,
},
filter: {
id,
}
}, option);
count++;
}
return count;
},
},
{
name: '当settlePlan执行settle时将关联的settlement执行settle并更新order的settled',
entity: 'settlePlan',
action: 'settle',
when: 'before',
asRoot: true,
priority: 99,
fn: async ({ operation }, context, option) => {
const { filter } = 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',
}
},
orderId: 1,
order: {
id: 1,
paid: 1,
refunded: 1,
settled: 1,
}
},
filter,
}, { forUpdate: true });
const now = Date.now();
const planArr = 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;
for (const plan of plans) {
const { id, price, settlement$plan: settlements, } = plan;
assert(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 generateNewIdAsync(),
action: 'settle',
data: {
settledAt: now,
accountOper$entity: [{
id: await generateNewIdAsync(),
action: 'create',
data: {
id: await generateNewIdAsync(),
totalPlus: settlementPrice,
availPlus: settlementPrice,
accountId: accountId,
type: 'settle',
}
}]
},
filter: {
id: settlementId,
}
}, option);
}
planPrice += price;
}
//更新order的settled
const newSettledPrice = order?.settled + planPrice;
await context.operate('order', {
id: await generateNewIdAsync(),
action: 'update',
data: {
settled: newSettledPrice,
},
filter: {
id: orderId,
}
}, option);
}
return settlePlans.length;
},
},
{
name: '当settlePlan执行close时将关联的settlement执行close并更新order的settlePlanned',
entity: 'settlePlan',
action: 'close',
when: 'before',
asRoot: true,
priority: 99,
fn: async ({ operation }, context, option) => {
const { filter } = 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',
}
},
orderId: 1,
order: {
id: 1,
settlePlanned: 1,
}
},
filter,
}, { forUpdate: true });
const now = Date.now();
const planArr = 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;
for (const plan of plans) {
const { id, price, settlement$plan: settlements, } = plan;
assert(settlements && settlements.length > 0);
//关联的settlement均执行close
const settlementIds = settlements.map((ele) => ele.id);
await context.operate('settlement', {
id: await generateNewIdAsync(),
action: 'close',
data: {
closedAt: now,
},
filter: {
id: {
$in: settlementIds,
},
iState: 'unsettled'
}
}, option);
planPrice += price;
}
//更新order的settlePlanned
const newSettlePlanned = order?.settlePlanned - planPrice;
await context.operate('order', {
id: await generateNewIdAsync(),
action: 'update',
data: {
settlePlanned: newSettlePlanned,
},
filter: {
id: orderId,
}
}, option);
}
return settlePlans.length;
},
},
];
export default triggers;