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

82 lines
3.0 KiB
JavaScript

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const assert_1 = tslib_1.__importDefault(require("assert"));
const types_1 = require("oak-domain/lib/types");
const executor_1 = require("oak-domain/lib/utils/executor");
function checkAttributes(data) {
const { type, channel, name, qrCode } = data;
switch (type) {
case 'bank': {
if (!channel || !name || !qrCode) {
throw new types_1.OakAttrNotNullException('offlineAccount', ['channel', 'name', 'qrCode'].filter(ele => !data[ele]));
}
break;
}
case 'shouqianba':
case 'wechat':
case 'alipay': {
if (!name && !qrCode) {
throw new types_1.OakInputIllegalException('offlineAccount', ['name', 'qrCode'], 'offlineAccount::error.nameQrCodeBothNull');
}
break;
}
case 'others': {
if (!name && !qrCode) {
throw new types_1.OakAttrNotNullException('offlineAccount', ['name', 'qrCode']);
}
if (!channel) {
throw new types_1.OakAttrNotNullException('offlineAccount', ['channel']);
}
}
}
const { refundCompensateRatio, refundGapDays } = data;
if (typeof refundGapDays !== 'number' || refundGapDays < 0) {
throw new types_1.OakInputIllegalException('offlineAccount', ['refundGapDays'], 'offlineAccount::error.refundGapDaysNotNegative');
}
if (typeof refundCompensateRatio !== 'number' || refundCompensateRatio < 0 || refundCompensateRatio > 100) {
throw new types_1.OakInputIllegalException('offlineAccount', ['refundGapDays'], 'offlineAccount::error.refundCompensateIllegal');
}
}
const checkers = [
{
entity: 'offlineAccount',
action: 'create',
type: 'data',
checker(data) {
(0, assert_1.default)(!(data instanceof Array));
checkAttributes(data);
}
},
{
entity: 'offlineAccount',
action: 'update',
type: 'logicalData',
checker: (operation, context) => {
const { data, filter } = operation;
return (0, executor_1.pipeline)(() => context.select('offlineAccount', {
data: {
id: 1,
type: 1,
channel: 1,
name: 1,
qrCode: 1,
allowDeposit: 1,
allowPay: 1,
systemId: 1,
enabled: 1,
refundCompensateRatio: 1,
taxLossRatio: 1,
refundGapDays: 1,
allowWithdrawTransfer: 1,
withdrawTransferLossRatio: 1,
},
filter
}, { dontCollect: true }), (accounts) => {
accounts.forEach((ele) => checkAttributes(Object.assign(ele, data)));
});
}
}
];
exports.default = checkers;