Skip to content

Commit

Permalink
Merge pull request novuhq#726 from novuhq/NV-550-Validate-that-each-t…
Browse files Browse the repository at this point in the history
…rigger-has-a-subscriberId

feat: subscriber id validation
  • Loading branch information
scopsy authored Jun 27, 2022
2 parents 0161411 + 07b9ebf commit 888b94e
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 10 deletions.
25 changes: 15 additions & 10 deletions apps/api/src/app/events/events.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,7 @@ export class EventsController {
@UseGuards(JwtAuthGuard)
@Post('/trigger')
trackEvent(@UserSession() user: IJwtPayload, @Body() body: TriggerEventDto) {
const subscribers = Array.isArray(body.to) ? body.to : [body.to];
const mappedSubscribers: ISubscribersDefine[] = subscribers.map((subscriber) => {
if (typeof subscriber === 'string') {
return {
subscriberId: subscriber,
};
} else {
return subscriber;
}
});
const mappedSubscribers = this.mapSubscribers(body);

return this.triggerEvent.execute(
TriggerEventCommand.create({
Expand All @@ -39,4 +30,18 @@ export class EventsController {
})
);
}

private mapSubscribers(body: TriggerEventDto): ISubscribersDefine[] {
const subscribers = Array.isArray(body.to) ? body.to : [body.to];

return subscribers.map((subscriber) => {
if (typeof subscriber === 'string') {
return {
subscriberId: subscriber,
};
} else {
return subscriber;
}
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { ProcessSubscriberCommand } from '../process-subscriber/process-subscrib
import { matchMessageWithFilters } from './message-filter.matcher';
import { WorkflowQueueService } from '../../services/workflow.queue.service';
import { ANALYTICS_SERVICE } from '../../../shared/shared.module';
import { ApiException } from '../../../shared/exceptions/api.exception';

@Injectable()
export class TriggerEvent {
Expand All @@ -31,6 +32,8 @@ export class TriggerEvent {
},
});

await this.validateSubscriberIdProperty(command);

this.logEventTriggered(command);

const template = await this.notificationTemplateRepository.findByTriggerIdentifier(
Expand Down Expand Up @@ -153,4 +156,41 @@ export class TriggerEvent {
// eslint-disable-next-line no-console
.catch((e) => console.error(e));
}

private async validateSubscriberIdProperty(command: TriggerEventCommand): Promise<boolean> {
for (const subscriber of command.to) {
const subscriberIdExists = typeof subscriber === 'string' ? subscriber : subscriber.subscriberId;

if (!subscriberIdExists) {
await this.logSubscriberIdMissing(command);
throw new ApiException(
'subscriberId under property to is not configured, please make sure all the subscriber contains subscriberId property'
);
}
}

return true;
}

private async logSubscriberIdMissing(command: TriggerEventCommand) {
await this.createLogUsecase.execute(
CreateLogCommand.create({
transactionId: command.transactionId,
status: LogStatusEnum.ERROR,
environmentId: command.environmentId,
organizationId: command.organizationId,
text: 'SubscriberId missing in to property',
userId: command.userId,
code: LogCodeEnum.SUBSCRIBER_ID_MISSING,
raw: {
triggerIdentifier: command.identifier,
},
})
);

return {
acknowledged: true,
status: 'subscriber_id_missing',
};
}
}
1 change: 1 addition & 0 deletions libs/shared/src/entities/log/log.enums.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export enum LogCodeEnum {
SUBSCRIBER_NOT_FOUND = 3003,
SUBSCRIBER_MISSING_EMAIL = 3002,
SUBSCRIBER_MISSING_PHONE = 3003,
SUBSCRIBER_ID_MISSING = 3004,
MISSING_EMAIL_INTEGRATION = 3004,
MISSING_SMS_INTEGRATION = 3005,
}

0 comments on commit 888b94e

Please sign in to comment.