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

117 lines
4.7 KiB
JavaScript

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const executor_1 = require("oak-domain/lib/utils/executor");
const assert_1 = tslib_1.__importDefault(require("assert"));
const wpProduct_1 = require("../utils/wpProduct");
const types_1 = require("oak-domain/lib/types");
const checkers = [
{
entity: 'wpProduct',
action: 'create',
type: 'row',
filter(operation) {
const { data } = operation;
if (data) {
const { type, enabled } = data;
if (enabled) {
return {
application: {
wpProduct$application: {
"#sqp": 'not in',
enabled: true,
type,
}
}
};
}
}
},
errMsg: '同一应用上不能存在同样类型的支付产品',
},
{
entity: 'wpProduct',
action: 'update',
type: 'row',
filter(operation, context) {
const { data, filter } = operation;
if (data && data.enabled) {
(0, assert_1.default)(filter.id && typeof filter.id === 'string');
return (0, executor_1.pipeline)(() => context.select('wpProduct', {
data: {
id: 1,
type: 1,
applicationId: 1,
},
filter,
}, { dontCollect: true }), (wpProducts) => {
const [wpProduct] = wpProducts;
const { type } = wpProduct;
return {
application: {
wpProduct$application: {
"#sqp": 'not in',
enabled: true,
type,
},
},
};
});
}
},
errMsg: 'error::wpProduct.repeatProductsOnSameApp',
},
{
entity: 'wpProduct',
action: 'create',
type: 'logicalData',
checker(operation, context) {
const { data } = operation;
if (data) {
const { type, applicationId } = data;
if (type && applicationId) {
return (0, executor_1.pipeline)(() => context.select('application', {
data: {
id: 1,
type: 1,
config: 1,
},
filter: {
id: applicationId,
}
}, { dontCollect: true }), (applications) => {
const { type, config } = applications[0];
if (!(0, wpProduct_1.getAppTypeFromProductType)(data.type).includes(type)) {
throw new types_1.OakInputIllegalException('wpProduct', ['applicationId'], 'error::wpProduct.TypeConflict', 'oak-pay-business');
}
switch (type) {
case 'web': {
const { wechatPay } = config;
if (!wechatPay?.appId) {
throw new types_1.OakInputIllegalException('wpProduct', ['applicationId'], 'error::wpProduct.NoWechatInfoOnApp', 'oak-pay-business');
}
break;
}
case 'wechatMp': {
const { appId } = config;
if (!appId) {
throw new types_1.OakInputIllegalException('wpProduct', ['applicationId'], 'error::wpProduct.NoWechatInfoOnApp', 'oak-pay-business');
}
break;
}
case 'wechatPublic': {
const { appId } = config;
if (!appId) {
throw new types_1.OakInputIllegalException('wpProduct', ['applicationId'], 'error::wpProduct.NoWechatInfoOnApp', 'oak-pay-business');
}
break;
}
}
});
}
}
}
},
];
exports.default = checkers;