2023-01-27 03:22:44 -08:00
|
|
|
import type {
|
2023-03-09 09:13:15 -08:00
|
|
|
IExecuteFunctions,
|
2020-11-10 09:28:03 -08:00
|
|
|
IDataObject,
|
|
|
|
ILoadOptionsFunctions,
|
|
|
|
INodeExecutionData,
|
|
|
|
INodePropertyOptions,
|
|
|
|
INodeType,
|
|
|
|
INodeTypeDescription,
|
|
|
|
} from 'n8n-workflow';
|
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
import { getresponseApiRequest, getResponseApiRequestAllItems } from './GenericFunctions';
|
2020-11-10 09:28:03 -08:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
import { contactFields, contactOperations } from './ContactDescription';
|
2020-11-10 09:28:03 -08:00
|
|
|
|
2022-04-08 14:32:08 -07:00
|
|
|
import moment from 'moment-timezone';
|
2020-11-10 09:28:03 -08:00
|
|
|
|
|
|
|
export class GetResponse implements INodeType {
|
|
|
|
description: INodeTypeDescription = {
|
|
|
|
displayName: 'GetResponse',
|
|
|
|
name: 'getResponse',
|
2022-06-20 07:54:01 -07:00
|
|
|
// eslint-disable-next-line n8n-nodes-base/node-class-description-icon-not-svg
|
2020-11-10 09:28:03 -08:00
|
|
|
icon: 'file:getResponse.png',
|
|
|
|
group: ['input'],
|
|
|
|
version: 1,
|
|
|
|
subtitle: '={{$parameter["operation"] + ": " + $parameter["resource"]}}',
|
2021-07-03 05:40:16 -07:00
|
|
|
description: 'Consume GetResponse API',
|
2020-11-10 09:28:03 -08:00
|
|
|
defaults: {
|
|
|
|
name: 'GetResponse',
|
|
|
|
},
|
|
|
|
inputs: ['main'],
|
|
|
|
outputs: ['main'],
|
|
|
|
credentials: [
|
|
|
|
{
|
|
|
|
name: 'getResponseApi',
|
|
|
|
required: true,
|
|
|
|
displayOptions: {
|
|
|
|
show: {
|
2022-08-17 08:50:24 -07:00
|
|
|
authentication: ['apiKey'],
|
2020-11-10 09:32:19 -08:00
|
|
|
},
|
2020-11-10 09:28:03 -08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'getResponseOAuth2Api',
|
|
|
|
required: true,
|
|
|
|
displayOptions: {
|
|
|
|
show: {
|
2022-08-17 08:50:24 -07:00
|
|
|
authentication: ['oAuth2'],
|
2020-11-10 09:28:03 -08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
properties: [
|
|
|
|
{
|
|
|
|
displayName: 'Authentication',
|
|
|
|
name: 'authentication',
|
|
|
|
type: 'options',
|
|
|
|
options: [
|
|
|
|
{
|
|
|
|
name: 'API Key',
|
|
|
|
value: 'apiKey',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'OAuth2',
|
|
|
|
value: 'oAuth2',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
default: 'apiKey',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
displayName: 'Resource',
|
|
|
|
name: 'resource',
|
|
|
|
type: 'options',
|
2022-05-20 14:47:24 -07:00
|
|
|
noDataExpression: true,
|
2020-11-10 09:28:03 -08:00
|
|
|
options: [
|
|
|
|
{
|
|
|
|
name: 'Contact',
|
|
|
|
value: 'contact',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
default: 'contact',
|
|
|
|
},
|
|
|
|
...contactOperations,
|
|
|
|
...contactFields,
|
|
|
|
],
|
|
|
|
};
|
2020-11-10 09:32:19 -08:00
|
|
|
|
2020-11-10 09:28:03 -08:00
|
|
|
methods = {
|
|
|
|
loadOptions: {
|
2023-04-19 07:00:49 -07:00
|
|
|
// Get all the campaigns to display them to user so that they can
|
2020-11-10 09:28:03 -08:00
|
|
|
// select them easily
|
2022-08-17 08:50:24 -07:00
|
|
|
async getCampaigns(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
|
2020-11-10 09:28:03 -08:00
|
|
|
const returnData: INodePropertyOptions[] = [];
|
2022-12-29 03:20:43 -08:00
|
|
|
const campaigns = await getresponseApiRequest.call(this, 'GET', '/campaigns');
|
2020-11-10 09:28:03 -08:00
|
|
|
for (const campaign of campaigns) {
|
|
|
|
returnData.push({
|
|
|
|
name: campaign.name as string,
|
|
|
|
value: campaign.campaignId,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return returnData;
|
|
|
|
},
|
2023-04-19 07:00:49 -07:00
|
|
|
// Get all the tagd to display them to user so that they can
|
2020-11-10 09:28:03 -08:00
|
|
|
// select them easily
|
2022-08-17 08:50:24 -07:00
|
|
|
async getTags(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
|
2020-11-10 09:28:03 -08:00
|
|
|
const returnData: INodePropertyOptions[] = [];
|
2022-12-29 03:20:43 -08:00
|
|
|
const tags = await getresponseApiRequest.call(this, 'GET', '/tags');
|
2020-11-10 09:28:03 -08:00
|
|
|
for (const tag of tags) {
|
|
|
|
returnData.push({
|
|
|
|
name: tag.name as string,
|
|
|
|
value: tag.tagId,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return returnData;
|
|
|
|
},
|
2023-04-19 07:00:49 -07:00
|
|
|
// Get all the custom fields to display them to user so that they can
|
2020-11-10 09:28:03 -08:00
|
|
|
// select them easily
|
2022-08-17 08:50:24 -07:00
|
|
|
async getCustomFields(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
|
2020-11-10 09:28:03 -08:00
|
|
|
const returnData: INodePropertyOptions[] = [];
|
2022-12-29 03:20:43 -08:00
|
|
|
const customFields = await getresponseApiRequest.call(this, 'GET', '/custom-fields');
|
2020-11-10 09:28:03 -08:00
|
|
|
for (const customField of customFields) {
|
|
|
|
returnData.push({
|
|
|
|
name: customField.name as string,
|
|
|
|
value: customField.customFieldId,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return returnData;
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
|
|
|
|
const items = this.getInputData();
|
2022-08-30 08:55:33 -07:00
|
|
|
const returnData: INodeExecutionData[] = [];
|
2022-04-22 09:29:51 -07:00
|
|
|
const length = items.length;
|
2020-11-10 09:28:03 -08:00
|
|
|
const qs: IDataObject = {};
|
|
|
|
let responseData;
|
2022-12-02 03:53:59 -08:00
|
|
|
const resource = this.getNodeParameter('resource', 0);
|
|
|
|
const operation = this.getNodeParameter('operation', 0);
|
2020-11-10 09:28:03 -08:00
|
|
|
for (let i = 0; i < length; i++) {
|
2021-07-19 23:58:54 -07:00
|
|
|
try {
|
|
|
|
if (resource === 'contact') {
|
|
|
|
//https://apireference.getresponse.com/#operation/createContact
|
|
|
|
if (operation === 'create') {
|
|
|
|
const email = this.getNodeParameter('email', i) as string;
|
|
|
|
|
|
|
|
const campaignId = this.getNodeParameter('campaignId', 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 body: IDataObject = {
|
|
|
|
email,
|
|
|
|
campaign: {
|
|
|
|
campaignId,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
Object.assign(body, additionalFields);
|
|
|
|
|
|
|
|
if (additionalFields.customFieldsUi) {
|
2022-08-17 08:50:24 -07:00
|
|
|
const customFieldValues = (additionalFields.customFieldsUi as IDataObject)
|
|
|
|
.customFieldValues as IDataObject[];
|
2021-07-19 23:58:54 -07:00
|
|
|
if (customFieldValues) {
|
|
|
|
body.customFieldValues = customFieldValues;
|
2022-12-02 12:54:28 -08:00
|
|
|
for (let index = 0; index < customFieldValues.length; index++) {
|
|
|
|
if (!Array.isArray(customFieldValues[index].value)) {
|
|
|
|
customFieldValues[index].value = [customFieldValues[index].value];
|
2021-07-19 23:58:54 -07:00
|
|
|
}
|
2020-11-10 09:28:03 -08:00
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
delete body.customFieldsUi;
|
2020-11-10 09:28:03 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
responseData = await getresponseApiRequest.call(this, 'POST', '/contacts', body);
|
2020-11-10 09:28:03 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
responseData = { success: true };
|
|
|
|
}
|
|
|
|
//https://apireference.getresponse.com/?_ga=2.160836350.2102802044.1604719933-1897033509.1604598019#operation/deleteContact
|
|
|
|
if (operation === 'delete') {
|
|
|
|
const contactId = this.getNodeParameter('contactId', i) as string;
|
2020-11-10 09:28:03 -08:00
|
|
|
|
2022-11-18 07:29:44 -08:00
|
|
|
const options = this.getNodeParameter('options', i);
|
2020-11-10 09:28:03 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
Object.assign(qs, options);
|
2020-11-10 09:28:03 -08:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
responseData = await getresponseApiRequest.call(
|
|
|
|
this,
|
|
|
|
'DELETE',
|
|
|
|
`/contacts/${contactId}`,
|
|
|
|
{},
|
|
|
|
qs,
|
|
|
|
);
|
2020-11-10 09:28:03 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
responseData = { success: true };
|
|
|
|
}
|
|
|
|
//https://apireference.getresponse.com/?_ga=2.160836350.2102802044.1604719933-1897033509.1604598019#operation/getContactById
|
|
|
|
if (operation === 'get') {
|
|
|
|
const contactId = this.getNodeParameter('contactId', i) as string;
|
2020-11-10 09:28:03 -08:00
|
|
|
|
2022-11-18 07:29:44 -08:00
|
|
|
const options = this.getNodeParameter('options', i);
|
2020-11-10 09:28:03 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
Object.assign(qs, options);
|
2020-11-10 09:28:03 -08:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
responseData = await getresponseApiRequest.call(
|
|
|
|
this,
|
|
|
|
'GET',
|
|
|
|
`/contacts/${contactId}`,
|
|
|
|
{},
|
|
|
|
qs,
|
|
|
|
);
|
2021-07-19 23:58:54 -07:00
|
|
|
}
|
|
|
|
//https://apireference.getresponse.com/?_ga=2.160836350.2102802044.1604719933-1897033509.1604598019#operation/getContactList
|
|
|
|
if (operation === 'getAll') {
|
2022-11-18 05:31:38 -08:00
|
|
|
const returnAll = this.getNodeParameter('returnAll', i);
|
2021-07-19 23:58:54 -07:00
|
|
|
|
2022-11-18 07:29:44 -08:00
|
|
|
const options = this.getNodeParameter('options', i);
|
2021-07-19 23:58:54 -07:00
|
|
|
|
|
|
|
const timezone = this.getTimezone();
|
|
|
|
|
|
|
|
Object.assign(qs, options);
|
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
const isNotQuery = ['sortBy', 'sortOrder', 'additionalFlags', 'fields', 'exactMatch'];
|
|
|
|
|
|
|
|
const isDate = ['createdOnFrom', 'createdOnTo', 'changeOnFrom', 'changeOnTo'];
|
|
|
|
|
|
|
|
const dateMapToKey: { [key: string]: string } = {
|
|
|
|
createdOnFrom: '[createdOn][from]',
|
|
|
|
createdOnTo: '[createdOn][to]',
|
|
|
|
changeOnFrom: '[changeOn][from]',
|
|
|
|
changeOnTo: '[changeOn][to]',
|
2021-07-19 23:58:54 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
for (const key of Object.keys(qs)) {
|
|
|
|
if (!isNotQuery.includes(key)) {
|
|
|
|
if (isDate.includes(key)) {
|
2022-08-17 08:50:24 -07:00
|
|
|
qs[`query${dateMapToKey[key]}`] = moment
|
|
|
|
.tz(qs[key], timezone)
|
|
|
|
.format('YYYY-MM-DDTHH:mm:ssZZ');
|
2021-07-19 23:58:54 -07:00
|
|
|
} else {
|
|
|
|
qs[`query[${key}]`] = qs[key];
|
|
|
|
}
|
|
|
|
delete qs[key];
|
2020-11-10 09:28:03 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (qs.sortBy) {
|
2023-01-19 04:37:19 -08:00
|
|
|
qs[`sort[${qs.sortBy}]`] = qs.sortOrder || 'ASC';
|
2021-07-19 23:58:54 -07:00
|
|
|
}
|
2020-11-10 09:28:03 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (qs.exactMatch === true) {
|
2022-12-02 12:54:28 -08:00
|
|
|
qs.additionalFlags = 'exactMatch';
|
2021-07-19 23:58:54 -07:00
|
|
|
delete qs.exactMatch;
|
|
|
|
}
|
2020-11-10 09:28:03 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (returnAll) {
|
2022-08-17 08:50:24 -07:00
|
|
|
responseData = await getResponseApiRequestAllItems.call(
|
|
|
|
this,
|
|
|
|
'GET',
|
2022-12-29 03:20:43 -08:00
|
|
|
'/contacts',
|
2022-08-17 08:50:24 -07:00
|
|
|
{},
|
|
|
|
qs,
|
|
|
|
);
|
2021-07-19 23:58:54 -07:00
|
|
|
} else {
|
2022-11-18 06:26:22 -08:00
|
|
|
qs.perPage = this.getNodeParameter('limit', i);
|
2022-12-29 03:20:43 -08:00
|
|
|
responseData = await getresponseApiRequest.call(this, 'GET', '/contacts', {}, qs);
|
2021-07-19 23:58:54 -07:00
|
|
|
}
|
2020-11-10 09:28:03 -08:00
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
//https://apireference.getresponse.com/?_ga=2.160836350.2102802044.1604719933-1897033509.1604598019#operation/updateContact
|
|
|
|
if (operation === 'update') {
|
|
|
|
const contactId = this.getNodeParameter('contactId', i) as string;
|
2020-11-10 09:28:03 -08:00
|
|
|
|
2022-11-18 07:29:44 -08:00
|
|
|
const updateFields = this.getNodeParameter('updateFields', i);
|
2020-11-10 09:28:03 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const body: IDataObject = {};
|
2020-11-10 09:28:03 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
Object.assign(body, updateFields);
|
2020-11-10 09:28:03 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (updateFields.customFieldsUi) {
|
2022-08-17 08:50:24 -07:00
|
|
|
const customFieldValues = (updateFields.customFieldsUi as IDataObject)
|
|
|
|
.customFieldValues as IDataObject[];
|
2021-07-19 23:58:54 -07:00
|
|
|
if (customFieldValues) {
|
|
|
|
body.customFieldValues = customFieldValues;
|
|
|
|
delete body.customFieldsUi;
|
|
|
|
}
|
2020-11-10 09:28:03 -08:00
|
|
|
}
|
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
responseData = await getresponseApiRequest.call(
|
|
|
|
this,
|
|
|
|
'POST',
|
|
|
|
`/contacts/${contactId}`,
|
|
|
|
body,
|
|
|
|
);
|
2021-07-19 23:58:54 -07:00
|
|
|
}
|
2020-11-10 09:28:03 -08:00
|
|
|
}
|
2022-08-30 08:55:33 -07:00
|
|
|
const executionData = this.helpers.constructExecutionMetaData(
|
2023-02-27 19:39:43 -08:00
|
|
|
this.helpers.returnJsonArray(responseData as IDataObject),
|
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 executionErrorData = this.helpers.constructExecutionMetaData(
|
|
|
|
this.helpers.returnJsonArray({ error: error.message }),
|
|
|
|
{ itemData: { item: i } },
|
|
|
|
);
|
|
|
|
returnData.push(...executionErrorData);
|
2021-07-19 23:58:54 -07:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
throw error;
|
2020-11-10 09:28:03 -08:00
|
|
|
}
|
|
|
|
}
|
2022-08-30 08:55:33 -07:00
|
|
|
return this.prepareOutputData(returnData);
|
2020-11-10 09:28:03 -08:00
|
|
|
}
|
|
|
|
}
|