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

576 lines
16 KiB
TypeScript
Raw Normal View History

2020-04-30 05:03:36 -07:00
import { IExecuteFunctions } from 'n8n-core';
import {
2020-05-06 01:27:01 -07:00
IDataObject,
2020-04-30 05:03:36 -07:00
INodeExecutionData,
INodeType,
2020-05-06 01:27:01 -07:00
INodeTypeDescription
2020-04-30 05:03:36 -07:00
} from 'n8n-workflow';
import {
2020-05-06 01:27:01 -07:00
contactFields,
contactOperations
2020-04-30 05:03:36 -07:00
} from './ContactDescription';
2020-05-05 04:59:45 -07:00
import {
2020-05-06 01:27:01 -07:00
companyFields,
companyOperations
2020-05-05 04:59:45 -07:00
} from './CompanyDescription';
2020-05-05 07:03:53 -07:00
import {
2020-05-06 01:27:01 -07:00
dealFields,
dealOperations
2020-05-05 07:03:53 -07:00
} from './DealDescription';
2020-05-06 01:27:01 -07:00
import { IContact, IContactUpdate } from './ContactInterface';
2020-05-11 11:22:31 -07:00
import { agileCrmApiRequest, agileCrmApiRequestUpdate, validateJSON } from './GenericFunctions';
import { IDeal } from './DealInterface';
2020-04-30 05:03:36 -07:00
export class AgileCrm implements INodeType {
description: INodeTypeDescription = {
displayName: 'AgileCRM',
2020-05-06 01:27:01 -07:00
name: 'agileCrm',
icon: 'file:agilecrm.png',
2020-04-30 05:03:36 -07:00
group: ['transform'],
version: 1,
description: 'Consume AgileCRM API',
defaults: {
name: 'AgileCRM',
color: '#772244',
},
inputs: ['main'],
2020-05-06 01:27:01 -07:00
outputs: ['main'],
credentials: [
2020-04-30 05:03:36 -07:00
{
name: 'agileCrmApi',
required: true,
}
],
properties: [
// Node properties which the user gets displayed and
// can change on the node.
{
displayName: 'Resource',
name: 'resource',
2020-05-06 01:27:01 -07:00
type: 'options',
options: [
2020-05-05 07:03:53 -07:00
{
2020-05-06 01:27:01 -07:00
name: 'Company',
value: 'company'
2020-05-05 07:03:53 -07:00
},
2020-05-06 01:27:01 -07:00
{
name: 'Contact',
value: 'contact'
2020-05-05 04:59:45 -07:00
},
{
2020-05-06 01:27:01 -07:00
name: 'Deal',
value: 'deal'
},
],
2020-04-30 05:03:36 -07:00
default: 'contact',
description: 'Resource to consume.',
},
// CONTACT
...contactOperations,
...contactFields,
2020-05-05 04:59:45 -07:00
// COMPANY
...companyOperations,
2020-05-05 07:03:53 -07:00
...companyFields,
// DEAL
...dealOperations,
...dealFields
2020-04-30 05:03:36 -07:00
],
};
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
const items = this.getInputData();
const returnData: IDataObject[] = [];
let responseData;
const resource = this.getNodeParameter('resource', 0) as string;
const operation = this.getNodeParameter('operation', 0) as string;
2020-05-11 11:22:31 -07:00
for (let i = 0; i < items.length; i++) {
2020-04-30 05:03:36 -07:00
2020-05-11 11:22:31 -07:00
if (resource === 'contact' || resource === 'company') {
2020-05-06 01:27:01 -07:00
const idGetter = resource === 'contact' ? 'contactId' : 'companyId';
2020-04-30 05:03:36 -07:00
2020-05-11 11:22:31 -07:00
if (operation === 'get') {
2020-05-05 04:59:45 -07:00
const contactId = this.getNodeParameter(idGetter, i) as string;
2020-04-30 05:03:36 -07:00
const endpoint = `api/contacts/${contactId}`;
2020-05-02 04:59:17 -07:00
responseData = await agileCrmApiRequest.call(this, 'GET', endpoint, {});
2020-04-30 05:03:36 -07:00
2020-05-11 11:22:31 -07:00
} else if (operation === 'delete') {
2020-05-05 04:59:45 -07:00
const contactId = this.getNodeParameter(idGetter, i) as string;
2020-05-04 06:14:22 -07:00
const endpoint = `api/contacts/${contactId}`;
responseData = await agileCrmApiRequest.call(this, 'DELETE', endpoint, {});
2020-05-11 11:22:31 -07:00
} else if (operation === 'getAll') {
2020-04-30 05:03:36 -07:00
const returnAll = this.getNodeParameter('returnAll', i) as boolean;
2020-05-11 11:22:31 -07:00
if (resource === 'contact') {
2020-05-05 04:59:45 -07:00
if (returnAll) {
2020-05-06 01:27:01 -07:00
const endpoint = 'api/contacts';
2020-05-05 04:59:45 -07:00
responseData = await agileCrmApiRequest.call(this, 'GET', endpoint, {});
} else {
const limit = this.getNodeParameter('limit', i) as number;
const endpoint = `api/contacts?page_size=${limit}`;
responseData = await agileCrmApiRequest.call(this, 'GET', endpoint, {});
}
2020-04-30 05:03:36 -07:00
} else {
2020-05-05 04:59:45 -07:00
if (returnAll) {
2020-05-06 01:27:01 -07:00
const endpoint = 'api/contacts/companies/list';
2020-05-05 04:59:45 -07:00
responseData = await agileCrmApiRequest.call(this, 'POST', endpoint, {});
} else {
const limit = this.getNodeParameter('limit', i) as number;
const endpoint = `api/contacts/companies/list?page_size=${limit}`;
responseData = await agileCrmApiRequest.call(this, 'POST', endpoint, {});
}
2020-04-30 05:03:36 -07:00
}
2020-05-05 04:59:45 -07:00
2020-05-11 11:22:31 -07:00
} else if (operation === 'create') {
2020-04-30 05:03:36 -07:00
const jsonParameters = this.getNodeParameter('jsonParameters', i) as boolean;
const body: IContact = {};
2020-05-11 11:22:31 -07:00
const properties: IDataObject[] = [];
2020-04-30 05:03:36 -07:00
if (jsonParameters) {
const additionalFieldsJson = this.getNodeParameter('additionalFieldsJson', i) as string;
2020-05-11 11:22:31 -07:00
if (additionalFieldsJson !== '') {
2020-04-30 05:03:36 -07:00
if (validateJSON(additionalFieldsJson) !== undefined) {
Object.assign(body, JSON.parse(additionalFieldsJson));
} else {
throw new Error('Additional fields must be a valid JSON');
}
}
} else {
const additionalFields = this.getNodeParameter('additionalFields', i) as IDataObject;
2020-05-05 04:59:45 -07:00
// if company, add 'company' as type. default is person
2020-05-11 11:22:31 -07:00
if (resource === 'company') {
2020-05-05 04:59:45 -07:00
body.type = 'COMPANY';
}
2020-04-30 05:03:36 -07:00
if (additionalFields.starValue) {
body.star_value = additionalFields.starValue as string;
}
if (additionalFields.tags) {
body.tags = additionalFields.tags as string[];
}
2020-05-05 04:59:45 -07:00
// Contact specific properties
2020-05-11 11:22:31 -07:00
if (resource === 'contact') {
if (additionalFields.firstName) {
2020-05-05 04:59:45 -07:00
properties.push({
type: 'SYSTEM',
name: 'first_name',
value: additionalFields.firstName as string
} as IDataObject);
}
2020-05-11 11:22:31 -07:00
if (additionalFields.lastName) {
2020-05-05 04:59:45 -07:00
properties.push({
type: 'SYSTEM',
name: 'last_name',
value: additionalFields.lastName as string
} as IDataObject);
}
2020-05-11 11:22:31 -07:00
if (additionalFields.company) {
2020-05-05 04:59:45 -07:00
properties.push({
type: 'SYSTEM',
name: 'company',
value: additionalFields.company as string
} as IDataObject);
}
2020-05-11 11:22:31 -07:00
if (additionalFields.title) {
2020-05-05 04:59:45 -07:00
properties.push({
type: 'SYSTEM',
name: 'title',
value: additionalFields.title as string
} as IDataObject);
}
2020-05-11 11:22:31 -07:00
if (additionalFields.emailOptions) {
2020-05-05 04:59:45 -07:00
//@ts-ignore
additionalFields.emailOptions.emailProperties.map(property => {
properties.push({
type: 'SYSTEM',
subtype: property.subtype as string,
name: 'email',
value: property.email as string
} as IDataObject);
2020-05-06 01:27:01 -07:00
});
2020-05-05 04:59:45 -07:00
}
2020-05-11 11:22:31 -07:00
if (additionalFields.addressOptions) {
2020-05-05 04:59:45 -07:00
//@ts-ignore
additionalFields.addressOptions.addressProperties.map(property => {
properties.push({
type: 'SYSTEM',
subtype: property.subtype as string,
name: 'address',
value: property.address as string
} as IDataObject);
2020-05-06 01:27:01 -07:00
});
2020-05-05 04:59:45 -07:00
}
2020-05-11 11:22:31 -07:00
if (additionalFields.phoneOptions) {
2020-05-05 04:59:45 -07:00
//@ts-ignore
additionalFields.phoneOptions.phoneProperties.map(property => {
properties.push({
type: 'SYSTEM',
subtype: property.subtype as string,
name: 'phone',
value: property.number as string
} as IDataObject);
2020-05-06 01:27:01 -07:00
});
2020-05-05 04:59:45 -07:00
}
} else if (resource === 'company') {
2020-05-11 11:22:31 -07:00
if (additionalFields.email) {
properties.push({
type: 'SYSTEM',
name: 'email',
2020-05-05 04:59:45 -07:00
value: additionalFields.email as string
} as IDataObject);
2020-05-05 04:59:45 -07:00
}
2020-05-11 11:22:31 -07:00
if (additionalFields.address) {
properties.push({
type: 'SYSTEM',
name: 'address',
2020-05-05 04:59:45 -07:00
value: additionalFields.address as string
} as IDataObject);
2020-05-05 04:59:45 -07:00
}
2020-05-11 11:22:31 -07:00
if (additionalFields.phone) {
2020-05-05 04:59:45 -07:00
properties.push({
type: 'SYSTEM',
name: 'phone',
value: additionalFields.phone as string
} as IDataObject);
}
}
2020-05-05 04:59:45 -07:00
2020-05-11 11:22:31 -07:00
if (additionalFields.websiteOptions) {
//@ts-ignore
additionalFields.websiteOptions.websiteProperties.map(property => {
properties.push({
type: 'SYSTEM',
subtype: property.subtype as string,
name: 'webiste',
value: property.url as string
} as IDataObject);
2020-05-06 01:27:01 -07:00
});
}
2020-05-05 04:59:45 -07:00
2020-05-11 11:22:31 -07:00
if (additionalFields.customProperties) {
//@ts-ignore
additionalFields.customProperties.customProperty.map(property => {
properties.push({
type: 'CUSTOM',
subtype: property.subtype as string,
name: property.name,
value: property.value as string
} as IDataObject);
2020-05-06 01:27:01 -07:00
});
}
body.properties = properties;
2020-04-30 05:03:36 -07:00
}
const endpoint = 'api/contacts';
responseData = await agileCrmApiRequest.call(this, 'POST', endpoint, body);
2020-05-11 11:22:31 -07:00
} else if (operation === 'update') {
2020-05-05 04:59:45 -07:00
const contactId = this.getNodeParameter(idGetter, i) as string;
2020-05-11 11:22:31 -07:00
const contactUpdatePayload: IContactUpdate = { id: contactId };
2020-05-05 02:08:30 -07:00
const jsonParameters = this.getNodeParameter('jsonParameters', i) as boolean;
const body: IContact = {};
2020-05-11 11:22:31 -07:00
const properties: IDataObject[] = [];
2020-05-05 02:08:30 -07:00
if (jsonParameters) {
const additionalFieldsJson = this.getNodeParameter('additionalFieldsJson', i) as string;
2020-05-11 11:22:31 -07:00
if (additionalFieldsJson !== '') {
2020-05-05 02:08:30 -07:00
if (validateJSON(additionalFieldsJson) !== undefined) {
Object.assign(body, JSON.parse(additionalFieldsJson));
} else {
throw new Error('Additional fields must be a valid JSON');
}
}
} else {
const additionalFields = this.getNodeParameter('additionalFields', i) as IDataObject;
if (additionalFields.starValue) {
body.star_value = additionalFields.starValue as string;
}
if (additionalFields.tags) {
body.tags = additionalFields.tags as string[];
}
2020-05-05 04:59:45 -07:00
// Contact specific properties
2020-05-11 11:22:31 -07:00
if (resource === 'contact') {
if (additionalFields.leadScore) {
body.lead_score = additionalFields.leadScore as string;
}
2020-05-11 11:22:31 -07:00
if (additionalFields.firstName) {
2020-05-05 04:59:45 -07:00
properties.push({
type: 'SYSTEM',
name: 'first_name',
value: additionalFields.firstName as string
} as IDataObject);
}
2020-05-11 11:22:31 -07:00
if (additionalFields.lastName) {
2020-05-05 04:59:45 -07:00
properties.push({
type: 'SYSTEM',
name: 'last_name',
value: additionalFields.lastName as string
} as IDataObject);
}
2020-05-11 11:22:31 -07:00
if (additionalFields.company) {
2020-05-05 04:59:45 -07:00
properties.push({
type: 'SYSTEM',
name: 'company',
value: additionalFields.company as string
} as IDataObject);
}
2020-05-11 11:22:31 -07:00
if (additionalFields.title) {
2020-05-05 04:59:45 -07:00
properties.push({
type: 'SYSTEM',
name: 'title',
value: additionalFields.title as string
} as IDataObject);
}
2020-05-11 11:22:31 -07:00
if (additionalFields.emailOptions) {
2020-05-05 04:59:45 -07:00
//@ts-ignore
additionalFields.emailOptions.emailProperties.map(property => {
properties.push({
type: 'SYSTEM',
subtype: property.subtype as string,
name: 'email',
value: property.email as string
} as IDataObject);
2020-05-06 01:27:01 -07:00
});
2020-05-05 04:59:45 -07:00
}
2020-05-11 11:22:31 -07:00
if (additionalFields.addressOptions) {
2020-05-05 04:59:45 -07:00
//@ts-ignore
additionalFields.addressOptions.addressProperties.map(property => {
properties.push({
type: 'SYSTEM',
subtype: property.subtype as string,
name: 'address',
value: property.address as string
} as IDataObject);
2020-05-06 01:27:01 -07:00
});
2020-05-05 04:59:45 -07:00
}
2020-05-11 11:22:31 -07:00
if (additionalFields.phoneOptions) {
2020-05-05 04:59:45 -07:00
//@ts-ignore
additionalFields.phoneOptions.phoneProperties.map(property => {
properties.push({
type: 'SYSTEM',
subtype: property.subtype as string,
name: 'phone',
value: property.number as string
} as IDataObject);
2020-05-06 01:27:01 -07:00
});
2020-05-05 04:59:45 -07:00
}
} else if (resource === 'company') {
2020-05-11 11:22:31 -07:00
if (additionalFields.email) {
2020-05-05 02:08:30 -07:00
properties.push({
type: 'SYSTEM',
name: 'email',
2020-05-05 04:59:45 -07:00
value: additionalFields.email as string
2020-05-05 02:08:30 -07:00
} as IDataObject);
2020-05-05 04:59:45 -07:00
}
2020-05-11 11:22:31 -07:00
if (additionalFields.address) {
2020-05-05 02:08:30 -07:00
properties.push({
type: 'SYSTEM',
name: 'address',
2020-05-05 04:59:45 -07:00
value: additionalFields.address as string
2020-05-05 02:08:30 -07:00
} as IDataObject);
2020-05-05 04:59:45 -07:00
}
2020-05-11 11:22:31 -07:00
if (additionalFields.phone) {
2020-05-05 04:59:45 -07:00
properties.push({
type: 'SYSTEM',
name: 'phone',
value: additionalFields.phone as string
} as IDataObject);
}
2020-05-05 02:08:30 -07:00
}
2020-05-05 04:59:45 -07:00
2020-05-11 11:22:31 -07:00
if (additionalFields.websiteOptions) {
2020-05-05 02:08:30 -07:00
//@ts-ignore
additionalFields.websiteOptions.websiteProperties.map(property => {
properties.push({
type: 'SYSTEM',
subtype: property.subtype as string,
name: 'webiste',
value: property.url as string
} as IDataObject);
2020-05-06 01:27:01 -07:00
});
2020-05-05 02:08:30 -07:00
}
2020-05-11 11:22:31 -07:00
if (additionalFields.customProperties) {
2020-05-05 02:08:30 -07:00
//@ts-ignore
additionalFields.customProperties.customProperty.map(property => {
properties.push({
type: 'CUSTOM',
subtype: property.subtype as string,
name: property.name,
value: property.value as string
} as IDataObject);
2020-05-06 01:27:01 -07:00
});
2020-05-05 02:08:30 -07:00
}
body.properties = properties;
}
Object.assign(contactUpdatePayload, body);
responseData = await agileCrmApiRequestUpdate.call(this, 'PUT', '', contactUpdatePayload);
}
2020-05-11 11:22:31 -07:00
} else if (resource === 'deal') {
2020-05-05 07:03:53 -07:00
2020-05-11 11:22:31 -07:00
if (operation === 'get') {
2020-05-05 07:03:53 -07:00
const dealId = this.getNodeParameter('dealId', i) as string;
const endpoint = `api/opportunity/${dealId}`;
responseData = await agileCrmApiRequest.call(this, 'GET', endpoint, {});
2020-05-11 11:22:31 -07:00
} else if (operation === 'delete') {
2020-05-05 07:03:53 -07:00
const contactId = this.getNodeParameter('dealId', i) as string;
const endpoint = `api/opportunity/${contactId}`;
responseData = await agileCrmApiRequest.call(this, 'DELETE', endpoint, {});
2020-05-11 11:22:31 -07:00
} else if (operation === 'getAll') {
2020-05-05 07:03:53 -07:00
const returnAll = this.getNodeParameter('returnAll', i) as boolean;
2020-05-11 11:22:31 -07:00
2020-05-05 07:03:53 -07:00
if (returnAll) {
2020-05-06 01:27:01 -07:00
const endpoint = 'api/opportunity';
2020-05-05 07:03:53 -07:00
responseData = await agileCrmApiRequest.call(this, 'GET', endpoint, {});
} else {
const limit = this.getNodeParameter('limit', i) as number;
const endpoint = `api/opportunity?page_size=${limit}`;
responseData = await agileCrmApiRequest.call(this, 'GET', endpoint, {});
}
2020-05-11 11:22:31 -07:00
} else if (operation === 'create') {
const jsonParameters = this.getNodeParameter('jsonParameters', i) as boolean;
const body: IDeal = {};
if (jsonParameters) {
const additionalFieldsJson = this.getNodeParameter('additionalFieldsJson', i) as string;
2020-05-11 11:22:31 -07:00
if (additionalFieldsJson !== '') {
if (validateJSON(additionalFieldsJson) !== undefined) {
Object.assign(body, JSON.parse(additionalFieldsJson));
} else {
throw new Error('Additional fields must be a valid JSON');
}
}
} else {
const additionalFields = this.getNodeParameter('additionalFields', i) as IDataObject;
2020-05-11 11:22:31 -07:00
body.close_date = new Date(this.getNodeParameter('closeDate', i) as string).getTime();
body.expected_value = this.getNodeParameter('expectedValue', i) as number;
body.milestone = this.getNodeParameter('milestone', i) as string;
body.probability = this.getNodeParameter('probability', i) as number;
body.name = this.getNodeParameter('name', i) as string;
2020-05-11 11:22:31 -07:00
if (additionalFields.contactIds) {
body.contactIds = additionalFields.contactIds as string[];
}
2020-05-11 11:22:31 -07:00
if (additionalFields.customData) {
// @ts-ignore
body.customData = additionalFields.customData.customProperty as IDealCustomProperty[];
}
}
2020-05-06 01:27:01 -07:00
const endpoint = 'api/opportunity';
responseData = await agileCrmApiRequest.call(this, 'POST', endpoint, body);
2020-05-11 11:22:31 -07:00
} else if (operation === 'update') {
const jsonParameters = this.getNodeParameter('jsonParameters', i) as boolean;
const body: IDeal = {};
if (jsonParameters) {
const additionalFieldsJson = this.getNodeParameter('additionalFieldsJson', i) as string;
2020-05-11 11:22:31 -07:00
if (additionalFieldsJson !== '') {
if (validateJSON(additionalFieldsJson) !== undefined) {
Object.assign(body, JSON.parse(additionalFieldsJson));
} else {
2020-05-11 11:22:31 -07:00
throw new Error('Additional fields must be valid JSON');
}
}
} else {
const additionalFields = this.getNodeParameter('additionalFields', i) as IDataObject;
body.id = this.getNodeParameter('dealId', i) as number;
2020-05-11 11:22:31 -07:00
if (additionalFields.expectedValue) {
body.expected_value = additionalFields.expectedValue as number;
}
2020-05-11 11:22:31 -07:00
if (additionalFields.name) {
body.name = additionalFields.name as string;
}
2020-05-11 11:22:31 -07:00
if (additionalFields.probability) {
body.probability = additionalFields.probability as number;
}
2020-05-11 11:22:31 -07:00
if (additionalFields.contactIds) {
body.contactIds = additionalFields.contactIds as string[];
}
2020-05-11 11:22:31 -07:00
if (additionalFields.customData) {
// @ts-ignore
body.customData = additionalFields.customData.customProperty as IDealCustomProperty[];
}
}
2020-05-06 01:27:01 -07:00
const endpoint = 'api/opportunity/partial-update';
responseData = await agileCrmApiRequest.call(this, 'PUT', endpoint, body);
}
2020-04-30 05:03:36 -07:00
}
2020-05-05 04:59:45 -07:00
2020-05-05 07:03:53 -07:00
if (Array.isArray(responseData)) {
returnData.push.apply(returnData, responseData as IDataObject[]);
} else {
returnData.push(responseData as IDataObject);
}
2020-04-30 05:03:36 -07:00
}
return [this.helpers.returnJsonArray(returnData)];
}
2020-05-11 11:22:31 -07:00
2020-04-30 05:03:36 -07:00
}