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

114 lines
4.4 KiB
JavaScript

import { pipeline } from 'oak-domain/lib/utils/executor';
import assert from 'assert';
import { getAppTypeFromProductType } from '../utils/wpProduct';
import { OakInputIllegalException } from '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) {
assert(filter.id && typeof filter.id === 'string');
return 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 pipeline(() => context.select('application', {
data: {
id: 1,
type: 1,
config: 1,
},
filter: {
id: applicationId,
}
}, { dontCollect: true }), (applications) => {
const { type, config } = applications[0];
if (!getAppTypeFromProductType(data.type).includes(type)) {
throw new OakInputIllegalException('wpProduct', ['applicationId'], 'error::wpProduct.TypeConflict', 'oak-pay-business');
}
switch (type) {
case 'web': {
const { wechatPay } = config;
if (!wechatPay?.appId) {
throw new OakInputIllegalException('wpProduct', ['applicationId'], 'error::wpProduct.NoWechatInfoOnApp', 'oak-pay-business');
}
break;
}
case 'wechatMp': {
const { appId } = config;
if (!appId) {
throw new OakInputIllegalException('wpProduct', ['applicationId'], 'error::wpProduct.NoWechatInfoOnApp', 'oak-pay-business');
}
break;
}
case 'wechatPublic': {
const { appId } = config;
if (!appId) {
throw new OakInputIllegalException('wpProduct', ['applicationId'], 'error::wpProduct.NoWechatInfoOnApp', 'oak-pay-business');
}
break;
}
}
});
}
}
}
},
];
export default checkers;