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

630 lines
18 KiB
TypeScript
Raw Normal View History

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,
:sparkles: Improve node error handling (#1309) * Add path mapping and response error interfaces * Add error handling and throwing functionality * Refactor error handling into a single function * Re-implement error handling in Hacker News node * Fix linting details * Re-implement error handling in Spotify node * Re-implement error handling in G Suite Admin node * :construction: create basic setup NodeError * :construction: add httpCodes * :construction: add path priolist * :construction: handle statusCode in error, adjust interfaces * :construction: fixing type issues w/Ivan * :construction: add error exploration * 👔 fix linter issues * :wrench: improve object check * :construction: remove path passing from NodeApiError * :construction: add multi error + refactor findProperty method * 👔 allow any * :wrench: handle multi error message callback * :zap: change return type of callback * :zap: add customCallback to MultiError * :construction: refactor to use INode * :hammer: handle arrays, continue search after first null property found * 🚫 refactor method access * :construction: setup NodeErrorView * :zap: change timestamp to Date.now * :books: Add documentation for methods and constants * :construction: change message setting * 🚚 move NodeErrors to workflow * :sparkles: add new ErrorView for Nodes * :art: improve error notification * :art: refactor interfaces * :zap: add WorkflowOperationError, refactor error throwing * 👕 fix linter issues * :art: rename param * :bug: fix handling normal errors * :zap: add usage of NodeApiError * :art: fix throw new error instead of constructor * :art: remove unnecessary code/comments * :art: adjusted spacing + updated status messages * :art: fix tab indentation * ✨ Replace current errors with custom errors (#1576) * :zap: Introduce NodeApiError in catch blocks * :zap: Introduce NodeOperationError in nodes * :zap: Add missing errors and remove incompatible * :zap: Fix NodeOperationError in incompatible nodes * :wrench: Adjust error handling in missed nodes PayPal, FileMaker, Reddit, Taiga and Facebook Graph API nodes * :hammer: Adjust Strava Trigger node error handling * :hammer: Adjust AWS nodes error handling * :hammer: Remove duplicate instantiation of NodeApiError * :bug: fix strava trigger node error handling * Add XML parsing to NodeApiError constructor (#1633) * :bug: Remove type annotation from catch variable * :sparkles: Add XML parsing to NodeApiError * :zap: Simplify error handling in Rekognition node * :zap: Pass in XML flag in generic functions * :fire: Remove try/catch wrappers at call sites * :hammer: Refactor setting description from XML * :hammer: Refactor let to const in resource loaders * :zap: Find property in parsed XML * :zap: Change let to const * :fire: Remove unneeded try/catch block * :shirt: Fix linting issues * :bug: Fix errors from merge conflict resolution * :zap: Add custom errors to latest contributions * :shirt: Fix linting issues * :zap: Refactor MongoDB helpers for custom errors * :bug: Correct custom error type * :zap: Apply feedback to A nodes * :zap: Apply feedback to missed A node * :zap: Apply feedback to B-D nodes * :zap: Apply feedback to E-F nodes * :zap: Apply feedback to G nodes * :zap: Apply feedback to H-L nodes * :zap: Apply feedback to M nodes * :zap: Apply feedback to P nodes * :zap: Apply feedback to R nodes * :zap: Apply feedback to S nodes * :zap: Apply feedback to T nodes * :zap: Apply feedback to V-Z nodes * :zap: Add HTTP code to iterable node error * :hammer: Standardize e as error * :hammer: Standardize err as error * :zap: Fix error handling for non-standard nodes Co-authored-by: Ben Hesseldieck <b.hesseldieck@gmail.com> Co-authored-by: Ben Hesseldieck <b.hesseldieck@gmail.com> Co-authored-by: Ben Hesseldieck <1849459+BHesseldieck@users.noreply.github.com>
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';
import {
IContact,
IContactUpdate,
} from './ContactInterface';
import {
agileCrmApiRequest, agileCrmApiRequestAllItems,
agileCrmApiRequestUpdate,
getFilterRules,
simplifyResponse,
validateJSON,
} from './GenericFunctions';
2020-04-30 05:03:36 -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 = {
displayName: 'Agile CRM',
2020-05-06 01:27:01 -07:00
name: 'agileCrm',
icon: 'file:agilecrm.png',
subtitle: '={{$parameter["operation"] + ": " + $parameter["resource"]}}',
2020-04-30 05:03:36 -07:00
group: ['transform'],
version: 1,
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') {
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') {
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 {
throw new NodeOperationError(this.getNode(), 'At least one condition must be added.');
2020-05-05 04:59:45 -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 {
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
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 {
:sparkles: Improve node error handling (#1309) * Add path mapping and response error interfaces * Add error handling and throwing functionality * Refactor error handling into a single function * Re-implement error handling in Hacker News node * Fix linting details * Re-implement error handling in Spotify node * Re-implement error handling in G Suite Admin node * :construction: create basic setup NodeError * :construction: add httpCodes * :construction: add path priolist * :construction: handle statusCode in error, adjust interfaces * :construction: fixing type issues w/Ivan * :construction: add error exploration * 👔 fix linter issues * :wrench: improve object check * :construction: remove path passing from NodeApiError * :construction: add multi error + refactor findProperty method * 👔 allow any * :wrench: handle multi error message callback * :zap: change return type of callback * :zap: add customCallback to MultiError * :construction: refactor to use INode * :hammer: handle arrays, continue search after first null property found * 🚫 refactor method access * :construction: setup NodeErrorView * :zap: change timestamp to Date.now * :books: Add documentation for methods and constants * :construction: change message setting * 🚚 move NodeErrors to workflow * :sparkles: add new ErrorView for Nodes * :art: improve error notification * :art: refactor interfaces * :zap: add WorkflowOperationError, refactor error throwing * 👕 fix linter issues * :art: rename param * :bug: fix handling normal errors * :zap: add usage of NodeApiError * :art: fix throw new error instead of constructor * :art: remove unnecessary code/comments * :art: adjusted spacing + updated status messages * :art: fix tab indentation * ✨ Replace current errors with custom errors (#1576) * :zap: Introduce NodeApiError in catch blocks * :zap: Introduce NodeOperationError in nodes * :zap: Add missing errors and remove incompatible * :zap: Fix NodeOperationError in incompatible nodes * :wrench: Adjust error handling in missed nodes PayPal, FileMaker, Reddit, Taiga and Facebook Graph API nodes * :hammer: Adjust Strava Trigger node error handling * :hammer: Adjust AWS nodes error handling * :hammer: Remove duplicate instantiation of NodeApiError * :bug: fix strava trigger node error handling * Add XML parsing to NodeApiError constructor (#1633) * :bug: Remove type annotation from catch variable * :sparkles: Add XML parsing to NodeApiError * :zap: Simplify error handling in Rekognition node * :zap: Pass in XML flag in generic functions * :fire: Remove try/catch wrappers at call sites * :hammer: Refactor setting description from XML * :hammer: Refactor let to const in resource loaders * :zap: Find property in parsed XML * :zap: Change let to const * :fire: Remove unneeded try/catch block * :shirt: Fix linting issues * :bug: Fix errors from merge conflict resolution * :zap: Add custom errors to latest contributions * :shirt: Fix linting issues * :zap: Refactor MongoDB helpers for custom errors * :bug: Correct custom error type * :zap: Apply feedback to A nodes * :zap: Apply feedback to missed A node * :zap: Apply feedback to B-D nodes * :zap: Apply feedback to E-F nodes * :zap: Apply feedback to G nodes * :zap: Apply feedback to H-L nodes * :zap: Apply feedback to M nodes * :zap: Apply feedback to P nodes * :zap: Apply feedback to R nodes * :zap: Apply feedback to S nodes * :zap: Apply feedback to T nodes * :zap: Apply feedback to V-Z nodes * :zap: Add HTTP code to iterable node error * :hammer: Standardize e as error * :hammer: Standardize err as error * :zap: Fix error handling for non-standard nodes Co-authored-by: Ben Hesseldieck <b.hesseldieck@gmail.com> Co-authored-by: Ben Hesseldieck <b.hesseldieck@gmail.com> Co-authored-by: Ben Hesseldieck <1849459+BHesseldieck@users.noreply.github.com>
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) {
properties.push({
type: 'SYSTEM',
name: 'email',
2020-10-22 06:46:03 -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-10-22 06:46:03 -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',
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 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',
2020-10-22 06:46:03 -07:00
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,
2020-10-22 06:46:03 -07:00
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 {
:sparkles: Improve node error handling (#1309) * Add path mapping and response error interfaces * Add error handling and throwing functionality * Refactor error handling into a single function * Re-implement error handling in Hacker News node * Fix linting details * Re-implement error handling in Spotify node * Re-implement error handling in G Suite Admin node * :construction: create basic setup NodeError * :construction: add httpCodes * :construction: add path priolist * :construction: handle statusCode in error, adjust interfaces * :construction: fixing type issues w/Ivan * :construction: add error exploration * 👔 fix linter issues * :wrench: improve object check * :construction: remove path passing from NodeApiError * :construction: add multi error + refactor findProperty method * 👔 allow any * :wrench: handle multi error message callback * :zap: change return type of callback * :zap: add customCallback to MultiError * :construction: refactor to use INode * :hammer: handle arrays, continue search after first null property found * 🚫 refactor method access * :construction: setup NodeErrorView * :zap: change timestamp to Date.now * :books: Add documentation for methods and constants * :construction: change message setting * 🚚 move NodeErrors to workflow * :sparkles: add new ErrorView for Nodes * :art: improve error notification * :art: refactor interfaces * :zap: add WorkflowOperationError, refactor error throwing * 👕 fix linter issues * :art: rename param * :bug: fix handling normal errors * :zap: add usage of NodeApiError * :art: fix throw new error instead of constructor * :art: remove unnecessary code/comments * :art: adjusted spacing + updated status messages * :art: fix tab indentation * ✨ Replace current errors with custom errors (#1576) * :zap: Introduce NodeApiError in catch blocks * :zap: Introduce NodeOperationError in nodes * :zap: Add missing errors and remove incompatible * :zap: Fix NodeOperationError in incompatible nodes * :wrench: Adjust error handling in missed nodes PayPal, FileMaker, Reddit, Taiga and Facebook Graph API nodes * :hammer: Adjust Strava Trigger node error handling * :hammer: Adjust AWS nodes error handling * :hammer: Remove duplicate instantiation of NodeApiError * :bug: fix strava trigger node error handling * Add XML parsing to NodeApiError constructor (#1633) * :bug: Remove type annotation from catch variable * :sparkles: Add XML parsing to NodeApiError * :zap: Simplify error handling in Rekognition node * :zap: Pass in XML flag in generic functions * :fire: Remove try/catch wrappers at call sites * :hammer: Refactor setting description from XML * :hammer: Refactor let to const in resource loaders * :zap: Find property in parsed XML * :zap: Change let to const * :fire: Remove unneeded try/catch block * :shirt: Fix linting issues * :bug: Fix errors from merge conflict resolution * :zap: Add custom errors to latest contributions * :shirt: Fix linting issues * :zap: Refactor MongoDB helpers for custom errors * :bug: Correct custom error type * :zap: Apply feedback to A nodes * :zap: Apply feedback to missed A node * :zap: Apply feedback to B-D nodes * :zap: Apply feedback to E-F nodes * :zap: Apply feedback to G nodes * :zap: Apply feedback to H-L nodes * :zap: Apply feedback to M nodes * :zap: Apply feedback to P nodes * :zap: Apply feedback to R nodes * :zap: Apply feedback to S nodes * :zap: Apply feedback to T nodes * :zap: Apply feedback to V-Z nodes * :zap: Add HTTP code to iterable node error * :hammer: Standardize e as error * :hammer: Standardize err as error * :zap: Fix error handling for non-standard nodes Co-authored-by: Ben Hesseldieck <b.hesseldieck@gmail.com> Co-authored-by: Ben Hesseldieck <b.hesseldieck@gmail.com> Co-authored-by: Ben Hesseldieck <1849459+BHesseldieck@users.noreply.github.com>
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') {
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') {
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) {
const limit = 100;
responseData = await agileCrmApiRequestAllItems.call(this, 'GET', endpoint, undefined, { page_size: limit });
2020-05-05 07:03:53 -07:00
} else {
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-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 {
:sparkles: Improve node error handling (#1309) * Add path mapping and response error interfaces * Add error handling and throwing functionality * Refactor error handling into a single function * Re-implement error handling in Hacker News node * Fix linting details * Re-implement error handling in Spotify node * Re-implement error handling in G Suite Admin node * :construction: create basic setup NodeError * :construction: add httpCodes * :construction: add path priolist * :construction: handle statusCode in error, adjust interfaces * :construction: fixing type issues w/Ivan * :construction: add error exploration * 👔 fix linter issues * :wrench: improve object check * :construction: remove path passing from NodeApiError * :construction: add multi error + refactor findProperty method * 👔 allow any * :wrench: handle multi error message callback * :zap: change return type of callback * :zap: add customCallback to MultiError * :construction: refactor to use INode * :hammer: handle arrays, continue search after first null property found * 🚫 refactor method access * :construction: setup NodeErrorView * :zap: change timestamp to Date.now * :books: Add documentation for methods and constants * :construction: change message setting * 🚚 move NodeErrors to workflow * :sparkles: add new ErrorView for Nodes * :art: improve error notification * :art: refactor interfaces * :zap: add WorkflowOperationError, refactor error throwing * 👕 fix linter issues * :art: rename param * :bug: fix handling normal errors * :zap: add usage of NodeApiError * :art: fix throw new error instead of constructor * :art: remove unnecessary code/comments * :art: adjusted spacing + updated status messages * :art: fix tab indentation * ✨ Replace current errors with custom errors (#1576) * :zap: Introduce NodeApiError in catch blocks * :zap: Introduce NodeOperationError in nodes * :zap: Add missing errors and remove incompatible * :zap: Fix NodeOperationError in incompatible nodes * :wrench: Adjust error handling in missed nodes PayPal, FileMaker, Reddit, Taiga and Facebook Graph API nodes * :hammer: Adjust Strava Trigger node error handling * :hammer: Adjust AWS nodes error handling * :hammer: Remove duplicate instantiation of NodeApiError * :bug: fix strava trigger node error handling * Add XML parsing to NodeApiError constructor (#1633) * :bug: Remove type annotation from catch variable * :sparkles: Add XML parsing to NodeApiError * :zap: Simplify error handling in Rekognition node * :zap: Pass in XML flag in generic functions * :fire: Remove try/catch wrappers at call sites * :hammer: Refactor setting description from XML * :hammer: Refactor let to const in resource loaders * :zap: Find property in parsed XML * :zap: Change let to const * :fire: Remove unneeded try/catch block * :shirt: Fix linting issues * :bug: Fix errors from merge conflict resolution * :zap: Add custom errors to latest contributions * :shirt: Fix linting issues * :zap: Refactor MongoDB helpers for custom errors * :bug: Correct custom error type * :zap: Apply feedback to A nodes * :zap: Apply feedback to missed A node * :zap: Apply feedback to B-D nodes * :zap: Apply feedback to E-F nodes * :zap: Apply feedback to G nodes * :zap: Apply feedback to H-L nodes * :zap: Apply feedback to M nodes * :zap: Apply feedback to P nodes * :zap: Apply feedback to R nodes * :zap: Apply feedback to S nodes * :zap: Apply feedback to T nodes * :zap: Apply feedback to V-Z nodes * :zap: Add HTTP code to iterable node error * :hammer: Standardize e as error * :hammer: Standardize err as error * :zap: Fix error handling for non-standard nodes Co-authored-by: Ben Hesseldieck <b.hesseldieck@gmail.com> Co-authored-by: Ben Hesseldieck <b.hesseldieck@gmail.com> Co-authored-by: Ben Hesseldieck <1849459+BHesseldieck@users.noreply.github.com>
2021-04-16 09:33:36 -07:00
throw new NodeOperationError(this.getNode(), '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 {
:sparkles: Improve node error handling (#1309) * Add path mapping and response error interfaces * Add error handling and throwing functionality * Refactor error handling into a single function * Re-implement error handling in Hacker News node * Fix linting details * Re-implement error handling in Spotify node * Re-implement error handling in G Suite Admin node * :construction: create basic setup NodeError * :construction: add httpCodes * :construction: add path priolist * :construction: handle statusCode in error, adjust interfaces * :construction: fixing type issues w/Ivan * :construction: add error exploration * 👔 fix linter issues * :wrench: improve object check * :construction: remove path passing from NodeApiError * :construction: add multi error + refactor findProperty method * 👔 allow any * :wrench: handle multi error message callback * :zap: change return type of callback * :zap: add customCallback to MultiError * :construction: refactor to use INode * :hammer: handle arrays, continue search after first null property found * 🚫 refactor method access * :construction: setup NodeErrorView * :zap: change timestamp to Date.now * :books: Add documentation for methods and constants * :construction: change message setting * 🚚 move NodeErrors to workflow * :sparkles: add new ErrorView for Nodes * :art: improve error notification * :art: refactor interfaces * :zap: add WorkflowOperationError, refactor error throwing * 👕 fix linter issues * :art: rename param * :bug: fix handling normal errors * :zap: add usage of NodeApiError * :art: fix throw new error instead of constructor * :art: remove unnecessary code/comments * :art: adjusted spacing + updated status messages * :art: fix tab indentation * ✨ Replace current errors with custom errors (#1576) * :zap: Introduce NodeApiError in catch blocks * :zap: Introduce NodeOperationError in nodes * :zap: Add missing errors and remove incompatible * :zap: Fix NodeOperationError in incompatible nodes * :wrench: Adjust error handling in missed nodes PayPal, FileMaker, Reddit, Taiga and Facebook Graph API nodes * :hammer: Adjust Strava Trigger node error handling * :hammer: Adjust AWS nodes error handling * :hammer: Remove duplicate instantiation of NodeApiError * :bug: fix strava trigger node error handling * Add XML parsing to NodeApiError constructor (#1633) * :bug: Remove type annotation from catch variable * :sparkles: Add XML parsing to NodeApiError * :zap: Simplify error handling in Rekognition node * :zap: Pass in XML flag in generic functions * :fire: Remove try/catch wrappers at call sites * :hammer: Refactor setting description from XML * :hammer: Refactor let to const in resource loaders * :zap: Find property in parsed XML * :zap: Change let to const * :fire: Remove unneeded try/catch block * :shirt: Fix linting issues * :bug: Fix errors from merge conflict resolution * :zap: Add custom errors to latest contributions * :shirt: Fix linting issues * :zap: Refactor MongoDB helpers for custom errors * :bug: Correct custom error type * :zap: Apply feedback to A nodes * :zap: Apply feedback to missed A node * :zap: Apply feedback to B-D nodes * :zap: Apply feedback to E-F nodes * :zap: Apply feedback to G nodes * :zap: Apply feedback to H-L nodes * :zap: Apply feedback to M nodes * :zap: Apply feedback to P nodes * :zap: Apply feedback to R nodes * :zap: Apply feedback to S nodes * :zap: Apply feedback to T nodes * :zap: Apply feedback to V-Z nodes * :zap: Add HTTP code to iterable node error * :hammer: Standardize e as error * :hammer: Standardize err as error * :zap: Fix error handling for non-standard nodes Co-authored-by: Ben Hesseldieck <b.hesseldieck@gmail.com> Co-authored-by: Ben Hesseldieck <b.hesseldieck@gmail.com> Co-authored-by: Ben Hesseldieck <1849459+BHesseldieck@users.noreply.github.com>
2021-04-16 09:33:36 -07:00
throw new NodeOperationError(this.getNode(), '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
}