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

82 lines
2.6 KiB
JavaScript

import { OakPreConditionUnsetException } from 'oak-domain/lib/types';
import { getOrderShipState } from '../utils/ship';
import { getShipClazz } from '../utils/shipClazz';
import { generateNewIdAsync } from 'oak-domain/lib/utils/uuid';
/**
* 获取小程序订单发货状态
* @param params
* @param context
*/
export async function getMpShipState(params, context) {
const application = context.getApplication();
const { type, } = application;
if (type === 'wechatMp') {
const { shipId } = params;
const shipState = await getOrderShipState(context, shipId);
return shipState;
}
}
/**
* 获取打印面单
*/
export async function getExpressPrintInfo(params, context) {
const { shipId } = params;
const [ship] = await context.select('ship', {
data: {
id: 1,
type: 1,
entity: 1,
entityId: 1,
extraShipId: 1,
},
filter: {
id: shipId,
}
}, { forUpdate: true });
const { type, entity, entityId, extraShipId } = ship;
if (!entity || !entityId || !extraShipId || type !== 'express') {
throw new OakPreConditionUnsetException("error::ship.noClazz", "ship", undefined, "oak-pay-business");
}
const clazz = await getShipClazz(entity, entityId, context);
return await clazz.getPrintInfo(shipId, context);
}
/**
* 小程序确认收货组件成功后更新ship状态
*/
export async function shipConfirmSuccess(params, context) {
const application = context.getApplication();
const { type, } = application;
if (type === 'wechatMp') {
try {
const { shipId } = params;
const shipState = await getOrderShipState(context, shipId);
if (shipState === 'received') {
//微信端已确认收货
const [ship] = await context.select('ship', {
data: {
id: 1,
iState: 1,
}, filter: {
id: shipId,
}
}, { forUpdate: true });
if (ship.iState === 'receiving') {
await context.operate('ship', {
id: await generateNewIdAsync(),
action: 'succeedReceiving',
data: {},
filter: {
id: shipId,
},
}, {});
return true;
}
}
}
catch (err) {
return false;
}
}
return false;
}