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

112 lines
4.3 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;
}
}
},
{
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', 'partiallyRefunded'].includes(iState));
(0, assert_1.default)(settlements && settlements.length > 0, '该结算订单需添加结算明细');
let amount = 0;
settlements.forEach((settlement) => {
amount += settlement.price;
});
(0, assert_1.default)(amount === paid - refunded);
});
}
}
];
exports.default = checkers;