From 3f24f95200533c507baf4775b211b98554eb76f5 Mon Sep 17 00:00:00 2001 From: p-fernandez Date: Mon, 2 Oct 2023 11:50:55 +0200 Subject: [PATCH] fix(api): malformed filters in messages throw errors --- .../shared/helpers/content.service.spec.ts | 95 +++++++++++++++++++ .../src/app/shared/helpers/content.service.ts | 24 ++--- 2 files changed, 108 insertions(+), 11 deletions(-) diff --git a/apps/api/src/app/shared/helpers/content.service.spec.ts b/apps/api/src/app/shared/helpers/content.service.spec.ts index 1dd0833410d..70f80455447 100644 --- a/apps/api/src/app/shared/helpers/content.service.spec.ts +++ b/apps/api/src/app/shared/helpers/content.service.spec.ts @@ -335,4 +335,99 @@ describe('ContentService', function () { expect(extractVariables[0].name).to.include('lastName'); }); }); + + describe('extractStepVariables', () => { + it('should not fail if no filters available', () => { + const contentService = new ContentService(); + const messages = [ + { + template: { + type: StepTypeEnum.EMAIL, + subject: 'Test {{subscriber.firstName}}', + content: [ + { + content: 'Test of {{subscriber.firstName}} {{lastName}}', + type: 'text', + }, + ], + }, + }, + ] as INotificationTemplateStep[]; + const variables = contentService.extractStepVariables(messages); + + expect(variables.length).to.equal(0); + }); + + it('should not fail if filters are set as non array', () => { + const contentService = new ContentService(); + const messages = [ + { + template: { + type: StepTypeEnum.EMAIL, + subject: 'Test {{subscriber.firstName}}', + content: [ + { + content: 'Test of {{subscriber.firstName}} {{lastName}}', + type: 'text', + }, + ], + }, + filters: {}, + }, + ] as INotificationTemplateStep[]; + const variables = contentService.extractStepVariables(messages); + + expect(variables.length).to.equal(0); + }); + + it('should not fail if filters are an empty array', () => { + const contentService = new ContentService(); + const messages = [ + { + template: { + type: StepTypeEnum.EMAIL, + subject: 'Test {{subscriber.firstName}}', + content: [ + { + content: 'Test of {{subscriber.firstName}} {{lastName}}', + type: 'text', + }, + ], + }, + filters: [], + }, + ] as INotificationTemplateStep[]; + const variables = contentService.extractStepVariables(messages); + + expect(variables.length).to.equal(0); + }); + + it('should not fail if filters have some wrong settings like missing children in filters', () => { + const contentService = new ContentService(); + const messages = [ + { + template: { + type: StepTypeEnum.EMAIL, + subject: 'Test {{subscriber.firstName}}', + content: [ + { + content: 'Test of {{subscriber.firstName}} {{lastName}}', + type: 'text', + }, + ], + }, + filters: [ + { + isNegated: false, + type: 'GROUP', + value: 'AND', + }, + ], + }, + ] as INotificationTemplateStep[]; + const variables = contentService.extractStepVariables(messages); + + expect(variables.length).to.equal(0); + }); + }); }); diff --git a/apps/api/src/app/shared/helpers/content.service.ts b/apps/api/src/app/shared/helpers/content.service.ts index 6dfcff38f89..4e5c14d9730 100644 --- a/apps/api/src/app/shared/helpers/content.service.ts +++ b/apps/api/src/app/shared/helpers/content.service.ts @@ -73,17 +73,19 @@ export class ContentService { for (const message of messages) { if (message.filters) { - const filterVariables = message.filters.flatMap((filter) => - filter.children - .filter((item) => item.on === FilterPartTypeEnum.PAYLOAD) - .map((item: IFieldFilterPart) => { - return { - name: item.field, - type: TemplateVariableTypeEnum.STRING, - }; - }) - ); - variables.push(...filterVariables); + const filters = Array.isArray(message.filters) ? message.filters : []; + const filteredVariables = filters.flatMap((filter) => { + const filteredChildren = filter.children?.filter((item) => item.on === FilterPartTypeEnum.PAYLOAD) || []; + const mappedChildren = filteredChildren.map((item: IFieldFilterPart) => { + return { + name: item.field, + type: TemplateVariableTypeEnum.STRING, + }; + }); + + return mappedChildren; + }); + variables.push(...filteredVariables); } if (message.metadata?.type === DelayTypeEnum.SCHEDULED && message.metadata.delayPath) {