Skip to content

Commit

Permalink
Merge pull request #4308 from novuhq/fix-content-service-filter-varia…
Browse files Browse the repository at this point in the history
…bles

fix(api): malformed filters in messages throw errors
  • Loading branch information
Pablo Fernández authored Oct 2, 2023
2 parents 4991111 + 3f24f95 commit a2337d8
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 11 deletions.
95 changes: 95 additions & 0 deletions apps/api/src/app/shared/helpers/content.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
});
24 changes: 13 additions & 11 deletions apps/api/src/app/shared/helpers/content.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit a2337d8

Please sign in to comment.