165 lines
6.0 KiB
JavaScript
165 lines
6.0 KiB
JavaScript
import assert from 'assert';
|
||
import { pipeline } from 'oak-domain/lib/utils/executor';
|
||
import { OakInputIllegalException } from 'oak-domain/lib/types';
|
||
const checkers = [
|
||
{
|
||
type: 'logical',
|
||
entity: 'order',
|
||
action: 'create',
|
||
checker: (operation, context) => {
|
||
const { data } = operation;
|
||
if (!data.creatorId) {
|
||
data.creatorId = context.getCurrentUserId();
|
||
}
|
||
data.paid = 0;
|
||
data.refunded = 0;
|
||
data.settled = 0;
|
||
data.settlePlanned = 0;
|
||
if (!data.systemId) {
|
||
const contextSystemId = context.getApplication()?.systemId;
|
||
assert(contextSystemId);
|
||
data.systemId = contextSystemId;
|
||
}
|
||
}
|
||
},
|
||
/* {
|
||
// 订单创建的时候要传入合法的settlement
|
||
type: 'logicalData',
|
||
entity: 'order',
|
||
action: 'startPaying',
|
||
checker: (operation, context) => {
|
||
const { data, filter } = operation as EntityDict['order']['Update'];
|
||
const { id } = filter!;
|
||
assert(id);
|
||
|
||
return pipeline(
|
||
() => context.select('order', {
|
||
data: {
|
||
id: 1,
|
||
price: 1,
|
||
settlement$order: {
|
||
$entity: 'settlement',
|
||
data: {
|
||
id: 1,
|
||
price: 1,
|
||
iState: 1,
|
||
accountId: 1,
|
||
},
|
||
},
|
||
},
|
||
filter: {
|
||
id,
|
||
},
|
||
}, { dontCollect: true }),
|
||
(orders: EntityDict['order']['Schema'][]) => {
|
||
const [ order ] = orders;
|
||
const { price, settlement$order } = order;
|
||
|
||
let total = 0 ;
|
||
settlement$order?.forEach(
|
||
(ele) => {
|
||
total += ele.price!;
|
||
assert(ele.iState === 'unsettled');
|
||
}
|
||
);
|
||
|
||
assert(total === price, '付款前订单应设置好分帐目标');
|
||
}
|
||
)
|
||
}
|
||
}, */
|
||
{
|
||
type: 'logicalData',
|
||
entity: 'order',
|
||
action: 'startPaying',
|
||
checker: (operation, context) => {
|
||
const { data, filter } = operation;
|
||
assert(typeof filter.id === 'string');
|
||
return pipeline(() => context.select('order', {
|
||
data: {
|
||
id: 1,
|
||
price: 1,
|
||
paid: 1,
|
||
refunded: 1,
|
||
iState: 1,
|
||
},
|
||
filter: {
|
||
id: filter.id,
|
||
},
|
||
}, {}), (orders) => {
|
||
const [order] = orders;
|
||
const { price, paid, iState, allowPartialPay } = order;
|
||
assert(['unpaid', 'partiallyRefunded'].includes(iState) && paid === 0);
|
||
const { pay$order: pays } = data;
|
||
if (!(pays instanceof Array) || !pays.length) {
|
||
throw new OakInputIllegalException('order', ['pay$order'], 'error::order.nonePay', 'oak-pay-business');
|
||
}
|
||
let amount = 0;
|
||
pays.forEach(({ action, data }) => {
|
||
assert(action === 'create');
|
||
const { price } = data;
|
||
amount += price;
|
||
});
|
||
if (!allowPartialPay && amount !== price) {
|
||
throw new OakInputIllegalException('order', ['pay$order'], 'error::order.payAmountNotEnough', 'oak-pay-business');
|
||
}
|
||
});
|
||
}
|
||
},
|
||
/**todo 调整至settlePlan 执行settle
|
||
{
|
||
// 订单结算,检查存在关联的未结算的settlement,其price之和等于订单的已支付与已退款的差值
|
||
type: 'logicalData',
|
||
entity: 'order',
|
||
action: 'settle',
|
||
checker: (operation, context) => {
|
||
const { data, filter } = operation as EntityDict['order']['Update'];
|
||
|
||
assert(typeof filter!.id === 'string');
|
||
|
||
data.settled = true;
|
||
return pipeline(
|
||
() => context.select('order', {
|
||
data: {
|
||
id: 1,
|
||
price: 1,
|
||
paid: 1,
|
||
refunded: 1,
|
||
iState: 1,
|
||
settlement$order: {
|
||
$entity: 'settlement',
|
||
data: {
|
||
id: 1,
|
||
accountId: 1,
|
||
price: 1,
|
||
iState: 1,
|
||
},
|
||
filter: {
|
||
iState: 'unsettled'
|
||
}
|
||
}
|
||
},
|
||
filter: {
|
||
id: filter!.id!,
|
||
},
|
||
}, {}),
|
||
(orders: EntityDict['order']['Schema'][]) => {
|
||
const [order] = orders;
|
||
const { price, paid, refunded, iState, settlement$order: settlements } = order;
|
||
assert(['paid'].includes(iState!) && refunded === 0 && paid === price);
|
||
assert(settlements && settlements.length > 0, '该结算订单需添加结算明细');
|
||
|
||
let amount = 0;
|
||
settlements.forEach((settlement) => {
|
||
amount += settlement.price;
|
||
assert(settlement.iState === 'unsettled', '订单结算前,settlement必须处于未分账状态');
|
||
})
|
||
assert(amount === paid);
|
||
}
|
||
);
|
||
}
|
||
},
|
||
*/
|
||
];
|
||
export default checkers;
|