mirror of
https://github.com/Abdulazizzn/n8n-enterprise-unlocked.git
synced 2025-12-17 18:12:04 +00:00
feat(Mailjet Node): Add credential tests and support for sandbox, JSON parameters & variables (#2987)
* Add Variables JSON to Mailjet Batch send * ⚡ Improvements * ⚡ Add credential verification * ⚡ Small improvement Co-authored-by: Marcin Koziej <marcin@cahoots.pl>
This commit is contained in:
@@ -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 {
|
||||
@@ -25,7 +33,6 @@ import {
|
||||
smsFields,
|
||||
smsOperations,
|
||||
} from './SmsDescription';
|
||||
|
||||
export class Mailjet implements INodeType {
|
||||
description: INodeTypeDescription = {
|
||||
displayName: 'Mailjet',
|
||||
@@ -44,6 +51,7 @@ export class Mailjet implements INodeType {
|
||||
{
|
||||
name: 'mailjetEmailApi',
|
||||
required: true,
|
||||
testedBy: 'mailjetEmailApiTest',
|
||||
displayOptions: {
|
||||
show: {
|
||||
resource: [
|
||||
@@ -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
|
||||
@@ -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: {
|
||||
@@ -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;
|
||||
}
|
||||
@@ -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: {
|
||||
@@ -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) {
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user