Skip to content

Commit

Permalink
⚡ improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
michael-radency committed Mar 10, 2022
1 parent 4a961bf commit 540a674
Show file tree
Hide file tree
Showing 2 changed files with 128 additions and 40 deletions.
122 changes: 97 additions & 25 deletions packages/nodes-base/nodes/Mailjet/EmailDescription.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export const emailOperations: INodeProperties[] = [
displayName: 'Operation',
name: 'operation',
type: 'options',
noDataExpression: true,
displayOptions: {
show: {
resource: [
Expand All @@ -25,7 +26,7 @@ export const emailOperations: INodeProperties[] = [
},
],
default: 'send',
description: 'The operation to perform.',
description: 'Choose the operation to perform',
},
];

Expand Down Expand Up @@ -78,7 +79,7 @@ export const emailFields: INodeProperties[] = [
type: 'string',
default: '',
placeholder: 'My subject line',
description: 'Subject line of the email.',
description: 'Subject line of the email',
},
{
displayName: 'Text',
Expand All @@ -98,7 +99,7 @@ export const emailFields: INodeProperties[] = [
},
},
default: '',
description: 'Plain text message of email.',
description: 'Plain text message of email',
},
{
displayName: 'HTML',
Expand All @@ -118,7 +119,7 @@ export const emailFields: INodeProperties[] = [
},
},
default: '',
description: 'HTML text message of email.',
description: 'HTML text message of email',
},
{
displayName: 'Additional Fields',
Expand Down Expand Up @@ -197,7 +198,7 @@ export const emailFields: INodeProperties[] = [
description: 'Enable tracking for this message',
},
],
description: 'Enable or disable open tracking on this message.',
description: 'Enable or disable open tracking on this message',
default: 'account_default',
},
{
Expand All @@ -221,11 +222,47 @@ export const emailFields: INodeProperties[] = [
description: 'Enable tracking for this message',
},
],
description: 'Enable or disable open tracking on this message.',
description: 'Enable or disable open tracking on this message',
default: 'account_default',
},
],
},
{
displayName: 'Use JSON for variables',
name: 'variablesAsJson',
type: 'boolean',
default: false,
displayOptions: {
show: {
resource: [
'email',
],
operation: [
'send',
],
},
},
},
{
displayName: 'Variables',
name: 'variablesUi',
type: 'json',
default: '',
description: 'Variables in JSON format',
displayOptions: {
show: {
resource: [
'email',
],
operation: [
'send',
],
variablesAsJson: [
true,
],
},
},
},
{
displayName: 'Variables',
name: 'variablesUi',
Expand All @@ -241,6 +278,9 @@ export const emailFields: INodeProperties[] = [
operation: [
'send',
],
variablesAsJson: [
false,
],
},
},
placeholder: 'Add Variable',
Expand Down Expand Up @@ -348,14 +388,14 @@ export const emailFields: INodeProperties[] = [
displayName: 'Bcc Email',
name: 'bccEmail',
type: 'string',
description: 'BCC Recipients of the email separated by ,.',
description: 'BCC Recipients of the email separated by ,',
default: '',
},
{
displayName: 'Cc Email',
name: 'ccEmail',
type: 'string',
description: 'Cc recipients of the email separated by ,.',
description: 'Cc recipients of the email separated by ,',
default: '',
},
{
Expand Down Expand Up @@ -384,27 +424,63 @@ export const emailFields: INodeProperties[] = [
default: '',
},
{
displayName: 'Track Opens',
name: 'trackOpens',
type: 'string',
description: 'Enable or disable open tracking on this message.',
default: '',
displayName: 'Template Language',
name: 'templateLanguage',
type: 'boolean',
default: false,
},
{
displayName: 'Track Clicks',
name: 'trackClicks',
type: 'string',
description: 'Enable or disable open tracking on this message.',
description: 'Enable or disable open tracking on this message',
default: '',
},
{
displayName: 'Template Language',
name: 'templateLanguage',
type: 'boolean',
default: false,
displayName: 'Track Opens',
name: 'trackOpens',
type: 'string',
description: 'Enable or disable open tracking on this message',
default: '',
},
],
},
{
displayName: 'Use JSON for variables',
name: 'variablesAsJson',
type: 'boolean',
default: false,
displayOptions: {
show: {
resource: [
'email',
],
operation: [
'sendTemplate',
],
},
},
},
{
displayName: 'Variables',
name: 'variablesUi',
type: 'json',
default: '',
description: 'Variables in JSON format',
displayOptions: {
show: {
resource: [
'email',
],
operation: [
'sendTemplate',
],
variablesAsJson: [
true,
],
},
},
},
{
displayName: 'Variables',
name: 'variablesUi',
Expand All @@ -420,6 +496,9 @@ export const emailFields: INodeProperties[] = [
operation: [
'sendTemplate',
],
variablesAsJson: [
false,
],
},
},
placeholder: 'Add Variable',
Expand All @@ -445,11 +524,4 @@ export const emailFields: INodeProperties[] = [
},
],
},
{
displayName: 'Variables (JSON)',
name: 'variables',
type: 'json',
default: '',
description: "Template variables in JSON"
}
];
46 changes: 31 additions & 15 deletions packages/nodes-base/nodes/Mailjet/Mailjet.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
INodePropertyOptions,
INodeType,
INodeTypeDescription,
JsonObject,
} from 'n8n-workflow';

import {
Expand Down Expand Up @@ -69,6 +70,7 @@ export class Mailjet implements INodeType {
displayName: 'Resource',
name: 'resource',
type: 'options',
noDataExpression: true,
options: [
{
name: 'Email',
Expand All @@ -80,7 +82,7 @@ export class Mailjet implements INodeType {
},
],
default: 'email',
description: 'Resource to consume.',
description: 'Resource to consume',
},
...emailOperations,
...emailFields,
Expand Down Expand Up @@ -126,7 +128,13 @@ 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[];
let variables;
const variablesAsJson = this.getNodeParameter('variablesAsJson', i) as boolean;
if (variablesAsJson) {
variables = this.getNodeParameter('variablesUi', i) as string;
} else {
variables = (this.getNodeParameter('variablesUi', i) as IDataObject).variablesValues as IDataObject[];
}

const body: IMessage = {
From: {
Expand All @@ -145,10 +153,15 @@ export class Mailjet implements INodeType {
});
}
if (variables) {
for (const variable of variables) {
body.Variables![variable.name as string] = variable.value;
if (variablesAsJson) {
Object.assign(body.Variables!, JSON.parse(variables as string));
} else {
for (const variable of variables as IDataObject[]) {
body.Variables![variable.name as string] = variable.value;
}
}
}
console.log(body.Variables);
if (htmlBody) {
body.HTMLPart = htmlBody;
}
Expand Down Expand Up @@ -201,10 +214,15 @@ 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 variablesJson = this.getNodeParameter('variables', i) as string;
const additionalFields = this.getNodeParameter('additionalFields', i) as IDataObject;
const toEmail = (this.getNodeParameter('toEmail', i) as string).split(',') as string[];
let variables;
const variablesAsJson = this.getNodeParameter('variablesAsJson', i) as boolean;
if (variablesAsJson) {
variables = this.getNodeParameter('variablesUi', i) as string;
} else {
variables = (this.getNodeParameter('variablesUi', i) as IDataObject).variablesValues as IDataObject[];
}

const body: IMessage = {
From: {
Expand All @@ -223,15 +241,13 @@ export class Mailjet implements INodeType {
Email: email,
});
}

if (variablesJson) {
const v = JSON.parse(variablesJson);
Object.assign(body.Variables!, v);
}

if (variables) {
for (const variable of variables) {
body.Variables![variable.name as string] = variable.value;
if (variablesAsJson) {
Object.assign(body.Variables!, JSON.parse(variables as string));
} else {
for (const variable of variables as IDataObject[]) {
body.Variables![variable.name as string] = variable.value;
}
}
}
if (additionalFields.bccEmail) {
Expand Down Expand Up @@ -296,7 +312,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 540a674

Please sign in to comment.