import assert from 'assert'; import { OakAttrNotNullException, OakInputIllegalException } from 'oak-domain/lib/types'; import { pipeline } from 'oak-domain/lib/utils/executor'; function checkAttributes(data) { const { type, channel, name, qrCode } = data; switch (type) { case 'bank': { if (!channel || !name || !qrCode) { throw new OakAttrNotNullException('offlineAccount', ['channel', 'name', 'qrCode'].filter(ele => !data[ele])); } break; } case 'shouqianba': case 'wechat': case 'alipay': { if (!name && !qrCode) { throw new OakInputIllegalException('offlineAccount', ['name', 'qrCode'], 'offlineAccount::error.nameQrCodeBothNull', 'oak-pay-business'); } break; } case 'others': { if (!name && !qrCode) { throw new OakAttrNotNullException('offlineAccount', ['name', 'qrCode']); } if (!channel) { throw new OakAttrNotNullException('offlineAccount', ['channel']); } } } const { refundCompensateRatio, refundGapDays, withdrawTransferLossRatio } = data; if (typeof refundGapDays !== 'number' || refundGapDays < 0) { throw new OakInputIllegalException('offlineAccount', ['refundGapDays'], 'offlineAccount::error.refundGapDaysNotNegative', 'oak-pay-business'); } if (typeof refundCompensateRatio !== 'number' || refundCompensateRatio < 0 || refundCompensateRatio > 100) { throw new OakInputIllegalException('offlineAccount', ['refundGapDays'], 'offlineAccount::error.refundCompensateIllegal', 'oak-pay-business'); } if (typeof withdrawTransferLossRatio !== 'number' || withdrawTransferLossRatio < 0) { throw new OakInputIllegalException('offlineAccount', ['withdrawTransferLossRatio'], 'offlineAccount::error.withdrawTransferLossRatioNotNegative', 'oak-pay-business'); } } const checkers = [ { entity: 'offlineAccount', action: 'create', type: 'data', checker(data) { assert(!(data instanceof Array)); checkAttributes(data); } }, { entity: 'offlineAccount', action: 'update', type: 'logicalData', checker: (operation, context) => { const { data, filter } = operation; return 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))); }); } } ]; export default checkers;