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

108 lines
5.2 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.

import assert from 'assert';
import { OakInputIllegalException } from 'oak-domain/lib/types';
const checkers = [
{
entity: 'accountOper',
action: 'create',
type: 'row',
filter: {
account: {
ableState: 'enabled',
},
},
errMsg: 'account.update.accountDisabled',
},
{
entity: 'accountOper',
action: 'create',
type: 'data',
checker(data, context) {
assert(!(data instanceof Array));
const { type, totalPlus, availPlus, refundablePlus } = data;
if (typeof totalPlus !== 'number') {
throw new OakInputIllegalException('accountOper', ['totalPlus'], 'accountOper中的totalPlus不是数字');
}
if (typeof availPlus !== 'number') {
throw new OakInputIllegalException('accountOper', ['availPlus'], 'accountOper中的availPlus不是数字');
}
switch (type) {
case 'consume': {
if (totalPlus >= 0 || availPlus > 0 || totalPlus > availPlus) {
throw new OakInputIllegalException('accountOper', ['availPlus'], 'accountOper为consume时其totalPlus/availPlus必须为0或负数且totalPlus的绝对值要更大');
}
break;
}
case 'deposit': {
if (totalPlus < 0 || availPlus < 0 || totalPlus !== availPlus ||
typeof refundablePlus === 'number' && refundablePlus < 0) {
throw new OakInputIllegalException('accountOper', ['availPlus'], 'accountOper为deposit时其totalPlus/availPlus必须为正数且相等, refundable必须为空或者正数');
}
break;
}
case 'loan': {
if (totalPlus !== 0 || availPlus >= 0) {
throw new OakInputIllegalException('accountOper', ['availPlus'], 'accountOper为loan时其totalPlus必须为0且availPlus必须为负数');
}
break;
}
case 'repay': {
if (totalPlus !== 0 || availPlus <= 0) {
throw new OakInputIllegalException('accountOper', ['availPlus'], 'accountOper为repay时其totalPlus必须为0且availPlus必须为正数');
}
break;
}
case 'refund':
case 'withdraw': {
if (totalPlus >= 0 || availPlus >= 0 || totalPlus !== availPlus) {
throw new OakInputIllegalException('accountOper', ['availPlus'], `accountOper为${type}其totalPlus和availPlus必须为相等的负数`);
}
break;
}
case 'refundFailure':
case 'withdrawBack':
case 'consumeBack': {
if (totalPlus < 0 || availPlus < 0 || totalPlus !== availPlus) {
throw new OakInputIllegalException('accountOper', ['availPlus'], `accountOper为${type}其totalPlus和availPlus必须为相等的非负数数`);
}
break;
}
case 'earn': {
if (totalPlus <= 0 || availPlus < 0) {
throw new OakInputIllegalException('accountOper', ['availPlus'], 'accountOper为earn时其totalPlus必须为正数、availPlus必须为非负数');
}
break;
}
case 'encash': {
if (totalPlus !== 0 || availPlus <= 0) {
throw new OakInputIllegalException('accountOper', ['availPlus'], 'accountOper为encash时其totalPlus必须为0、availPlus必须为正数');
}
break;
}
case 'cutoffRefundable': {
if (totalPlus !== 0 || availPlus !== 0 || typeof refundablePlus !== 'number' || refundablePlus >= 0) {
throw new OakInputIllegalException('accountOper', ['refundablePlus'], 'accountOper为cutoffRefundable时其totalPlus/availPlus必须为0且refundablePlus必须为负数');
}
break;
}
case 'tax': {
if (totalPlus >= 0 || availPlus >= 0 || totalPlus !== availPlus) {
throw new OakInputIllegalException('accountOper', ['totalPlus', 'availPlus'], 'accountOper为tax时其totalPlus/availPlus必须为负且相等');
}
break;
}
case 'taxRefund': {
if (totalPlus <= 0 || availPlus <= 0 || totalPlus !== availPlus) {
throw new OakInputIllegalException('accountOper', ['totalPlus', 'availPlus'], 'accountOper为taxRefund时其totalPlus/availPlus必须为正且相等');
}
break;
}
default: {
assert(false);
break;
}
}
}
}
];
export default checkers;