Skip to content

Commit

Permalink
feat(Mailjet Node): Add credential tests and support for sandbox, JSO…
Browse files Browse the repository at this point in the history
…N parameters & variables (#2987)

* Add Variables JSON to Mailjet Batch send

* ⚡ Improvements

* ⚡ Add credential verification

* ⚡ Small improvement

Co-authored-by: Marcin Koziej <marcin@cahoots.pl>
  • Loading branch information
RicardoE105 and marcinkoziej authored Mar 20, 2022
1 parent 26a7c61 commit d2756de
Show file tree
Hide file tree
Showing 4 changed files with 197 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,12 @@ export class MailjetEmailApi implements ICredentialType {
type: 'string',
default: '',
},
{
displayName: 'Sandbox Mode',
name: 'sandboxMode',
type: 'boolean',
default: false,
description: 'Allow to run the API call in a Sandbox mode, where all validations of the payload will be done without delivering the message',
},
];
}
89 changes: 87 additions & 2 deletions packages/nodes-base/nodes/Mailjet/EmailDescription.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { INodeProperties } from 'n8n-workflow';
import {
INodeProperties,
} from 'n8n-workflow';

export const emailOperations: INodeProperties[] = [
{
Expand All @@ -25,7 +27,6 @@ export const emailOperations: INodeProperties[] = [
},
],
default: 'send',
description: 'The operation to perform.',
},
];

Expand Down Expand Up @@ -120,6 +121,22 @@ export const emailFields: INodeProperties[] = [
default: '',
description: 'HTML text message of email.',
},
{
displayName: 'JSON Parameters',
name: 'jsonParameters',
type: 'boolean',
default: false,
displayOptions: {
show: {
resource: [
'email',
],
operation: [
'send',
],
},
},
},
{
displayName: 'Additional Fields',
name: 'additionalFields',
Expand Down Expand Up @@ -226,6 +243,29 @@ export const emailFields: INodeProperties[] = [
},
],
},
{
displayName: 'Variables (JSON)',
name: 'variablesJson',
type: 'string',
typeOptions: {
alwaysOpenEditWindow: true,
},
displayOptions: {
show: {
resource: [
'email',
],
operation: [
'send',
],
jsonParameters: [
true,
],
},
},
default: '',
description: 'HTML text message of email.',
},
{
displayName: 'Variables',
name: 'variablesUi',
Expand All @@ -241,6 +281,9 @@ export const emailFields: INodeProperties[] = [
operation: [
'send',
],
jsonParameters: [
false,
],
},
},
placeholder: 'Add Variable',
Expand Down Expand Up @@ -327,6 +370,22 @@ export const emailFields: INodeProperties[] = [
},
},
},
{
displayName: 'JSON Parameters',
name: 'jsonParameters',
type: 'boolean',
default: false,
displayOptions: {
show: {
resource: [
'email',
],
operation: [
'sendTemplate',
],
},
},
},
{
displayName: 'Additional Fields',
name: 'additionalFields',
Expand Down Expand Up @@ -420,6 +479,9 @@ export const emailFields: INodeProperties[] = [
operation: [
'sendTemplate',
],
jsonParameters: [
false,
],
},
},
placeholder: 'Add Variable',
Expand All @@ -445,4 +507,27 @@ export const emailFields: INodeProperties[] = [
},
],
},
{
displayName: 'Variables (JSON)',
name: 'variablesJson',
type: 'string',
typeOptions: {
alwaysOpenEditWindow: true,
},
displayOptions: {
show: {
resource: [
'email',
],
operation: [
'sendTemplate',
],
jsonParameters: [
true,
],
},
},
default: '',
description: 'HTML text message of email.',
},
];
53 changes: 50 additions & 3 deletions packages/nodes-base/nodes/Mailjet/GenericFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,16 @@ import {
} from 'n8n-core';

import {
ICredentialDataDecryptedObject,
ICredentialTestFunctions,
IDataObject,
IHookFunctions,
JsonObject,
NodeApiError,
} from 'n8n-workflow';

export async function mailjetApiRequest(this: IExecuteFunctions | IExecuteSingleFunctions | IHookFunctions | ILoadOptionsFunctions, method: string, resource: string, body: any = {}, qs: IDataObject = {}, uri?: string, option: IDataObject = {}): Promise<any> { // tslint:disable-line:no-any
const emailApiCredentials = await this.getCredentials('mailjetEmailApi');
const emailApiCredentials = await this.getCredentials('mailjetEmailApi') as { apiKey: string, secretKey: string, sandboxMode: boolean };
let options: OptionsWithUri = {
headers: {
Accept: 'application/json',
Expand All @@ -33,8 +35,12 @@ export async function mailjetApiRequest(this: IExecuteFunctions | IExecuteSingle
delete options.body;
}
if (emailApiCredentials !== undefined) {
const base64Credentials = Buffer.from(`${emailApiCredentials.apiKey}:${emailApiCredentials.secretKey}`).toString('base64');
options.headers!['Authorization'] = `Basic ${base64Credentials}`;
const { sandboxMode } = emailApiCredentials;
Object.assign(body, { SandboxMode: sandboxMode });
options.auth = {
username: emailApiCredentials.apiKey,
password: emailApiCredentials.secretKey,
};
} else {
const smsApiCredentials = await this.getCredentials('mailjetSmsApi');
options.headers!['Authorization'] = `Bearer ${smsApiCredentials!.token}`;
Expand Down Expand Up @@ -65,6 +71,47 @@ export async function mailjetApiRequestAllItems(this: IExecuteFunctions | IHookF
return returnData;
}

export async function validateCredentials(
this: ICredentialTestFunctions,
decryptedCredentials: ICredentialDataDecryptedObject,
): Promise<any> { // tslint:disable-line:no-any
const credentials = decryptedCredentials;

const {
apiKey,
secretKey,
} = credentials as {
apiKey: string,
secretKey: string,
};

const options: OptionsWithUri = {
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
},
auth: {
username: apiKey,
password: secretKey,
},
method: 'GET',
uri: `https://api.mailjet.com/v3/REST/template`,
json: true,
};

return await this.helpers.request(options);
}

export function validateJSON(json: string | undefined): IDataObject | undefined { // tslint:disable-line:no-any
let result;
try {
result = JSON.parse(json!);
} catch (exception) {
result = undefined;
}
return result;
}

export interface IMessage {
From?: { Email?: string, Name?: string };
Subject?: string;
Expand Down
59 changes: 53 additions & 6 deletions packages/nodes-base/nodes/Mailjet/Mailjet.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,25 @@ import {
} from 'n8n-core';

import {
ICredentialDataDecryptedObject,
ICredentialsDecrypted,
ICredentialTestFunctions,
IDataObject,
ILoadOptionsFunctions,
INodeCredentialTestResult,
INodeExecutionData,
INodePropertyOptions,
INodeType,
INodeTypeDescription,
JsonObject,
NodeOperationError,
} from 'n8n-workflow';

import {
IMessage,
mailjetApiRequest,
validateCredentials,
validateJSON,
} from './GenericFunctions';

import {
Expand All @@ -25,7 +33,6 @@ import {
smsFields,
smsOperations,
} from './SmsDescription';

export class Mailjet implements INodeType {
description: INodeTypeDescription = {
displayName: 'Mailjet',
Expand All @@ -44,6 +51,7 @@ export class Mailjet implements INodeType {
{
name: 'mailjetEmailApi',
required: true,
testedBy: 'mailjetEmailApiTest',
displayOptions: {
show: {
resource: [
Expand Down Expand Up @@ -90,6 +98,25 @@ export class Mailjet implements INodeType {
};

methods = {
credentialTest: {
async mailjetEmailApiTest(this: ICredentialTestFunctions, credential: ICredentialsDecrypted): Promise<INodeCredentialTestResult> {
try {
await validateCredentials.call(this, credential.data as ICredentialDataDecryptedObject);
} catch (error) {
const err = error as JsonObject;
if (err.statusCode === 401) {
return {
status: 'Error',
message: `Invalid credentials`,
};
}
}
return {
status: 'OK',
message: 'Authentication successful',
};
},
},
loadOptions: {
// Get all the available custom fields to display them to user so that he can
// select them easily
Expand Down Expand Up @@ -126,7 +153,7 @@ export class Mailjet implements INodeType {
const subject = this.getNodeParameter('subject', i) as string;
const additionalFields = this.getNodeParameter('additionalFields', i) as IDataObject;
const toEmail = (this.getNodeParameter('toEmail', i) as string).split(',') as string[];
const variables = (this.getNodeParameter('variablesUi', i) as IDataObject).variablesValues as IDataObject[];
const jsonParameters = this.getNodeParameter('jsonParameters', i) as boolean;

const body: IMessage = {
From: {
Expand All @@ -144,11 +171,21 @@ export class Mailjet implements INodeType {
Email: email,
});
}
if (variables) {

if (jsonParameters) {
const variablesJson = this.getNodeParameter('variablesJson', i) as string;
const parsedJson = validateJSON(variablesJson);
if (parsedJson === undefined) {
throw new NodeOperationError(this.getNode(),`Parameter 'Variables (JSON)' has a invalid JSON`);
}
body.Variables = parsedJson;
} else {
const variables = (this.getNodeParameter('variablesUi', i) as IDataObject).variablesValues as IDataObject[] || [];
for (const variable of variables) {
body.Variables![variable.name as string] = variable.value;
}
}

if (htmlBody) {
body.HTMLPart = htmlBody;
}
Expand Down Expand Up @@ -201,9 +238,9 @@ export class Mailjet implements INodeType {
const fromEmail = this.getNodeParameter('fromEmail', i) as string;
const templateId = parseInt(this.getNodeParameter('templateId', i) as string, 10);
const subject = this.getNodeParameter('subject', i) as string;
const variables = (this.getNodeParameter('variablesUi', i) as IDataObject).variablesValues as IDataObject[];
const additionalFields = this.getNodeParameter('additionalFields', i) as IDataObject;
const toEmail = (this.getNodeParameter('toEmail', i) as string).split(',') as string[];
const jsonParameters = this.getNodeParameter('jsonParameters', i) as boolean;

const body: IMessage = {
From: {
Expand All @@ -222,11 +259,21 @@ export class Mailjet implements INodeType {
Email: email,
});
}
if (variables) {

if (jsonParameters) {
const variablesJson = this.getNodeParameter('variablesJson', i) as string;
const parsedJson = validateJSON(variablesJson);
if (parsedJson === undefined) {
throw new NodeOperationError(this.getNode(), `Parameter 'Variables (JSON)' has a invalid JSON`);
}
body.Variables = parsedJson;
} else {
const variables = (this.getNodeParameter('variablesUi', i) as IDataObject).variablesValues as IDataObject[] || [];
for (const variable of variables) {
body.Variables![variable.name as string] = variable.value;
}
}

if (additionalFields.bccEmail) {
const bccEmail = (additionalFields.bccEmail as string).split(',') as string[];
for (const email of bccEmail) {
Expand Down Expand Up @@ -289,7 +336,7 @@ export class Mailjet implements INodeType {
}
} catch (error) {
if (this.continueOnFail()) {
returnData.push({ error: error.message });
returnData.push({ error: (error as JsonObject).message });
continue;
}
throw error;
Expand Down

0 comments on commit d2756de

Please sign in to comment.