Throw invalid businessType for new clients

This commit is contained in:
Joan Sanchez 2022-01-05 15:41:26 +01:00
parent d85f95a8f4
commit ebee852de6
1 changed files with 14 additions and 5 deletions

View File

@ -184,17 +184,26 @@ module.exports = Self => {
const changes = ctx.data || ctx.instance; const changes = ctx.data || ctx.instance;
const orgData = ctx.currentInstance; const orgData = ctx.currentInstance;
if (!ctx.isNewInstance) {
const businessTypeFk = changes && changes.businessTypeFk || orgData && orgData.businessTypeFk; const businessTypeFk = changes && changes.businessTypeFk || orgData && orgData.businessTypeFk;
const isTaxDataChecked = changes && changes.isTaxDataChecked || orgData && orgData.isTaxDataChecked; const isTaxDataChecked = changes && changes.isTaxDataChecked || orgData && orgData.isTaxDataChecked;
let invalidBusinessType = false;
if (!ctx.isNewInstance) {
const isWorker = await Self.app.models.UserAccount.findById(orgData.id);
const changedFields = Object.keys(changes); const changedFields = Object.keys(changes);
const hasChangedOtherFields = changedFields.some(key => key !== 'businessTypeFk'); const hasChangedOtherFields = changedFields.some(key => key !== 'businessTypeFk');
const isWorker = await Self.app.models.UserAccount.findById(orgData.id);
if (!businessTypeFk && !isTaxDataChecked && !isWorker && !hasChangedOtherFields) if (!businessTypeFk && !isTaxDataChecked && !isWorker && !hasChangedOtherFields)
throw new UserError(`The type of business must be filled in basic data`); invalidBusinessType = true;
} }
if (ctx.isNewInstance) {
if (!businessTypeFk && !isTaxDataChecked)
invalidBusinessType = true;
}
if (invalidBusinessType)
throw new UserError(`The type of business must be filled in basic data`);
}); });
Self.observe('before save', async function(ctx) { Self.observe('before save', async function(ctx) {