oak-pay-business/lib/checkers/order.js

177 lines
6.4 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.

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const assert_1 = tslib_1.__importDefault(require("assert"));
const executor_1 = require("oak-domain/lib/utils/executor");
const types_1 = require("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 = false;
if (!data.systemId) {
const contextSystemId = context.getApplication()?.systemId;
(0, assert_1.default)(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;
(0, assert_1.default)(typeof filter.id === 'string');
return (0, executor_1.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;
(0, assert_1.default)(['unpaid', 'partiallyRefunded'].includes(iState) && paid === 0);
const { pay$order: pays } = data;
if (!(pays instanceof Array) || !pays.length) {
throw new types_1.OakInputIllegalException('order', ['pay$order'], 'error::order.nonePay', 'oak-pay-business');
}
let amount = 0;
pays.forEach(({ action, data }) => {
(0, assert_1.default)(action === 'create');
const { price } = data;
amount += price;
});
if (!allowPartialPay && amount !== price) {
throw new types_1.OakInputIllegalException('order', ['pay$order'], 'error::order.payAmountNotEnough', 'oak-pay-business');
}
});
}
},
{
// 订单结算检查存在关联的未结算的settlement其price之和等于订单的已支付与已退款的差值
type: 'logicalData',
entity: 'order',
action: 'settle',
checker: (operation, context) => {
const { data, filter } = operation;
(0, assert_1.default)(typeof filter.id === 'string');
data.settled = true;
return (0, executor_1.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) => {
const [order] = orders;
const { price, paid, refunded, iState, settlement$order: settlements } = order;
(0, assert_1.default)(['paid'].includes(iState) && refunded === 0 && paid === price);
(0, assert_1.default)(settlements && settlements.length > 0, '该结算订单需添加结算明细');
let amount = 0;
settlements.forEach((settlement) => {
amount += settlement.price;
(0, assert_1.default)(settlement.iState === 'unsettled', '订单结算前settlement必须处于未分账状态');
});
(0, assert_1.default)(amount === paid);
});
}
},
{
// 订单根据receivingMethod决定货品发送动作
entity: 'order',
type: 'row',
action: ['send', 'turnBack', 'receive'],
filter: {
receivingMethod: 'express',
},
},
{
// 订单根据receivingMethod决定货品发送动作
entity: 'order',
type: 'row',
action: ['store', 'take'],
filter: {
receivingMethod: 'pickup',
},
},
];
exports.default = checkers;