2021-10-26 20:45:26 -07:00
|
|
|
import {
|
|
|
|
IExecuteFunctions,
|
|
|
|
} from 'n8n-core';
|
|
|
|
|
2020-04-30 05:03:36 -07:00
|
|
|
import {
|
2020-05-06 01:27:01 -07:00
|
|
|
IDataObject,
|
2020-04-30 05:03:36 -07:00
|
|
|
INodeExecutionData,
|
|
|
|
INodeType,
|
2021-04-16 09:33:36 -07:00
|
|
|
INodeTypeDescription,
|
|
|
|
NodeOperationError,
|
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';
|
|
|
|
|
2021-10-26 20:45:26 -07:00
|
|
|
import {
|
|
|
|
IContact,
|
|
|
|
IContactUpdate,
|
|
|
|
} from './ContactInterface';
|
|
|
|
|
|
|
|
import {
|
|
|
|
agileCrmApiRequest, agileCrmApiRequestAllItems,
|
|
|
|
agileCrmApiRequestUpdate,
|
|
|
|
getFilterRules,
|
|
|
|
simplifyResponse,
|
|
|
|
validateJSON,
|
|
|
|
} from './GenericFunctions';
|
2020-04-30 05:03:36 -07:00
|
|
|
|
2021-10-26 20:45:26 -07:00
|
|
|
import {
|
|
|
|
IDeal,
|
|
|
|
} from './DealInterface';
|
|
|
|
|
|
|
|
import {
|
|
|
|
IFilter,
|
|
|
|
ISearchConditions,
|
|
|
|
} from './FilterInterface';
|
2020-04-30 05:03:36 -07:00
|
|
|
|
|
|
|
export class AgileCrm implements INodeType {
|
|
|
|
description: INodeTypeDescription = {
|
2020-07-24 03:56:41 -07:00
|
|
|
displayName: 'Agile CRM',
|
2020-05-06 01:27:01 -07:00
|
|
|
name: 'agileCrm',
|
|
|
|
icon: 'file:agilecrm.png',
|
2021-10-26 20:45:26 -07:00
|
|
|
subtitle: '={{$parameter["operation"] + ": " + $parameter["resource"]}}',
|
2020-04-30 05:03:36 -07:00
|
|
|
group: ['transform'],
|
|
|
|
version: 1,
|
2020-07-24 03:56:41 -07:00
|
|
|
description: 'Consume Agile CRM API',
|
2020-04-30 05:03:36 -07:00
|
|
|
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,
|
2020-10-22 06:46:03 -07:00
|
|
|
},
|
2020-04-30 05:03:36 -07:00
|
|
|
],
|
|
|
|
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',
|
2020-10-22 06:46:03 -07:00
|
|
|
value: 'company',
|
2020-05-05 07:03:53 -07:00
|
|
|
},
|
2020-05-06 01:27:01 -07:00
|
|
|
{
|
|
|
|
name: 'Contact',
|
2020-10-22 06:46:03 -07:00
|
|
|
value: 'contact',
|
2020-05-05 04:59:45 -07:00
|
|
|
},
|
|
|
|
{
|
2020-05-06 01:27:01 -07:00
|
|
|
name: 'Deal',
|
2020-10-22 06:46:03 -07:00
|
|
|
value: 'deal',
|
2020-05-06 01:27:01 -07:00
|
|
|
},
|
|
|
|
],
|
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,
|
2020-10-22 06:46:03 -07:00
|
|
|
...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') {
|
2021-10-26 20:45:26 -07:00
|
|
|
const simple = this.getNodeParameter('simple', 0) as boolean;
|
|
|
|
const returnAll = this.getNodeParameter('returnAll', 0) as boolean;
|
|
|
|
const filterType = this.getNodeParameter('filterType', i) as string;
|
|
|
|
const sort = this.getNodeParameter('options.sort.sort', i, {}) as { direction: string, field: string };
|
|
|
|
const body: IDataObject = {};
|
|
|
|
const filterJson: IFilter = {};
|
|
|
|
|
|
|
|
let contactType = '';
|
2020-05-11 11:22:31 -07:00
|
|
|
if (resource === 'contact') {
|
2021-10-26 20:45:26 -07:00
|
|
|
contactType = 'PERSON';
|
|
|
|
} else {
|
|
|
|
contactType = 'COMPANY';
|
|
|
|
}
|
|
|
|
filterJson.contact_type = contactType;
|
|
|
|
|
|
|
|
if (filterType === 'manual') {
|
|
|
|
const conditions = this.getNodeParameter('filters.conditions', i, []) as ISearchConditions[];
|
|
|
|
const matchType = this.getNodeParameter('matchType', i) as string;
|
|
|
|
let rules;
|
|
|
|
if (conditions.length !== 0) {
|
|
|
|
rules = getFilterRules(conditions, matchType);
|
|
|
|
Object.assign(filterJson, rules);
|
2020-05-05 04:59:45 -07:00
|
|
|
} else {
|
2021-10-26 20:45:26 -07:00
|
|
|
throw new NodeOperationError(this.getNode(), 'At least one condition must be added.');
|
2020-05-05 04:59:45 -07:00
|
|
|
}
|
2021-10-26 20:45:26 -07:00
|
|
|
} else if (filterType === 'json') {
|
|
|
|
const filterJsonRules = this.getNodeParameter('filterJson', i) as string;
|
|
|
|
if (validateJSON(filterJsonRules) !== undefined) {
|
|
|
|
Object.assign(filterJson, JSON.parse(filterJsonRules) as IFilter);
|
2020-05-05 04:59:45 -07:00
|
|
|
} else {
|
2021-10-26 20:45:26 -07:00
|
|
|
throw new NodeOperationError(this.getNode(), 'Filter (JSON) must be a valid json');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
body.filterJson = JSON.stringify(filterJson);
|
|
|
|
|
|
|
|
if (sort) {
|
|
|
|
if (sort.direction === 'ASC') {
|
|
|
|
body.global_sort_key = sort.field;
|
|
|
|
} else if (sort.direction === 'DESC') {
|
|
|
|
body.global_sort_key = `-${sort.field}`;
|
2020-05-05 04:59:45 -07:00
|
|
|
}
|
2020-04-30 05:03:36 -07:00
|
|
|
}
|
2020-05-05 04:59:45 -07:00
|
|
|
|
2021-10-26 20:45:26 -07:00
|
|
|
if (returnAll) {
|
|
|
|
body.page_size = 100;
|
|
|
|
responseData = await agileCrmApiRequestAllItems.call(this, 'POST', `api/filters/filter/dynamic-filter`, body, undefined, undefined, true);
|
|
|
|
} else {
|
|
|
|
body.page_size = this.getNodeParameter('limit', 0) as number;
|
|
|
|
responseData = await agileCrmApiRequest.call(this, 'POST', `api/filters/filter/dynamic-filter`, body, undefined, undefined, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (simple) {
|
|
|
|
responseData = simplifyResponse(responseData);
|
|
|
|
}
|
|
|
|
|
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 {
|
2021-04-16 09:33:36 -07:00
|
|
|
throw new NodeOperationError(this.getNode(), 'Additional fields must be a valid JSON');
|
2020-04-30 05:03:36 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} 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',
|
2020-10-22 06:46:03 -07:00
|
|
|
value: additionalFields.firstName as string,
|
2020-05-05 04:59:45 -07:00
|
|
|
} 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',
|
2020-10-22 06:46:03 -07:00
|
|
|
value: additionalFields.lastName as string,
|
2020-05-05 04:59:45 -07:00
|
|
|
} 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',
|
2020-10-22 06:46:03 -07:00
|
|
|
value: additionalFields.company as string,
|
2020-05-05 04:59:45 -07:00
|
|
|
} 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',
|
2020-10-22 06:46:03 -07:00
|
|
|
value: additionalFields.title as string,
|
2020-05-05 04:59:45 -07:00
|
|
|
} 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',
|
2020-10-22 06:46:03 -07:00
|
|
|
value: property.email as string,
|
2020-05-05 04:59:45 -07:00
|
|
|
} 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',
|
2020-10-22 06:46:03 -07:00
|
|
|
value: property.address as string,
|
2020-05-05 04:59:45 -07:00
|
|
|
} 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',
|
2020-10-22 06:46:03 -07:00
|
|
|
value: property.number as string,
|
2020-05-05 04:59:45 -07:00
|
|
|
} 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-02 11:06:49 -07:00
|
|
|
properties.push({
|
|
|
|
type: 'SYSTEM',
|
|
|
|
name: 'email',
|
2020-10-22 06:46:03 -07:00
|
|
|
value: additionalFields.email as string,
|
2020-05-02 11:06:49 -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-02 11:06:49 -07:00
|
|
|
properties.push({
|
|
|
|
type: 'SYSTEM',
|
|
|
|
name: 'address',
|
2020-10-22 06:46:03 -07:00
|
|
|
value: additionalFields.address as string,
|
2020-05-02 11:06:49 -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',
|
2020-10-22 06:46:03 -07:00
|
|
|
value: additionalFields.phone as string,
|
2020-05-05 04:59:45 -07:00
|
|
|
} as IDataObject);
|
|
|
|
}
|
|
|
|
|
2020-05-02 11:06:49 -07:00
|
|
|
}
|
2020-05-05 04:59:45 -07:00
|
|
|
|
2020-05-11 11:22:31 -07:00
|
|
|
if (additionalFields.websiteOptions) {
|
2020-05-02 11:06:49 -07:00
|
|
|
//@ts-ignore
|
|
|
|
additionalFields.websiteOptions.websiteProperties.map(property => {
|
|
|
|
properties.push({
|
|
|
|
type: 'SYSTEM',
|
|
|
|
subtype: property.subtype as string,
|
|
|
|
name: 'webiste',
|
2020-10-22 06:46:03 -07:00
|
|
|
value: property.url as string,
|
2020-05-02 11:06:49 -07:00
|
|
|
} as IDataObject);
|
2020-05-06 01:27:01 -07:00
|
|
|
});
|
2020-05-02 11:06:49 -07:00
|
|
|
}
|
2020-05-05 04:59:45 -07:00
|
|
|
|
2020-05-11 11:22:31 -07:00
|
|
|
if (additionalFields.customProperties) {
|
2020-05-02 11:06:49 -07:00
|
|
|
//@ts-ignore
|
|
|
|
additionalFields.customProperties.customProperty.map(property => {
|
|
|
|
properties.push({
|
|
|
|
type: 'CUSTOM',
|
|
|
|
subtype: property.subtype as string,
|
|
|
|
name: property.name,
|
2020-10-22 06:46:03 -07:00
|
|
|
value: property.value as string,
|
2020-05-02 11:06:49 -07:00
|
|
|
} as IDataObject);
|
2020-05-06 01:27:01 -07:00
|
|
|
});
|
2020-05-02 11:06:49 -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 {
|
2021-04-16 09:33:36 -07:00
|
|
|
throw new NodeOperationError(this.getNode(), 'Additional fields must be a valid JSON');
|
2020-05-05 02:08:30 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} 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') {
|
2020-05-06 04:01:52 -07:00
|
|
|
|
|
|
|
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',
|
2020-10-22 06:46:03 -07:00
|
|
|
value: additionalFields.firstName as string,
|
2020-05-05 04:59:45 -07:00
|
|
|
} 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',
|
2020-10-22 06:46:03 -07:00
|
|
|
value: additionalFields.lastName as string,
|
2020-05-05 04:59:45 -07:00
|
|
|
} 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',
|
2020-10-22 06:46:03 -07:00
|
|
|
value: additionalFields.company as string,
|
2020-05-05 04:59:45 -07:00
|
|
|
} 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',
|
2020-10-22 06:46:03 -07:00
|
|
|
value: additionalFields.title as string,
|
2020-05-05 04:59:45 -07:00
|
|
|
} 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',
|
2020-10-22 06:46:03 -07:00
|
|
|
value: property.email as string,
|
2020-05-05 04:59:45 -07:00
|
|
|
} 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',
|
2020-10-22 06:46:03 -07:00
|
|
|
value: property.address as string,
|
2020-05-05 04:59:45 -07:00
|
|
|
} 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',
|
2020-10-22 06:46:03 -07:00
|
|
|
value: property.number as string,
|
2020-05-05 04:59:45 -07:00
|
|
|
} 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-10-22 06:46:03 -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-10-22 06:46:03 -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',
|
2020-10-22 06:46:03 -07:00
|
|
|
value: additionalFields.phone as string,
|
2020-05-05 04:59:45 -07:00
|
|
|
} 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',
|
2020-10-22 06:46:03 -07:00
|
|
|
value: property.url as string,
|
2020-05-05 02:08:30 -07:00
|
|
|
} 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,
|
2020-10-22 06:46:03 -07:00
|
|
|
value: property.value as string,
|
2020-05-05 02:08:30 -07:00
|
|
|
} 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') {
|
2021-10-26 20:45:26 -07:00
|
|
|
const returnAll = this.getNodeParameter('returnAll', 0) as boolean;
|
|
|
|
const endpoint = 'api/opportunity';
|
2020-05-11 11:22:31 -07:00
|
|
|
|
2020-05-05 07:03:53 -07:00
|
|
|
if (returnAll) {
|
2021-10-26 20:45:26 -07:00
|
|
|
const limit = 100;
|
|
|
|
responseData = await agileCrmApiRequestAllItems.call(this, 'GET', endpoint, undefined, { page_size: limit });
|
2020-05-05 07:03:53 -07:00
|
|
|
} else {
|
2021-10-26 20:45:26 -07:00
|
|
|
const limit = this.getNodeParameter('limit', 0) as number;
|
|
|
|
responseData = await agileCrmApiRequest.call(this, 'GET', endpoint, undefined, { page_size: limit });
|
2020-05-05 07:03:53 -07:00
|
|
|
}
|
2020-05-05 08:44:02 -07:00
|
|
|
|
2020-05-11 11:22:31 -07:00
|
|
|
} else if (operation === 'create') {
|
2020-05-05 08:44:02 -07:00
|
|
|
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 !== '') {
|
2020-05-05 08:44:02 -07:00
|
|
|
if (validateJSON(additionalFieldsJson) !== undefined) {
|
|
|
|
Object.assign(body, JSON.parse(additionalFieldsJson));
|
|
|
|
} else {
|
2021-04-16 09:33:36 -07:00
|
|
|
throw new NodeOperationError(this.getNode(), 'Additional fields must be a valid JSON');
|
2020-05-05 08:44:02 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
const additionalFields = this.getNodeParameter('additionalFields', i) as IDataObject;
|
2020-05-11 11:22:31 -07:00
|
|
|
|
2020-05-05 08:44:02 -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) {
|
2020-05-05 08:44:02 -07:00
|
|
|
body.contactIds = additionalFields.contactIds as string[];
|
|
|
|
}
|
|
|
|
|
2020-05-11 11:22:31 -07:00
|
|
|
if (additionalFields.customData) {
|
2020-05-05 08:44:02 -07:00
|
|
|
// @ts-ignore
|
|
|
|
body.customData = additionalFields.customData.customProperty as IDealCustomProperty[];
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-05-06 01:27:01 -07:00
|
|
|
const endpoint = 'api/opportunity';
|
2020-05-05 08:44:02 -07:00
|
|
|
responseData = await agileCrmApiRequest.call(this, 'POST', endpoint, body);
|
|
|
|
|
2020-05-11 11:22:31 -07:00
|
|
|
} else if (operation === 'update') {
|
2020-05-05 08:44:02 -07:00
|
|
|
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 !== '') {
|
2020-05-05 08:44:02 -07:00
|
|
|
|
|
|
|
if (validateJSON(additionalFieldsJson) !== undefined) {
|
|
|
|
|
|
|
|
Object.assign(body, JSON.parse(additionalFieldsJson));
|
|
|
|
|
|
|
|
} else {
|
2021-04-16 09:33:36 -07:00
|
|
|
throw new NodeOperationError(this.getNode(), 'Additional fields must be valid JSON');
|
2020-05-05 08:44:02 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} 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) {
|
2020-05-05 08:44:02 -07:00
|
|
|
body.expected_value = additionalFields.expectedValue as number;
|
|
|
|
}
|
|
|
|
|
2020-05-11 11:22:31 -07:00
|
|
|
if (additionalFields.name) {
|
2020-05-05 08:44:02 -07:00
|
|
|
body.name = additionalFields.name as string;
|
|
|
|
}
|
|
|
|
|
2020-05-11 11:22:31 -07:00
|
|
|
if (additionalFields.probability) {
|
2020-05-05 08:44:02 -07:00
|
|
|
body.probability = additionalFields.probability as number;
|
|
|
|
}
|
|
|
|
|
2020-05-11 11:22:31 -07:00
|
|
|
if (additionalFields.contactIds) {
|
2020-05-05 08:44:02 -07:00
|
|
|
body.contactIds = additionalFields.contactIds as string[];
|
|
|
|
}
|
|
|
|
|
2020-05-11 11:22:31 -07:00
|
|
|
if (additionalFields.customData) {
|
2020-05-05 08:44:02 -07:00
|
|
|
// @ts-ignore
|
|
|
|
body.customData = additionalFields.customData.customProperty as IDealCustomProperty[];
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-05-06 01:27:01 -07:00
|
|
|
const endpoint = 'api/opportunity/partial-update';
|
2020-05-05 08:44:02 -07:00
|
|
|
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
|
|
|
}
|