From 9d0d85875e28e5da7132acef3603a24ff8689b42 Mon Sep 17 00:00:00 2001 From: Ricardo Espinoza Date: Sat, 26 Oct 2019 12:58:04 -0400 Subject: [PATCH 1/5] Added Mandrill support --- .../credentials/MandrillApi.credentials.ts | 18 + .../nodes/Mandrill/GenericFunctions.ts | 69 ++++ .../nodes/Mandrill/Mandrill.node.ts | 322 ++++++++++++++++++ .../nodes-base/nodes/Mandrill/mandrill.png | Bin 0 -> 1291 bytes packages/nodes-base/package.json | 6 +- 5 files changed, 413 insertions(+), 2 deletions(-) create mode 100644 packages/nodes-base/credentials/MandrillApi.credentials.ts create mode 100644 packages/nodes-base/nodes/Mandrill/GenericFunctions.ts create mode 100644 packages/nodes-base/nodes/Mandrill/Mandrill.node.ts create mode 100644 packages/nodes-base/nodes/Mandrill/mandrill.png diff --git a/packages/nodes-base/credentials/MandrillApi.credentials.ts b/packages/nodes-base/credentials/MandrillApi.credentials.ts new file mode 100644 index 0000000000..3a3d2425d7 --- /dev/null +++ b/packages/nodes-base/credentials/MandrillApi.credentials.ts @@ -0,0 +1,18 @@ +import { + ICredentialType, + NodePropertyTypes, +} from 'n8n-workflow'; + + +export class MandrillApi implements ICredentialType { + name = 'mandrillApi'; + displayName = 'Mandrill API'; + properties = [ + { + displayName: 'API Key', + name: 'apiKey', + type: 'string' as NodePropertyTypes, + default: '', + }, + ]; +} diff --git a/packages/nodes-base/nodes/Mandrill/GenericFunctions.ts b/packages/nodes-base/nodes/Mandrill/GenericFunctions.ts new file mode 100644 index 0000000000..f1c526c8a4 --- /dev/null +++ b/packages/nodes-base/nodes/Mandrill/GenericFunctions.ts @@ -0,0 +1,69 @@ +import { OptionsWithUri } from 'request'; + +import { + IExecuteFunctions, + IHookFunctions, + ILoadOptionsFunctions, + IExecuteSingleFunctions +} from 'n8n-core'; + +import * as _ from 'lodash'; + +export async function mandrillApiRequest(this: IHookFunctions | IExecuteFunctions | IExecuteSingleFunctions | ILoadOptionsFunctions, resource: string, method: string, action: string, body: any = {}, headers?: object): Promise { // tslint:disable-line:no-any + const credentials = this.getCredentials('mandrillApi'); + + if (credentials === undefined) { + throw new Error('No credentials got returned!'); + } + + const data = Object.assign({ }, body, { key: credentials.apiKey }) + + const endpoint = 'mandrillapp.com/api/1.0'; + + const options: OptionsWithUri = { + headers: headers, + method, + uri: `https://${endpoint}${resource}${action}.json`, + body: data, + json: true + }; + + try { + return await this.helpers.request!(options); + } catch (error) { + console.error(error); + + const errorMessage = error.response.body.message || error.response.body.Message; + if (error.name === 'Invalid_Key') { + throw new Error('The provided API key is not a valid Mandrill API key'); + } else if (error.name === 'ValidationError') { + throw new Error('The parameters passed to the API call are invalid or not provided when required'); + } else if (error.name === 'GeneralError') { + throw new Error('An unexpected error occurred processing the request. Mandrill developers will be notified.'); + } + + if (errorMessage !== undefined) { + throw errorMessage; + } + throw error.response.body; + } +} + + export function getToEmailArray(toEmail: String): Array { // tslint:disable-line:no-any + let toEmailArray + if (toEmail.split(',').length > 0) { + const array = toEmail.split(',') + toEmailArray = _.map(array, (email) => { + return { + email: email, + type: 'to' + } + }) + } else { + toEmailArray = [{ + email: toEmail, + type: 'to' + }] + } + return toEmailArray + } diff --git a/packages/nodes-base/nodes/Mandrill/Mandrill.node.ts b/packages/nodes-base/nodes/Mandrill/Mandrill.node.ts new file mode 100644 index 0000000000..4140e03bdc --- /dev/null +++ b/packages/nodes-base/nodes/Mandrill/Mandrill.node.ts @@ -0,0 +1,322 @@ +import { + BINARY_ENCODING, + IExecuteSingleFunctions, +} from 'n8n-core'; +import { + IDataObject, + INodeTypeDescription, + INodeExecutionData, + INodeType, + ILoadOptionsFunctions, + INodePropertyOptions, +} from 'n8n-workflow'; +import { mandrillApiRequest, getToEmailArray } from './GenericFunctions'; + +export class Mandrill implements INodeType { + + // TODO + + /* + https://mandrillapp.com/api/docs/messages.JSON.html#method=send-template + + 1 - add url strip qs support + 2 - add subaccount support + 3 - add bcc address support + 4 - add google analytics campaign support + 5 - add tracking domain support + 6 - add signing domain support + 7 - add return path domainsupport + 8 - add ip pool support + 9 - add return path domain support + 10 - add tags support + 11 - add google analytics domains support + 12 - add attachments support + 13 - add metadata support + 14 - add ip pool support + 15 - add send at support + 16 - add recipient metadata + */ + + description: INodeTypeDescription = { + displayName: 'Mandrill', + name: 'mandrill', + icon: 'file:mandrill.png', + group: ['output'], + version: 1, + description: 'Sends an email via Mandrill', + defaults: { + name: 'Mandrill', + color: '#c02428', + }, + inputs: ['main'], + outputs: ['main'], + credentials: [ + { + name: 'mandrillApi', + required: true, + } + ], + properties: [ + { + displayName: 'Operation', + name: 'operation', + type: 'options', + options: [ + { + name: 'Send template', + value: 'sendTemplate', + description: 'Send template', + }, + { + name: 'Send html', + value: 'sendHtml', + description: 'Send Html', + }, + ], + default: 'sendTemplate', + description: 'The operation to perform.', + }, + { + displayName: 'Template', + name: 'template', + type: 'options', + typeOptions: { + loadOptionsMethod: 'getTemplates', + }, + displayOptions: { + show: { + operation: [ + 'sendTemplate', + ], + }, + }, + default: '', + options: [], + required: true, + description: 'The template you want to send', + }, + { + displayName: 'HTML', + name: 'html', + type: 'string', + displayOptions: { + show: { + operation: [ + 'sendHtml', + ], + }, + }, + default: '', + typeOptions: { + rows: 5, + }, + options: [], + required: true, + description: 'The html you want to send', + }, + { + displayName: 'From Email', + name: 'fromEmail', + type: 'string', + default: '', + required: true, + placeholder: 'Admin ', + description: 'Email address of the sender optional with name.', + }, + { + displayName: 'To Email', + name: 'toEmail', + type: 'string', + default: '', + required: true, + placeholder: 'info@example.com', + description: 'Email address of the recipient. Multiple ones can be separated by comma.', + }, + { + displayName: 'Subject', + name: 'subject', + type: 'string', + default: '', + placeholder: 'My subject line', + description: 'Subject line of the email.', + }, + { + displayName: 'Merge vars', + name: 'mergeVars', + type: 'json', + typeOptions: { + alwaysOpenEditWindow: true, + rows: 5, + }, + default: '', + placeholder: `mergeVars: [{ + rcpt: 'example@example.com', + vars: [ + { name: 'name', content: 'content' } + ] + }]`, + description: 'Per-recipient merge variables', + }, + { + displayName: 'Important', + name: 'important', + type: 'boolean', + default: false, + description: 'whether or not this message is important, and should be delivered ahead of non-important messages', + }, + { + displayName: 'Track opens', + name: 'trackOpens', + type: 'boolean', + default: false, + description: 'whether or not to turn on open tracking for the message', + }, + { + displayName: 'Track clicks', + name: 'trackClicks', + type: 'boolean', + default: false, + description: 'whether or not to turn on click tracking for the message', + }, + { + displayName: 'Auto text', + name: 'autoText', + type: 'boolean', + default: false, + description: 'whether or not to automatically generate a text part for messages that are not given text', + }, + { + displayName: 'Auto HTML', + name: 'autoHtml', + type: 'boolean', + default: false, + description: 'whether or not to automatically generate an HTML part for messages that are not given HTML', + }, + { + displayName: 'Inline css', + name: 'inlineCss', + type: 'boolean', + default: false, + description: 'whether or not to automatically inline all CSS styles provided in the message HTML - only for HTML documents less than 256KB in size', + }, + { + displayName: 'Url strip qs', + name: 'urlStripQs', + type: 'boolean', + default: false, + description: 'whether or not to strip the query string from URLs when aggregating tracked URL data', + }, + { + displayName: 'Preserve recipients', + name: 'preserveRecipients', + type: 'boolean', + default: false, + description: 'whether or not to expose all recipients in to "To" header for each email', + }, + { + displayName: 'View content link', + name: 'viewContentLink', + type: 'boolean', + default: false, + description: 'set to false to remove content logging for sensitive emails', + }, + { + displayName: 'Async', + name: 'async', + type: 'boolean', + default: false, + description: `enable a background sending mode that is optimized for bulk sending. In async mode, messages/send will immediately return a status of "queued" for every recipient. To handle rejections when sending in async mode, set up a webhook for the 'reject' event. Defaults to false for messages with no more than 10 recipients; messages with more than 10 recipients are always sent asynchronously, regardless of the value of async.`, + } + ], + }; + + methods = { + loadOptions: { + // Get all the available templates to display them to user so that he can + // select them easily + async getTemplates(this: ILoadOptionsFunctions): Promise { + const returnData: INodePropertyOptions[] = []; + let templates; + try { + templates = await mandrillApiRequest.call(this, '/templates', 'POST', '/list'); + } catch (err) { + throw new Error(`Mandrill Error: ${err}`); + } + for (const template of templates) { + const templateName = template.name; + const templateSlug = template.slug + + returnData.push({ + name: templateName, + value: templateSlug, + }); + } + + return returnData; + } + }, + }; + + + async executeSingle(this: IExecuteSingleFunctions): Promise { + + const operation = this.getNodeParameter('operation') as string; + const fromEmail = this.getNodeParameter('fromEmail') as string; + const toEmail = this.getNodeParameter('toEmail') as string; + const subject = this.getNodeParameter('subject') as string; + const important = this.getNodeParameter('important') as boolean; + const trackOpens = this.getNodeParameter('trackOpens') as boolean; + const trackClicks = this.getNodeParameter('trackClicks') as boolean; + const autoText = this.getNodeParameter('autoText') as boolean; + const autoHtml = this.getNodeParameter('autoHtml') as boolean; + const inlineCss = this.getNodeParameter('inlineCss') as boolean; + const urlStripQs = this.getNodeParameter('urlStripQs') as boolean; + const preserveRecipients = this.getNodeParameter('preserveRecipients') as boolean; + const viewContentLink = this.getNodeParameter('viewContentLink') as boolean; + const async = this.getNodeParameter('async') as boolean; + const toEmailArray = getToEmailArray(toEmail) + + const credentials = this.getCredentials('mandrillApi'); + + if (credentials === undefined) { + throw new Error('No credentials got returned!'); + } + + const body: IDataObject = { + message: { + subject: subject, + from_email: fromEmail, + to: toEmailArray, + important: important, + track_opens: trackOpens, + track_clicks: trackClicks, + auto_text: autoText, + auto_html: autoHtml, + inline_css: inlineCss, + url_strip_qs: urlStripQs, + preserve_recipients: preserveRecipients, + view_content_link: viewContentLink, + async: async, + } + }; + + if (operation === 'sendTemplate') { + const template = this.getNodeParameter('template') as string; + body.template_name = template + } else if (operation === 'sendHtml') { + const html = this.getNodeParameter('html') as string; + body.html = html + } + + let message; + try { + message = await mandrillApiRequest.call(this, '/messages', 'POST', '/send', body); + } catch (err) { + throw new Error(`Mandrill Error: ${err}`); + } + + return { + json: message, + }; + } +} diff --git a/packages/nodes-base/nodes/Mandrill/mandrill.png b/packages/nodes-base/nodes/Mandrill/mandrill.png new file mode 100644 index 0000000000000000000000000000000000000000..5ebb66e097ddf7ede2f5865d6a879515bf493c5b GIT binary patch literal 1291 zcmV+m1@!ufP)002t}0{{R350Hx30007fP)t-s5{<(X zjl>g<#1oIi7?Z~sl*k&D${Uu-9E`goA0Q*2(Ig-tB_SatAtEIrA|;W&C85$LAs{9p zBPXKND5TXXrq(PaB`hc@ER?-1Dk?B6Eio-FGA}PPFEKPRGc>Q=HH@$|vfnnd;5aNJ zIJDt8H8wmiBRn}dJ-X#RyXHQ-=0CjWKrtjiK0Z-RPEk%!TUS|KT3TORUt~xsW@BV$ zWo2ngD{Eyr>LqOFRL6d zt8_K2bTzDXHLQzhtgEc895AgNFs^kqu5~uAbvCbeHLs6quy;1Fm3FcwGO{W*vUfJK zc{jAOv$nOixE(XP9W%QfG`t=&y&g2bfH=T2Jix!d!9ha7fjPq7>BF?7#Dh7=ysya~ zHp?D2%bsk^9yZL&&CMP+&BM6P!M4uf>Cwr+(b3Y=A2`z=In*CH)gL+6*VoumW7v>9 z+1c9KRcP9qm)hFf+mSrmnUver(A{c#-PqIJ+}_@&rQWQj-rnEfjhf=(TVz ztl9=F-;Y<>u+=>FMd|>FMg~;nwQn+Un!n>g3+)gMF?=jH3* z)9jo-?bW*N*S_u8zwO$^?wvsH(XsB+wC>fn@6W97(y;KKKk&|`^1p@iyLAM@y?A7F0i$i@NrUEi1fMceb>W zVnOR%)@v4PfgaCFE+AIYkQ~$#7HjnOxRzEck7YsCy%F=(k79+_>G3VCj9AbzV$Ni- z7V3#ujS7kd{cP)F7HguOkfqhmD^`_V)_CQESm6x`T3T7Lpt&`^Ww93NiCW2p#DZSN z_Kd}vq$g}?b@Pc;X&Vs{UH&1~z%&<@R!%HvMnshHp2b?MyRo#2VnH_|dMOky-RaJ1 zUsp?>bx-B}&`+8);Q(mUI;p6_#OQ41S3-9}}Sd+R_oi>fp2g+=|o?!lsw zNd{H43$C`Tr{u+=Hip#-5f2*F8}eqItOo!9^~WOOoJY*e{ON{Ai{_OEfKuHN5pl&= zX3oA(UM)%ifVTLrJ35Ejiq#1RyXMFTK3Jt*t& z&`spp+S0+Scf|eCh|{SfW*5~zjIxxaEM@&i>o0R~keVjJ^*8_k002ovPDHLkV1lbH Bp^N|k literal 0 HcmV?d00001 diff --git a/packages/nodes-base/package.json b/packages/nodes-base/package.json index 3ee69a1b9a..872ab2ea4a 100644 --- a/packages/nodes-base/package.json +++ b/packages/nodes-base/package.json @@ -52,7 +52,8 @@ "dist/credentials/TelegramApi.credentials.js", "dist/credentials/TrelloApi.credentials.js", "dist/credentials/TwilioApi.credentials.js", - "dist/credentials/TypeformApi.credentials.js" + "dist/credentials/TypeformApi.credentials.js", + "dist/credentials/MandrillApi.credentials.js" ], "nodes": [ "dist/nodes/ActiveCampaign/ActiveCampaign.node.js", @@ -108,7 +109,8 @@ "dist/nodes/Typeform/TypeformTrigger.node.js", "dist/nodes/WriteBinaryFile.node.js", "dist/nodes/Webhook.node.js", - "dist/nodes/Xml.node.js" + "dist/nodes/Xml.node.js", + "dist/nodes/Mandrill/Mandrill.node.js" ] }, "devDependencies": { From d029c0de17b5e58435874b44f0c09fc96126db1e Mon Sep 17 00:00:00 2001 From: Ricardo Espinoza Date: Mon, 28 Oct 2019 17:23:25 -0400 Subject: [PATCH 2/5] done --- .../nodes/Mandrill/GenericFunctions.ts | 33 +- .../nodes/Mandrill/Mandrill.node.ts | 690 +++++++++++++++--- 2 files changed, 607 insertions(+), 116 deletions(-) diff --git a/packages/nodes-base/nodes/Mandrill/GenericFunctions.ts b/packages/nodes-base/nodes/Mandrill/GenericFunctions.ts index f1c526c8a4..a55027d1da 100644 --- a/packages/nodes-base/nodes/Mandrill/GenericFunctions.ts +++ b/packages/nodes-base/nodes/Mandrill/GenericFunctions.ts @@ -26,7 +26,8 @@ export async function mandrillApiRequest(this: IHookFunctions | IExecuteFunction uri: `https://${endpoint}${resource}${action}.json`, body: data, json: true - }; + }; + try { return await this.helpers.request!(options); @@ -67,3 +68,33 @@ export async function mandrillApiRequest(this: IHookFunctions | IExecuteFunction } return toEmailArray } + + export function getGoogleAnalyticsDomainsArray(string: String): Array { + let array = [] + if (string.split(',').length > 0) { + array = string.split(',') + } else { + array = [string] + } + return array + } + + export function getTags(string: String): Array { + let array = [] + if (string.split(',').length > 0) { + array = string.split(',') + } else { + array = [string] + } + return array + } + + export function validateJSON(json: any): any { + let result + try { + result = JSON.parse(json) + } catch (exception) { + result = [] + } + return result + } diff --git a/packages/nodes-base/nodes/Mandrill/Mandrill.node.ts b/packages/nodes-base/nodes/Mandrill/Mandrill.node.ts index 4140e03bdc..55eb000015 100644 --- a/packages/nodes-base/nodes/Mandrill/Mandrill.node.ts +++ b/packages/nodes-base/nodes/Mandrill/Mandrill.node.ts @@ -1,5 +1,4 @@ import { - BINARY_ENCODING, IExecuteSingleFunctions, } from 'n8n-core'; import { @@ -10,32 +9,21 @@ import { ILoadOptionsFunctions, INodePropertyOptions, } from 'n8n-workflow'; -import { mandrillApiRequest, getToEmailArray } from './GenericFunctions'; +import { + mandrillApiRequest, + getToEmailArray, + getGoogleAnalyticsDomainsArray, + getTags, + validateJSON +} from './GenericFunctions'; + +import * as moment from 'moment'; export class Mandrill implements INodeType { - - // TODO - /* - https://mandrillapp.com/api/docs/messages.JSON.html#method=send-template - - 1 - add url strip qs support - 2 - add subaccount support - 3 - add bcc address support - 4 - add google analytics campaign support - 5 - add tracking domain support - 6 - add signing domain support - 7 - add return path domainsupport - 8 - add ip pool support - 9 - add return path domain support - 10 - add tags support - 11 - add google analytics domains support - 12 - add attachments support - 13 - add metadata support - 14 - add ip pool support - 15 - add send at support - 16 - add recipient metadata - */ + + //https://mandrillapp.com/api/docs/messages.JSON.html#method=send-template + description: INodeTypeDescription = { displayName: 'Mandrill', @@ -43,7 +31,7 @@ export class Mandrill implements INodeType { icon: 'file:mandrill.png', group: ['output'], version: 1, - description: 'Sends an email via Mandrill', + description: 'Consume mandrill API', defaults: { name: 'Mandrill', color: '#c02428', @@ -57,10 +45,31 @@ export class Mandrill implements INodeType { } ], properties: [ + { + displayName: 'Resource', + name: 'resource', + type: 'options', + options: [ + { + name: 'Messages', + value: 'messages', + description: 'API path', + }, + ], + default: '', + description: 'Resource to consume', + }, { displayName: 'Operation', name: 'operation', type: 'options', + displayOptions: { + show: { + resource: [ + 'messages', + ], + }, + }, options: [ { name: 'Send template', @@ -94,25 +103,6 @@ export class Mandrill implements INodeType { options: [], required: true, description: 'The template you want to send', - }, - { - displayName: 'HTML', - name: 'html', - type: 'string', - displayOptions: { - show: { - operation: [ - 'sendHtml', - ], - }, - }, - default: '', - typeOptions: { - rows: 5, - }, - options: [], - required: true, - description: 'The html you want to send', }, { displayName: 'From Email', @@ -122,6 +112,13 @@ export class Mandrill implements INodeType { required: true, placeholder: 'Admin ', description: 'Email address of the sender optional with name.', + displayOptions: { + show: { + operation: [ + 'sendHtml', 'sendTemplate' + ], + }, + }, }, { displayName: 'To Email', @@ -131,6 +128,13 @@ export class Mandrill implements INodeType { required: true, placeholder: 'info@example.com', description: 'Email address of the recipient. Multiple ones can be separated by comma.', + displayOptions: { + show: { + operation: [ + 'sendHtml', 'sendTemplate' + ], + }, + }, }, { displayName: 'Subject', @@ -139,7 +143,88 @@ export class Mandrill implements INodeType { default: '', placeholder: 'My subject line', description: 'Subject line of the email.', + displayOptions: { + show: { + operation: [ + 'sendHtml', 'sendTemplate' + ], + }, + }, }, + { + displayName: 'From name', + name: 'fromName', + type: 'string', + default: '', + placeholder: 'John Doe', + description: 'optional from name to be used.', + displayOptions: { + show: { + operation: [ + 'sendHtml', 'sendTemplate' + ], + }, + }, + }, + { + displayName: 'HTML', + name: 'html', + type: 'string', + displayOptions: { + show: { + operation: [ + 'sendHtml', 'sendTemplate', + ], + }, + }, + default: '', + typeOptions: { + rows: 5, + }, + options: [], + description: 'The html you want to send', + }, + { + displayName: 'Text', + name: 'text', + type: 'string', + default: '', + typeOptions: { + rows: 5, + }, + displayOptions: { + show: { + operation: [ + 'sendHtml', 'sendTemplate' + ], + }, + }, + options: [], + description: 'Example text content', + }, + { + displayName: 'Headers', + name: 'headers', + type: 'json', + default: '', + placeholder: ` + { + "Reply-To": "replies@example.com" + }, + `, + typeOptions: { + alwaysOpenEditWindow: true, + rows: 5, + }, + description: 'optional extra headers to add to the message (most headers are allowed)', + displayOptions: { + show: { + operation: [ + 'sendHtml', 'sendTemplate' + ], + }, + }, + }, { displayName: 'Merge vars', name: 'mergeVars', @@ -149,84 +234,410 @@ export class Mandrill implements INodeType { rows: 5, }, default: '', - placeholder: `mergeVars: [{ - rcpt: 'example@example.com', - vars: [ - { name: 'name', content: 'content' } - ] - }]`, + placeholder: ` + [{ + "rcpt": "example@example.com", + "vars": [ + { "name": "name", "content": "content" } + ] + }]`, description: 'Per-recipient merge variables', + displayOptions: { + show: { + operation: [ + 'sendHtml', 'sendTemplate' + ], + }, + }, + }, + { + displayName: 'Metadata', + name: 'metadata', + type: 'json', + typeOptions: { + alwaysOpenEditWindow: true, + rows: 5, + }, + default: '', + placeholder: ` + { + "website": "www.example.com" + }`, + description: 'metadata an associative array of user metadata. Mandrill will store this metadata and make it available for retrieval. In addition, you can select up to 10 metadata fields to index and make searchable using the Mandrill search api.', + displayOptions: { + show: { + operation: [ + 'sendHtml', 'sendTemplate' + ], + }, + }, + }, + { + displayName: 'Recipient metadata', + name: 'recipientMetadata', + type: 'json', + typeOptions: { + alwaysOpenEditWindow: true, + rows: 5, + }, + default: '', + placeholder: ` [ + { + "rcpt": "recipient.email@example.com", + "values": { + "user_id": 123456 + } + } + ]`, + description: 'Per-recipient metadata that will override the global values specified in the metadata parameter.', + displayOptions: { + show: { + operation: [ + 'sendHtml', 'sendTemplate' + ], + }, + }, + }, + { + displayName: 'Attachments', + name: 'attachments', + type: 'json', + typeOptions: { + alwaysOpenEditWindow: true, + rows: 5, + }, + default: '', + placeholder: ` [ + { + "type": "text/plain" (the MIME type of the attachment), + "name": "myfile.txt" (the file name of the attachment), + "content": "ZXhhbXBsZSBmaWxl" (the content of the attachment as a base64-encoded string) + } + ],`, + description: 'an array of supported attachments to add to the message', + displayOptions: { + show: { + operation: [ + 'sendHtml', 'sendTemplate' + ], + }, + }, + }, + { + displayName: 'Images', + name: 'images', + type: 'json', + typeOptions: { + alwaysOpenEditWindow: true, + rows: 5, + }, + default: '', + placeholder: ` [ + { + "type": "image/png" (the MIME type of the image - must start with "image/"), + "name": "IMAGECID" (the Content ID of the image - use to reference the image in your HTML content), + "content": "ZXhhbXBsZSBmaWxl" (the content of the image as a base64-encoded string) + } + ]`, + description: 'an array of embedded images to add to the message', + displayOptions: { + show: { + operation: [ + 'sendHtml', 'sendTemplate' + ], + }, + }, + }, + { + displayName: 'Bcc address', + name: 'bccAddress', + type: 'string', + default: '', + placeholder: 'message.bcc_address@example.com', + description: `an optional address to receive an exact copy of each recipient's email`, + displayOptions: { + show: { + operation: [ + 'sendHtml', 'sendTemplate' + ], + }, + }, + }, + { + displayName: 'Tracking domain', + name: 'trackingDomain', + type: 'string', + default: '', + placeholder: '', + description: `a custom domain to use for tracking opens and clicks instead of mandrillapp.com`, + displayOptions: { + show: { + operation: [ + 'sendHtml', 'sendTemplate' + ], + }, + }, + }, + { + displayName: 'Signing domain', + name: 'signingDomain', + type: 'string', + default: '', + placeholder: '', + description: `a custom domain to use for SPF/DKIM signing instead of mandrill (for "via" or "on behalf of" in email clients)`, + displayOptions: { + show: { + operation: [ + 'sendHtml', 'sendTemplate' + ], + }, + }, + }, + { + displayName: 'Return path domain', + name: 'returnPathDomain', + type: 'string', + default: '', + placeholder: '', + description: `a custom domain to use for the messages's return-path`, + displayOptions: { + show: { + operation: [ + 'sendHtml', 'sendTemplate' + ], + }, + }, }, { displayName: 'Important', name: 'important', type: 'boolean', default: false, - description: 'whether or not this message is important, and should be delivered ahead of non-important messages', + description: 'whether or not this message is important, and should be delivered ahead of non-important messages', + displayOptions: { + show: { + operation: [ + 'sendHtml', 'sendTemplate' + ], + }, + }, }, { displayName: 'Track opens', name: 'trackOpens', type: 'boolean', default: false, - description: 'whether or not to turn on open tracking for the message', + description: 'whether or not to turn on open tracking for the message', + displayOptions: { + show: { + operation: [ + 'sendHtml', 'sendTemplate' + ], + }, + }, }, { displayName: 'Track clicks', name: 'trackClicks', type: 'boolean', default: false, - description: 'whether or not to turn on click tracking for the message', + description: 'whether or not to turn on click tracking for the message', + displayOptions: { + show: { + operation: [ + 'sendHtml', 'sendTemplate' + ], + }, + }, }, { displayName: 'Auto text', name: 'autoText', type: 'boolean', default: false, - description: 'whether or not to automatically generate a text part for messages that are not given text', + description: 'whether or not to automatically generate a text part for messages that are not given text', + displayOptions: { + show: { + operation: [ + 'sendHtml', 'sendTemplate' + ], + }, + }, }, { displayName: 'Auto HTML', name: 'autoHtml', type: 'boolean', default: false, - description: 'whether or not to automatically generate an HTML part for messages that are not given HTML', + description: 'whether or not to automatically generate an HTML part for messages that are not given HTML', + displayOptions: { + show: { + operation: [ + 'sendHtml', 'sendTemplate' + ], + }, + }, }, { displayName: 'Inline css', name: 'inlineCss', type: 'boolean', default: false, - description: 'whether or not to automatically inline all CSS styles provided in the message HTML - only for HTML documents less than 256KB in size', + description: 'whether or not to automatically inline all CSS styles provided in the message HTML - only for HTML documents less than 256KB in size', + displayOptions: { + show: { + operation: [ + 'sendHtml', 'sendTemplate' + ], + }, + }, }, { displayName: 'Url strip qs', name: 'urlStripQs', type: 'boolean', default: false, - description: 'whether or not to strip the query string from URLs when aggregating tracked URL data', + description: 'whether or not to strip the query string from URLs when aggregating tracked URL data', + displayOptions: { + show: { + operation: [ + 'sendHtml', 'sendTemplate' + ], + }, + }, }, { displayName: 'Preserve recipients', name: 'preserveRecipients', type: 'boolean', default: false, - description: 'whether or not to expose all recipients in to "To" header for each email', + description: 'whether or not to expose all recipients in to "To" header for each email', + displayOptions: { + show: { + operation: [ + 'sendHtml', 'sendTemplate' + ], + }, + }, }, { displayName: 'View content link', name: 'viewContentLink', type: 'boolean', default: false, - description: 'set to false to remove content logging for sensitive emails', + description: 'set to false to remove content logging for sensitive emails', + displayOptions: { + show: { + operation: [ + 'sendHtml', 'sendTemplate' + ], + }, + }, }, { displayName: 'Async', name: 'async', type: 'boolean', default: false, - description: `enable a background sending mode that is optimized for bulk sending. In async mode, messages/send will immediately return a status of "queued" for every recipient. To handle rejections when sending in async mode, set up a webhook for the 'reject' event. Defaults to false for messages with no more than 10 recipients; messages with more than 10 recipients are always sent asynchronously, regardless of the value of async.`, - } + description: `enable a background sending mode that is optimized for bulk sending. In async mode, messages/send will immediately return a status of "queued" for every recipient. To handle rejections when sending in async mode, set up a webhook for the 'reject' event. Defaults to false for messages with no more than 10 recipients; messages with more than 10 recipients are always sent asynchronously, regardless of the value of async.`, + displayOptions: { + show: { + operation: [ + 'sendHtml', 'sendTemplate' + ], + }, + }, + }, + { + displayName: 'Subaccount', + name: 'subAccount', + type: 'string', + default: '', + placeholder: '', + description: 'the unique id of a subaccount for this message - must already exist or will fail with an error', + displayOptions: { + show: { + operation: [ + 'sendHtml', 'sendTemplate' + ], + }, + }, + }, + { + displayName: 'Google analytics campaign', + name: 'googleAnalyticsCampaign', + type: 'string', + default: '', + placeholder: '', + description: `optional string indicating the value to set for the utm_campaign tracking parameter. If this isn't provided the email's from address will be used instead.`, + displayOptions: { + show: { + operation: [ + 'sendHtml', 'sendTemplate' + ], + }, + }, + }, + { + displayName: 'Google analytics domains', + name: 'googleAnalyticsDomains', + type: 'string', + default: '', + placeholder: '', + description: `an array of strings separated by , indicating for which any matching URLs will automatically have Google Analytics parameters appended to their query string automatically.`, + displayOptions: { + show: { + operation: [ + 'sendHtml', 'sendTemplate' + ], + }, + }, + }, + { + displayName: 'Tags', + name: 'tags', + type: 'string', + default: '', + placeholder: '', + description: `an array of string separated by , to tag the message with. Stats are accumulated using tags, though we only store the first 100 we see, so this should not be unique or change frequently. Tags should be 50 characters or less. Any tags starting with an underscore are reserved for internal use and will cause errors.`, + displayOptions: { + show: { + operation: [ + 'sendHtml', 'sendTemplate' + ], + }, + }, + }, + { + displayName: 'Ip pool', + name: 'ipPool', + type: 'string', + default: '', + placeholder: '', + description: `the name of the dedicated ip pool that should be used to send the message. If you do not have any dedicated IPs, this parameter has no effect. If you specify a pool that does not exist, your default pool will be used instead.`, + displayOptions: { + show: { + operation: [ + 'sendHtml', 'sendTemplate' + ], + }, + }, + }, + { + displayName: 'Sent at', + name: 'sendAt', + type: 'dateTime', + default: '', + placeholder: '', + description: `When this message should be sent as a UTC timestamp in YYYY-MM-DD HH:MM:SS format. If you specify a time in the past, the message will be sent immediately. An additional fee applies for scheduled email, and this feature is only available to accounts with a positive balance.`, + displayOptions: { + show: { + operation: [ + 'sendHtml', 'sendTemplate' + ], + }, + }, + }, ], }; @@ -250,7 +661,7 @@ export class Mandrill implements INodeType { name: templateName, value: templateSlug, }); - } + } return returnData; } @@ -260,63 +671,112 @@ export class Mandrill implements INodeType { async executeSingle(this: IExecuteSingleFunctions): Promise { - const operation = this.getNodeParameter('operation') as string; - const fromEmail = this.getNodeParameter('fromEmail') as string; - const toEmail = this.getNodeParameter('toEmail') as string; - const subject = this.getNodeParameter('subject') as string; - const important = this.getNodeParameter('important') as boolean; - const trackOpens = this.getNodeParameter('trackOpens') as boolean; - const trackClicks = this.getNodeParameter('trackClicks') as boolean; - const autoText = this.getNodeParameter('autoText') as boolean; - const autoHtml = this.getNodeParameter('autoHtml') as boolean; - const inlineCss = this.getNodeParameter('inlineCss') as boolean; - const urlStripQs = this.getNodeParameter('urlStripQs') as boolean; - const preserveRecipients = this.getNodeParameter('preserveRecipients') as boolean; - const viewContentLink = this.getNodeParameter('viewContentLink') as boolean; - const async = this.getNodeParameter('async') as boolean; - const toEmailArray = getToEmailArray(toEmail) + let response; + const resource = this.getNodeParameter('resource') as string; + + if (resource === 'messages') { - const credentials = this.getCredentials('mandrillApi'); + const operation = this.getNodeParameter('operation') as string; + const fromEmail = this.getNodeParameter('fromEmail') as string; + const toEmail = this.getNodeParameter('toEmail') as string; + const subject = this.getNodeParameter('subject') as string; + const fromName = this.getNodeParameter('fromName') as string; + const important = this.getNodeParameter('important') as boolean; + const trackOpens = this.getNodeParameter('trackOpens') as boolean; + const trackClicks = this.getNodeParameter('trackClicks') as boolean; + const autoText = this.getNodeParameter('autoText') as boolean; + const autoHtml = this.getNodeParameter('autoHtml') as boolean; + const inlineCss = this.getNodeParameter('inlineCss') as boolean; + const urlStripQs = this.getNodeParameter('urlStripQs') as boolean; + const preserveRecipients = this.getNodeParameter('preserveRecipients') as boolean; + const viewContentLink = this.getNodeParameter('viewContentLink') as boolean; + const async = this.getNodeParameter('async') as boolean; + const subAccount = this.getNodeParameter('subAccount') as string; + const googleAnalyticsCampaign = this.getNodeParameter('googleAnalyticsCampaign') as string; + const googleAnalyticsDomains = this.getNodeParameter('googleAnalyticsDomains') as string; + const tags = this.getNodeParameter('tags') as string; + const ipPool = this.getNodeParameter('ipPool') as string; + const bccAddress = this.getNodeParameter('bccAddress') as string; + const trackingDomain = this.getNodeParameter('trackingDomain') as string; + const signingDomain = this.getNodeParameter('signingDomain') as string; + const returnPathDomain = this.getNodeParameter('returnPathDomain') as string; + const html = this.getNodeParameter('html') as string; + const text = this.getNodeParameter('text') as string; + const sendAt = moment(this.getNodeParameter('sendAt') as string).utc().format('YYYY-MM-DD HH:mm:ss'); + const headers = validateJSON(this.getNodeParameter('headers') as string); + const recipientMetadata = validateJSON(this.getNodeParameter('recipientMetadata') as string); + const attachments = validateJSON(this.getNodeParameter('attachments') as string); + const images = validateJSON(this.getNodeParameter('images') as string); + const mergeVars = validateJSON(this.getNodeParameter('mergeVars') as string); + const metadata = validateJSON(this.getNodeParameter('metadata') as string); - if (credentials === undefined) { - throw new Error('No credentials got returned!'); - } - - const body: IDataObject = { - message: { - subject: subject, - from_email: fromEmail, - to: toEmailArray, - important: important, - track_opens: trackOpens, - track_clicks: trackClicks, - auto_text: autoText, - auto_html: autoHtml, - inline_css: inlineCss, - url_strip_qs: urlStripQs, - preserve_recipients: preserveRecipients, - view_content_link: viewContentLink, - async: async, - } - }; - - if (operation === 'sendTemplate') { - const template = this.getNodeParameter('template') as string; - body.template_name = template - } else if (operation === 'sendHtml') { - const html = this.getNodeParameter('html') as string; - body.html = html - } - - let message; - try { - message = await mandrillApiRequest.call(this, '/messages', 'POST', '/send', body); - } catch (err) { - throw new Error(`Mandrill Error: ${err}`); - } + const toEmailArray = getToEmailArray(toEmail) + const googleAnalyticsDomainsArray = getGoogleAnalyticsDomainsArray(googleAnalyticsDomains) + const tagsArray = getTags(tags) + + const credentials = this.getCredentials('mandrillApi'); + + if (credentials === undefined) { + throw new Error('No credentials got returned!'); + } + + const body: IDataObject = { + template_content: [], + message: { + html: html, + text: text, + subject: subject, + from_name: fromName, + from_email: fromEmail, + to: toEmailArray, + important: important, + track_opens: trackOpens, + track_clicks: trackClicks, + auto_text: autoText, + auto_html: autoHtml, + inline_css: inlineCss, + url_strip_qs: urlStripQs, + preserve_recipients: preserveRecipients, + view_content_link: viewContentLink, + async: async, + subaccount: subAccount || null, + google_analytics_campaign: googleAnalyticsCampaign, + google_analytics_domains: googleAnalyticsDomainsArray, + tags: tagsArray, + ip_pool: ipPool, + bcc_address: bccAddress, + tracking_domain: trackingDomain, + signing_domain: signingDomain, + return_path_domain: returnPathDomain, + recipient_metadata: recipientMetadata, + headers: headers, + metadata: metadata, + merge_vars: mergeVars, + attachments: attachments, + images: images + }, + send_at: (sendAt === 'Invalid date' ) ? moment().utc().format('YYYY-MM-DD HH:mm:ss'): sendAt, + }; + + let message + + if (operation === 'sendTemplate') { + const template = this.getNodeParameter('template') as string; + body.template_name = template + message = mandrillApiRequest.call(this, '/messages', 'POST', '/send-template', body); + } else if (operation === 'sendHtml') { + message = mandrillApiRequest.call(this, '/messages', 'POST', '/send', body); + } + + try { + response = await message + } catch (err) { + throw new Error(`Mandrill Error: ${err}`); + } + } return { - json: message, + json: response, }; } } From 7a624fd6a934fc94af4bc14560966554dafc3936 Mon Sep 17 00:00:00 2001 From: Ricardo Espinoza Date: Sun, 3 Nov 2019 08:39:03 -0500 Subject: [PATCH 3/5] changes on UI --- .../nodes/Mandrill/Mandrill.node.ts | 955 ++++++++++-------- 1 file changed, 532 insertions(+), 423 deletions(-) diff --git a/packages/nodes-base/nodes/Mandrill/Mandrill.node.ts b/packages/nodes-base/nodes/Mandrill/Mandrill.node.ts index 55eb000015..46974b3b82 100644 --- a/packages/nodes-base/nodes/Mandrill/Mandrill.node.ts +++ b/packages/nodes-base/nodes/Mandrill/Mandrill.node.ts @@ -31,6 +31,7 @@ export class Mandrill implements INodeType { icon: 'file:mandrill.png', group: ['output'], version: 1, + subtitle: '={{$parameter["operation"] + ": " + $parameter["resource"]}}', description: 'Consume mandrill API', defaults: { name: 'Mandrill', @@ -56,7 +57,7 @@ export class Mandrill implements INodeType { description: 'API path', }, ], - default: '', + default: 'messages', description: 'Resource to consume', }, { @@ -137,27 +138,11 @@ export class Mandrill implements INodeType { }, }, { - displayName: 'Subject', - name: 'subject', - type: 'string', - default: '', - placeholder: 'My subject line', - description: 'Subject line of the email.', - displayOptions: { - show: { - operation: [ - 'sendHtml', 'sendTemplate' - ], - }, - }, - }, - { - displayName: 'From name', - name: 'fromName', - type: 'string', - default: '', - placeholder: 'John Doe', - description: 'optional from name to be used.', + displayName: 'Json Parameters', + name: 'jsonParameters', + type: 'boolean', + default: false, + description: '', displayOptions: { show: { operation: [ @@ -167,73 +152,218 @@ export class Mandrill implements INodeType { }, }, { - displayName: 'HTML', - name: 'html', - type: 'string', - displayOptions: { + displayName: 'Options', + name: 'options', + type: 'collection', + placeholder: 'add Option', + default: {}, + displayOptions: { show: { - operation: [ - 'sendHtml', 'sendTemplate', - ], + 'operation': [ + 'sendHtml', 'sendTemplate' + ] + } + }, + options: [ + { + displayName: 'Subject', + name: 'subject', + type: 'string', + default: '', + placeholder: 'My subject line', + description: 'Subject line of the email.', }, - }, - default: '', - typeOptions: { - rows: 5, - }, - options: [], - description: 'The html you want to send', + { + displayName: 'From name', + name: 'fromName', + type: 'string', + default: '', + placeholder: 'John Doe', + description: 'optional from name to be used.', + }, + { + displayName: 'HTML', + name: 'html', + type: 'string', + default: '', + typeOptions: { + rows: 5, + }, + options: [], + description: 'The html you want to send', + }, + { + displayName: 'Text', + name: 'text', + type: 'string', + default: '', + typeOptions: { + rows: 5, + }, + options: [], + description: 'Example text content', + }, + { + displayName: 'Bcc address', + name: 'bccAddress', + type: 'string', + default: '', + placeholder: 'message.bcc_address@example.com', + description: `an optional address to receive an exact copy of each recipient's email`, + }, + { + displayName: 'Tracking domain', + name: 'trackingDomain', + type: 'string', + default: '', + placeholder: '', + description: `a custom domain to use for tracking opens and clicks instead of mandrillapp.com`, + }, + { + displayName: 'Signing domain', + name: 'signingDomain', + type: 'string', + default: '', + placeholder: '', + description: `a custom domain to use for SPF/DKIM signing instead of mandrill (for "via" or "on behalf of" in email clients)`, + }, + { + displayName: 'Return path domain', + name: 'returnPathDomain', + type: 'string', + default: '', + placeholder: '', + description: `a custom domain to use for the messages's return-path`, + }, + { + displayName: 'Important', + name: 'important', + type: 'boolean', + default: false, + description: 'whether or not this message is important, and should be delivered ahead of non-important messages', + }, + { + displayName: 'Track opens', + name: 'trackOpens', + type: 'boolean', + default: false, + description: 'whether or not to turn on open tracking for the message', + }, + { + displayName: 'Track clicks', + name: 'trackClicks', + type: 'boolean', + default: false, + description: 'whether or not to turn on click tracking for the message', + }, + { + displayName: 'Auto text', + name: 'autoText', + type: 'boolean', + default: false, + description: 'whether or not to automatically generate a text part for messages that are not given text', + }, + { + displayName: 'Auto HTML', + name: 'autoHtml', + type: 'boolean', + default: false, + description: 'whether or not to automatically generate an HTML part for messages that are not given HTML', + }, + { + displayName: 'Inline css', + name: 'inlineCss', + type: 'boolean', + default: false, + description: 'whether or not to automatically inline all CSS styles provided in the message HTML - only for HTML documents less than 256KB in size', + }, + { + displayName: 'Url strip qs', + name: 'urlStripQs', + type: 'boolean', + default: false, + description: 'whether or not to strip the query string from URLs when aggregating tracked URL data', + }, + { + displayName: 'Preserve recipients', + name: 'preserveRecipients', + type: 'boolean', + default: false, + description: 'whether or not to expose all recipients in to "To" header for each email', + }, + { + displayName: 'View content link', + name: 'viewContentLink', + type: 'boolean', + default: false, + description: 'set to false to remove content logging for sensitive emails', + }, + { + displayName: 'Async', + name: 'async', + type: 'boolean', + default: false, + description: `enable a background sending mode that is optimized for bulk sending. In async mode, messages/send will immediately return a status of "queued" for every recipient. To handle rejections when sending in async mode, set up a webhook for the 'reject' event. Defaults to false for messages with no more than 10 recipients; messages with more than 10 recipients are always sent asynchronously, regardless of the value of async.`, + }, + { + displayName: 'Subaccount', + name: 'subAccount', + type: 'string', + default: '', + placeholder: '', + description: 'the unique id of a subaccount for this message - must already exist or will fail with an error', + }, + { + displayName: 'Google analytics campaign', + name: 'googleAnalyticsCampaign', + type: 'string', + default: '', + placeholder: '', + description: `optional string indicating the value to set for the utm_campaign tracking parameter. If this isn't provided the email's from address will be used instead.`, + }, + { + displayName: 'Google analytics domains', + name: 'googleAnalyticsDomains', + type: 'string', + default: '', + placeholder: '', + description: `an array of strings separated by , indicating for which any matching URLs will automatically have Google Analytics parameters appended to their query string automatically.`, + }, + { + displayName: 'Tags', + name: 'tags', + type: 'string', + default: '', + placeholder: '', + description: `an array of string separated by , to tag the message with. Stats are accumulated using tags, though we only store the first 100 we see, so this should not be unique or change frequently. Tags should be 50 characters or less. Any tags starting with an underscore are reserved for internal use and will cause errors.`, + }, + { + displayName: 'Ip pool', + name: 'ipPool', + type: 'string', + default: '', + placeholder: '', + description: `the name of the dedicated ip pool that should be used to send the message. If you do not have any dedicated IPs, this parameter has no effect. If you specify a pool that does not exist, your default pool will be used instead.`, + }, + { + displayName: 'Sent at', + name: 'sendAt', + type: 'dateTime', + default: '', + placeholder: '', + description: `When this message should be sent as a UTC timestamp in YYYY-MM-DD HH:MM:SS format. If you specify a time in the past, the message will be sent immediately. An additional fee applies for scheduled email, and this feature is only available to accounts with a positive balance.`, + }, + ] }, - { - displayName: 'Text', - name: 'text', - type: 'string', - default: '', - typeOptions: { - rows: 5, - }, - displayOptions: { - show: { - operation: [ - 'sendHtml', 'sendTemplate' - ], - }, - }, - options: [], - description: 'Example text content', - }, - { - displayName: 'Headers', - name: 'headers', - type: 'json', - default: '', - placeholder: ` - { - "Reply-To": "replies@example.com" - }, - `, - typeOptions: { - alwaysOpenEditWindow: true, - rows: 5, - }, - description: 'optional extra headers to add to the message (most headers are allowed)', - displayOptions: { - show: { - operation: [ - 'sendHtml', 'sendTemplate' - ], - }, - }, - }, { displayName: 'Merge vars', - name: 'mergeVars', - type: 'json', - typeOptions: { - alwaysOpenEditWindow: true, - rows: 5, - }, - default: '', + name: 'mergeVarsJson', + type: 'json', + typeOptions: { + alwaysOpenEditWindow: true, + rows: 5, + }, + default: '', placeholder: ` [{ "rcpt": "example@example.com", @@ -241,47 +371,132 @@ export class Mandrill implements INodeType { { "name": "name", "content": "content" } ] }]`, - description: 'Per-recipient merge variables', displayOptions: { show: { - operation: [ - 'sendHtml', 'sendTemplate' - ], - }, + jsonParameters: [ + true + ] + } }, + description: 'Per-recipient merge variables', + }, + { + displayName: 'Merge vars', + name: 'mergeVarsUi', + type: 'fixedCollection', + default: '', + typeOptions: { + multipleValues: true + }, + displayOptions: { + show: { + jsonParameters: [ + false + ] + } + }, + description: 'Per-recipient merge variables', + options: [ + { + name: 'parameter', + displayName: 'Vars', + values: [ + { + displayName: 'Name', + name: 'name', + type: 'string', + default: '', + description: '' + }, + { + displayName: 'Value', + name: 'value', + type: 'string', + default: '', + description: 'aasasas' + } + ] + } + ] + }, + { + displayName: 'Metadata', + name: 'metadataUi', + type: 'fixedCollection', + default: '', + typeOptions: { + multipleValues: true + }, + displayOptions: { + show: { + jsonParameters: [ + false + ] + } + }, + description: 'metadata an associative array of user metadata. Mandrill will store this metadata and make it available for retrieval. In addition, you can select up to 10 metadata fields to index and make searchable using the Mandrill search api.', + options: [ + { + name: 'parameter', + displayName: 'Metadata', + values: [ + { + displayName: 'Name', + name: 'name', + type: 'string', + default: '', + description: '' + }, + { + displayName: 'Value', + name: 'value', + type: 'string', + default: '', + description: 'aasasas' + } + ] + } + ] }, { displayName: 'Metadata', name: 'metadata', - type: 'json', - typeOptions: { - alwaysOpenEditWindow: true, - rows: 5, - }, - default: '', + type: 'json', + typeOptions: { + alwaysOpenEditWindow: true, + rows: 5, + }, + displayOptions: { + show: { + jsonParameters: [ + true + ] + } + }, + default: '', placeholder: ` { "website": "www.example.com" }`, description: 'metadata an associative array of user metadata. Mandrill will store this metadata and make it available for retrieval. In addition, you can select up to 10 metadata fields to index and make searchable using the Mandrill search api.', - displayOptions: { - show: { - operation: [ - 'sendHtml', 'sendTemplate' - ], - }, - }, }, { displayName: 'Recipient metadata', name: 'recipientMetadata', - type: 'json', - typeOptions: { - alwaysOpenEditWindow: true, - rows: 5, - }, - default: '', - placeholder: ` [ + type: 'json', + typeOptions: { + alwaysOpenEditWindow: true, + rows: 5, + }, + displayOptions: { + show: { + jsonParameters: [ + true + ] + } + }, + default: '', + placeholder: ` [ { "rcpt": "recipient.email@example.com", "values": { @@ -290,24 +505,63 @@ export class Mandrill implements INodeType { } ]`, description: 'Per-recipient metadata that will override the global values specified in the metadata parameter.', + }, + { + displayName: 'Recipient metadata', + name: 'recipientMetadataUi', + type: 'fixedCollection', + typeOptions: { + multipleValues: true + }, + options: [ + { + name: 'parameter', + displayName: 'Values', + values: [ + { + displayName: 'Name', + name: 'name', + type: 'string', + default: '', + description: '' + }, + { + displayName: 'Value', + name: 'value', + type: 'string', + default: '', + description: 'aasasas' + } + ] + } + ], displayOptions: { show: { - operation: [ - 'sendHtml', 'sendTemplate' - ], - }, + jsonParameters: [ + false + ] + } }, + default: '', + description: 'Per-recipient metadata that will override the global values specified in the metadata parameter.', }, { displayName: 'Attachments', name: 'attachments', - type: 'json', - typeOptions: { - alwaysOpenEditWindow: true, - rows: 5, - }, - default: '', - placeholder: ` [ + type: 'json', + typeOptions: { + alwaysOpenEditWindow: true, + rows: 5, + }, + displayOptions: { + show: { + jsonParameters: [ + true + ] + } + }, + default: '', + placeholder: ` [ { "type": "text/plain" (the MIME type of the attachment), "name": "myfile.txt" (the file name of the attachment), @@ -315,24 +569,129 @@ export class Mandrill implements INodeType { } ],`, description: 'an array of supported attachments to add to the message', + }, + { + displayName: 'Attachments', + name: 'attachmentsUi', + type: 'fixedCollection', + typeOptions: { + multipleValues: true + }, displayOptions: { show: { - operation: [ - 'sendHtml', 'sendTemplate' - ], - }, + jsonParameters: [ + false + ] + } }, + options: [ + { + name: 'parameter', + displayName: 'Values', + values: [ + { + displayName: 'Type', + name: 'type', + type: 'string', + default: '', + description: 'text/plain" (the MIME type of the attachment)' + }, + { + displayName: 'Name', + name: 'name', + type: 'string', + default: '', + description: 'myfile.txt" (the file name of the attachment)' + }, + { + displayName: 'Content', + name: 'content', + type: 'string', + default: '', + description: 'ZXhhbXBsZSBmaWxl" (the content of the attachment as a base64-encoded string)' + } + ] + } + ], + default: '', + description: 'an array of supported attachments to add to the message', + }, + { + displayName: 'Headers', + name: 'headers', + type: 'json', + default: '', + placeholder: ` + { + "Reply-To": "replies@example.com" + }, + `, + displayOptions: { + show: { + jsonParameters: [ + true + ] + } + }, + typeOptions: { + alwaysOpenEditWindow: true, + rows: 5, + }, + description: 'optional extra headers to add to the message (most headers are allowed)', + }, + { + displayName: 'Headers', + name: 'headersUi', + type: 'fixedCollection', + default: '', + displayOptions: { + show: { + jsonParameters: [ + false + ] + } + }, + options: [ + { + name: 'parameter', + displayName: 'Values', + values: [ + { + displayName: 'Name', + name: 'name', + type: 'string', + default: '', + description: '' + }, + { + displayName: 'Value', + name: 'value', + type: 'string', + default: '', + description: '' + } + ] + } + ], + description: 'optional extra headers to add to the message (most headers are allowed)', }, { displayName: 'Images', name: 'images', - type: 'json', - typeOptions: { - alwaysOpenEditWindow: true, - rows: 5, - }, - default: '', - placeholder: ` [ + type: 'json', + typeOptions: { + alwaysOpenEditWindow: true, + rows: 5, + }, + displayOptions: { + show: { + jsonParameters: [ + true + ] + } + }, + default: '', + placeholder: ` [ { "type": "image/png" (the MIME type of the image - must start with "image/"), "name": "IMAGECID" (the Content ID of the image - use to reference the image in your HTML content), @@ -340,304 +699,54 @@ export class Mandrill implements INodeType { } ]`, description: 'an array of embedded images to add to the message', - displayOptions: { - show: { - operation: [ - 'sendHtml', 'sendTemplate' - ], - }, - }, }, { - displayName: 'Bcc address', - name: 'bccAddress', - type: 'string', - default: '', - placeholder: 'message.bcc_address@example.com', - description: `an optional address to receive an exact copy of each recipient's email`, + displayName: 'Images', + name: 'images', + type: 'fixedCollection', displayOptions: { show: { - operation: [ - 'sendHtml', 'sendTemplate' - ], - }, - }, - }, - { - displayName: 'Tracking domain', - name: 'trackingDomain', - type: 'string', - default: '', - placeholder: '', - description: `a custom domain to use for tracking opens and clicks instead of mandrillapp.com`, - displayOptions: { - show: { - operation: [ - 'sendHtml', 'sendTemplate' - ], - }, - }, - }, - { - displayName: 'Signing domain', - name: 'signingDomain', - type: 'string', - default: '', - placeholder: '', - description: `a custom domain to use for SPF/DKIM signing instead of mandrill (for "via" or "on behalf of" in email clients)`, - displayOptions: { - show: { - operation: [ - 'sendHtml', 'sendTemplate' - ], - }, - }, - }, - { - displayName: 'Return path domain', - name: 'returnPathDomain', - type: 'string', - default: '', - placeholder: '', - description: `a custom domain to use for the messages's return-path`, - displayOptions: { - show: { - operation: [ - 'sendHtml', 'sendTemplate' - ], - }, - }, - }, - { - displayName: 'Important', - name: 'important', - type: 'boolean', - default: false, - description: 'whether or not this message is important, and should be delivered ahead of non-important messages', - displayOptions: { - show: { - operation: [ - 'sendHtml', 'sendTemplate' - ], - }, - }, - }, - { - displayName: 'Track opens', - name: 'trackOpens', - type: 'boolean', - default: false, - description: 'whether or not to turn on open tracking for the message', - displayOptions: { - show: { - operation: [ - 'sendHtml', 'sendTemplate' - ], - }, - }, - }, - { - displayName: 'Track clicks', - name: 'trackClicks', - type: 'boolean', - default: false, - description: 'whether or not to turn on click tracking for the message', - displayOptions: { - show: { - operation: [ - 'sendHtml', 'sendTemplate' - ], - }, - }, - }, - { - displayName: 'Auto text', - name: 'autoText', - type: 'boolean', - default: false, - description: 'whether or not to automatically generate a text part for messages that are not given text', - displayOptions: { - show: { - operation: [ - 'sendHtml', 'sendTemplate' - ], - }, - }, - }, - { - displayName: 'Auto HTML', - name: 'autoHtml', - type: 'boolean', - default: false, - description: 'whether or not to automatically generate an HTML part for messages that are not given HTML', - displayOptions: { - show: { - operation: [ - 'sendHtml', 'sendTemplate' - ], - }, - }, - }, - { - displayName: 'Inline css', - name: 'inlineCss', - type: 'boolean', - default: false, - description: 'whether or not to automatically inline all CSS styles provided in the message HTML - only for HTML documents less than 256KB in size', - displayOptions: { - show: { - operation: [ - 'sendHtml', 'sendTemplate' - ], - }, - }, - }, - { - displayName: 'Url strip qs', - name: 'urlStripQs', - type: 'boolean', - default: false, - description: 'whether or not to strip the query string from URLs when aggregating tracked URL data', - displayOptions: { - show: { - operation: [ - 'sendHtml', 'sendTemplate' - ], - }, - }, - }, - { - displayName: 'Preserve recipients', - name: 'preserveRecipients', - type: 'boolean', - default: false, - description: 'whether or not to expose all recipients in to "To" header for each email', - displayOptions: { - show: { - operation: [ - 'sendHtml', 'sendTemplate' - ], - }, - }, - }, - { - displayName: 'View content link', - name: 'viewContentLink', - type: 'boolean', - default: false, - description: 'set to false to remove content logging for sensitive emails', - displayOptions: { - show: { - operation: [ - 'sendHtml', 'sendTemplate' - ], - }, - }, - }, - { - displayName: 'Async', - name: 'async', - type: 'boolean', - default: false, - description: `enable a background sending mode that is optimized for bulk sending. In async mode, messages/send will immediately return a status of "queued" for every recipient. To handle rejections when sending in async mode, set up a webhook for the 'reject' event. Defaults to false for messages with no more than 10 recipients; messages with more than 10 recipients are always sent asynchronously, regardless of the value of async.`, - displayOptions: { - show: { - operation: [ - 'sendHtml', 'sendTemplate' - ], - }, - }, - }, - { - displayName: 'Subaccount', - name: 'subAccount', - type: 'string', - default: '', - placeholder: '', - description: 'the unique id of a subaccount for this message - must already exist or will fail with an error', - displayOptions: { - show: { - operation: [ - 'sendHtml', 'sendTemplate' - ], - }, - }, - }, - { - displayName: 'Google analytics campaign', - name: 'googleAnalyticsCampaign', - type: 'string', - default: '', - placeholder: '', - description: `optional string indicating the value to set for the utm_campaign tracking parameter. If this isn't provided the email's from address will be used instead.`, - displayOptions: { - show: { - operation: [ - 'sendHtml', 'sendTemplate' - ], - }, - }, - }, - { - displayName: 'Google analytics domains', - name: 'googleAnalyticsDomains', - type: 'string', - default: '', - placeholder: '', - description: `an array of strings separated by , indicating for which any matching URLs will automatically have Google Analytics parameters appended to their query string automatically.`, - displayOptions: { - show: { - operation: [ - 'sendHtml', 'sendTemplate' - ], - }, - }, - }, - { - displayName: 'Tags', - name: 'tags', - type: 'string', - default: '', - placeholder: '', - description: `an array of string separated by , to tag the message with. Stats are accumulated using tags, though we only store the first 100 we see, so this should not be unique or change frequently. Tags should be 50 characters or less. Any tags starting with an underscore are reserved for internal use and will cause errors.`, - displayOptions: { - show: { - operation: [ - 'sendHtml', 'sendTemplate' - ], - }, - }, - }, - { - displayName: 'Ip pool', - name: 'ipPool', - type: 'string', - default: '', - placeholder: '', - description: `the name of the dedicated ip pool that should be used to send the message. If you do not have any dedicated IPs, this parameter has no effect. If you specify a pool that does not exist, your default pool will be used instead.`, - displayOptions: { - show: { - operation: [ - 'sendHtml', 'sendTemplate' - ], - }, - }, - }, - { - displayName: 'Sent at', - name: 'sendAt', - type: 'dateTime', - default: '', - placeholder: '', - description: `When this message should be sent as a UTC timestamp in YYYY-MM-DD HH:MM:SS format. If you specify a time in the past, the message will be sent immediately. An additional fee applies for scheduled email, and this feature is only available to accounts with a positive balance.`, - displayOptions: { - show: { - operation: [ - 'sendHtml', 'sendTemplate' - ], - }, + jsonParameters: [ + false + ] + } }, + typeOptions: { + multipleValues: true + }, + default: '', + options: [ + { + name: 'parameter', + displayName: 'Values', + values: [ + { + displayName: 'Type', + name: 'type', + type: 'string', + default: '', + description: 'text/plain" (the MIME type of the attachment)' + }, + { + displayName: 'Name', + name: 'name', + type: 'string', + default: '', + description: 'myfile.txt" (the file name of the attachment)' + }, + { + displayName: 'Content', + name: 'content', + type: 'string', + default: '', + description: 'ZXhhbXBsZSBmaWxl" (the content of the attachment as a base64-encoded string)' + } + ] + } + ], + description: 'an array of embedded images to add to the message', }, + ], }; From 4c2f5c93921d81f347b50f18a485dadcf412a6c6 Mon Sep 17 00:00:00 2001 From: Ricardo Espinoza Date: Sun, 3 Nov 2019 15:50:37 -0500 Subject: [PATCH 4/5] Final UI --- .../nodes/Mandrill/Mandrill.node.ts | 121 +++++++++++++----- 1 file changed, 86 insertions(+), 35 deletions(-) diff --git a/packages/nodes-base/nodes/Mandrill/Mandrill.node.ts b/packages/nodes-base/nodes/Mandrill/Mandrill.node.ts index 46974b3b82..b50fd5f214 100644 --- a/packages/nodes-base/nodes/Mandrill/Mandrill.node.ts +++ b/packages/nodes-base/nodes/Mandrill/Mandrill.node.ts @@ -138,7 +138,24 @@ export class Mandrill implements INodeType { }, }, { - displayName: 'Json Parameters', + displayName: 'Binary Data', + name: 'binaryData', + type: 'boolean', + default: false, + description: '', + displayOptions: { + show: { + jsonParameters: [ + false + ], + operation: [ + 'sendHtml', 'sendTemplate' + ] + }, + }, + }, + { + displayName: 'JSON Parameters', name: 'jsonParameters', type: 'boolean', default: false, @@ -147,6 +164,40 @@ export class Mandrill implements INodeType { show: { operation: [ 'sendHtml', 'sendTemplate' + ] + }, + }, + }, + { + displayName: 'Attachments', + name: 'attachmentsBinary', + type: 'string', + default: '', + description: 'Name of the binary properties which contain data which should be added to email as attachment. Multiple ones can be comma separated.', + displayOptions: { + show: { + binaryData: [ + true + ], + jsonParameters: [ + false + ], + }, + }, + }, + { + displayName: 'Images', + name: 'imagesBinary', + type: 'string', + default: '', + description: 'Name of the binary properties which contain data which should be added to email as attachment. Multiple ones can be comma separated.', + displayOptions: { + show: { + binaryData: [ + true + ], + jsonParameters: [ + false ], }, }, @@ -155,7 +206,7 @@ export class Mandrill implements INodeType { displayName: 'Options', name: 'options', type: 'collection', - placeholder: 'add Option', + placeholder: 'Add Option', default: {}, displayOptions: { show: { @@ -204,7 +255,7 @@ export class Mandrill implements INodeType { description: 'Example text content', }, { - displayName: 'Bcc address', + displayName: 'BCC Address', name: 'bccAddress', type: 'string', default: '', @@ -212,7 +263,7 @@ export class Mandrill implements INodeType { description: `an optional address to receive an exact copy of each recipient's email`, }, { - displayName: 'Tracking domain', + displayName: 'Tracking Domain', name: 'trackingDomain', type: 'string', default: '', @@ -220,7 +271,7 @@ export class Mandrill implements INodeType { description: `a custom domain to use for tracking opens and clicks instead of mandrillapp.com`, }, { - displayName: 'Signing domain', + displayName: 'Signing Domain', name: 'signingDomain', type: 'string', default: '', @@ -228,7 +279,7 @@ export class Mandrill implements INodeType { description: `a custom domain to use for SPF/DKIM signing instead of mandrill (for "via" or "on behalf of" in email clients)`, }, { - displayName: 'Return path domain', + displayName: 'Return Path Domain', name: 'returnPathDomain', type: 'string', default: '', @@ -243,21 +294,21 @@ export class Mandrill implements INodeType { description: 'whether or not this message is important, and should be delivered ahead of non-important messages', }, { - displayName: 'Track opens', + displayName: 'Track Opens', name: 'trackOpens', type: 'boolean', default: false, description: 'whether or not to turn on open tracking for the message', }, { - displayName: 'Track clicks', + displayName: 'Track Clicks', name: 'trackClicks', type: 'boolean', default: false, description: 'whether or not to turn on click tracking for the message', }, { - displayName: 'Auto text', + displayName: 'Auto Text', name: 'autoText', type: 'boolean', default: false, @@ -271,28 +322,28 @@ export class Mandrill implements INodeType { description: 'whether or not to automatically generate an HTML part for messages that are not given HTML', }, { - displayName: 'Inline css', + displayName: 'Inline CSS', name: 'inlineCss', type: 'boolean', default: false, description: 'whether or not to automatically inline all CSS styles provided in the message HTML - only for HTML documents less than 256KB in size', }, { - displayName: 'Url strip qs', + displayName: 'Url Strip Qs', name: 'urlStripQs', type: 'boolean', default: false, description: 'whether or not to strip the query string from URLs when aggregating tracked URL data', }, { - displayName: 'Preserve recipients', + displayName: 'Preserve Recipients', name: 'preserveRecipients', type: 'boolean', default: false, description: 'whether or not to expose all recipients in to "To" header for each email', }, { - displayName: 'View content link', + displayName: 'View Content Link', name: 'viewContentLink', type: 'boolean', default: false, @@ -314,7 +365,7 @@ export class Mandrill implements INodeType { description: 'the unique id of a subaccount for this message - must already exist or will fail with an error', }, { - displayName: 'Google analytics campaign', + displayName: 'Google Analytics Campaign', name: 'googleAnalyticsCampaign', type: 'string', default: '', @@ -322,7 +373,7 @@ export class Mandrill implements INodeType { description: `optional string indicating the value to set for the utm_campaign tracking parameter. If this isn't provided the email's from address will be used instead.`, }, { - displayName: 'Google analytics domains', + displayName: 'Google Analytics Domains', name: 'googleAnalyticsDomains', type: 'string', default: '', @@ -338,7 +389,7 @@ export class Mandrill implements INodeType { description: `an array of string separated by , to tag the message with. Stats are accumulated using tags, though we only store the first 100 we see, so this should not be unique or change frequently. Tags should be 50 characters or less. Any tags starting with an underscore are reserved for internal use and will cause errors.`, }, { - displayName: 'Ip pool', + displayName: 'Ip Pool', name: 'ipPool', type: 'string', default: '', @@ -346,7 +397,7 @@ export class Mandrill implements INodeType { description: `the name of the dedicated ip pool that should be used to send the message. If you do not have any dedicated IPs, this parameter has no effect. If you specify a pool that does not exist, your default pool will be used instead.`, }, { - displayName: 'Sent at', + displayName: 'Sent At', name: 'sendAt', type: 'dateTime', default: '', @@ -356,12 +407,11 @@ export class Mandrill implements INodeType { ] }, { - displayName: 'Merge vars', + displayName: 'Merge Vars', name: 'mergeVarsJson', type: 'json', typeOptions: { alwaysOpenEditWindow: true, - rows: 5, }, default: '', placeholder: ` @@ -381,8 +431,9 @@ export class Mandrill implements INodeType { description: 'Per-recipient merge variables', }, { - displayName: 'Merge vars', + displayName: 'Merge Vars', name: 'mergeVarsUi', + placeholder: 'Add Merge Vars', type: 'fixedCollection', default: '', typeOptions: { @@ -422,6 +473,7 @@ export class Mandrill implements INodeType { { displayName: 'Metadata', name: 'metadataUi', + placeholder: 'Add Metadata', type: 'fixedCollection', default: '', typeOptions: { @@ -460,11 +512,10 @@ export class Mandrill implements INodeType { }, { displayName: 'Metadata', - name: 'metadata', + name: 'metadataJson', type: 'json', typeOptions: { alwaysOpenEditWindow: true, - rows: 5, }, displayOptions: { show: { @@ -478,15 +529,14 @@ export class Mandrill implements INodeType { { "website": "www.example.com" }`, - description: 'metadata an associative array of user metadata. Mandrill will store this metadata and make it available for retrieval. In addition, you can select up to 10 metadata fields to index and make searchable using the Mandrill search api.', + description: 'Metadata an associative array of user metadata. Mandrill will store this metadata and make it available for retrieval. In addition, you can select up to 10 metadata fields to index and make searchable using the Mandrill search api.', }, { - displayName: 'Recipient metadata', + displayName: 'Recipient Metadata', name: 'recipientMetadata', type: 'json', typeOptions: { alwaysOpenEditWindow: true, - rows: 5, }, displayOptions: { show: { @@ -507,8 +557,9 @@ export class Mandrill implements INodeType { description: 'Per-recipient metadata that will override the global values specified in the metadata parameter.', }, { - displayName: 'Recipient metadata', + displayName: 'Recipient Metadata', name: 'recipientMetadataUi', + placeholder: 'Add Recipient Metatada', type: 'fixedCollection', typeOptions: { multipleValues: true @@ -551,7 +602,6 @@ export class Mandrill implements INodeType { type: 'json', typeOptions: { alwaysOpenEditWindow: true, - rows: 5, }, displayOptions: { show: { @@ -573,6 +623,7 @@ export class Mandrill implements INodeType { { displayName: 'Attachments', name: 'attachmentsUi', + placeholder: 'Add Attachments', type: 'fixedCollection', typeOptions: { multipleValues: true @@ -594,21 +645,21 @@ export class Mandrill implements INodeType { name: 'type', type: 'string', default: '', - description: 'text/plain" (the MIME type of the attachment)' + description: 'text/plain (the MIME type of the attachment)' }, { displayName: 'Name', name: 'name', type: 'string', default: '', - description: 'myfile.txt" (the file name of the attachment)' + description: 'myfile.txt (the file name of the attachment)' }, { displayName: 'Content', name: 'content', type: 'string', default: '', - description: 'ZXhhbXBsZSBmaWxl" (the content of the attachment as a base64-encoded string)' + description: 'ZXhhbXBsZSBmaWxl (the content of the attachment as a base64-encoded string)' } ] } @@ -635,13 +686,13 @@ export class Mandrill implements INodeType { }, typeOptions: { alwaysOpenEditWindow: true, - rows: 5, }, description: 'optional extra headers to add to the message (most headers are allowed)', }, { displayName: 'Headers', name: 'headersUi', + placeholder: 'Add Headers', type: 'fixedCollection', default: '', displayOptions: { @@ -681,7 +732,6 @@ export class Mandrill implements INodeType { type: 'json', typeOptions: { alwaysOpenEditWindow: true, - rows: 5, }, displayOptions: { show: { @@ -703,6 +753,7 @@ export class Mandrill implements INodeType { { displayName: 'Images', name: 'images', + placeholder: 'Add images', type: 'fixedCollection', displayOptions: { show: { @@ -725,21 +776,21 @@ export class Mandrill implements INodeType { name: 'type', type: 'string', default: '', - description: 'text/plain" (the MIME type of the attachment)' + description: 'text/plain (the MIME type of the attachment)' }, { displayName: 'Name', name: 'name', type: 'string', default: '', - description: 'myfile.txt" (the file name of the attachment)' + description: 'myfile.txt (the file name of the attachment)' }, { displayName: 'Content', name: 'content', type: 'string', default: '', - description: 'ZXhhbXBsZSBmaWxl" (the content of the attachment as a base64-encoded string)' + description: 'ZXhhbXBsZSBmaWxl (the content of the attachment as a base64-encoded string)' } ] } From 7206bbace0765238a6a4c1ee872d7bb6717f3bee Mon Sep 17 00:00:00 2001 From: Ricardo Espinoza Date: Mon, 4 Nov 2019 12:29:20 -0500 Subject: [PATCH 5/5] added changes --- .../nodes/Mandrill/GenericFunctions.ts | 57 +- .../nodes/Mandrill/Mandrill.node.ts | 556 ++++++++---------- 2 files changed, 283 insertions(+), 330 deletions(-) diff --git a/packages/nodes-base/nodes/Mandrill/GenericFunctions.ts b/packages/nodes-base/nodes/Mandrill/GenericFunctions.ts index a55027d1da..34b6b9e3a8 100644 --- a/packages/nodes-base/nodes/Mandrill/GenericFunctions.ts +++ b/packages/nodes-base/nodes/Mandrill/GenericFunctions.ts @@ -8,6 +8,7 @@ import { } from 'n8n-core'; import * as _ from 'lodash'; +import { IDataObject } from 'n8n-workflow'; export async function mandrillApiRequest(this: IHookFunctions | IExecuteFunctions | IExecuteSingleFunctions | ILoadOptionsFunctions, resource: string, method: string, action: string, body: any = {}, headers?: object): Promise { // tslint:disable-line:no-any const credentials = this.getCredentials('mandrillApi'); @@ -16,12 +17,12 @@ export async function mandrillApiRequest(this: IHookFunctions | IExecuteFunction throw new Error('No credentials got returned!'); } - const data = Object.assign({ }, body, { key: credentials.apiKey }) + const data = Object.assign({ }, body, { key: credentials.apiKey }); const endpoint = 'mandrillapp.com/api/1.0'; const options: OptionsWithUri = { - headers: headers, + headers, method, uri: `https://${endpoint}${resource}${action}.json`, body: data, @@ -49,52 +50,52 @@ export async function mandrillApiRequest(this: IHookFunctions | IExecuteFunction throw error.response.body; } } - - export function getToEmailArray(toEmail: String): Array { // tslint:disable-line:no-any - let toEmailArray + // @ts-ignore + export function getToEmailArray(toEmail: string): any { + let toEmailArray; if (toEmail.split(',').length > 0) { - const array = toEmail.split(',') + const array = toEmail.split(','); toEmailArray = _.map(array, (email) => { return { - email: email, + email, type: 'to' - } - }) + }; + }); } else { toEmailArray = [{ email: toEmail, type: 'to' - }] + }]; } - return toEmailArray + return toEmailArray; } - export function getGoogleAnalyticsDomainsArray(string: String): Array { - let array = [] - if (string.split(',').length > 0) { - array = string.split(',') + export function getGoogleAnalyticsDomainsArray(s: string): string[] { + let array: string[] = []; + if (s.split(',').length > 0) { + array = s.split(','); } else { - array = [string] + array = [s]; } - return array + return array; } - export function getTags(string: String): Array { - let array = [] - if (string.split(',').length > 0) { - array = string.split(',') + export function getTags(s: string): Array { + let array = []; + if (s.split(',').length > 0) { + array = s.split(','); } else { - array = [string] + array = [s]; } - return array + return array; } - export function validateJSON(json: any): any { - let result + export function validateJSON(json: string | undefined): any { + let result; try { - result = JSON.parse(json) + result = JSON.parse(json!); } catch (exception) { - result = [] + result = []; } - return result + return result; } diff --git a/packages/nodes-base/nodes/Mandrill/Mandrill.node.ts b/packages/nodes-base/nodes/Mandrill/Mandrill.node.ts index b50fd5f214..68b39cdba4 100644 --- a/packages/nodes-base/nodes/Mandrill/Mandrill.node.ts +++ b/packages/nodes-base/nodes/Mandrill/Mandrill.node.ts @@ -18,13 +18,84 @@ import { } from './GenericFunctions'; import * as moment from 'moment'; +import * as _ from 'lodash'; + +interface Attachments { + type: string; + name: string; + content: string; +} + +interface Message { + html?: string; + text?: string; + subject?: string; + from_name?: string; + from_email: string; + to: string[]; + important?: boolean; + track_opens?: boolean; + track_clicks?: boolean; + auto_text?: boolean; + auto_html?: boolean; + inline_css?: boolean; + url_strip_qs?: boolean; + preserve_recipients?: boolean; + view_content_link?: boolean; + async?: boolean; + subaccount?: string; + google_analytics_campaign?: string; + google_analytics_domains?: string[]; + tags?: string[]; + ip_pool?: string; + bcc_address?: string; + tracking_domain?: string; + signing_domain?: string; + return_path_domain?: string; + headers?: IDataObject; + metadata?: IDataObject; + global_merge_vars?: [IDataObject]; + attachments?: Attachments[]; +} + +interface Body { + template_content: []; + template_name?: string; + message: Message; + send_at?: string; +} + +interface Options { + html: string; + fromName?: string; + text: string; + subject?: string; + important: boolean; + trackOpens: boolean; + trackClicks: boolean; + autoText: boolean; + autoHtml: boolean; + inlineCss: boolean; + urlStripQs: boolean; + preserveRecipients: boolean; + viewContentLink: boolean; + async: boolean; + subaccount: string; + googleAnalyticsCampaign: string; + googleAnalyticsDomains?: string; + tags: string; + ipPool: string; + bccAddress: string; + trackingDomain: string; + signingDomain: string; + returnPathDomain: string; + sendAt: string; +} export class Mandrill implements INodeType { - //https://mandrillapp.com/api/docs/messages.JSON.html#method=send-template - description: INodeTypeDescription = { displayName: 'Mandrill', name: 'mandrill', @@ -137,23 +208,6 @@ export class Mandrill implements INodeType { }, }, }, - { - displayName: 'Binary Data', - name: 'binaryData', - type: 'boolean', - default: false, - description: '', - displayOptions: { - show: { - jsonParameters: [ - false - ], - operation: [ - 'sendHtml', 'sendTemplate' - ] - }, - }, - }, { displayName: 'JSON Parameters', name: 'jsonParameters', @@ -168,40 +222,6 @@ export class Mandrill implements INodeType { }, }, }, - { - displayName: 'Attachments', - name: 'attachmentsBinary', - type: 'string', - default: '', - description: 'Name of the binary properties which contain data which should be added to email as attachment. Multiple ones can be comma separated.', - displayOptions: { - show: { - binaryData: [ - true - ], - jsonParameters: [ - false - ], - }, - }, - }, - { - displayName: 'Images', - name: 'imagesBinary', - type: 'string', - default: '', - description: 'Name of the binary properties which contain data which should be added to email as attachment. Multiple ones can be comma separated.', - displayOptions: { - show: { - binaryData: [ - true - ], - jsonParameters: [ - false - ], - }, - }, - }, { displayName: 'Options', name: 'options', @@ -230,7 +250,7 @@ export class Mandrill implements INodeType { type: 'string', default: '', placeholder: 'John Doe', - description: 'optional from name to be used.', + description: 'Optional from name to be used.', }, { displayName: 'HTML', @@ -260,7 +280,7 @@ export class Mandrill implements INodeType { type: 'string', default: '', placeholder: 'message.bcc_address@example.com', - description: `an optional address to receive an exact copy of each recipient's email`, + description: `An optional address to receive an exact copy of each recipient's email`, }, { displayName: 'Tracking Domain', @@ -268,7 +288,7 @@ export class Mandrill implements INodeType { type: 'string', default: '', placeholder: '', - description: `a custom domain to use for tracking opens and clicks instead of mandrillapp.com`, + description: `A custom domain to use for tracking opens and clicks instead of mandrillapp.com`, }, { displayName: 'Signing Domain', @@ -276,7 +296,7 @@ export class Mandrill implements INodeType { type: 'string', default: '', placeholder: '', - description: `a custom domain to use for SPF/DKIM signing instead of mandrill (for "via" or "on behalf of" in email clients)`, + description: `A custom domain to use for SPF/DKIM signing instead of mandrill (for "via" or "on behalf of" in email clients)`, }, { displayName: 'Return Path Domain', @@ -284,77 +304,77 @@ export class Mandrill implements INodeType { type: 'string', default: '', placeholder: '', - description: `a custom domain to use for the messages's return-path`, + description: `A custom domain to use for the messages's return-path`, }, { displayName: 'Important', name: 'important', type: 'boolean', default: false, - description: 'whether or not this message is important, and should be delivered ahead of non-important messages', + description: 'Whether or not this message is important, and should be delivered ahead of non-important messages', }, { displayName: 'Track Opens', name: 'trackOpens', type: 'boolean', default: false, - description: 'whether or not to turn on open tracking for the message', + description: 'Whether or not to turn on open tracking for the message', }, { displayName: 'Track Clicks', name: 'trackClicks', type: 'boolean', default: false, - description: 'whether or not to turn on click tracking for the message', + description: 'Whether or not to turn on click tracking for the message', }, { displayName: 'Auto Text', name: 'autoText', type: 'boolean', default: false, - description: 'whether or not to automatically generate a text part for messages that are not given text', + description: 'Whether or not to automatically generate a text part for messages that are not given text', }, { displayName: 'Auto HTML', name: 'autoHtml', type: 'boolean', default: false, - description: 'whether or not to automatically generate an HTML part for messages that are not given HTML', + description: 'Whether or not to automatically generate an HTML part for messages that are not given HTML', }, { displayName: 'Inline CSS', name: 'inlineCss', type: 'boolean', default: false, - description: 'whether or not to automatically inline all CSS styles provided in the message HTML - only for HTML documents less than 256KB in size', + description: 'Whether or not to automatically inline all CSS styles provided in the message HTML - only for HTML documents less than 256KB in size', }, { displayName: 'Url Strip Qs', name: 'urlStripQs', type: 'boolean', default: false, - description: 'whether or not to strip the query string from URLs when aggregating tracked URL data', + description: 'Whether or not to strip the query string from URLs when aggregating tracked URL data', }, { displayName: 'Preserve Recipients', name: 'preserveRecipients', type: 'boolean', default: false, - description: 'whether or not to expose all recipients in to "To" header for each email', + description: 'Whether or not to expose all recipients in to "To" header for each email', }, { displayName: 'View Content Link', name: 'viewContentLink', type: 'boolean', default: false, - description: 'set to false to remove content logging for sensitive emails', + description: 'Set to false to remove content logging for sensitive emails', }, { displayName: 'Async', name: 'async', type: 'boolean', default: false, - description: `enable a background sending mode that is optimized for bulk sending. In async mode, messages/send will immediately return a status of "queued" for every recipient. To handle rejections when sending in async mode, set up a webhook for the 'reject' event. Defaults to false for messages with no more than 10 recipients; messages with more than 10 recipients are always sent asynchronously, regardless of the value of async.`, + description: `Enable a background sending mode that is optimized for bulk sending. In async mode, messages/send will immediately return a status of "queued" for every recipient. To handle rejections when sending in async mode, set up a webhook for the 'reject' event. Defaults to false for messages with no more than 10 recipients; messages with more than 10 recipients are always sent asynchronously, regardless of the value of async.`, }, { displayName: 'Subaccount', @@ -362,7 +382,7 @@ export class Mandrill implements INodeType { type: 'string', default: '', placeholder: '', - description: 'the unique id of a subaccount for this message - must already exist or will fail with an error', + description: 'The unique id of a subaccount for this message - must already exist or will fail with an error', }, { displayName: 'Google Analytics Campaign', @@ -370,7 +390,7 @@ export class Mandrill implements INodeType { type: 'string', default: '', placeholder: '', - description: `optional string indicating the value to set for the utm_campaign tracking parameter. If this isn't provided the email's from address will be used instead.`, + description: `Optional string indicating the value to set for the utm_campaign tracking parameter. If this isn't provided the email's from address will be used instead.`, }, { displayName: 'Google Analytics Domains', @@ -378,7 +398,7 @@ export class Mandrill implements INodeType { type: 'string', default: '', placeholder: '', - description: `an array of strings separated by , indicating for which any matching URLs will automatically have Google Analytics parameters appended to their query string automatically.`, + description: `An array of strings separated by , indicating for which any matching URLs will automatically have Google Analytics parameters appended to their query string automatically.`, }, { displayName: 'Tags', @@ -386,7 +406,7 @@ export class Mandrill implements INodeType { type: 'string', default: '', placeholder: '', - description: `an array of string separated by , to tag the message with. Stats are accumulated using tags, though we only store the first 100 we see, so this should not be unique or change frequently. Tags should be 50 characters or less. Any tags starting with an underscore are reserved for internal use and will cause errors.`, + description: `An array of string separated by , to tag the message with. Stats are accumulated using tags, though we only store the first 100 we see, so this should not be unique or change frequently. Tags should be 50 characters or less. Any tags starting with an underscore are reserved for internal use and will cause errors.`, }, { displayName: 'Ip Pool', @@ -394,7 +414,7 @@ export class Mandrill implements INodeType { type: 'string', default: '', placeholder: '', - description: `the name of the dedicated ip pool that should be used to send the message. If you do not have any dedicated IPs, this parameter has no effect. If you specify a pool that does not exist, your default pool will be used instead.`, + description: `The name of the dedicated ip pool that should be used to send the message. If you do not have any dedicated IPs, this parameter has no effect. If you specify a pool that does not exist, your default pool will be used instead.`, }, { displayName: 'Sent At', @@ -416,10 +436,7 @@ export class Mandrill implements INodeType { default: '', placeholder: ` [{ - "rcpt": "example@example.com", - "vars": [ { "name": "name", "content": "content" } - ] }]`, displayOptions: { show: { @@ -428,7 +445,7 @@ export class Mandrill implements INodeType { ] } }, - description: 'Per-recipient merge variables', + description: 'Global merge variables', }, { displayName: 'Merge Vars', @@ -449,7 +466,7 @@ export class Mandrill implements INodeType { description: 'Per-recipient merge variables', options: [ { - name: 'parameter', + name: 'mergeVarsValues', displayName: 'Vars', values: [ { @@ -460,8 +477,8 @@ export class Mandrill implements INodeType { description: '' }, { - displayName: 'Value', - name: 'value', + displayName: 'Content', + name: 'content', type: 'string', default: '', description: 'aasasas' @@ -486,10 +503,10 @@ export class Mandrill implements INodeType { ] } }, - description: 'metadata an associative array of user metadata. Mandrill will store this metadata and make it available for retrieval. In addition, you can select up to 10 metadata fields to index and make searchable using the Mandrill search api.', + description: 'Metadata an associative array of user metadata. Mandrill will store this metadata and make it available for retrieval. In addition, you can select up to 10 metadata fields to index and make searchable using the Mandrill search api.', options: [ { - name: 'parameter', + name: 'metadataValues', displayName: 'Metadata', values: [ { @@ -531,71 +548,6 @@ export class Mandrill implements INodeType { }`, description: 'Metadata an associative array of user metadata. Mandrill will store this metadata and make it available for retrieval. In addition, you can select up to 10 metadata fields to index and make searchable using the Mandrill search api.', }, - { - displayName: 'Recipient Metadata', - name: 'recipientMetadata', - type: 'json', - typeOptions: { - alwaysOpenEditWindow: true, - }, - displayOptions: { - show: { - jsonParameters: [ - true - ] - } - }, - default: '', - placeholder: ` [ - { - "rcpt": "recipient.email@example.com", - "values": { - "user_id": 123456 - } - } - ]`, - description: 'Per-recipient metadata that will override the global values specified in the metadata parameter.', - }, - { - displayName: 'Recipient Metadata', - name: 'recipientMetadataUi', - placeholder: 'Add Recipient Metatada', - type: 'fixedCollection', - typeOptions: { - multipleValues: true - }, - options: [ - { - name: 'parameter', - displayName: 'Values', - values: [ - { - displayName: 'Name', - name: 'name', - type: 'string', - default: '', - description: '' - }, - { - displayName: 'Value', - name: 'value', - type: 'string', - default: '', - description: 'aasasas' - } - ] - } - ], - displayOptions: { - show: { - jsonParameters: [ - false - ] - } - }, - default: '', - description: 'Per-recipient metadata that will override the global values specified in the metadata parameter.', - }, { displayName: 'Attachments', name: 'attachments', @@ -618,7 +570,7 @@ export class Mandrill implements INodeType { "content": "ZXhhbXBsZSBmaWxl" (the content of the attachment as a base64-encoded string) } ],`, - description: 'an array of supported attachments to add to the message', + description: 'An array of supported attachments to add to the message', }, { displayName: 'Attachments', @@ -637,8 +589,8 @@ export class Mandrill implements INodeType { }, options: [ { - name: 'parameter', - displayName: 'Values', + name: 'attachmentsValues', + displayName: 'Attachments Values', values: [ { displayName: 'Type', @@ -662,6 +614,19 @@ export class Mandrill implements INodeType { description: 'ZXhhbXBsZSBmaWxl (the content of the attachment as a base64-encoded string)' } ] + }, + { + name: 'attachmentsBinary', + displayName: 'Attachments Binary', + values: [ + { + displayName: 'Property', + name: 'property', + type: 'string', + default: '', + description: 'Name of the binary properties which contain data which should be added to email as attachment' + }, + ] } ], default: '', @@ -669,7 +634,7 @@ export class Mandrill implements INodeType { }, { displayName: 'Headers', - name: 'headers', + name: 'headersJson', type: 'json', default: '', placeholder: ` @@ -695,6 +660,9 @@ export class Mandrill implements INodeType { placeholder: 'Add Headers', type: 'fixedCollection', default: '', + typeOptions: { + multipleValues: true + }, displayOptions: { show: { jsonParameters: [ @@ -704,7 +672,7 @@ export class Mandrill implements INodeType { }, options: [ { - name: 'parameter', + name: 'headersValues', displayName: 'Values', values: [ { @@ -726,78 +694,6 @@ export class Mandrill implements INodeType { ], description: 'optional extra headers to add to the message (most headers are allowed)', }, - { - displayName: 'Images', - name: 'images', - type: 'json', - typeOptions: { - alwaysOpenEditWindow: true, - }, - displayOptions: { - show: { - jsonParameters: [ - true - ] - } - }, - default: '', - placeholder: ` [ - { - "type": "image/png" (the MIME type of the image - must start with "image/"), - "name": "IMAGECID" (the Content ID of the image - use to reference the image in your HTML content), - "content": "ZXhhbXBsZSBmaWxl" (the content of the image as a base64-encoded string) - } - ]`, - description: 'an array of embedded images to add to the message', - }, - { - displayName: 'Images', - name: 'images', - placeholder: 'Add images', - type: 'fixedCollection', - displayOptions: { - show: { - jsonParameters: [ - false - ] - } - }, - typeOptions: { - multipleValues: true - }, - default: '', - options: [ - { - name: 'parameter', - displayName: 'Values', - values: [ - { - displayName: 'Type', - name: 'type', - type: 'string', - default: '', - description: 'text/plain (the MIME type of the attachment)' - }, - { - displayName: 'Name', - name: 'name', - type: 'string', - default: '', - description: 'myfile.txt (the file name of the attachment)' - }, - { - displayName: 'Content', - name: 'content', - type: 'string', - default: '', - description: 'ZXhhbXBsZSBmaWxl (the content of the attachment as a base64-encoded string)' - } - ] - } - ], - description: 'an array of embedded images to add to the message', - }, - ], }; @@ -815,7 +711,7 @@ export class Mandrill implements INodeType { } for (const template of templates) { const templateName = template.name; - const templateSlug = template.slug + const templateSlug = template.slug; returnData.push({ name: templateName, @@ -830,106 +726,162 @@ export class Mandrill implements INodeType { async executeSingle(this: IExecuteSingleFunctions): Promise { - - let response; + + const item = this.getInputData(); + let emailSentResponse, response; const resource = this.getNodeParameter('resource') as string; if (resource === 'messages') { + const options = this.getNodeParameter('options') as Options; const operation = this.getNodeParameter('operation') as string; const fromEmail = this.getNodeParameter('fromEmail') as string; const toEmail = this.getNodeParameter('toEmail') as string; - const subject = this.getNodeParameter('subject') as string; - const fromName = this.getNodeParameter('fromName') as string; - const important = this.getNodeParameter('important') as boolean; - const trackOpens = this.getNodeParameter('trackOpens') as boolean; - const trackClicks = this.getNodeParameter('trackClicks') as boolean; - const autoText = this.getNodeParameter('autoText') as boolean; - const autoHtml = this.getNodeParameter('autoHtml') as boolean; - const inlineCss = this.getNodeParameter('inlineCss') as boolean; - const urlStripQs = this.getNodeParameter('urlStripQs') as boolean; - const preserveRecipients = this.getNodeParameter('preserveRecipients') as boolean; - const viewContentLink = this.getNodeParameter('viewContentLink') as boolean; - const async = this.getNodeParameter('async') as boolean; - const subAccount = this.getNodeParameter('subAccount') as string; - const googleAnalyticsCampaign = this.getNodeParameter('googleAnalyticsCampaign') as string; - const googleAnalyticsDomains = this.getNodeParameter('googleAnalyticsDomains') as string; - const tags = this.getNodeParameter('tags') as string; - const ipPool = this.getNodeParameter('ipPool') as string; - const bccAddress = this.getNodeParameter('bccAddress') as string; - const trackingDomain = this.getNodeParameter('trackingDomain') as string; - const signingDomain = this.getNodeParameter('signingDomain') as string; - const returnPathDomain = this.getNodeParameter('returnPathDomain') as string; - const html = this.getNodeParameter('html') as string; - const text = this.getNodeParameter('text') as string; - const sendAt = moment(this.getNodeParameter('sendAt') as string).utc().format('YYYY-MM-DD HH:mm:ss'); - const headers = validateJSON(this.getNodeParameter('headers') as string); - const recipientMetadata = validateJSON(this.getNodeParameter('recipientMetadata') as string); - const attachments = validateJSON(this.getNodeParameter('attachments') as string); - const images = validateJSON(this.getNodeParameter('images') as string); - const mergeVars = validateJSON(this.getNodeParameter('mergeVars') as string); - const metadata = validateJSON(this.getNodeParameter('metadata') as string); + const jsonActive = this.getNodeParameter('jsonParameters') as boolean; + const toEmailArray = getToEmailArray(toEmail); - const toEmailArray = getToEmailArray(toEmail) - const googleAnalyticsDomainsArray = getGoogleAnalyticsDomainsArray(googleAnalyticsDomains) - const tagsArray = getTags(tags) - const credentials = this.getCredentials('mandrillApi'); if (credentials === undefined) { throw new Error('No credentials got returned!'); } - const body: IDataObject = { - template_content: [], - message: { - html: html, - text: text, - subject: subject, - from_name: fromName, - from_email: fromEmail, - to: toEmailArray, - important: important, - track_opens: trackOpens, - track_clicks: trackClicks, - auto_text: autoText, - auto_html: autoHtml, - inline_css: inlineCss, - url_strip_qs: urlStripQs, - preserve_recipients: preserveRecipients, - view_content_link: viewContentLink, - async: async, - subaccount: subAccount || null, - google_analytics_campaign: googleAnalyticsCampaign, - google_analytics_domains: googleAnalyticsDomainsArray, - tags: tagsArray, - ip_pool: ipPool, - bcc_address: bccAddress, - tracking_domain: trackingDomain, - signing_domain: signingDomain, - return_path_domain: returnPathDomain, - recipient_metadata: recipientMetadata, - headers: headers, - metadata: metadata, - merge_vars: mergeVars, - attachments: attachments, - images: images - }, - send_at: (sendAt === 'Invalid date' ) ? moment().utc().format('YYYY-MM-DD HH:mm:ss'): sendAt, + const message: Message = { + html: (options.html) ? options.html : '', + text: (options.text) ? options.text : '', + subject: (options.subject) ? options.subject : '', + from_email: fromEmail, + to: toEmailArray, + important: (options.important) ? options.important : false , + track_opens: (options.trackOpens) ? options.trackOpens : false, + track_clicks: (options.trackClicks) ? options.trackClicks : false, + auto_text: (options.autoText) ? options.autoText : false, + auto_html: (options.autoHtml) ? options.autoHtml : false, + inline_css: (options.inlineCss) ? options.inlineCss : false, + url_strip_qs: (options.urlStripQs) ? options.urlStripQs : false, + preserve_recipients: (options.preserveRecipients) ? options.preserveRecipients : false, + view_content_link: (options.viewContentLink) ? options.viewContentLink : false, + async: (options.async) ? options.async : false, + google_analytics_campaign: (options.googleAnalyticsCampaign) ? options.googleAnalyticsCampaign : '', + ip_pool: (options.ipPool) ? options.ipPool : '', + bcc_address: (options.bccAddress) ? options.bccAddress : '', + tracking_domain: (options.trackingDomain) ? options.trackingDomain : '', + signing_domain: (options.signingDomain) ? options.signingDomain: '', + return_path_domain: (options.returnPathDomain) ? options.returnPathDomain : '', }; + + if (options.googleAnalyticsDomains) { + message.google_analytics_domains = getGoogleAnalyticsDomainsArray(options.googleAnalyticsDomains); + } + + if (options.tags) { + message.tags = getTags(options.tags); + } + + if (options.fromName) { + message.from_name = options.fromName; + } + + if (options.subaccount) { + message.subaccount = options.subaccount; + } + + const body: Body = { + template_content: [], + message, + }; + + if (options.sendAt) { + body.send_at = moment(options.sendAt).utc().format('YYYY-MM-DD HH:mm:ss'); + } + + if (jsonActive) { + + body.message.headers = validateJSON(this.getNodeParameter('headersJson') as string); + body.message.metadata = validateJSON(this.getNodeParameter('metadataJson') as string); + body.message.global_merge_vars = validateJSON(this.getNodeParameter('mergeVarsJson') as string); + body.message.attachments = validateJSON(this.getNodeParameter('attachmentsJson') as string); + + } else { - let message + const headersUi = this.getNodeParameter('headersUi') as IDataObject; + if (!_.isEmpty(headersUi)) { + // @ts-ignore + body.message.headers = _.map(headersUi.headersValues, (o) => { + const aux: IDataObject = { }; + // @ts-ignore + aux[o.name] = o.value; + return aux; + }); + } + + const metadataUi = this.getNodeParameter('metadataUi') as IDataObject; + if (!_.isEmpty(metadataUi)) { + // @ts-ignore + body.message.metadata = _.map(metadataUi.metadataValues, (o) => { + const aux: IDataObject = { }; + aux[o.name] = o.value; + return aux; + }); + } + + const mergeVarsUi = this.getNodeParameter('mergeVarsUi') as IDataObject; + if (!_.isEmpty(mergeVarsUi)) { + // @ts-ignore + body.message.global_merge_vars = _.map(mergeVarsUi.mergeVarsValues, (o) => { + const aux: IDataObject = { }; + aux.name = o.name; + aux.content = o.content; + return aux; + }); + } + + const attachmentsUi = this.getNodeParameter('attachmentsUi') as IDataObject; + let attachmentsBinary = [], attachmentsValues = []; + if (!_.isEmpty(attachmentsUi)) { + + if (attachmentsUi.hasOwnProperty('attachmentsValues') + && !_.isEmpty(attachmentsUi.attachmentsValues)) { + // @ts-ignore + attachmentsValues = _.map(attachmentsUi.attachmentsValues, (o) => { + const aux: IDataObject = { }; + // @ts-ignore + aux.name = o.name; + aux.content = o.content; + aux.type = o.type; + return aux; + }); + } + + if (attachmentsUi.hasOwnProperty('attachmentsBinary') + && !_.isEmpty(attachmentsUi.attachmentsBinary) + && item.binary) { + // @ts-ignore + attachmentsBinary = _.map(attachmentsUi.attachmentsBinary, (o) => { + if (item.binary!.hasOwnProperty(o.property)) { + const aux: IDataObject = { }; + aux.name = item.binary![o.property].fileName || 'unknown'; + aux.content = item.binary![o.property].data; + aux.type = item.binary![o.property].mimeType; + return aux; + } + }); + } + } + + body.message.attachments = attachmentsBinary.concat(attachmentsValues); + } if (operation === 'sendTemplate') { const template = this.getNodeParameter('template') as string; - body.template_name = template - message = mandrillApiRequest.call(this, '/messages', 'POST', '/send-template', body); + body.template_name = template; + emailSentResponse = mandrillApiRequest.call(this, '/messages', 'POST', '/send-template', body); } else if (operation === 'sendHtml') { - message = mandrillApiRequest.call(this, '/messages', 'POST', '/send', body); + emailSentResponse = mandrillApiRequest.call(this, '/messages', 'POST', '/send', body); } try { - response = await message + response = await emailSentResponse; } catch (err) { throw new Error(`Mandrill Error: ${err}`); }