Skip to content

Commit

Permalink
feat(api): add usage of bridge provider options in send message useca…
Browse files Browse the repository at this point in the history
…ses a… (#6062)

Co-authored-by: Richard Fontein <[email protected]>
  • Loading branch information
davidsoderberg and rifont authored Aug 8, 2024
1 parent 4a363ce commit 01268c6
Show file tree
Hide file tree
Showing 235 changed files with 8,527 additions and 3,976 deletions.
50 changes: 49 additions & 1 deletion .cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -660,7 +660,55 @@
"zwnj",
"Embedder",
"credentialless",
"coep"
"coep",
"prepush",
"xcodebuild",
"vstack",
"nimma",
"jpath",
"Nimma",
"jpath",
"vstack",
"liquidjs",
"tailwindcss",
"focusable",
"textareas",
"frameworkterminal",
"unarchived",
"Unarchived",
"grayscale",
"directcss",
"deliverytime",
"testmode",
"attatchment",
"Attatchment",
"ical",
"ganalytics",
"myicon",
"rrggbb",
"playerids",
"TVVV",
"Udnw",
"ARGB",
"EMUI",
"notif",
"suported",
"subresource",
"htmlonly",
"apidevtools",
"contenteditable",
"powerpack",
"deepmerge",
"kebabcase",
"nonenumerable",
"Deepmerge",
"RBPT",
"Primitiveboolean",
"Primitivenumber",
"Primitivestring",
"Listofobjects",
"Nestedobject",
"Listoflistofobjects"
],
"flagWords": [],
"patterns": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ export class SendTestEmail {

let html = '';
let subject = '';
let bridgeProviderData: Record<string, unknown> = {};

if (!command.bridge) {
const template = await this.compileEmailTemplateUsecase.execute(
Expand Down Expand Up @@ -111,6 +112,10 @@ export class SendTestEmail {

html = data.outputs.body;
subject = data.outputs.subject;

if (data.providers && typeof data.providers === 'object') {
bridgeProviderData = data.providers[integration.providerId] || {};
}
}

if (email && integration) {
Expand All @@ -121,7 +126,7 @@ export class SendTestEmail {
from: command.payload.$sender_email || integration?.credentials.from || '[email protected]',
};

await this.sendMessage(integration, mailData, mailFactory, command);
await this.sendMessage(integration, mailData, mailFactory, command, bridgeProviderData);

return;
}
Expand All @@ -131,13 +136,14 @@ export class SendTestEmail {
integration: IntegrationEntity,
mailData: IEmailOptions,
mailFactory: MailFactory,
command: SendTestEmailCommand
command: SendTestEmailCommand,
bridgeProviderData: Record<string, unknown>
) {
const { providerId } = integration;

try {
const mailHandler = mailFactory.getHandler(integration, mailData.from);
await mailHandler.send(mailData);
await mailHandler.send({ ...mailData, bridgeProviderData });
this.analyticsService.track('Test Email Sent - [Events]', command.userId, {
_organization: command.organizationId,
_environment: command.environmentId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -397,8 +397,10 @@ export class SendMessageChat extends SendMessageBase {
...(command.overrides[integration?.channel] || {}),
...(command.overrides[integration?.providerId] || {}),
};
const bridgeProviderData = command.bridgeData?.providers?.[integration.providerId] || {};

const result = await chatHandler.send({
bridgeProviderData,
phoneNumber,
customData: overrides,
webhookUrl: chatWebhookUrl,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -446,9 +446,10 @@ 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] || {};

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

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,7 @@ export class SendMessagePush extends SendMessageBase {
try {
const pushHandler = this.getIntegrationHandler(integration);
const bridgeOutputs = command.bridgeData?.outputs;
const bridgeProviderData = command.bridgeData?.providers?.[integration.providerId] || {};

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

await this.executionLogRoute.execute(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -280,13 +280,15 @@ export class SendMessageSms extends SendMessageBase {
if (!smsHandler) {
throw new PlatformException(`Sms handler for provider ${integration.providerId} is not found`);
}
const bridgeProviderData = command.bridgeData?.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,
});

await this.executionLogRoute.execute(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ export abstract class BaseChatHandler implements IChatHandler {
return {};
}

return await this.provider.sendMessage(chatContent);
const { bridgeProviderData, ...content } = chatContent;

return await this.provider.sendMessage(content, bridgeProviderData);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ export abstract class BaseHandler implements IMailHandler {
return {};
}

return await this.provider.sendMessage(mailData);
const { bridgeProviderData, ...otherOptions } = mailData;

return await this.provider.sendMessage(otherOptions, bridgeProviderData);
}

public getProvider(): IEmailProvider {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ export abstract class BasePushHandler implements IPushHandler {
);
}

return await this.provider.sendMessage(options);
const { bridgeProviderData, ...otherOptions } = options;

return await this.provider.sendMessage(otherOptions, bridgeProviderData);
}

abstract buildProvider(credentials: ICredentials);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ export abstract class BaseSmsHandler implements ISmsHandler {
);
}

return await this.provider.sendMessage(options);
const { bridgeProviderData, ...otherOptions } = options;

return await this.provider.sendMessage(otherOptions, bridgeProviderData);
}

abstract buildProvider(credentials: ICredentials);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ import {
I<%= pascalType %>Options,
I<%= pascalType %>Provider,
} from '@novu/stateless';
import { BaseProvider } from '../../../base.provider';
import { WithPassthrough } from '../../../utils/types';

export class <%= pascalName %><%= pascalType %>Provider implements I<%= pascalType %>Provider {
export class <%= pascalName %><%= pascalType %>Provider extends BaseProvider implements I<%= pascalType %>Provider {
id = '<%= name %>';
channelType = ChannelTypeEnum.<%= upperType %> as ChannelTypeEnum.<%= upperType %>;

Expand All @@ -14,11 +16,14 @@ export class <%= pascalName %><%= pascalType %>Provider implements I<%= pascalTy
<%= upperType === 'EMAIL' ? 'apiKey: string;' : null %>
}
) {
super();
}

async sendMessage(
options: I<%= pascalType %>Options
options: I<%= pascalType %>Options,
bridgeProviderData: WithPassthrough<Record<string, unknown>> = {}
): Promise<ISendMessageSuccessResponse> {
const data = this.transform(bridgeProviderData, options);


return {
Expand Down
10 changes: 5 additions & 5 deletions libs/automation/src/generators/provider/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ const PROVIDERS_BASE_FOLDER = path.join('..', '..', 'packages', 'providers', 'sr
export async function providerGenerator(tree: Tree, options: IProviderGeneratorSchema) {
options = enrichOptionsWithMultipleCases(options);
const providerNameInKebabCase = options.name;
const providerInnerFolder = path.join(PROVIDERS_BASE_FOLDER, providerNameInKebabCase);
const providerInnerFolder = path.join(PROVIDERS_BASE_FOLDER, options.type.toLowerCase(), providerNameInKebabCase);
buildAndAddProjectConfiguration(tree, options, providerInnerFolder);
generateFilesBasedOnTemplate(tree, providerInnerFolder, options);
addExportToIndexTs(providerNameInKebabCase);
addExportToIndexTs(providerNameInKebabCase, options.type);
removeDefaultProjectJsonFromTree(tree, providerInnerFolder);
await formatFiles(tree);
}
Expand Down Expand Up @@ -65,7 +65,7 @@ function buildAndAddProjectConfiguration(tree: Tree, options: IProviderGenerator
addProjectConfiguration(tree, options.name, {
root: projectRoot,
projectType: 'library',
sourceRoot: `${projectRoot}/src`,
sourceRoot: projectRoot,
targets: {},
});
}
Expand All @@ -74,8 +74,8 @@ function buildExportLine(providerName: string) {
return `export * from './${providerName}/${providerName}.provider';`;
}

function addExportToIndexTs(providerName: string) {
const indexTsPath = PROVIDERS_BASE_FOLDER + '/index.ts';
function addExportToIndexTs(providerName: string, type: string) {
const indexTsPath = path.join(PROVIDERS_BASE_FOLDER, type.toLowerCase(), 'index.ts');
addLineToFile(indexTsPath, buildExportLine(providerName));
}

Expand Down
1 change: 1 addition & 0 deletions libs/shared/src/types/events/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export interface IEmailOptions {
customData?: Record<string, any>;
headers?: Record<string, string>;
senderName?: string;
bridgeProviderData?: Record<string, unknown>;
}

export interface ITriggerPayload {
Expand Down
4 changes: 2 additions & 2 deletions packages/framework/src/schemas/providers/sms/twilio.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const twilioOutputSchema = {
properties: {
to: {
type: 'string',
format: 'phone-number',
pattern: '^\\+[1-9]\\d{1,14}$',
description:
"The recipient's phone number in [E.164](https://www.twilio.com/docs/glossary/what-e164) format (for SMS/MMS) or [channel address](https://www.twilio.com/docs/messaging/channels), e.g. `whatsapp:+15552229999`.",
},
Expand Down Expand Up @@ -108,7 +108,7 @@ const twilioOutputSchema = {
},
from: {
type: 'string',
format: 'phone-number',
pattern: '^\\+[1-9]\\d{1,14}$',
description:
"The sender's Twilio phone number (in [E.164](https://en.wikipedia.org/wiki/E.164) format), [alphanumeric sender ID](https://www.twilio.com/docs/sms/quickstart), [Wireless SIM](https://www.twilio.com/docs/iot/wireless/programmable-wireless-send-machine-machine-sms-commands), [short code](https://www.twilio.com/en-us/messaging/channels/sms/short-codes), or [channel address](https://www.twilio.com/docs/messaging/channels) (e.g., `whatsapp:+15554449999`). The value of the `from` parameter must be a sender that is hosted within Twilio and belongs to the Account creating the Message. If you are using `messagingServiceSid`, this parameter can be empty (Twilio assigns a `from` value from the Messaging Service's Sender Pool) or you can provide a specific sender from your Sender Pool.",
},
Expand Down
1 change: 0 additions & 1 deletion packages/providers/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
.nyc_output
build
node_modules
test
src/**.js
coverage
*.log
Expand Down
17 changes: 0 additions & 17 deletions packages/providers/jest.config.js

This file was deleted.

16 changes: 4 additions & 12 deletions packages/providers/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,10 @@
"fix": "run-s fix:*",
"fix:prettier": "prettier \"src/**/*.ts\" --write",
"fix:lint": "eslint src --ext .ts --fix",
"test": "run-s test:* ",
"lint": "eslint src --ext .ts",
"test:unit": "jest src ",
"test": "vitest",
"watch:build": "tsc -p tsconfig.json -w",
"watch:test": "jest src --watch",
"watch:test": "vitest",
"reset-hard": "git clean -dfx && git reset --hard && yarn",
"prepare-release": "run-s reset-hard test"
},
Expand Down Expand Up @@ -77,9 +76,6 @@
"@babel/preset-env": "^7.23.2",
"@babel/preset-typescript": "^7.13.0",
"@istanbuljs/nyc-config-typescript": "~1.0.1",
"@jest/globals": "^29.7.0",
"@types/jest": "~29.5.2",
"@types/mocha": "^10.0.2",
"@types/node-mailjet": "^3.3.7",
"@types/nodemailer": "^6.4.4",
"@types/sparkpost": "^2.1.5",
Expand All @@ -89,21 +85,17 @@
"cspell": "~6.19.2",
"eslint": "^8.16.0",
"eslint-plugin-eslint-comments": "^3.2.0",
"fetch-mock": "^9.11.0",
"jest": "~27.5.1",
"jest-fetch-mock": "^3.0.3",
"jest-node-exports-resolver": "^1.1.6",
"nock": "^13.1.3",
"npm-run-all": "^4.1.5",
"nyc": "~15.1.0",
"open-cli": "^6.0.1",
"prettier": "~2.8.0",
"rimraf": "~3.0.2",
"ts-jest": "~27.1.5",
"ts-node": "~10.9.1",
"typedoc": "^0.24.0",
"typescript": "^4.9.5",
"uuid": "^9.0.0"
"uuid": "^9.0.0",
"vitest": "^2.0.5"
},
"files": [
"build/main",
Expand Down
Loading

0 comments on commit 01268c6

Please sign in to comment.