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'); } break; } case 'others': { if (!name && !qrCode) { throw new OakAttrNotNullException('offlineAccount', ['name', 'qrCode']); } if (!channel) { throw new OakAttrNotNullException('offlineAccount', ['channel']); } } } } 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, }, filter }, { dontCollect: true }), (accounts) => { accounts.forEach((ele) => checkAttributes(Object.assign(ele, data))); }); } } ]; export default checkers;