2023-01-27 03:22:44 -08:00
|
|
|
import type {
|
2023-03-09 09:13:15 -08:00
|
|
|
IExecuteFunctions,
|
2021-03-11 04:36:55 -08:00
|
|
|
IDataObject,
|
|
|
|
ILoadOptionsFunctions,
|
|
|
|
INodeExecutionData,
|
|
|
|
INodePropertyOptions,
|
|
|
|
INodeType,
|
|
|
|
INodeTypeDescription,
|
|
|
|
} from 'n8n-workflow';
|
|
|
|
|
2022-08-01 13:47:55 -07:00
|
|
|
import { autopilotApiRequest, autopilotApiRequestAllItems } from './GenericFunctions';
|
2021-03-11 04:36:55 -08:00
|
|
|
|
2022-08-01 13:47:55 -07:00
|
|
|
import { contactFields, contactOperations } from './ContactDescription';
|
2021-03-11 04:36:55 -08:00
|
|
|
|
2022-08-01 13:47:55 -07:00
|
|
|
import { contactJourneyFields, contactJourneyOperations } from './ContactJourneyDescription';
|
2021-03-11 04:36:55 -08:00
|
|
|
|
2022-08-01 13:47:55 -07:00
|
|
|
import { contactListFields, contactListOperations } from './ContactListDescription';
|
2021-03-11 04:36:55 -08:00
|
|
|
|
2022-08-01 13:47:55 -07:00
|
|
|
import { listFields, listOperations } from './ListDescription';
|
2021-03-11 04:36:55 -08:00
|
|
|
|
|
|
|
export class Autopilot implements INodeType {
|
|
|
|
description: INodeTypeDescription = {
|
|
|
|
displayName: 'Autopilot',
|
|
|
|
name: 'autopilot',
|
|
|
|
icon: 'file:autopilot.svg',
|
|
|
|
group: ['input'],
|
|
|
|
version: 1,
|
|
|
|
subtitle: '={{$parameter["operation"] + ": " + $parameter["resource"]}}',
|
|
|
|
description: 'Consume Autopilot API',
|
|
|
|
defaults: {
|
|
|
|
name: 'Autopilot',
|
|
|
|
},
|
|
|
|
inputs: ['main'],
|
|
|
|
outputs: ['main'],
|
|
|
|
credentials: [
|
|
|
|
{
|
|
|
|
name: 'autopilotApi',
|
|
|
|
required: true,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
properties: [
|
|
|
|
{
|
|
|
|
displayName: 'Resource',
|
|
|
|
name: 'resource',
|
|
|
|
type: 'options',
|
2022-05-20 14:47:24 -07:00
|
|
|
noDataExpression: true,
|
2021-03-11 04:36:55 -08:00
|
|
|
options: [
|
|
|
|
{
|
|
|
|
name: 'Contact',
|
|
|
|
value: 'contact',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'Contact Journey',
|
|
|
|
value: 'contactJourney',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'Contact List',
|
|
|
|
value: 'contactList',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'List',
|
|
|
|
value: 'list',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
default: 'contact',
|
|
|
|
},
|
|
|
|
|
|
|
|
...contactOperations,
|
|
|
|
...contactFields,
|
|
|
|
...contactJourneyOperations,
|
|
|
|
...contactJourneyFields,
|
|
|
|
...contactListOperations,
|
|
|
|
...contactListFields,
|
|
|
|
...listOperations,
|
|
|
|
...listFields,
|
|
|
|
],
|
|
|
|
};
|
|
|
|
|
|
|
|
methods = {
|
|
|
|
loadOptions: {
|
2022-08-01 13:47:55 -07:00
|
|
|
async getCustomFields(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
|
2021-03-11 04:36:55 -08:00
|
|
|
const returnData: INodePropertyOptions[] = [];
|
2022-08-01 13:47:55 -07:00
|
|
|
const customFields = await autopilotApiRequest.call(this, 'GET', '/contacts/custom_fields');
|
2021-03-11 04:36:55 -08:00
|
|
|
for (const customField of customFields) {
|
|
|
|
returnData.push({
|
|
|
|
name: customField.name,
|
|
|
|
value: `${customField.name}-${customField.fieldType}`,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return returnData;
|
|
|
|
},
|
2022-08-01 13:47:55 -07:00
|
|
|
async getLists(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
|
2021-03-11 04:36:55 -08:00
|
|
|
const returnData: INodePropertyOptions[] = [];
|
2022-08-01 13:47:55 -07:00
|
|
|
const { lists } = await autopilotApiRequest.call(this, 'GET', '/lists');
|
2021-03-11 04:36:55 -08:00
|
|
|
for (const list of lists) {
|
|
|
|
returnData.push({
|
|
|
|
name: list.title,
|
|
|
|
value: list.list_id,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return returnData;
|
|
|
|
},
|
2022-08-01 13:47:55 -07:00
|
|
|
async getTriggers(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
|
2021-03-11 04:36:55 -08:00
|
|
|
const returnData: INodePropertyOptions[] = [];
|
2022-08-01 13:47:55 -07:00
|
|
|
const { triggers } = await autopilotApiRequest.call(this, 'GET', '/triggers');
|
2021-03-11 04:36:55 -08:00
|
|
|
for (const trigger of triggers) {
|
|
|
|
returnData.push({
|
|
|
|
name: trigger.journey,
|
|
|
|
value: trigger.trigger_id,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return returnData;
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
|
|
|
|
const items = this.getInputData();
|
|
|
|
const returnData: IDataObject[] = [];
|
2022-04-22 09:29:51 -07:00
|
|
|
const length = items.length;
|
2021-03-11 04:36:55 -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);
|
2021-03-11 04:36:55 -08:00
|
|
|
for (let i = 0; i < length; i++) {
|
|
|
|
try {
|
|
|
|
if (resource === 'contact') {
|
|
|
|
if (operation === 'upsert') {
|
|
|
|
const email = this.getNodeParameter('email', i) as string;
|
|
|
|
|
2022-11-18 07:29:44 -08:00
|
|
|
const additionalFields = this.getNodeParameter('additionalFields', i);
|
2021-03-11 04:36:55 -08:00
|
|
|
|
|
|
|
const body: IDataObject = {
|
|
|
|
Email: email,
|
|
|
|
};
|
|
|
|
|
|
|
|
Object.assign(body, additionalFields);
|
|
|
|
|
|
|
|
if (body.customFieldsUi) {
|
2022-08-01 13:47:55 -07:00
|
|
|
const customFieldsValues = (body.customFieldsUi as IDataObject)
|
|
|
|
.customFieldsValues as IDataObject[];
|
2021-03-11 04:36:55 -08:00
|
|
|
|
|
|
|
body.custom = {};
|
|
|
|
|
|
|
|
for (const customField of customFieldsValues) {
|
|
|
|
const [name, fieldType] = (customField.key as string).split('-');
|
|
|
|
|
|
|
|
const fieldName = name.replace(/\s/g, '--');
|
|
|
|
|
|
|
|
//@ts-ignore
|
|
|
|
body.custom[`${fieldType}--${fieldName}`] = customField.value;
|
|
|
|
}
|
|
|
|
delete body.customFieldsUi;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (body.autopilotList) {
|
|
|
|
body._autopilot_list = body.autopilotList;
|
|
|
|
delete body.autopilotList;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (body.autopilotSessionId) {
|
|
|
|
body._autopilot_session_id = body.autopilotSessionId;
|
|
|
|
delete body.autopilotSessionId;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (body.newEmail) {
|
|
|
|
body._NewEmail = body.newEmail;
|
|
|
|
delete body.newEmail;
|
|
|
|
}
|
|
|
|
|
2022-12-29 03:20:43 -08:00
|
|
|
responseData = await autopilotApiRequest.call(this, 'POST', '/contact', {
|
2022-08-01 13:47:55 -07:00
|
|
|
contact: body,
|
|
|
|
});
|
2021-03-11 04:36:55 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (operation === 'delete') {
|
|
|
|
const contactId = this.getNodeParameter('contactId', i) as string;
|
|
|
|
|
2022-08-01 13:47:55 -07:00
|
|
|
responseData = await autopilotApiRequest.call(this, 'DELETE', `/contact/${contactId}`);
|
2021-03-11 04:36:55 -08:00
|
|
|
|
|
|
|
responseData = { success: true };
|
|
|
|
}
|
|
|
|
|
|
|
|
if (operation === 'get') {
|
|
|
|
const contactId = this.getNodeParameter('contactId', i) as string;
|
|
|
|
|
2022-08-01 13:47:55 -07:00
|
|
|
responseData = await autopilotApiRequest.call(this, 'GET', `/contact/${contactId}`);
|
2021-03-11 04:36:55 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (operation === 'getAll') {
|
2022-11-18 05:31:38 -08:00
|
|
|
const returnAll = this.getNodeParameter('returnAll', i);
|
2021-03-11 04:36:55 -08:00
|
|
|
|
2022-12-02 12:54:28 -08:00
|
|
|
if (!returnAll) {
|
2022-11-18 06:26:22 -08:00
|
|
|
qs.limit = this.getNodeParameter('limit', i);
|
2021-03-11 04:36:55 -08:00
|
|
|
}
|
|
|
|
responseData = await autopilotApiRequestAllItems.call(
|
|
|
|
this,
|
|
|
|
'contacts',
|
|
|
|
'GET',
|
2022-12-29 03:20:43 -08:00
|
|
|
'/contacts',
|
2021-03-11 04:36:55 -08:00
|
|
|
{},
|
|
|
|
qs,
|
|
|
|
);
|
|
|
|
|
2022-12-02 12:54:28 -08:00
|
|
|
if (!returnAll) {
|
2021-03-11 04:36:55 -08:00
|
|
|
responseData = responseData.splice(0, qs.limit);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (resource === 'contactJourney') {
|
|
|
|
if (operation === 'add') {
|
|
|
|
const triggerId = this.getNodeParameter('triggerId', i) as string;
|
|
|
|
|
|
|
|
const contactId = this.getNodeParameter('contactId', i) as string;
|
|
|
|
|
|
|
|
responseData = await autopilotApiRequest.call(
|
|
|
|
this,
|
|
|
|
'POST',
|
|
|
|
`/trigger/${triggerId}/contact/${contactId}`,
|
|
|
|
);
|
|
|
|
|
|
|
|
responseData = { success: true };
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (resource === 'contactList') {
|
|
|
|
if (['add', 'remove', 'exist'].includes(operation)) {
|
|
|
|
const listId = this.getNodeParameter('listId', i) as string;
|
|
|
|
|
|
|
|
const contactId = this.getNodeParameter('contactId', i) as string;
|
|
|
|
|
|
|
|
const method: { [key: string]: string } = {
|
2022-08-01 13:47:55 -07:00
|
|
|
add: 'POST',
|
|
|
|
remove: 'DELETE',
|
|
|
|
exist: 'GET',
|
2021-03-11 04:36:55 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
const endpoint = `/list/${listId}/contact/${contactId}`;
|
|
|
|
|
|
|
|
if (operation === 'exist') {
|
|
|
|
try {
|
|
|
|
await autopilotApiRequest.call(this, method[operation], endpoint);
|
|
|
|
responseData = { exist: true };
|
|
|
|
} catch (error) {
|
|
|
|
responseData = { exist: false };
|
|
|
|
}
|
|
|
|
} else if (operation === 'add' || operation === 'remove') {
|
|
|
|
responseData = await autopilotApiRequest.call(this, method[operation], endpoint);
|
2022-12-02 12:54:28 -08:00
|
|
|
responseData.success = true;
|
2021-03-11 04:36:55 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (operation === 'getAll') {
|
2022-11-18 05:31:38 -08:00
|
|
|
const returnAll = this.getNodeParameter('returnAll', i);
|
2021-03-11 04:36:55 -08:00
|
|
|
|
|
|
|
const listId = this.getNodeParameter('listId', i) as string;
|
|
|
|
|
2022-12-02 12:54:28 -08:00
|
|
|
if (!returnAll) {
|
2022-11-18 06:26:22 -08:00
|
|
|
qs.limit = this.getNodeParameter('limit', i);
|
2021-03-11 04:36:55 -08:00
|
|
|
}
|
|
|
|
responseData = await autopilotApiRequestAllItems.call(
|
|
|
|
this,
|
|
|
|
'contacts',
|
|
|
|
'GET',
|
|
|
|
`/list/${listId}/contacts`,
|
|
|
|
{},
|
|
|
|
qs,
|
|
|
|
);
|
|
|
|
|
2022-12-02 12:54:28 -08:00
|
|
|
if (!returnAll) {
|
2021-03-11 04:36:55 -08:00
|
|
|
responseData = responseData.splice(0, qs.limit);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (resource === 'list') {
|
|
|
|
if (operation === 'create') {
|
|
|
|
const name = this.getNodeParameter('name', i) as string;
|
|
|
|
|
|
|
|
const body: IDataObject = {
|
|
|
|
name,
|
|
|
|
};
|
|
|
|
|
2022-12-29 03:20:43 -08:00
|
|
|
responseData = await autopilotApiRequest.call(this, 'POST', '/list', body);
|
2021-03-11 04:36:55 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (operation === 'getAll') {
|
2022-11-18 05:31:38 -08:00
|
|
|
const returnAll = this.getNodeParameter('returnAll', i);
|
2021-03-11 04:36:55 -08:00
|
|
|
|
2022-12-02 12:54:28 -08:00
|
|
|
if (!returnAll) {
|
2022-11-18 06:26:22 -08:00
|
|
|
qs.limit = this.getNodeParameter('limit', i);
|
2021-03-11 04:36:55 -08:00
|
|
|
}
|
2022-08-01 13:47:55 -07:00
|
|
|
responseData = await autopilotApiRequest.call(this, 'GET', '/lists');
|
2021-03-11 04:36:55 -08:00
|
|
|
|
|
|
|
responseData = responseData.lists;
|
|
|
|
|
2022-12-02 12:54:28 -08:00
|
|
|
if (!returnAll) {
|
2021-03-11 04:36:55 -08:00
|
|
|
responseData = responseData.splice(0, qs.limit);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-03-11 04:36:55 -08:00
|
|
|
} catch (error) {
|
|
|
|
if (this.continueOnFail()) {
|
2022-08-30 08:55:33 -07:00
|
|
|
const exectionErrorWithMetaData = this.helpers.constructExecutionMetaData(
|
|
|
|
[{ json: { error: error.message } }],
|
|
|
|
{ itemData: { item: i } },
|
|
|
|
);
|
|
|
|
responseData.push(...exectionErrorWithMetaData);
|
2021-03-11 04:36:55 -08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
}
|
2022-08-30 08:55:33 -07:00
|
|
|
return [returnData as INodeExecutionData[]];
|
2021-03-11 04:36:55 -08:00
|
|
|
}
|
|
|
|
}
|