158 lines
5.7 KiB
JavaScript
158 lines
5.7 KiB
JavaScript
import { generateNewIdAsync } from 'oak-domain/lib/utils/uuid';
|
||
import { DATA_SUBSCRIBER_KEYS } from '../config/constants';
|
||
import assert from 'assert';
|
||
const triggers = [
|
||
{
|
||
name: '订单全部支付后根据关联的settlement创建type为preSettle的accountOper',
|
||
entity: 'order',
|
||
action: 'payAll',
|
||
when: 'commit',
|
||
asRoot: true,
|
||
fn: async ({ ids }, context, option) => {
|
||
for (const id of ids) {
|
||
const settlements = await context.select('settlement', {
|
||
data: {
|
||
id: 1,
|
||
price: 1,
|
||
accountId: 1,
|
||
orderId: 1,
|
||
iState: 1,
|
||
},
|
||
filter: {
|
||
orderId: id,
|
||
iState: 'unsettled',
|
||
}
|
||
}, {
|
||
forUpdate: true,
|
||
});
|
||
assert(settlements && settlements.length > 0);
|
||
let opers = [];
|
||
for (const settlement of settlements) {
|
||
const { price, accountId } = settlement;
|
||
//创建对应的accountOper
|
||
const oper = [{
|
||
id: await generateNewIdAsync(),
|
||
data: {
|
||
id: await generateNewIdAsync(),
|
||
totalPlus: price,
|
||
availPlus: 0,
|
||
accountId: accountId,
|
||
type: 'preSettle',
|
||
},
|
||
action: 'create',
|
||
}];
|
||
opers.push(...oper);
|
||
}
|
||
await context.operate('order', {
|
||
id: await generateNewIdAsync(),
|
||
action: 'update',
|
||
data: {
|
||
accountOper$entity: opers,
|
||
},
|
||
filter: {
|
||
id,
|
||
}
|
||
}, {});
|
||
}
|
||
return;
|
||
}
|
||
},
|
||
{
|
||
name: '订单执行settle前,对关联的settlement执行settle,创建type为settle的accountOper',
|
||
entity: 'order',
|
||
action: 'settle',
|
||
when: 'before',
|
||
asRoot: true,
|
||
priority: 99,
|
||
fn: async ({ operation }, context, option) => {
|
||
const { filter } = operation;
|
||
let cnt = 0;
|
||
const orders = await context.select('order', {
|
||
data: {
|
||
id: 1,
|
||
settlement$order: {
|
||
$entity: 'settlement',
|
||
data: {
|
||
id: 1,
|
||
price: 1,
|
||
accountId: 1,
|
||
orderId: 1,
|
||
iState: 1,
|
||
},
|
||
filter: {
|
||
iState: 'unsettled',
|
||
}
|
||
}
|
||
},
|
||
filter,
|
||
}, {
|
||
forUpdate: true,
|
||
dontCollect: true,
|
||
});
|
||
for (const order of orders) {
|
||
const { id, settlement$order: settlements } = order || {};
|
||
assert(settlements && settlements.length > 0);
|
||
const settlementIds = settlements?.map((ele) => ele.id);
|
||
if (settlementIds && settlementIds.length > 0) {
|
||
//将关联的settlement执行settle
|
||
await context.operate('settlement', {
|
||
id: await generateNewIdAsync(),
|
||
action: 'settle',
|
||
data: {},
|
||
filter: {
|
||
id: {
|
||
$in: settlementIds
|
||
}
|
||
}
|
||
}, {});
|
||
cnt++;
|
||
}
|
||
//创建对应的accountOper
|
||
let opers = [];
|
||
for (const settlement of settlements) {
|
||
const { price, accountId } = settlement;
|
||
const oper = [{
|
||
id: await generateNewIdAsync(),
|
||
data: {
|
||
id: await generateNewIdAsync(),
|
||
totalPlus: 0,
|
||
availPlus: price,
|
||
accountId: accountId,
|
||
type: 'settle',
|
||
},
|
||
action: 'create',
|
||
}];
|
||
opers.push(...oper);
|
||
}
|
||
await context.operate('order', {
|
||
id: await generateNewIdAsync(),
|
||
action: 'update',
|
||
data: {
|
||
accountOper$entity: opers,
|
||
},
|
||
filter: {
|
||
id: id,
|
||
}
|
||
}, {});
|
||
cnt++;
|
||
}
|
||
return cnt;
|
||
},
|
||
},
|
||
{
|
||
name: '订单状态改变时,向订阅者发送消息',
|
||
entity: 'order',
|
||
action: ['startPaying', 'payAll', 'payPartially', 'payNone', 'timeout', 'cancel', 'startRefunding', 'refundAll', 'refundPartially', 'refundNone'],
|
||
when: 'after',
|
||
fn: async ({ operation }, context, option) => {
|
||
const { filter, id } = operation;
|
||
if (filter.id) {
|
||
context.saveOperationToEvent(id, `${DATA_SUBSCRIBER_KEYS.orderStateChanged}-${filter.id}`);
|
||
return 1;
|
||
}
|
||
return 0;
|
||
}
|
||
},
|
||
];
|
||
export default triggers;
|