Skip to content

Commit

Permalink
Merge pull request #4188 from novuhq/vatiants-polishing
Browse files Browse the repository at this point in the history
Variants polishing
  • Loading branch information
djabarovgeorge authored Oct 2, 2023
2 parents 399e2db + be992b9 commit 8b99137
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,21 @@ describe('Create Workflow - /workflows (POST)', async () => {
type: StepTypeEnum.EMAIL,
},
active: defaultMessageIsActive,
filters: [
{
isNegated: false,
type: 'GROUP',
value: 'AND',
children: [
{
on: FilterPartTypeEnum.TENANT,
field: 'name',
value: 'Titans',
operator: 'EQUAL',
},
],
},
],
},
],
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { expect } from 'chai';
import { UserSession, NotificationTemplateService } from '@novu/testing';
import { StepTypeEnum, INotificationTemplate, IUpdateNotificationTemplateDto } from '@novu/shared';
import { StepTypeEnum, INotificationTemplate, IUpdateNotificationTemplateDto, FilterPartTypeEnum } from '@novu/shared';
import { ChangeRepository } from '@novu/dal';
import { CreateWorkflowRequestDto, UpdateWorkflowRequestDto } from '../dto';
import { WorkflowResponse } from '../dto/workflow-response.dto';
Expand Down Expand Up @@ -31,12 +31,42 @@ describe('Update workflow by id - /workflows/:workflowId (PUT)', async () => {
},
variants: [
{
filters: [
{
isNegated: false,
type: 'GROUP',
value: 'AND',
children: [
{
on: FilterPartTypeEnum.TENANT,
field: 'name',
value: 'Titans',
operator: 'EQUAL',
},
],
},
],
template: {
type: StepTypeEnum.IN_APP,
content: 'first content',
},
},
{
filters: [
{
isNegated: false,
type: 'GROUP',
value: 'AND',
children: [
{
on: FilterPartTypeEnum.TENANT,
field: 'name',
value: 'Titans',
operator: 'EQUAL',
},
],
},
],
template: {
type: StepTypeEnum.IN_APP,
content: 'second content',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ export class CreateNotificationTemplate {
async execute(usecaseCommand: CreateNotificationTemplateCommand) {
const blueprintCommand = await this.processBlueprint(usecaseCommand);
const command = blueprintCommand ?? usecaseCommand;

this.validatePayload(command);

const triggerIdentifier = `${slugify(command.name, {
lower: true,
strict: true,
Expand All @@ -61,6 +64,16 @@ export class CreateNotificationTemplate {
return storedWorkflow;
}

private validatePayload(command: CreateNotificationTemplateCommand) {
const variants = command.steps ? command.steps?.flatMap((step) => step.variants || []) : [];

for (const variant of variants) {
if (!variant.filters?.length) {
throw new ApiException(`Variant conditions are required, variant name ${variant.name} id ${variant._id}`);
}
}
}

private async createNotificationTrigger(
command: CreateNotificationTemplateCommand,
triggerIdentifier: string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ export class UpdateNotificationTemplate {
) {}

async execute(command: UpdateNotificationTemplateCommand): Promise<NotificationTemplateEntity> {
this.validatePayload(command);

const existingTemplate = await this.notificationTemplateRepository.findById(command.id, command.environmentId);
if (!existingTemplate) throw new NotFoundException(`Notification template with id ${command.id} not found`);

Expand Down Expand Up @@ -208,6 +210,16 @@ export class UpdateNotificationTemplate {
}
}

private validatePayload(command: UpdateNotificationTemplateCommand) {
const variants = command.steps ? command.steps?.flatMap((step) => step.variants || []) : [];

for (const variant of variants) {
if (!variant.filters?.length) {
throw new ApiException(`Variant filters are required, variant name ${variant.name} id ${variant._id}`);
}
}
}

private async updateMessageTemplates(
steps: NotificationStep[],
command: UpdateNotificationTemplateCommand,
Expand Down

0 comments on commit 8b99137

Please sign in to comment.