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

105 lines
4.0 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');
}
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');
}
});
}
},
{
// 订单结算将收入分帐到相关的Account中
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,
},
filter: {
id: filter.id,
},
}, {}), (orders) => {
const [order] = orders;
const { price, paid, refunded, iState } = order;
(0, assert_1.default)(['paid', 'partiallyRefunded'].includes(iState));
const { accountOper$entity: opers } = data;
(0, assert_1.default)(opers instanceof Array);
let amount = 0;
opers.forEach(({ action, data }) => {
(0, assert_1.default)(action === 'create');
const { type, totalPlus, availPlus, refundablePlus } = data;
(0, assert_1.default)(type === 'earn');
// (0, assert_1.default)(totalPlus === availPlus);
amount += totalPlus;
});
(0, assert_1.default)(amount === paid - refunded);
});
}
}
];
exports.default = checkers;