Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(api-service): provider trigger overrides alignment - WIP #7448

Draft
wants to merge 6 commits into
base: next
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion apps/api/src/app/events/dtos/trigger-event-request.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
TriggerRecipientsTypeEnum,
TriggerRecipientSubscriber,
TriggerTenantContext,
TriggerOverrides,
} from '@novu/shared';
import { CreateSubscriberRequestDto } from '../../subscribers/dtos';
import { UpdateTenantRequestDto } from '../../tenant/dtos';
Expand Down Expand Up @@ -99,7 +100,7 @@ export class TriggerEventRequestDto {
})
@IsObject()
@IsOptional()
overrides?: Record<string, Record<string, unknown>>;
overrides?: TriggerOverrides;

@ApiProperty({
description: 'The recipients list of people who will receive the notification.',
Expand Down
2 changes: 1 addition & 1 deletion apps/api/src/app/events/events.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ export class EventsController {
organizationId: user.organizationId,
identifier: body.name,
payload: body.payload || {},
overrides: body.overrides || {},
overrides: body.overrides || ({} as any),
to: body.to,
actor: body.actor,
tenant: body.tenant,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
TriggerRecipientSubscriber,
TriggerRequestCategoryEnum,
TriggerTenantContext,
TriggerOverrides,
} from '@novu/shared';

import { EnvironmentWithUserCommand } from '../../../shared/commands/project.command';
Expand All @@ -19,7 +20,7 @@ export class ParseEventRequestBaseCommand extends EnvironmentWithUserCommand {
payload: any; // eslint-disable-line @typescript-eslint/no-explicit-any

@IsDefined()
overrides: Record<string, Record<string, unknown>>;
overrides: TriggerOverrides | {};

@IsString()
@IsOptional()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export class ProcessBulkTrigger {
organizationId: command.organizationId,
identifier: event.name,
payload: event.payload,
overrides: event.overrides || {},
overrides: event.overrides || ({} as any),
to: event.to,
actor: event.actor,
tenant: event.tenant,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
import { Injectable } from '@nestjs/common';
import { SubscriberRepository } from '@novu/dal';
import { AddressingTypeEnum, TriggerEventStatusEnum, TriggerRequestCategoryEnum } from '@novu/shared';

import { TriggerEventToAllCommand } from './trigger-event-to-all.command';
import { ParseEventRequest, ParseEventRequestBroadcastCommand } from '../parse-event-request';

@Injectable()
export class TriggerEventToAll {
constructor(
private subscriberRepository: SubscriberRepository,
private parseEventRequest: ParseEventRequest
) {}
constructor(private parseEventRequest: ParseEventRequest) {}

public async execute(command: TriggerEventToAllCommand) {
await this.parseEventRequest.execute(
Expand All @@ -22,7 +18,7 @@ export class TriggerEventToAll {
payload: command.payload || {},
addressingType: AddressingTypeEnum.BROADCAST,
transactionId: command.transactionId,
overrides: command.overrides || {},
overrides: command.overrides || ({} as any),
actor: command.actor,
tenant: command.tenant,
requestCategory: TriggerRequestCategoryEnum.SINGLE,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -393,9 +393,12 @@ export class SendMessageChat extends SendMessageBase {
...(command.overrides[integration?.providerId] || {}),
};
const bridgeProviderData = command.bridgeData?.providers?.[integration.providerId] || {};
const triggerOverrides = command.step.stepId
? command.overrides?.steps?.[command.step.stepId]?.providers[integration.providerId] || {}
: {};

const result = await chatHandler.send({
bridgeProviderData,
bridgeProviderData: { ...bridgeProviderData, ...triggerOverrides },
phoneNumber,
customData: overrides,
webhookUrl: chatWebhookUrl,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { addBreadcrumb } from '@sentry/node';

import {
MessageRepository,
NotificationStepEntity,
SubscriberRepository,
EnvironmentRepository,
IntegrationEntity,
Expand Down Expand Up @@ -440,9 +439,15 @@ export class SendMessageEmail extends SendMessageBase {
const mailFactory = new MailFactory();
const mailHandler = mailFactory.getHandler(this.buildFactoryIntegration(integration), mailData.from);
const bridgeProviderData = command.bridgeData?.providers?.[integration.providerId] || {};
const triggerOverrides = command.step.stepId
? command.overrides?.steps?.[command.step.stepId]?.providers[integration.providerId] || {}
: {};

try {
const result = await mailHandler.send({ ...mailData, bridgeProviderData });
const result = await mailHandler.send({
...mailData,
bridgeProviderData: { ...bridgeProviderData, ...triggerOverrides },
});

Logger.verbose({ command }, 'Email message has been sent', LOG_CONTEXT);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,7 @@ import { Injectable, Logger } from '@nestjs/common';
import { addBreadcrumb } from '@sentry/node';
import { ModuleRef } from '@nestjs/core';

import {
MessageRepository,
NotificationStepEntity,
SubscriberRepository,
MessageEntity,
IntegrationEntity,
JobEntity,
} from '@novu/dal';
import { MessageRepository, SubscriberRepository, MessageEntity, IntegrationEntity, JobEntity } from '@novu/dal';
import {
ChannelTypeEnum,
LogCodeEnum,
Expand Down Expand Up @@ -298,6 +291,9 @@ export class SendMessagePush extends SendMessageBase {
const pushHandler = this.getIntegrationHandler(integration);
const bridgeOutputs = command.bridgeData?.outputs;
const bridgeProviderData = command.bridgeData?.providers?.[integration.providerId] || {};
const triggerOverrides = command.step.stepId
? command.overrides?.steps?.[command.step.stepId]?.providers[integration.providerId] || {}
: {};

const result = await pushHandler.send({
target: [deviceToken],
Expand All @@ -307,7 +303,7 @@ export class SendMessagePush extends SendMessageBase {
overrides,
subscriber,
step,
bridgeProviderData,
bridgeProviderData: { ...bridgeProviderData, ...triggerOverrides },
});

await this.executionLogRoute.execute(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,7 @@ import { Injectable } from '@nestjs/common';
import { addBreadcrumb } from '@sentry/node';
import { ModuleRef } from '@nestjs/core';

import {
MessageRepository,
NotificationStepEntity,
SubscriberRepository,
MessageEntity,
IntegrationEntity,
} from '@novu/dal';
import { MessageRepository, SubscriberRepository, MessageEntity, IntegrationEntity } from '@novu/dal';
import { ChannelTypeEnum, LogCodeEnum, ExecutionDetailsSourceEnum, ExecutionDetailsStatusEnum } from '@novu/shared';
import {
InstrumentUsecase,
Expand Down Expand Up @@ -276,14 +270,17 @@ export class SendMessageSms extends SendMessageBase {
throw new PlatformException(`Sms handler for provider ${integration.providerId} is not found`);
}
const bridgeProviderData = command.bridgeData?.providers?.[integration.providerId] || {};
const triggerOverrides = command.step.stepId
? command.overrides?.steps?.[command.step.stepId]?.providers[integration.providerId] || {}
: {};

const result = await smsHandler.send({
to: overrides.to || phone,
from: overrides.from || integration.credentials.from,
content: bridgeBody || overrides.content || content,
id: message._id,
customData: overrides.customData || {},
bridgeProviderData,
bridgeProviderData: { ...bridgeProviderData, ...triggerOverrides },
});

await this.executionLogRoute.execute(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { IsDefined, IsOptional, IsString } from 'class-validator';
import { NotificationStepEntity, JobEntity } from '@novu/dal';
import { EnvironmentWithUserCommand } from '@novu/application-generic';
import { ExecuteOutput } from '@novu/framework/internal';
import { WorkflowPreferences } from '@novu/shared';
import { WorkflowPreferences, TriggerOverrides } from '@novu/shared';

export class SendMessageCommand extends EnvironmentWithUserCommand {
@IsDefined()
Expand All @@ -16,7 +16,7 @@ export class SendMessageCommand extends EnvironmentWithUserCommand {
compileContext?: any; // eslint-disable-line @typescript-eslint/no-explicit-any

@IsDefined()
overrides: Record<string, Record<string, unknown>>;
overrides: TriggerOverrides;

@IsDefined()
step: NotificationStepEntity;
Expand Down
1 change: 1 addition & 0 deletions packages/framework/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ export { NovuRequestHandler, type ServeHandlerOptions } from './handler';
export { workflow } from './resources';
export type { Workflow } from './types';
export { CronExpression } from './constants';
export { providerSchemas } from './schemas';
12 changes: 11 additions & 1 deletion packages/shared/src/dto/events/event.interface.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ISubscribersDefine, ITenantDefine, ITopic } from '../../types';
import { ISubscribersDefine, ITenantDefine, ITopic, ProvidersIdEnum } from '../../types';

export type TriggerRecipientSubscriber = string | ISubscribersDefine;

Expand All @@ -9,3 +9,13 @@ export type TriggerRecipients = TriggerRecipient[];
export type TriggerRecipientsPayload = TriggerRecipientSubscriber | TriggerRecipients;

export type TriggerTenantContext = string | ITenantDefine;

export type TriggerOverrides = {
steps: Record<
string,
{
providers: Record<ProvidersIdEnum, Record<string, unknown>>;
}
>;
[key: string]: Record<string, Record<string, unknown>>;
};
Loading