n8n/packages/nodes-base/nodes/ActiveCampaign/ActiveCampaign.node.ts

706 lines
21 KiB
TypeScript
Raw Normal View History

2019-08-20 23:45:03 -07:00
import {
2019-10-25 07:56:15 -07:00
IExecuteFunctions,
2019-08-20 23:45:03 -07:00
} from 'n8n-core';
2019-10-29 11:51:09 -07:00
2019-08-20 23:45:03 -07:00
import {
IDataObject,
INodeTypeDescription,
INodeExecutionData,
INodeType,
} from 'n8n-workflow';
import {
activeCampaignApiRequest,
activeCampaignApiRequestAllItems,
2019-11-02 15:11:55 -07:00
IProduct,
2019-08-20 23:45:03 -07:00
} from './GenericFunctions';
2019-11-03 03:47:36 -08:00
import {
contactOperations,
contactFields
} from './ContactDescription';
import {
dealOperations,
dealFields
} from './DealDescription';
import {
ecomOrderOperations,
ecomOrderFields
} from './EcomOrderDescription';
2019-10-29 11:51:09 -07:00
2019-11-03 07:42:18 -08:00
import {
ecomCustomerOperations,
ecomCustomerFields
} from './EcomCustomerDescription';
2019-11-03 13:17:43 -08:00
import {
ecomOrderProductsOperations,
ecomOrderProductsFields
} from './EcomOrderProductsDescription';
import {
connectionOperations,
connectionFields
} from './ConnectionDescription';
2019-11-03 07:42:18 -08:00
2019-08-20 23:45:03 -07:00
interface CustomProperty {
name: string;
value: string;
}
/**
* Add the additional fields to the body
*
* @param {IDataObject} body The body object to add fields to
* @param {IDataObject} additionalFields The fields to add
*/
function addAdditionalFields(body: IDataObject, additionalFields: IDataObject) {
for (const key of Object.keys(additionalFields)) {
if (key === 'customProperties' && (additionalFields.customProperties as IDataObject).property !== undefined) {
for (const customProperty of (additionalFields.customProperties as IDataObject)!.property! as CustomProperty[]) {
body[customProperty.name] = customProperty.value;
}
} else {
body[key] = additionalFields[key];
}
}
}
export class ActiveCampaign implements INodeType {
description: INodeTypeDescription = {
displayName: 'ActiveCampaign',
name: 'activeCampaign',
icon: 'file:activeCampaign.png',
group: ['transform'],
version: 1,
subtitle: '={{$parameter["operation"] + ": " + $parameter["resource"]}}',
description: 'Create and edit data in ActiveCampaign',
defaults: {
name: 'ActiveCampaign',
color: '#356ae6',
},
inputs: ['main'],
outputs: ['main'],
credentials: [
{
name: 'activeCampaignApi',
required: true,
}
],
properties: [
2019-10-24 11:15:57 -07:00
// ----------------------------------
// resources
// ----------------------------------
2019-08-20 23:45:03 -07:00
{
displayName: 'Resource',
name: 'resource',
type: 'options',
options: [
{
name: 'Contact',
value: 'contact',
},
2019-10-24 11:15:57 -07:00
{
name: 'Deal',
value: 'deal',
2019-11-02 11:48:29 -07:00
},
2019-11-03 13:17:43 -08:00
{
name: 'Connection',
value: 'connection'
},
2019-11-02 11:48:29 -07:00
{
name: 'E-commerce Order',
value: 'ecommerceOrder',
2019-11-03 07:42:18 -08:00
},
{
name: 'E-Commerce Customer',
value: 'ecommerceCustomer',
2019-11-03 07:42:18 -08:00
},
{
name: 'E-commerce Order Products',
value: 'ecommerceOrderProducts'
}
2019-08-20 23:45:03 -07:00
],
default: 'contact',
description: 'The resource to operate on.',
},
// ----------------------------------
// operations
// ----------------------------------
2019-11-03 03:47:36 -08:00
...contactOperations,
...dealOperations,
2019-11-03 13:17:43 -08:00
...connectionOperations,
2019-11-03 03:47:36 -08:00
...ecomOrderOperations,
2019-11-03 07:42:18 -08:00
...ecomCustomerOperations,
...ecomOrderProductsOperations,
2019-08-20 23:45:03 -07:00
2019-11-03 13:17:43 -08:00
// ----------------------------------
// fields
// ----------------------------------
2019-08-20 23:45:03 -07:00
// ----------------------------------
// contact
// ----------------------------------
2019-11-03 03:47:36 -08:00
...contactFields,
2019-10-24 17:50:03 -07:00
2019-10-24 11:15:57 -07:00
// ----------------------------------
// deal
// ----------------------------------
2019-11-03 03:47:36 -08:00
...dealFields,
2019-11-02 11:48:29 -07:00
2019-11-03 13:17:43 -08:00
// ----------------------------------
// connection
// ----------------------------------
...connectionFields,
2019-11-02 11:48:29 -07:00
// ----------------------------------
// ecommerceOrder
// ----------------------------------
2019-11-03 03:47:36 -08:00
...ecomOrderFields,
2019-11-02 11:48:29 -07:00
2019-11-03 07:42:18 -08:00
// ----------------------------------
// ecommerceCustomer
// ----------------------------------
...ecomCustomerFields,
// ----------------------------------
// ecommerceOrderProducts
// ----------------------------------
...ecomOrderProductsFields,
2019-08-20 23:45:03 -07:00
],
};
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
const items = this.getInputData();
const returnData: IDataObject[] = [];
let resource: string;
let operation: string;
// For Post
let body: IDataObject;
// For Query string
let qs: IDataObject;
let requestMethod: string;
2019-09-11 12:56:45 -07:00
let endpoint: string;
2019-08-20 23:45:03 -07:00
let returnAll = false;
2019-09-11 12:56:45 -07:00
let dataKey: string | undefined;
2019-08-20 23:45:03 -07:00
for (let i = 0; i < items.length; i++) {
2019-09-11 12:56:45 -07:00
dataKey = undefined;
2019-08-20 23:45:03 -07:00
resource = this.getNodeParameter('resource', 0) as string;
operation = this.getNodeParameter('operation', 0) as string;
requestMethod = 'GET';
2019-09-11 12:56:45 -07:00
endpoint = '';
2019-08-20 23:45:03 -07:00
body = {} as IDataObject;
qs = {} as IDataObject;
if (resource === 'contact') {
if (operation === 'create') {
// ----------------------------------
// contact:create
// ----------------------------------
requestMethod = 'POST';
const updateIfExists = this.getNodeParameter('updateIfExists', i) as boolean;
if (updateIfExists === true) {
2019-09-11 12:56:45 -07:00
endpoint = '/api/3/contact/sync';
2019-08-20 23:45:03 -07:00
} else {
2019-09-11 12:56:45 -07:00
endpoint = '/api/3/contacts';
2019-08-20 23:45:03 -07:00
}
2019-09-11 12:56:45 -07:00
dataKey = 'contact';
2019-11-03 12:47:23 -08:00
2019-09-11 12:56:45 -07:00
body.contact = {
email: this.getNodeParameter('email', i) as string,
} as IDataObject;
2019-11-03 12:47:23 -08:00
2019-08-20 23:45:03 -07:00
const additionalFields = this.getNodeParameter('additionalFields', i) as IDataObject;
2019-09-11 12:56:45 -07:00
addAdditionalFields(body.contact as IDataObject, additionalFields);
2019-08-20 23:45:03 -07:00
} else if (operation === 'delete') {
// ----------------------------------
// contact:delete
// ----------------------------------
2019-09-11 12:56:45 -07:00
requestMethod = 'DELETE';
2019-08-20 23:45:03 -07:00
const contactId = this.getNodeParameter('contactId', i) as number;
2019-09-11 12:56:45 -07:00
endpoint = `/api/3/contacts/${contactId}`;
2019-08-20 23:45:03 -07:00
} else if (operation === 'get') {
// ----------------------------------
// contact:get
// ----------------------------------
requestMethod = 'GET';
const contactId = this.getNodeParameter('contactId', i) as number;
2019-09-11 12:56:45 -07:00
endpoint = `/api/3/contacts/${contactId}`;
2019-08-20 23:45:03 -07:00
2019-09-11 12:56:45 -07:00
} else if (operation === 'getAll') {
// ----------------------------------
2019-10-25 07:48:53 -07:00
// contacts:getAll
2019-09-11 12:56:45 -07:00
// ----------------------------------
2019-08-20 23:45:03 -07:00
2019-09-11 12:56:45 -07:00
requestMethod = 'GET';
2019-08-20 23:45:03 -07:00
2019-09-11 12:56:45 -07:00
returnAll = this.getNodeParameter('returnAll', i) as boolean;
if (returnAll === false) {
qs.limit = this.getNodeParameter('limit', i) as number;
}
2019-08-20 23:45:03 -07:00
2019-09-11 12:56:45 -07:00
dataKey = 'contacts';
endpoint = `/api/3/contacts`;
2019-08-20 23:45:03 -07:00
} else if (operation === 'update') {
// ----------------------------------
// contact:update
// ----------------------------------
2019-09-11 12:56:45 -07:00
requestMethod = 'PUT';
2019-08-20 23:45:03 -07:00
const contactId = this.getNodeParameter('contactId', i) as number;
2019-09-11 12:56:45 -07:00
endpoint = `/api/3/contacts/${contactId}`;
2019-08-20 23:45:03 -07:00
2019-09-11 12:56:45 -07:00
dataKey = 'contact';
2019-09-11 12:56:45 -07:00
body.contact = {} as IDataObject;
2019-08-20 23:45:03 -07:00
const updateFields = this.getNodeParameter('updateFields', i) as IDataObject;
2019-09-11 12:56:45 -07:00
addAdditionalFields(body.contact as IDataObject, updateFields);
2019-08-20 23:45:03 -07:00
} else {
2019-10-25 07:48:53 -07:00
throw new Error(`The operation "${operation}" is not known`);
2019-08-20 23:45:03 -07:00
}
2019-10-25 05:24:24 -07:00
} else if (resource === 'deal') {
if (operation === 'create') {
// ----------------------------------
// deal:create
// ----------------------------------
2019-08-20 23:45:03 -07:00
2019-10-25 05:24:24 -07:00
requestMethod = 'POST';
endpoint = '/api/3/deals';
body.deal = {
title: this.getNodeParameter('title', i) as string,
contact: this.getNodeParameter('contact', i) as string,
value: this.getNodeParameter('value', i) as number,
2019-11-03 12:47:23 -08:00
currency: this.getNodeParameter('currency', i) as string,
} as IDataObject;
2019-11-03 12:47:23 -08:00
const group = this.getNodeParameter('group', i) as string;
if (group !== '') {
2019-11-03 12:47:23 -08:00
addAdditionalFields(body.deal as IDataObject, { group });
}
2019-11-03 12:47:23 -08:00
const owner = this.getNodeParameter('owner', i) as string;
if (owner !== '') {
2019-11-03 12:47:23 -08:00
addAdditionalFields(body.deal as IDataObject, { owner });
}
2019-11-03 12:47:23 -08:00
const stage = this.getNodeParameter('stage', i) as string;
if (stage !== '') {
2019-11-03 12:47:23 -08:00
addAdditionalFields(body.deal as IDataObject, { stage });
}
const additionalFields = this.getNodeParameter('additionalFields', i) as IDataObject;
addAdditionalFields(body.deal as IDataObject, additionalFields);
} else if (operation === 'update') {
// ----------------------------------
// deal:update
// ----------------------------------
requestMethod = 'PUT';
const dealId = this.getNodeParameter('dealId', i) as number;
endpoint = `/api/3/deals/${dealId}`;
body.deal = {} as IDataObject;
const updateFields = this.getNodeParameter('updateFields', i) as IDataObject;
addAdditionalFields(body.deal as IDataObject, updateFields);
2019-10-25 05:24:24 -07:00
} else if (operation === 'delete') {
// ----------------------------------
// deal:delete
// ----------------------------------
requestMethod = 'DELETE';
const dealId = this.getNodeParameter('dealId', i) as number;
endpoint = `/api/3/deals/${dealId}`;
} else if (operation === 'get') {
// ----------------------------------
// deal:get
// ----------------------------------
requestMethod = 'GET';
const dealId = this.getNodeParameter('dealId', i) as number;
endpoint = `/api/3/deals/${dealId}`;
} else if (operation === 'getAll') {
// ----------------------------------
2019-10-25 07:48:53 -07:00
// deals:getAll
2019-10-25 05:24:24 -07:00
// ----------------------------------
2019-08-20 23:45:03 -07:00
2019-10-25 05:24:24 -07:00
requestMethod = 'GET';
returnAll = this.getNodeParameter('returnAll', i) as boolean;
if (returnAll === false) {
qs.limit = this.getNodeParameter('limit', i) as number;
}
endpoint = `/api/3/deals`;
2019-10-25 07:48:53 -07:00
} else if (operation === 'createNote') {
// ----------------------------------
// deal:createNote
// ----------------------------------
2019-11-03 12:47:23 -08:00
requestMethod = 'POST';
2019-10-25 07:48:53 -07:00
body.note = {
2019-10-25 09:18:33 -07:00
note: this.getNodeParameter('dealNote', i) as string,
2019-11-03 12:47:23 -08:00
} as IDataObject;
2019-10-25 07:48:53 -07:00
const dealId = this.getNodeParameter('dealId', i) as number;
endpoint = `/api/3/deals/${dealId}/notes`;
} else if (operation === 'updateNote') {
// ----------------------------------
// deal:updateNote
// ----------------------------------
2019-11-03 12:47:23 -08:00
requestMethod = 'PUT';
2019-10-25 07:48:53 -07:00
body.note = {
2019-10-25 09:18:33 -07:00
note: this.getNodeParameter('dealNote', i) as string,
2019-11-03 12:47:23 -08:00
} as IDataObject;
2019-10-25 07:48:53 -07:00
const dealId = this.getNodeParameter('dealId', i) as number;
const dealNoteId = this.getNodeParameter('dealNoteId', i) as number;
endpoint = `/api/3/deals/${dealId}/notes/${dealNoteId}`;
2019-11-02 15:11:55 -07:00
} else {
throw new Error(`The operation "${operation}" is not known`);
2019-11-03 13:17:43 -08:00
}
} else if (resource === 'connection') {
if (operation === 'create') {
// ----------------------------------
// connection:create
// ----------------------------------
requestMethod = 'POST';
endpoint = '/api/3/connections';
2019-11-03 13:17:43 -08:00
body.connection = {
service: this.getNodeParameter('service', i) as string,
externalid: this.getNodeParameter('externalid', i) as string,
name: this.getNodeParameter('name', i) as string,
logoUrl: this.getNodeParameter('logoUrl', i) as string,
linkUrl: this.getNodeParameter('linkUrl', i) as string,
2019-11-03 13:17:43 -08:00
} as IDataObject;
} else if (operation === 'update') {
// ----------------------------------
// connection:update
// ----------------------------------
requestMethod = 'PUT';
const connectionId = this.getNodeParameter('connectionId', i) as number;
endpoint = `/api/3/connections/${connectionId}`;
body.connection = {} as IDataObject;
const updateFields = this.getNodeParameter('updateFields', i) as IDataObject;
addAdditionalFields(body.connection as IDataObject, updateFields);
2019-11-03 13:17:43 -08:00
} else if (operation === 'delete') {
// ----------------------------------
// connection:delete
// ----------------------------------
requestMethod = 'DELETE';
const connectionId = this.getNodeParameter('connectionId', i) as number;
endpoint = `/api/3/connections/${connectionId}`;
} else if (operation === 'get') {
// ----------------------------------
// connection:get
// ----------------------------------
requestMethod = 'GET';
const connectionId = this.getNodeParameter('connectionId', i) as number;
endpoint = `/api/3/connections/${connectionId}`;
} else if (operation === 'getAll') {
// ----------------------------------
// connections:getAll
// ----------------------------------
requestMethod = 'GET';
returnAll = this.getNodeParameter('returnAll', i) as boolean;
if (returnAll === false) {
qs.limit = this.getNodeParameter('limit', i) as number;
}
endpoint = `/api/3/connections`;
} else {
throw new Error(`The operation "${operation}" is not known`);
2019-11-02 15:11:55 -07:00
}
} else if (resource === 'ecommerceOrder') {
if (operation === 'create') {
// ----------------------------------
// ecommerceOrder:create
// ----------------------------------
requestMethod = 'POST';
endpoint = '/api/3/ecomOrders';
body.ecomOrder = {
source: this.getNodeParameter('source', i) as string,
email: this.getNodeParameter('email', i) as string,
totalPrice: this.getNodeParameter('totalPrice', i) as number,
currency: this.getNodeParameter('currency', i).toString().toUpperCase() as string,
externalCreatedDate: this.getNodeParameter('externalCreatedDate', i) as string,
connectionid: this.getNodeParameter('connectionid', i) as number,
customerid: this.getNodeParameter('customerid', i) as number,
} as IDataObject;
const externalid = this.getNodeParameter('externalid', i) as string;
if (externalid !== '') {
addAdditionalFields(body.ecomOrder as IDataObject, { externalid });
}
const externalcheckoutid = this.getNodeParameter('externalcheckoutid', i) as string;
if (externalcheckoutid !== '') {
addAdditionalFields(body.ecomOrder as IDataObject, { externalcheckoutid });
}
const abandonedDate = this.getNodeParameter('abandonedDate', i) as string;
if (abandonedDate !== '') {
addAdditionalFields(body.ecomOrder as IDataObject, { abandonedDate });
}
const orderProducts = this.getNodeParameter('orderProducts', i) as unknown as IProduct[];
addAdditionalFields(body.ecomOrder as IDataObject, { orderProducts });
const additionalFields = this.getNodeParameter('additionalFields', i) as IDataObject;
addAdditionalFields(body.ecomOrder as IDataObject, additionalFields);
} else if (operation === 'update') {
// ----------------------------------
// ecommerceOrder:update
// ----------------------------------
requestMethod = 'PUT';
const orderId = this.getNodeParameter('orderId', i) as number;
endpoint = `/api/3/ecomOrders/${orderId}`;
body.ecomOrder = {} as IDataObject;
const updateFields = this.getNodeParameter('updateFields', i) as IDataObject;
addAdditionalFields(body.ecomOrder as IDataObject, updateFields);
} else if (operation === 'delete') {
// ----------------------------------
// ecommerceOrder:delete
// ----------------------------------
requestMethod = 'DELETE';
const orderId = this.getNodeParameter('orderId', i) as number;
endpoint = `/api/3/ecomOrders/${orderId}`;
} else if (operation === 'get') {
// ----------------------------------
// ecommerceOrder:get
// ----------------------------------
requestMethod = 'GET';
const orderId = this.getNodeParameter('orderId', i) as number;
endpoint = `/api/3/ecomOrders/${orderId}`;
} else if (operation === 'getAll') {
// ----------------------------------
// ecommerceOrders:getAll
// ----------------------------------
requestMethod = 'GET';
returnAll = this.getNodeParameter('returnAll', i) as boolean;
if (returnAll === false) {
qs.limit = this.getNodeParameter('limit', i) as number;
}
endpoint = `/api/3/ecomOrders`;
2019-10-25 05:24:24 -07:00
} else {
2019-10-25 07:48:53 -07:00
throw new Error(`The operation "${operation}" is not known`);
2019-10-25 05:24:24 -07:00
}
2019-11-03 07:42:18 -08:00
} else if (resource === 'ecommerceCustomer') {
if (operation === 'create') {
// ----------------------------------
// ecommerceCustomer:create
// ----------------------------------
requestMethod = 'POST';
endpoint = '/api/3/ecomCustomers';
body.ecomCustomer = {
connectionid: this.getNodeParameter('connectionid', i) as string,
externalid: this.getNodeParameter('externalid', i) as string,
email: this.getNodeParameter('email', i) as string,
} as IDataObject;
const additionalFields = this.getNodeParameter('additionalFields', i) as IDataObject;
if (additionalFields.acceptsMarketing !== undefined) {
2019-11-03 12:47:23 -08:00
if (additionalFields.acceptsMarketing === true) {
2019-11-03 07:42:18 -08:00
additionalFields.acceptsMarketing = '1';
} else {
additionalFields.acceptsMarketing = '0';
}
}
addAdditionalFields(body.ecomCustomer as IDataObject, additionalFields);
2019-11-03 07:42:18 -08:00
} else if (operation === 'update') {
// ----------------------------------
// ecommerceCustomer:update
// ----------------------------------
requestMethod = 'PUT';
const ecommerceCustomerId = this.getNodeParameter('ecommerceCustomerId', i) as number;
endpoint = `/api/3/ecomCustomers/${ecommerceCustomerId}`;
body.ecomCustomer = {} as IDataObject;
const updateFields = this.getNodeParameter('updateFields', i) as IDataObject;
2019-11-03 12:47:23 -08:00
if (updateFields.acceptsMarketing !== undefined) {
if (updateFields.acceptsMarketing === true) {
2019-11-03 07:42:18 -08:00
updateFields.acceptsMarketing = '1';
} else {
updateFields.acceptsMarketing = '0';
}
}
addAdditionalFields(body.ecomCustomer as IDataObject, updateFields);
} else if (operation === 'delete') {
// ----------------------------------
// ecommerceCustomer:delete
// ----------------------------------
requestMethod = 'DELETE';
const ecommerceCustomerId = this.getNodeParameter('ecommerceCustomerId', i) as number;
endpoint = `/api/3/ecomCustomers/${ecommerceCustomerId}`;
} else if (operation === 'get') {
// ----------------------------------
// ecommerceCustomer:get
// ----------------------------------
requestMethod = 'GET';
const ecommerceCustomerId = this.getNodeParameter('ecommerceCustomerId', i) as number;
endpoint = `/api/3/ecomCustomers/${ecommerceCustomerId}`;
} else if (operation === 'getAll') {
// ----------------------------------
// ecommerceCustomers:getAll
// ----------------------------------
requestMethod = 'GET';
returnAll = this.getNodeParameter('returnAll', i) as boolean;
if (returnAll === false) {
qs.limit = this.getNodeParameter('limit', i) as number;
}
endpoint = `/api/3/ecomCustomers`;
} else {
throw new Error(`The operation "${operation}" is not known`);
}
} else if (resource === 'ecommerceOrderProducts') {
if (operation === 'getByProductId') {
// ----------------------------------
// ecommerceOrderProducts:getByProductId
// ----------------------------------
requestMethod = 'GET';
const procuctId = this.getNodeParameter('procuctId', i) as number;
endpoint = `/api/3/ecomOrderProducts/${procuctId}`;
} else if (operation === 'getByOrderId') {
// ----------------------------------
// ecommerceOrderProducts:getByOrderId
// ----------------------------------
requestMethod = 'GET';
const orderId = this.getNodeParameter('orderId', i) as number;
endpoint = `/api/3/ecomOrders/${orderId}/orderProducts`;
} else if (operation === 'getAll') {
// ----------------------------------
// ecommerceOrderProductss:getAll
// ----------------------------------
requestMethod = 'GET';
returnAll = this.getNodeParameter('returnAll', i) as boolean;
if (returnAll === false) {
qs.limit = this.getNodeParameter('limit', i) as number;
}
endpoint = `/api/3/ecomOrderProducts`;
} else {
throw new Error(`The operation "${operation}" is not known`);
}
} else {
throw new Error(`The resource "${resource}" is not known!`);
}
2019-10-25 09:26:43 -07:00
let responseData;
if (returnAll === true) {
responseData = await activeCampaignApiRequestAllItems.call(this, requestMethod, endpoint, body, qs, dataKey);
} else {
responseData = await activeCampaignApiRequest.call(this, requestMethod, endpoint, body, qs, dataKey);
2019-08-20 23:45:03 -07:00
}
if (Array.isArray(responseData)) {
returnData.push.apply(returnData, responseData as IDataObject[]);
} else {
returnData.push(responseData as IDataObject);
}
2019-10-25 05:24:24 -07:00
}
return [this.helpers.returnJsonArray(returnData)];
2019-08-20 23:45:03 -07:00
}
}