2022-01-03 13:42:42 -08:00
|
|
|
import { IExecuteFunctions } from 'n8n-core';
|
2021-01-19 15:16:25 -08:00
|
|
|
|
|
|
|
import {
|
|
|
|
IDataObject,
|
|
|
|
ILoadOptionsFunctions,
|
|
|
|
INodeExecutionData,
|
|
|
|
INodePropertyOptions,
|
|
|
|
INodeType,
|
2021-04-16 09:33:36 -07:00
|
|
|
INodeTypeDescription,
|
2022-08-17 08:50:24 -07:00
|
|
|
NodeOperationError,
|
2021-01-19 15:16:25 -08:00
|
|
|
} from 'n8n-workflow';
|
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
import { listFields, listOperations } from './ListDescription';
|
2021-01-19 15:16:25 -08:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
import { contactFields, contactOperations } from './ContactDescription';
|
2021-01-19 15:16:25 -08:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
import { mailFields, mailOperations, SendMailBody } from './MailDescription';
|
2021-03-26 00:48:52 -07:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
import { sendGridApiRequest, sendGridApiRequestAllItems } from './GenericFunctions';
|
2021-01-19 15:16:25 -08:00
|
|
|
|
2022-04-08 14:32:08 -07:00
|
|
|
import moment from 'moment-timezone';
|
2021-03-26 00:48:52 -07:00
|
|
|
|
2021-01-19 15:16:25 -08:00
|
|
|
export class SendGrid implements INodeType {
|
|
|
|
description: INodeTypeDescription = {
|
|
|
|
displayName: 'SendGrid',
|
|
|
|
name: 'sendGrid',
|
|
|
|
icon: 'file:sendGrid.svg',
|
|
|
|
group: ['transform'],
|
|
|
|
version: 1,
|
|
|
|
subtitle: '={{$parameter["operation"] + ":" + $parameter["resource"]}}',
|
|
|
|
description: 'Consume SendGrid API',
|
|
|
|
defaults: {
|
|
|
|
name: 'SendGrid',
|
|
|
|
},
|
|
|
|
inputs: ['main'],
|
|
|
|
outputs: ['main'],
|
|
|
|
credentials: [
|
|
|
|
{
|
|
|
|
name: 'sendGridApi',
|
|
|
|
required: true,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
properties: [
|
|
|
|
// Node properties which the user gets displayed and
|
|
|
|
// can change on the node.
|
|
|
|
{
|
|
|
|
displayName: 'Resource',
|
|
|
|
name: 'resource',
|
|
|
|
type: 'options',
|
2022-05-20 14:47:24 -07:00
|
|
|
noDataExpression: true,
|
2021-01-19 15:16:25 -08:00
|
|
|
options: [
|
|
|
|
{
|
|
|
|
name: 'Contact',
|
|
|
|
value: 'contact',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'List',
|
|
|
|
value: 'list',
|
|
|
|
},
|
2021-03-26 00:48:52 -07:00
|
|
|
{
|
|
|
|
name: 'Mail',
|
|
|
|
value: 'mail',
|
|
|
|
},
|
2021-01-19 15:16:25 -08:00
|
|
|
],
|
|
|
|
default: 'list',
|
|
|
|
required: true,
|
|
|
|
},
|
|
|
|
...listOperations,
|
|
|
|
...listFields,
|
|
|
|
...contactOperations,
|
|
|
|
...contactFields,
|
2021-03-26 00:48:52 -07:00
|
|
|
...mailOperations,
|
|
|
|
...mailFields,
|
2021-01-19 15:16:25 -08:00
|
|
|
],
|
|
|
|
};
|
|
|
|
|
2021-03-26 00:48:52 -07:00
|
|
|
methods = {
|
2021-01-19 15:16:25 -08:00
|
|
|
loadOptions: {
|
|
|
|
// Get custom fields to display to user so that they can select them easily
|
2022-08-17 08:50:24 -07:00
|
|
|
async getCustomFields(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
|
2021-01-19 15:16:25 -08:00
|
|
|
const returnData: INodePropertyOptions[] = [];
|
2022-08-17 08:50:24 -07:00
|
|
|
const { custom_fields } = await sendGridApiRequest.call(
|
|
|
|
this,
|
|
|
|
'/marketing/field_definitions',
|
|
|
|
'GET',
|
|
|
|
{},
|
|
|
|
{},
|
|
|
|
);
|
2021-01-19 15:16:25 -08:00
|
|
|
if (custom_fields !== undefined) {
|
2021-03-26 00:48:52 -07:00
|
|
|
for (const customField of custom_fields) {
|
2021-01-19 15:16:25 -08:00
|
|
|
returnData.push({
|
|
|
|
name: customField.name,
|
|
|
|
value: customField.id,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return returnData;
|
|
|
|
},
|
|
|
|
// Get lists to display to user so that they can select them easily
|
|
|
|
async getListIds(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
|
|
|
|
const returnData: INodePropertyOptions[] = [];
|
2022-08-17 08:50:24 -07:00
|
|
|
const lists = await sendGridApiRequestAllItems.call(
|
|
|
|
this,
|
|
|
|
`/marketing/lists`,
|
|
|
|
'GET',
|
|
|
|
'result',
|
|
|
|
{},
|
|
|
|
{},
|
|
|
|
);
|
2021-01-19 15:16:25 -08:00
|
|
|
for (const list of lists) {
|
|
|
|
returnData.push({
|
|
|
|
name: list.name,
|
|
|
|
value: list.id,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return returnData;
|
|
|
|
},
|
2021-03-26 00:48:52 -07:00
|
|
|
async getTemplateIds(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
|
2022-08-17 08:50:24 -07:00
|
|
|
const responseData = await sendGridApiRequest.call(
|
|
|
|
this,
|
|
|
|
'/templates',
|
|
|
|
'GET',
|
|
|
|
{},
|
|
|
|
{ generations: 'dynamic' },
|
|
|
|
);
|
|
|
|
return responseData.templates.map(({ id, name }: { id: string; name: string }) => ({
|
|
|
|
name,
|
|
|
|
value: id,
|
|
|
|
}));
|
2021-03-26 00:48:52 -07:00
|
|
|
},
|
2021-01-19 15:16:25 -08:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
|
|
|
|
const items = this.getInputData();
|
2022-04-22 09:29:51 -07:00
|
|
|
const length = items.length;
|
2021-01-19 15:16:25 -08:00
|
|
|
const qs: IDataObject = {};
|
|
|
|
let responseData;
|
2021-03-26 00:48:52 -07:00
|
|
|
const timezone = this.getTimezone();
|
2022-08-30 08:55:33 -07:00
|
|
|
const returnData: INodeExecutionData[] = [];
|
2022-12-02 03:53:59 -08:00
|
|
|
const resource = this.getNodeParameter('resource', 0);
|
|
|
|
const operation = this.getNodeParameter('operation', 0);
|
2021-01-19 15:16:25 -08:00
|
|
|
// https://sendgrid.com/docs/api-reference/
|
|
|
|
if (resource === 'contact') {
|
|
|
|
if (operation === 'getAll') {
|
|
|
|
for (let i = 0; i < length; i++) {
|
2021-07-19 23:58:54 -07:00
|
|
|
try {
|
2022-11-18 05:31:38 -08:00
|
|
|
const returnAll = this.getNodeParameter('returnAll', i);
|
2022-11-18 07:29:44 -08:00
|
|
|
const filters = this.getNodeParameter('filters', i);
|
2021-07-19 23:58:54 -07:00
|
|
|
let endpoint = '/marketing/contacts';
|
|
|
|
let method = 'GET';
|
|
|
|
const body: IDataObject = {};
|
|
|
|
if (filters.query && filters.query !== '') {
|
|
|
|
endpoint = '/marketing/contacts/search';
|
|
|
|
method = 'POST';
|
|
|
|
Object.assign(body, { query: filters.query });
|
|
|
|
}
|
2022-08-17 08:50:24 -07:00
|
|
|
responseData = await sendGridApiRequestAllItems.call(
|
|
|
|
this,
|
|
|
|
endpoint,
|
|
|
|
method,
|
|
|
|
'result',
|
|
|
|
body,
|
|
|
|
qs,
|
|
|
|
);
|
2022-12-02 12:54:28 -08:00
|
|
|
if (!returnAll) {
|
2022-11-18 06:26:22 -08:00
|
|
|
const limit = this.getNodeParameter('limit', i);
|
2021-07-19 23:58:54 -07:00
|
|
|
responseData = responseData.splice(0, limit);
|
|
|
|
}
|
2022-08-30 08:55:33 -07:00
|
|
|
const executionData = this.helpers.constructExecutionMetaData(
|
|
|
|
this.helpers.returnJsonArray(responseData),
|
2022-11-18 05:31:38 -08:00
|
|
|
{ itemData: { item: i } },
|
2022-08-30 08:55:33 -07:00
|
|
|
);
|
|
|
|
returnData.push(...executionData);
|
2021-07-19 23:58:54 -07:00
|
|
|
} catch (error) {
|
|
|
|
if (this.continueOnFail()) {
|
2022-08-30 08:55:33 -07:00
|
|
|
const executionData = this.helpers.constructExecutionMetaData(
|
2022-11-18 05:31:38 -08:00
|
|
|
this.helpers.returnJsonArray({ error: error.message }),
|
|
|
|
{ itemData: { item: i } },
|
2022-08-30 08:55:33 -07:00
|
|
|
);
|
|
|
|
returnData.push(...executionData);
|
2021-07-19 23:58:54 -07:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
throw error;
|
2021-01-19 15:16:25 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (operation === 'get') {
|
|
|
|
const by = this.getNodeParameter('by', 0) as string;
|
|
|
|
let endpoint;
|
|
|
|
let method;
|
|
|
|
const body: IDataObject = {};
|
|
|
|
for (let i = 0; i < length; i++) {
|
2021-07-19 23:58:54 -07:00
|
|
|
try {
|
|
|
|
if (by === 'id') {
|
|
|
|
method = 'GET';
|
|
|
|
const contactId = this.getNodeParameter('contactId', i) as string;
|
|
|
|
endpoint = `/marketing/contacts/${contactId}`;
|
|
|
|
} else {
|
|
|
|
const email = this.getNodeParameter('email', i) as string;
|
|
|
|
endpoint = '/marketing/contacts/search';
|
|
|
|
method = 'POST';
|
|
|
|
Object.assign(body, { query: `email LIKE '${email}' ` });
|
|
|
|
}
|
|
|
|
responseData = await sendGridApiRequest.call(this, endpoint, method, body, qs);
|
|
|
|
responseData = responseData.result || responseData;
|
|
|
|
if (Array.isArray(responseData)) {
|
|
|
|
responseData = responseData[0];
|
|
|
|
}
|
2022-08-30 08:55:33 -07:00
|
|
|
|
|
|
|
const executionData = this.helpers.constructExecutionMetaData(
|
|
|
|
this.helpers.returnJsonArray(responseData),
|
2022-11-18 05:31:38 -08:00
|
|
|
{ itemData: { item: i } },
|
2022-08-30 08:55:33 -07:00
|
|
|
);
|
|
|
|
returnData.push(...executionData);
|
2021-07-19 23:58:54 -07:00
|
|
|
} catch (error) {
|
|
|
|
if (this.continueOnFail()) {
|
2022-08-30 08:55:33 -07:00
|
|
|
const executionData = this.helpers.constructExecutionMetaData(
|
2022-11-18 05:31:38 -08:00
|
|
|
this.helpers.returnJsonArray({ error: error.message }),
|
|
|
|
{ itemData: { item: i } },
|
2022-08-30 08:55:33 -07:00
|
|
|
);
|
|
|
|
returnData.push(...executionData);
|
2021-07-19 23:58:54 -07:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
throw error;
|
2021-01-19 15:16:25 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (operation === 'upsert') {
|
2021-07-19 23:58:54 -07:00
|
|
|
try {
|
|
|
|
const contacts = [];
|
2021-09-22 00:40:51 -07:00
|
|
|
let lists;
|
2021-07-19 23:58:54 -07:00
|
|
|
for (let i = 0; i < length; i++) {
|
|
|
|
const email = this.getNodeParameter('email', i) as string;
|
2022-11-18 07:29:44 -08:00
|
|
|
const additionalFields = this.getNodeParameter('additionalFields', i);
|
2021-07-19 23:58:54 -07:00
|
|
|
const contact: IDataObject = {
|
|
|
|
email,
|
|
|
|
};
|
|
|
|
if (additionalFields.addressUi) {
|
2022-08-17 08:50:24 -07:00
|
|
|
const addressValues = (additionalFields.addressUi as IDataObject)
|
|
|
|
.addressValues as IDataObject;
|
2021-07-19 23:58:54 -07:00
|
|
|
const addressLine1 = addressValues.address1 as string;
|
|
|
|
const addressLine2 = addressValues.address2 as string;
|
|
|
|
if (addressLine2) {
|
|
|
|
Object.assign(contact, { address_line_2: addressLine2 });
|
|
|
|
}
|
|
|
|
Object.assign(contact, { address_line_1: addressLine1 });
|
2021-01-19 15:16:25 -08:00
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
if (additionalFields.city) {
|
|
|
|
const city = additionalFields.city as string;
|
|
|
|
Object.assign(contact, { city });
|
|
|
|
}
|
|
|
|
if (additionalFields.country) {
|
|
|
|
const country = additionalFields.country as string;
|
|
|
|
Object.assign(contact, { country });
|
|
|
|
}
|
|
|
|
if (additionalFields.firstName) {
|
|
|
|
const firstName = additionalFields.firstName as string;
|
|
|
|
Object.assign(contact, { first_name: firstName });
|
|
|
|
}
|
|
|
|
if (additionalFields.lastName) {
|
|
|
|
const lastName = additionalFields.lastName as string;
|
|
|
|
Object.assign(contact, { last_name: lastName });
|
|
|
|
}
|
|
|
|
if (additionalFields.postalCode) {
|
|
|
|
const postalCode = additionalFields.postalCode as string;
|
|
|
|
Object.assign(contact, { postal_code: postalCode });
|
|
|
|
}
|
|
|
|
if (additionalFields.stateProvinceRegion) {
|
|
|
|
const stateProvinceRegion = additionalFields.stateProvinceRegion as string;
|
|
|
|
Object.assign(contact, { state_province_region: stateProvinceRegion });
|
|
|
|
}
|
|
|
|
if (additionalFields.alternateEmails) {
|
2022-12-02 12:54:28 -08:00
|
|
|
const alternateEmails = (additionalFields.alternateEmails as string)
|
|
|
|
.split(',')
|
|
|
|
.filter((mail) => !!mail);
|
2021-07-19 23:58:54 -07:00
|
|
|
if (alternateEmails.length !== 0) {
|
|
|
|
Object.assign(contact, { alternate_emails: alternateEmails });
|
|
|
|
}
|
2021-01-19 15:16:25 -08:00
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
if (additionalFields.listIdsUi) {
|
2022-08-17 08:50:24 -07:00
|
|
|
const listIdValues = (additionalFields.listIdsUi as IDataObject)
|
|
|
|
.listIdValues as IDataObject;
|
2021-07-19 23:58:54 -07:00
|
|
|
const listIds = listIdValues.listIds as IDataObject[];
|
2021-09-22 00:40:51 -07:00
|
|
|
lists = listIds;
|
2021-07-19 23:58:54 -07:00
|
|
|
}
|
|
|
|
if (additionalFields.customFieldsUi) {
|
2022-08-17 08:50:24 -07:00
|
|
|
const customFields = (additionalFields.customFieldsUi as IDataObject)
|
|
|
|
.customFieldValues as IDataObject[];
|
2021-07-19 23:58:54 -07:00
|
|
|
if (customFields) {
|
2022-08-17 08:50:24 -07:00
|
|
|
const data = customFields.reduce(
|
|
|
|
(obj, value) => Object.assign(obj, { [`${value.fieldId}`]: value.fieldValue }),
|
|
|
|
{},
|
|
|
|
);
|
2021-07-19 23:58:54 -07:00
|
|
|
Object.assign(contact, { custom_fields: data });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
contacts.push(contact);
|
2021-01-19 15:16:25 -08:00
|
|
|
}
|
2022-08-17 08:50:24 -07:00
|
|
|
responseData = await sendGridApiRequest.call(
|
|
|
|
this,
|
|
|
|
'/marketing/contacts',
|
|
|
|
'PUT',
|
|
|
|
{ list_ids: lists, contacts },
|
|
|
|
qs,
|
|
|
|
);
|
2021-07-19 23:58:54 -07:00
|
|
|
returnData.push(responseData);
|
|
|
|
} catch (error) {
|
|
|
|
if (this.continueOnFail()) {
|
2022-11-18 05:31:38 -08:00
|
|
|
returnData.push({ json: { error: error.message } });
|
2021-12-23 04:30:35 -08:00
|
|
|
} else {
|
2021-07-19 23:58:54 -07:00
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
}
|
2021-01-19 15:16:25 -08:00
|
|
|
}
|
|
|
|
if (operation === 'delete') {
|
|
|
|
for (let i = 0; i < length; i++) {
|
2021-07-19 23:58:54 -07:00
|
|
|
try {
|
|
|
|
const deleteAll = this.getNodeParameter('deleteAll', i) as boolean;
|
2022-12-02 12:54:28 -08:00
|
|
|
if (deleteAll) {
|
2021-07-19 23:58:54 -07:00
|
|
|
qs.delete_all_contacts = 'true';
|
|
|
|
}
|
|
|
|
qs.ids = (this.getNodeParameter('ids', i) as string).replace(/\s/g, '');
|
2022-08-17 08:50:24 -07:00
|
|
|
responseData = await sendGridApiRequest.call(
|
|
|
|
this,
|
|
|
|
`/marketing/contacts`,
|
|
|
|
'DELETE',
|
|
|
|
{},
|
|
|
|
qs,
|
|
|
|
);
|
2022-08-30 08:55:33 -07:00
|
|
|
|
|
|
|
const executionData = this.helpers.constructExecutionMetaData(
|
|
|
|
this.helpers.returnJsonArray(responseData),
|
2022-11-18 05:31:38 -08:00
|
|
|
{ itemData: { item: i } },
|
2022-08-30 08:55:33 -07:00
|
|
|
);
|
|
|
|
returnData.push(...executionData);
|
2021-07-19 23:58:54 -07:00
|
|
|
} catch (error) {
|
|
|
|
if (this.continueOnFail()) {
|
2022-08-30 08:55:33 -07:00
|
|
|
const executionData = this.helpers.constructExecutionMetaData(
|
2022-11-18 06:26:22 -08:00
|
|
|
this.helpers.returnJsonArray({ error: error.message }),
|
|
|
|
{ itemData: { item: i } },
|
2022-08-30 08:55:33 -07:00
|
|
|
);
|
|
|
|
returnData.push(...executionData);
|
2021-07-19 23:58:54 -07:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
throw error;
|
2021-01-19 15:16:25 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (resource === 'list') {
|
2021-03-26 00:48:52 -07:00
|
|
|
if (operation === 'getAll') {
|
2021-01-19 15:16:25 -08:00
|
|
|
for (let i = 0; i < length; i++) {
|
2021-07-19 23:58:54 -07:00
|
|
|
try {
|
2022-11-18 05:31:38 -08:00
|
|
|
const returnAll = this.getNodeParameter('returnAll', i);
|
2022-08-17 08:50:24 -07:00
|
|
|
responseData = await sendGridApiRequestAllItems.call(
|
|
|
|
this,
|
|
|
|
`/marketing/lists`,
|
|
|
|
'GET',
|
|
|
|
'result',
|
|
|
|
{},
|
|
|
|
qs,
|
|
|
|
);
|
2022-12-02 12:54:28 -08:00
|
|
|
if (!returnAll) {
|
2022-11-18 06:26:22 -08:00
|
|
|
const limit = this.getNodeParameter('limit', i);
|
2021-07-19 23:58:54 -07:00
|
|
|
responseData = responseData.splice(0, limit);
|
|
|
|
}
|
2022-08-30 08:55:33 -07:00
|
|
|
|
|
|
|
const executionData = this.helpers.constructExecutionMetaData(
|
|
|
|
this.helpers.returnJsonArray(responseData),
|
|
|
|
{ itemData: { item: i } },
|
|
|
|
);
|
|
|
|
returnData.push(...executionData);
|
2021-07-19 23:58:54 -07:00
|
|
|
} catch (error) {
|
|
|
|
if (this.continueOnFail()) {
|
2022-08-30 08:55:33 -07:00
|
|
|
const executionData = this.helpers.constructExecutionMetaData(
|
2022-11-18 06:26:22 -08:00
|
|
|
this.helpers.returnJsonArray({ error: error.message }),
|
|
|
|
{ itemData: { item: i } },
|
2022-08-30 08:55:33 -07:00
|
|
|
);
|
|
|
|
returnData.push(...executionData);
|
2021-07-19 23:58:54 -07:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
throw error;
|
2021-01-19 15:16:25 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (operation === 'get') {
|
|
|
|
for (let i = 0; i < length; i++) {
|
2021-07-19 23:58:54 -07:00
|
|
|
try {
|
|
|
|
const listId = this.getNodeParameter('listId', i) as string;
|
|
|
|
qs.contact_sample = this.getNodeParameter('contactSample', i) as boolean;
|
2022-08-17 08:50:24 -07:00
|
|
|
responseData = await sendGridApiRequest.call(
|
|
|
|
this,
|
|
|
|
`/marketing/lists/${listId}`,
|
|
|
|
'GET',
|
|
|
|
{},
|
|
|
|
qs,
|
|
|
|
);
|
2022-08-30 08:55:33 -07:00
|
|
|
|
|
|
|
const executionData = this.helpers.constructExecutionMetaData(
|
|
|
|
this.helpers.returnJsonArray(responseData),
|
2022-11-18 06:26:22 -08:00
|
|
|
{ itemData: { item: i } },
|
2022-08-30 08:55:33 -07:00
|
|
|
);
|
|
|
|
returnData.push(...executionData);
|
2021-07-19 23:58:54 -07:00
|
|
|
} catch (error) {
|
|
|
|
if (this.continueOnFail()) {
|
2022-08-30 08:55:33 -07:00
|
|
|
const executionData = this.helpers.constructExecutionMetaData(
|
2022-11-18 06:26:22 -08:00
|
|
|
this.helpers.returnJsonArray({ error: error.message }),
|
|
|
|
{ itemData: { item: i } },
|
2022-08-30 08:55:33 -07:00
|
|
|
);
|
|
|
|
returnData.push(...executionData);
|
2021-07-19 23:58:54 -07:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
throw error;
|
|
|
|
}
|
2021-01-19 15:16:25 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (operation === 'create') {
|
|
|
|
for (let i = 0; i < length; i++) {
|
2021-07-19 23:58:54 -07:00
|
|
|
try {
|
|
|
|
const name = this.getNodeParameter('name', i) as string;
|
2022-08-17 08:50:24 -07:00
|
|
|
responseData = await sendGridApiRequest.call(
|
|
|
|
this,
|
|
|
|
'/marketing/lists',
|
|
|
|
'POST',
|
|
|
|
{ name },
|
|
|
|
qs,
|
|
|
|
);
|
2022-08-30 08:55:33 -07:00
|
|
|
|
|
|
|
const executionData = this.helpers.constructExecutionMetaData(
|
|
|
|
this.helpers.returnJsonArray(responseData),
|
|
|
|
{ itemData: { item: i } },
|
|
|
|
);
|
|
|
|
returnData.push(...executionData);
|
2021-07-19 23:58:54 -07:00
|
|
|
} catch (error) {
|
|
|
|
if (this.continueOnFail()) {
|
2022-08-30 08:55:33 -07:00
|
|
|
const executionData = this.helpers.constructExecutionMetaData(
|
2022-11-18 06:26:22 -08:00
|
|
|
this.helpers.returnJsonArray({ error: error.message }),
|
2022-08-30 08:55:33 -07:00
|
|
|
{ itemData: { item: i } },
|
|
|
|
);
|
|
|
|
returnData.push(...executionData);
|
2021-07-19 23:58:54 -07:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
throw error;
|
|
|
|
}
|
2021-01-19 15:16:25 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (operation === 'delete') {
|
|
|
|
for (let i = 0; i < length; i++) {
|
2021-07-19 23:58:54 -07:00
|
|
|
try {
|
|
|
|
const listId = this.getNodeParameter('listId', i) as string;
|
|
|
|
qs.delete_contacts = this.getNodeParameter('deleteContacts', i) as boolean;
|
2022-08-17 08:50:24 -07:00
|
|
|
responseData = await sendGridApiRequest.call(
|
|
|
|
this,
|
|
|
|
`/marketing/lists/${listId}`,
|
|
|
|
'DELETE',
|
|
|
|
{},
|
|
|
|
qs,
|
|
|
|
);
|
2021-07-19 23:58:54 -07:00
|
|
|
responseData = { success: true };
|
2022-08-30 08:55:33 -07:00
|
|
|
const executionData = this.helpers.constructExecutionMetaData(
|
|
|
|
this.helpers.returnJsonArray(responseData),
|
|
|
|
{ itemData: { item: i } },
|
|
|
|
);
|
|
|
|
returnData.push(...executionData);
|
2021-07-19 23:58:54 -07:00
|
|
|
} catch (error) {
|
|
|
|
if (this.continueOnFail()) {
|
2022-08-30 08:55:33 -07:00
|
|
|
const executionData = this.helpers.constructExecutionMetaData(
|
|
|
|
this.helpers.returnJsonArray({ error: error.message }),
|
|
|
|
{ itemData: { item: i } },
|
|
|
|
);
|
|
|
|
returnData.push(...executionData);
|
2021-07-19 23:58:54 -07:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
throw error;
|
|
|
|
}
|
2021-01-19 15:16:25 -08:00
|
|
|
}
|
|
|
|
}
|
2021-03-26 00:48:52 -07:00
|
|
|
if (operation === 'update') {
|
2021-01-19 15:16:25 -08:00
|
|
|
for (let i = 0; i < length; i++) {
|
2021-07-19 23:58:54 -07:00
|
|
|
try {
|
|
|
|
const name = this.getNodeParameter('name', i) as string;
|
|
|
|
const listId = this.getNodeParameter('listId', i) as string;
|
2022-08-17 08:50:24 -07:00
|
|
|
responseData = await sendGridApiRequest.call(
|
|
|
|
this,
|
|
|
|
`/marketing/lists/${listId}`,
|
|
|
|
'PATCH',
|
|
|
|
{ name },
|
|
|
|
qs,
|
|
|
|
);
|
2022-08-30 08:55:33 -07:00
|
|
|
const executionData = this.helpers.constructExecutionMetaData(
|
|
|
|
this.helpers.returnJsonArray(responseData),
|
|
|
|
{ itemData: { item: i } },
|
|
|
|
);
|
|
|
|
returnData.push(...executionData);
|
2021-07-19 23:58:54 -07:00
|
|
|
} catch (error) {
|
|
|
|
if (this.continueOnFail()) {
|
2022-08-30 08:55:33 -07:00
|
|
|
const executionData = this.helpers.constructExecutionMetaData(
|
2022-11-18 06:26:22 -08:00
|
|
|
this.helpers.returnJsonArray({ error: error.message }),
|
2022-08-30 08:55:33 -07:00
|
|
|
{ itemData: { item: i } },
|
|
|
|
);
|
|
|
|
returnData.push(...executionData);
|
2021-07-19 23:58:54 -07:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
throw error;
|
|
|
|
}
|
2021-01-19 15:16:25 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-03-26 00:48:52 -07:00
|
|
|
if (resource === 'mail') {
|
|
|
|
if (operation === 'send') {
|
|
|
|
for (let i = 0; i < length; i++) {
|
2021-07-19 23:58:54 -07:00
|
|
|
try {
|
|
|
|
const toEmail = this.getNodeParameter('toEmail', i) as string;
|
|
|
|
|
|
|
|
const parsedToEmail = toEmail.includes(',')
|
2022-12-02 12:54:28 -08:00
|
|
|
? toEmail.split(',').map((entry) => ({ email: entry.trim() }))
|
2021-07-19 23:58:54 -07:00
|
|
|
: [{ email: toEmail.trim() }];
|
|
|
|
|
|
|
|
const {
|
|
|
|
bccEmail,
|
|
|
|
ccEmail,
|
|
|
|
enableSandbox,
|
|
|
|
sendAt,
|
|
|
|
headers,
|
|
|
|
attachments,
|
|
|
|
categories,
|
|
|
|
ipPoolName,
|
|
|
|
} = this.getNodeParameter('additionalFields', i) as {
|
|
|
|
bccEmail: string;
|
|
|
|
ccEmail: string;
|
2022-08-17 08:50:24 -07:00
|
|
|
enableSandbox: boolean;
|
2021-07-19 23:58:54 -07:00
|
|
|
sendAt: string;
|
|
|
|
headers: { details: Array<{ key: string; value: string }> };
|
|
|
|
attachments: string;
|
|
|
|
categories: string;
|
|
|
|
ipPoolName: string;
|
|
|
|
};
|
2021-03-26 00:48:52 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const body: SendMailBody = {
|
2022-08-17 08:50:24 -07:00
|
|
|
personalizations: [
|
|
|
|
{
|
|
|
|
to: parsedToEmail,
|
|
|
|
},
|
|
|
|
],
|
2021-07-19 23:58:54 -07:00
|
|
|
from: {
|
|
|
|
email: (this.getNodeParameter('fromEmail', i) as string).trim(),
|
|
|
|
name: this.getNodeParameter('fromName', i) as string,
|
2021-03-26 00:48:52 -07:00
|
|
|
},
|
2021-07-19 23:58:54 -07:00
|
|
|
mail_settings: {
|
|
|
|
sandbox_mode: {
|
|
|
|
enable: enableSandbox || false,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
2021-03-26 00:48:52 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const dynamicTemplateEnabled = this.getNodeParameter('dynamicTemplate', i);
|
2021-03-26 00:48:52 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
// dynamic template
|
|
|
|
if (dynamicTemplateEnabled) {
|
|
|
|
body.template_id = this.getNodeParameter('templateId', i) as string;
|
2021-03-26 00:48:52 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const { fields } = this.getNodeParameter('dynamicTemplateFields', i) as {
|
2022-08-17 08:50:24 -07:00
|
|
|
fields: Array<{ [key: string]: string }>;
|
2021-07-19 23:58:54 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
if (fields) {
|
|
|
|
body.personalizations[0].dynamic_template_data = {};
|
2022-08-17 08:50:24 -07:00
|
|
|
fields.forEach((field) => {
|
2021-07-19 23:58:54 -07:00
|
|
|
body.personalizations[0].dynamic_template_data![field.key] = field.value;
|
|
|
|
});
|
|
|
|
}
|
2021-03-26 00:48:52 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
// message body
|
|
|
|
} else {
|
|
|
|
body.personalizations[0].subject = this.getNodeParameter('subject', i) as string;
|
2022-08-17 08:50:24 -07:00
|
|
|
body.content = [
|
|
|
|
{
|
|
|
|
type: this.getNodeParameter('contentType', i) as string,
|
|
|
|
value: this.getNodeParameter('contentValue', i) as string,
|
|
|
|
},
|
|
|
|
];
|
2021-03-26 00:48:52 -07:00
|
|
|
}
|
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (attachments) {
|
|
|
|
const attachmentsToSend = [];
|
|
|
|
const binaryProperties = attachments.split(',').map((p) => p.trim());
|
|
|
|
|
|
|
|
for (const property of binaryProperties) {
|
|
|
|
if (!items[i].binary?.hasOwnProperty(property)) {
|
2022-08-17 08:50:24 -07:00
|
|
|
throw new NodeOperationError(
|
|
|
|
this.getNode(),
|
|
|
|
`The binary property ${property} does not exist`,
|
|
|
|
{ itemIndex: i },
|
|
|
|
);
|
2021-07-19 23:58:54 -07:00
|
|
|
}
|
2021-03-26 00:48:52 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const binaryProperty = items[i].binary![property];
|
2021-03-26 00:48:52 -07:00
|
|
|
|
2022-04-28 08:39:38 -07:00
|
|
|
const dataBuffer = await this.helpers.getBinaryDataBuffer(i, property);
|
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
attachmentsToSend.push({
|
2022-04-28 08:39:38 -07:00
|
|
|
content: dataBuffer.toString('base64'),
|
2021-07-19 23:58:54 -07:00
|
|
|
filename: binaryProperty.fileName || 'unknown',
|
|
|
|
type: binaryProperty.mimeType,
|
|
|
|
});
|
2021-03-26 00:48:52 -07:00
|
|
|
}
|
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (attachmentsToSend.length) {
|
|
|
|
body.attachments = attachmentsToSend;
|
|
|
|
}
|
|
|
|
}
|
2021-03-26 00:48:52 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (bccEmail) {
|
2022-12-02 12:54:28 -08:00
|
|
|
body.personalizations[0].bcc = bccEmail
|
|
|
|
.split(',')
|
|
|
|
.map((entry) => ({ email: entry.trim() }));
|
2021-03-26 00:48:52 -07:00
|
|
|
}
|
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (ccEmail) {
|
2022-12-02 12:54:28 -08:00
|
|
|
body.personalizations[0].cc = ccEmail
|
|
|
|
.split(',')
|
|
|
|
.map((entry) => ({ email: entry.trim() }));
|
2021-03-26 00:48:52 -07:00
|
|
|
}
|
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (headers?.details.length) {
|
|
|
|
const parsedHeaders: { [key: string]: string } = {};
|
2022-12-02 12:54:28 -08:00
|
|
|
headers.details.forEach((obj) => (parsedHeaders[obj.key] = obj.value));
|
2021-07-19 23:58:54 -07:00
|
|
|
body.headers = parsedHeaders;
|
|
|
|
}
|
2021-03-26 00:48:52 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (categories) {
|
2022-12-02 12:54:28 -08:00
|
|
|
body.categories = categories.split(',');
|
2021-07-19 23:58:54 -07:00
|
|
|
}
|
2021-03-26 00:48:52 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (ipPoolName) {
|
2022-12-02 12:54:28 -08:00
|
|
|
body.ip_pool_name = ipPoolName;
|
2021-07-19 23:58:54 -07:00
|
|
|
}
|
2021-03-26 00:48:52 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (sendAt) {
|
|
|
|
body.personalizations[0].send_at = moment.tz(sendAt, timezone).unix();
|
|
|
|
}
|
2021-03-26 00:48:52 -07:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
const data = await sendGridApiRequest.call(this, '/mail/send', 'POST', body, qs, {
|
|
|
|
resolveWithFullResponse: true,
|
|
|
|
});
|
2021-03-26 00:48:52 -07:00
|
|
|
|
2022-08-30 08:55:33 -07:00
|
|
|
const executionData = this.helpers.constructExecutionMetaData(
|
2022-12-02 12:54:28 -08:00
|
|
|
this.helpers.returnJsonArray({ messageId: data.headers['x-message-id'] }),
|
2022-08-30 08:55:33 -07:00
|
|
|
{ itemData: { item: i } },
|
|
|
|
);
|
|
|
|
returnData.push(...executionData);
|
2021-07-19 23:58:54 -07:00
|
|
|
} catch (error) {
|
|
|
|
if (this.continueOnFail()) {
|
2022-08-30 08:55:33 -07:00
|
|
|
const executionData = this.helpers.constructExecutionMetaData(
|
|
|
|
this.helpers.returnJsonArray({ error: error.message }),
|
|
|
|
{ itemData: { item: i } },
|
|
|
|
);
|
|
|
|
returnData.push(...executionData);
|
2021-07-19 23:58:54 -07:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
throw error;
|
2021-03-26 00:48:52 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-08-30 08:55:33 -07:00
|
|
|
return this.prepareOutputData(returnData);
|
2021-01-19 15:16:25 -08:00
|
|
|
}
|
|
|
|
}
|