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

83 lines
2.9 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 { DATA_SUBSCRIBER_KEYS } from '../config/constants';
import { getShipState, uploadShippingInfo } from '../utils/ship';
const triggers = [
{
name: '当虚拟发货创建后自动发货',
entity: 'ship',
action: 'create',
when: 'commit',
strict: 'makeSure',
asRoot: true,
check: (operation) => operation.data.type === 'virtual',
fn: async ({ ids }, context, option) => {
for (const id of ids) {
await context.operate('ship', {
id: await generateNewIdAsync(),
action: 'ship',
data: {},
filter: {
id,
}
}, option);
}
return;
},
},
{
entity: 'ship',
name: '当虚拟的ship变为shipping状态时调用小程序发货信息录入接口',
action: 'ship',
when: 'before',
asRoot: true,
priority: 99,
fn: async ({ operation }, context) => {
const { data, filter } = operation;
const [ship] = await context.select('ship', {
data: {
id: 1,
type: 1,
deposit$ship: {
$entity: 'deposit',
data: {
id: 1,
},
indexFrom: 0,
count: 1,
}
},
filter,
}, {});
const { type, deposit$ship: deposits } = ship || {};
if (type === 'virtual') {
const deposit = deposits?.[0];
//当已发货的订单再次调用小程序发货信息录入接口视为重新发货(仅可重新发货一次)
//发货前先查询,检查是否为未同步微信端发货状态
const shipState = await getShipState(context, deposit);
if (shipState === 'unshipped') {
const result = await uploadShippingInfo({ depositId: deposit?.id, }, context);
if (result) {
return 1;
}
}
}
return 0;
}
},
{
entity: 'ship',
name: '当ship状态发生变化时尝试向订阅者推送',
action: ['ship', 'receive', 'giveUp', 'reject'],
when: 'after',
fn: async ({ operation }, context, option) => {
const { filter, id } = operation;
const { id: shipId } = filter;
if (shipId) {
context.saveOperationToEvent(id, `${DATA_SUBSCRIBER_KEYS.shipStateChanged}-${shipId}`);
}
return 1;
}
}
];
export default triggers;