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

1191 lines
34 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,
ILoadOptionsFunctions,
INodeExecutionData,
INodePropertyOptions,
INodeType,
INodeTypeDescription,
2019-08-20 23:45:03 -07:00
} 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 {
contactFields,
2019-11-03 03:47:36 -08:00
contactOperations,
} from './ContactDescription';
import {
dealFields,
2019-11-03 03:47:36 -08:00
dealOperations,
} from './DealDescription';
import {
ecomOrderFields,
2019-11-03 03:47:36 -08:00
ecomOrderOperations,
} from './EcomOrderDescription';
2019-10-29 11:51:09 -07:00
2019-11-03 07:42:18 -08:00
import {
ecomCustomerFields,
2019-11-03 07:42:18 -08:00
ecomCustomerOperations,
} from './EcomCustomerDescription';
2019-11-03 13:17:43 -08:00
import {
ecomOrderProductsFields,
2019-11-03 13:17:43 -08:00
ecomOrderProductsOperations,
} from './EcomOrderProductsDescription';
import {
connectionFields,
2019-11-03 13:17:43 -08:00
connectionOperations,
} from './ConnectionDescription';
2019-11-03 07:42:18 -08:00
import {
accountFields,
accountOperations
} from "./AccountDescription";
import {
tagFields,
tagOperations
} from "./TagDescription";
import {
accountContactFields,
accountContactOperations
} from "./AccountContactDescription";
import {
contactListFields,
contactListOperations,
} from "./ContactListDescription";
import {
contactTagFields,
contactTagOperations,
} from "./ContactTagDescription";
import {
listFields,
listOperations,
} from "./ListDescription";
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 if (key === 'fieldValues' && (additionalFields.fieldValues as IDataObject).property !== undefined) {
body.fieldValues = (additionalFields.fieldValues as IDataObject).property;
} else if (key === 'fields' && (additionalFields.fields as IDataObject).property !== undefined) {
body.fields = (additionalFields.fields as IDataObject).property;
2019-08-20 23:45:03 -07:00
} 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,
},
2019-08-20 23:45:03 -07:00
],
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: 'Account',
value: 'account',
},
{
name: 'Account Contact',
value: 'accountContact',
},
2019-08-20 23:45:03 -07:00
{
name: 'Contact',
value: 'contact',
},
{
name: 'Contact List',
value: 'contactList',
},
2019-10-24 11:15:57 -07:00
{
name: 'Contact Tag',
value: 'contactTag',
2019-11-02 11:48:29 -07:00
},
2019-11-03 13:17:43 -08:00
{
name: 'Connection',
2020-10-22 06:46:03 -07:00
value: 'connection',
},
{
name: 'Deal',
value: 'deal',
},
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',
2020-10-22 06:46:03 -07:00
value: 'ecommerceOrderProducts',
},
{
name: 'List',
value: 'list',
},
{
name: 'Tag',
value: 'tag',
},
2019-08-20 23:45:03 -07:00
],
default: 'contact',
description: 'The resource to operate on.',
},
// ----------------------------------
// operations
// ----------------------------------
...accountOperations,
2019-11-03 03:47:36 -08:00
...contactOperations,
...accountContactOperations,
...contactListOperations,
...contactTagOperations,
...listOperations,
...tagOperations,
2019-11-03 03:47:36 -08:00
...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
// ----------------------------------
// ----------------------------------
// tag
// ----------------------------------
...tagFields,
// ----------------------------------
// list
// ----------------------------------
...listFields,
// ----------------------------------
// ----------------------------------
// tag
// ----------------------------------
...contactTagFields,
// ----------------------------------
// Contact List
// ----------------------------------
...contactListFields,
// ----------------------------------
// account
// ----------------------------------
...accountFields,
// ----------------------------------
// account
// ----------------------------------
...accountContactFields,
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
],
};
methods = {
loadOptions: {
// Get all the available custom fields to display them to user so that he can
// select them easily
async getContactCustomFields(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
const returnData: INodePropertyOptions[] = [];
const { fields } = await activeCampaignApiRequest.call(this, 'GET', '/api/3/fields', {}, { limit: 100 });
for (const field of fields) {
const fieldName = field.title;
const fieldId = field.id;
returnData.push({
name: fieldName,
value: fieldId,
});
}
return returnData;
},
// Get all the available custom fields to display them to user so that he can
// select them easily
async getAccountCustomFields(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
const returnData: INodePropertyOptions[] = [];
const { accountCustomFieldMeta: fields } = await activeCampaignApiRequest.call(this, 'GET', '/api/3/accountCustomFieldMeta', {}, { limit: 100 });
for (const field of fields) {
const fieldName = field.fieldLabel;
const fieldId = field.id;
returnData.push({
name: fieldName,
value: fieldId,
});
}
return returnData;
},
// Get all the available tags to display them to user so that he can
// select them easily
async getTags(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
const returnData: INodePropertyOptions[] = [];
const { tags } = await activeCampaignApiRequest.call(this, 'GET', '/api/3/tags', {}, { limit: 100 });
for (const tag of tags) {
returnData.push({
name: tag.tag,
value: tag.id,
});
}
return returnData;
},
},
};
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;
const simple = this.getNodeParameter('simple', i, true) as boolean;
const additionalFields = this.getNodeParameter('additionalFields', i) as IDataObject;
2019-09-11 12:56:45 -07:00
if (returnAll === false) {
qs.limit = this.getNodeParameter('limit', i) as number;
}
2019-08-20 23:45:03 -07:00
Object.assign(qs, additionalFields);
if (qs.orderBy) {
qs[qs.orderBy as string] = true;
delete qs.orderBy;
}
if (simple === true) {
dataKey = 'contacts';
}
2019-09-11 12:56:45 -07:00
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 {
throw new Error(`The operation "${operation}" is not known`);
}
} else if (resource === 'account') {
if (operation === 'create') {
// ----------------------------------
// account:create
// ----------------------------------
requestMethod = 'POST';
endpoint = '/api/3/accounts';
dataKey = 'account';
body.account = {
name: this.getNodeParameter('name', i) as string,
} as IDataObject;
const additionalFields = this.getNodeParameter('additionalFields', i) as IDataObject;
addAdditionalFields(body.account as IDataObject, additionalFields);
} else if (operation === 'delete') {
// ----------------------------------
// account:delete
// ----------------------------------
requestMethod = 'DELETE';
const accountId = this.getNodeParameter('accountId', i) as number;
endpoint = `/api/3/accounts/${accountId}`;
} else if (operation === 'get') {
// ----------------------------------
// account:get
// ----------------------------------
requestMethod = 'GET';
const accountId = this.getNodeParameter('accountId', i) as number;
endpoint = `/api/3/accounts/${accountId}`;
} else if (operation === 'getAll') {
// ----------------------------------
// account:getAll
// ----------------------------------
requestMethod = 'GET';
const simple = this.getNodeParameter('simple', i, true) as boolean;
returnAll = this.getNodeParameter('returnAll', i) as boolean;
if (returnAll === false) {
qs.limit = this.getNodeParameter('limit', i) as number;
}
if (simple === true) {
dataKey = 'accounts';
}
endpoint = `/api/3/accounts`;
const filters = this.getNodeParameter('filters', i) as IDataObject;
Object.assign(qs, filters);
} else if (operation === 'update') {
// ----------------------------------
// account:update
// ----------------------------------
requestMethod = 'PUT';
const accountId = this.getNodeParameter('accountId', i) as number;
endpoint = `/api/3/accounts/${accountId}`;
dataKey = 'account';
body.account = {} as IDataObject;
const updateFields = this.getNodeParameter('updateFields', i) as IDataObject;
addAdditionalFields(body.account as IDataObject, updateFields);
} else {
throw new Error(`The operation "${operation}" is not known`);
}
} else if (resource === 'accountContact') {
if (operation === 'create') {
// ----------------------------------
// account:create
// ----------------------------------
requestMethod = 'POST';
endpoint = '/api/3/accountContacts';
dataKey = 'accountContact';
body.accountContact = {
contact: this.getNodeParameter('contact', i) as string,
account: this.getNodeParameter('account', i) as string,
} as IDataObject;
const additionalFields = this.getNodeParameter('additionalFields', i) as IDataObject;
addAdditionalFields(body.account as IDataObject, additionalFields);
} else if (operation === 'update') {
// ----------------------------------
// accountContact:update
// ----------------------------------
requestMethod = 'PUT';
const accountContactId = this.getNodeParameter('accountContactId', i) as number;
endpoint = `/api/3/accountContacts/${accountContactId}`;
dataKey = 'accountContact';
body.accountContact = {} as IDataObject;
const updateFields = this.getNodeParameter('updateFields', i) as IDataObject;
addAdditionalFields(body.accountContact as IDataObject, updateFields);
} else if (operation === 'delete') {
// ----------------------------------
// accountContact:delete
// ----------------------------------
requestMethod = 'DELETE';
const accountContactId = this.getNodeParameter('accountContactId', i) as number;
endpoint = `/api/3/accountContacts/${accountContactId}`;
} else {
throw new Error(`The operation "${operation}" is not known`);
}
} else if (resource === 'contactTag') {
if (operation === 'add') {
// ----------------------------------
// contactTag:add
// ----------------------------------
requestMethod = 'POST';
endpoint = '/api/3/contactTags';
dataKey = 'contactTag';
body.contactTag = {
contact: this.getNodeParameter('contactId', i) as string,
tag: this.getNodeParameter('tagId', i) as string,
} as IDataObject;
} else if (operation === 'remove') {
// ----------------------------------
// contactTag:remove
// ----------------------------------
requestMethod = 'DELETE';
const contactTagId = this.getNodeParameter('contactTagId', i) as number;
endpoint = `/api/3/contactTags/${contactTagId}`;
} else {
throw new Error(`The operation "${operation}" is not known`);
}
} else if (resource === 'contactList') {
if (operation === 'add') {
// ----------------------------------
// contactList:add
// ----------------------------------
requestMethod = 'POST';
endpoint = '/api/3/contactLists';
dataKey = 'contactTag';
body.contactList = {
list: this.getNodeParameter('listId', i) as string,
contact: this.getNodeParameter('contactId', i) as string,
status: 1,
} as IDataObject;
} else if (operation === 'remove') {
// ----------------------------------
// contactList:remove
// ----------------------------------
requestMethod = 'POST';
endpoint = '/api/3/contactLists';
body.contactList = {
list: this.getNodeParameter('listId', i) as string,
contact: this.getNodeParameter('contactId', i) as string,
status: 2,
} as IDataObject;
dataKey = 'contacts';
} else {
throw new Error(`The operation "${operation}" is not known`);
}
} else if (resource === 'list') {
if (operation === 'getAll') {
// ----------------------------------
// list:getAll
// ----------------------------------
requestMethod = 'GET';
returnAll = this.getNodeParameter('returnAll', i) as boolean;
const simple = this.getNodeParameter('simple', i, true) as boolean;
if (returnAll === false) {
qs.limit = this.getNodeParameter('limit', i) as number;
}
if (simple === true) {
dataKey = 'lists';
}
endpoint = `/api/3/lists`;
}
} else if (resource === 'tag') {
if (operation === 'create') {
// ----------------------------------
// tag:create
// ----------------------------------
requestMethod = 'POST';
endpoint = '/api/3/tags';
dataKey = 'tag';
body.tag = {
tag: this.getNodeParameter('name', i) as string,
tagType: this.getNodeParameter('tagType', i) as string,
} as IDataObject;
const additionalFields = this.getNodeParameter('additionalFields', i) as IDataObject;
addAdditionalFields(body.tag as IDataObject, additionalFields);
} else if (operation === 'delete') {
// ----------------------------------
// tag:delete
// ----------------------------------
requestMethod = 'DELETE';
const tagId = this.getNodeParameter('tagId', i) as number;
endpoint = `/api/3/tags/${tagId}`;
} else if (operation === 'get') {
// ----------------------------------
// tag:get
// ----------------------------------
requestMethod = 'GET';
const tagId = this.getNodeParameter('tagId', i) as number;
endpoint = `/api/3/tags/${tagId}`;
} else if (operation === 'getAll') {
// ----------------------------------
// tags:getAll
// ----------------------------------
requestMethod = 'GET';
const simple = this.getNodeParameter('simple', i, true) as boolean;
returnAll = this.getNodeParameter('returnAll', i) as boolean;
if (returnAll === false) {
qs.limit = this.getNodeParameter('limit', i) as number;
}
if (simple === true) {
dataKey = 'tags';
}
endpoint = `/api/3/tags`;
} else if (operation === 'update') {
// ----------------------------------
// tags:update
// ----------------------------------
requestMethod = 'PUT';
const tagId = this.getNodeParameter('tagId', i) as number;
endpoint = `/api/3/tags/${tagId}`;
dataKey = 'tag';
body.tag = {} as IDataObject;
const updateFields = this.getNodeParameter('updateFields', i) as IDataObject;
addAdditionalFields(body.tag as IDataObject, updateFields);
} 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';
const simple = this.getNodeParameter('simple', i, true) as boolean;
2019-10-25 05:24:24 -07:00
returnAll = this.getNodeParameter('returnAll', i) as boolean;
if (returnAll === false) {
qs.limit = this.getNodeParameter('limit', i) as number;
}
if (simple === true) {
dataKey = 'deals';
}
2019-10-25 05:24:24 -07:00
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';
const simple = this.getNodeParameter('simple', i, true) as boolean;
2019-11-03 13:17:43 -08:00
returnAll = this.getNodeParameter('returnAll', i) as boolean;
if (returnAll === false) {
qs.limit = this.getNodeParameter('limit', i) as number;
}
if (simple === true) {
dataKey = 'connections';
}
2019-11-03 13:17:43 -08:00
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,
2019-11-02 15:11:55 -07:00
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';
const simple = this.getNodeParameter('simple', i, true) as boolean;
2019-11-02 15:11:55 -07:00
returnAll = this.getNodeParameter('returnAll', i) as boolean;
if (returnAll === false) {
qs.limit = this.getNodeParameter('limit', i) as number;
}
if (simple === true) {
dataKey = 'ecomOrders';
}
2019-11-02 15:11:55 -07:00
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';
const simple = this.getNodeParameter('simple', i, true) as boolean;
2019-11-03 07:42:18 -08:00
returnAll = this.getNodeParameter('returnAll', i) as boolean;
if (returnAll === false) {
qs.limit = this.getNodeParameter('limit', i) as number;
}
if (simple === true) {
dataKey = 'ecomCustomers';
}
2019-11-03 07:42:18 -08:00
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';
//dataKey = 'ecomOrderProducts';
const orderId = this.getNodeParameter('orderId', i) as number;
endpoint = `/api/3/ecomOrders/${orderId}/orderProducts`;
} else if (operation === 'getAll') {
// ----------------------------------
// ecommerceOrderProductss:getAll
// ----------------------------------
requestMethod = 'GET';
const simple = this.getNodeParameter('simple', i, true) as boolean;
returnAll = this.getNodeParameter('returnAll', i) as boolean;
if (returnAll === false) {
qs.limit = this.getNodeParameter('limit', i) as number;
}
if (simple === true) {
dataKey = 'ecomOrderProducts';
}
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 (resource === 'contactList' && operation === 'add' && responseData === undefined) {
responseData = { success: true };
}
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
}
}