mirror of
https://github.com/n8n-io/n8n.git
synced 2024-12-26 13:14:07 -08:00
1d27a9e87e
* 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 * 🚧 create basic setup NodeError * 🚧 add httpCodes * 🚧 add path priolist * 🚧 handle statusCode in error, adjust interfaces * 🚧 fixing type issues w/Ivan * 🚧 add error exploration * 👔 fix linter issues * 🔧 improve object check * 🚧 remove path passing from NodeApiError * 🚧 add multi error + refactor findProperty method * 👔 allow any * 🔧 handle multi error message callback * ⚡ change return type of callback * ⚡ add customCallback to MultiError * 🚧 refactor to use INode * 🔨 handle arrays, continue search after first null property found * 🚫 refactor method access * 🚧 setup NodeErrorView * ⚡ change timestamp to Date.now * 📚 Add documentation for methods and constants * 🚧 change message setting * 🚚 move NodeErrors to workflow * ✨ add new ErrorView for Nodes * 🎨 improve error notification * 🎨 refactor interfaces * ⚡ add WorkflowOperationError, refactor error throwing * 👕 fix linter issues * 🎨 rename param * 🐛 fix handling normal errors * ⚡ add usage of NodeApiError * 🎨 fix throw new error instead of constructor * 🎨 remove unnecessary code/comments * 🎨 adjusted spacing + updated status messages * 🎨 fix tab indentation * ✨ Replace current errors with custom errors (#1576) * ⚡ Introduce NodeApiError in catch blocks * ⚡ Introduce NodeOperationError in nodes * ⚡ Add missing errors and remove incompatible * ⚡ Fix NodeOperationError in incompatible nodes * 🔧 Adjust error handling in missed nodes PayPal, FileMaker, Reddit, Taiga and Facebook Graph API nodes * 🔨 Adjust Strava Trigger node error handling * 🔨 Adjust AWS nodes error handling * 🔨 Remove duplicate instantiation of NodeApiError * 🐛 fix strava trigger node error handling * Add XML parsing to NodeApiError constructor (#1633) * 🐛 Remove type annotation from catch variable * ✨ Add XML parsing to NodeApiError * ⚡ Simplify error handling in Rekognition node * ⚡ Pass in XML flag in generic functions * 🔥 Remove try/catch wrappers at call sites * 🔨 Refactor setting description from XML * 🔨 Refactor let to const in resource loaders * ⚡ Find property in parsed XML * ⚡ Change let to const * 🔥 Remove unneeded try/catch block * 👕 Fix linting issues * 🐛 Fix errors from merge conflict resolution * ⚡ Add custom errors to latest contributions * 👕 Fix linting issues * ⚡ Refactor MongoDB helpers for custom errors * 🐛 Correct custom error type * ⚡ Apply feedback to A nodes * ⚡ Apply feedback to missed A node * ⚡ Apply feedback to B-D nodes * ⚡ Apply feedback to E-F nodes * ⚡ Apply feedback to G nodes * ⚡ Apply feedback to H-L nodes * ⚡ Apply feedback to M nodes * ⚡ Apply feedback to P nodes * ⚡ Apply feedback to R nodes * ⚡ Apply feedback to S nodes * ⚡ Apply feedback to T nodes * ⚡ Apply feedback to V-Z nodes * ⚡ Add HTTP code to iterable node error * 🔨 Standardize e as error * 🔨 Standardize err as error * ⚡ 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>
577 lines
17 KiB
TypeScript
577 lines
17 KiB
TypeScript
import { IExecuteFunctions } from 'n8n-core';
|
|
import {
|
|
IDataObject,
|
|
INodeExecutionData,
|
|
INodeType,
|
|
INodeTypeDescription,
|
|
NodeOperationError,
|
|
} from 'n8n-workflow';
|
|
|
|
import {
|
|
contactFields,
|
|
contactOperations
|
|
} from './ContactDescription';
|
|
|
|
import {
|
|
companyFields,
|
|
companyOperations
|
|
} from './CompanyDescription';
|
|
|
|
import {
|
|
dealFields,
|
|
dealOperations
|
|
} from './DealDescription';
|
|
|
|
import { IContact, IContactUpdate } from './ContactInterface';
|
|
import { agileCrmApiRequest, agileCrmApiRequestUpdate, validateJSON } from './GenericFunctions';
|
|
import { IDeal } from './DealInterface';
|
|
|
|
|
|
export class AgileCrm implements INodeType {
|
|
description: INodeTypeDescription = {
|
|
displayName: 'Agile CRM',
|
|
name: 'agileCrm',
|
|
icon: 'file:agilecrm.png',
|
|
group: ['transform'],
|
|
version: 1,
|
|
description: 'Consume Agile CRM API',
|
|
defaults: {
|
|
name: 'AgileCRM',
|
|
color: '#772244',
|
|
},
|
|
inputs: ['main'],
|
|
outputs: ['main'],
|
|
credentials: [
|
|
{
|
|
name: 'agileCrmApi',
|
|
required: true,
|
|
},
|
|
],
|
|
properties: [
|
|
// Node properties which the user gets displayed and
|
|
// can change on the node.
|
|
{
|
|
displayName: 'Resource',
|
|
name: 'resource',
|
|
type: 'options',
|
|
options: [
|
|
{
|
|
name: 'Company',
|
|
value: 'company',
|
|
},
|
|
{
|
|
name: 'Contact',
|
|
value: 'contact',
|
|
},
|
|
{
|
|
name: 'Deal',
|
|
value: 'deal',
|
|
},
|
|
],
|
|
default: 'contact',
|
|
description: 'Resource to consume.',
|
|
},
|
|
// CONTACT
|
|
...contactOperations,
|
|
...contactFields,
|
|
|
|
// COMPANY
|
|
...companyOperations,
|
|
...companyFields,
|
|
|
|
// DEAL
|
|
...dealOperations,
|
|
...dealFields,
|
|
],
|
|
|
|
};
|
|
|
|
|
|
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;
|
|
|
|
for (let i = 0; i < items.length; i++) {
|
|
|
|
if (resource === 'contact' || resource === 'company') {
|
|
const idGetter = resource === 'contact' ? 'contactId' : 'companyId';
|
|
|
|
if (operation === 'get') {
|
|
const contactId = this.getNodeParameter(idGetter, i) as string;
|
|
|
|
const endpoint = `api/contacts/${contactId}`;
|
|
responseData = await agileCrmApiRequest.call(this, 'GET', endpoint, {});
|
|
|
|
} else if (operation === 'delete') {
|
|
const contactId = this.getNodeParameter(idGetter, i) as string;
|
|
|
|
const endpoint = `api/contacts/${contactId}`;
|
|
responseData = await agileCrmApiRequest.call(this, 'DELETE', endpoint, {});
|
|
|
|
} else if (operation === 'getAll') {
|
|
const returnAll = this.getNodeParameter('returnAll', i) as boolean;
|
|
|
|
if (resource === 'contact') {
|
|
if (returnAll) {
|
|
const endpoint = 'api/contacts';
|
|
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, {});
|
|
}
|
|
} else {
|
|
if (returnAll) {
|
|
const endpoint = 'api/contacts/companies/list';
|
|
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, {});
|
|
}
|
|
}
|
|
|
|
} else if (operation === 'create') {
|
|
const jsonParameters = this.getNodeParameter('jsonParameters', i) as boolean;
|
|
const body: IContact = {};
|
|
const properties: IDataObject[] = [];
|
|
|
|
if (jsonParameters) {
|
|
const additionalFieldsJson = this.getNodeParameter('additionalFieldsJson', i) as string;
|
|
|
|
if (additionalFieldsJson !== '') {
|
|
|
|
if (validateJSON(additionalFieldsJson) !== undefined) {
|
|
|
|
Object.assign(body, JSON.parse(additionalFieldsJson));
|
|
|
|
} else {
|
|
throw new NodeOperationError(this.getNode(), 'Additional fields must be a valid JSON');
|
|
}
|
|
}
|
|
|
|
} else {
|
|
|
|
const additionalFields = this.getNodeParameter('additionalFields', i) as IDataObject;
|
|
|
|
// if company, add 'company' as type. default is person
|
|
if (resource === 'company') {
|
|
body.type = 'COMPANY';
|
|
}
|
|
if (additionalFields.starValue) {
|
|
body.star_value = additionalFields.starValue as string;
|
|
}
|
|
if (additionalFields.tags) {
|
|
body.tags = additionalFields.tags as string[];
|
|
}
|
|
|
|
// Contact specific properties
|
|
if (resource === 'contact') {
|
|
if (additionalFields.firstName) {
|
|
properties.push({
|
|
type: 'SYSTEM',
|
|
name: 'first_name',
|
|
value: additionalFields.firstName as string,
|
|
} as IDataObject);
|
|
}
|
|
if (additionalFields.lastName) {
|
|
properties.push({
|
|
type: 'SYSTEM',
|
|
name: 'last_name',
|
|
value: additionalFields.lastName as string,
|
|
} as IDataObject);
|
|
}
|
|
if (additionalFields.company) {
|
|
properties.push({
|
|
type: 'SYSTEM',
|
|
name: 'company',
|
|
value: additionalFields.company as string,
|
|
} as IDataObject);
|
|
}
|
|
if (additionalFields.title) {
|
|
properties.push({
|
|
type: 'SYSTEM',
|
|
name: 'title',
|
|
value: additionalFields.title as string,
|
|
} as IDataObject);
|
|
}
|
|
if (additionalFields.emailOptions) {
|
|
//@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);
|
|
});
|
|
}
|
|
if (additionalFields.addressOptions) {
|
|
//@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);
|
|
});
|
|
}
|
|
|
|
if (additionalFields.phoneOptions) {
|
|
//@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);
|
|
});
|
|
}
|
|
} else if (resource === 'company') {
|
|
if (additionalFields.email) {
|
|
properties.push({
|
|
type: 'SYSTEM',
|
|
name: 'email',
|
|
value: additionalFields.email as string,
|
|
} as IDataObject);
|
|
}
|
|
|
|
if (additionalFields.address) {
|
|
properties.push({
|
|
type: 'SYSTEM',
|
|
name: 'address',
|
|
value: additionalFields.address as string,
|
|
} as IDataObject);
|
|
}
|
|
|
|
if (additionalFields.phone) {
|
|
properties.push({
|
|
type: 'SYSTEM',
|
|
name: 'phone',
|
|
value: additionalFields.phone as string,
|
|
} as IDataObject);
|
|
}
|
|
|
|
}
|
|
|
|
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);
|
|
});
|
|
}
|
|
|
|
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);
|
|
});
|
|
}
|
|
body.properties = properties;
|
|
|
|
}
|
|
const endpoint = 'api/contacts';
|
|
responseData = await agileCrmApiRequest.call(this, 'POST', endpoint, body);
|
|
|
|
} else if (operation === 'update') {
|
|
const contactId = this.getNodeParameter(idGetter, i) as string;
|
|
const contactUpdatePayload: IContactUpdate = { id: contactId };
|
|
const jsonParameters = this.getNodeParameter('jsonParameters', i) as boolean;
|
|
const body: IContact = {};
|
|
const properties: IDataObject[] = [];
|
|
|
|
if (jsonParameters) {
|
|
const additionalFieldsJson = this.getNodeParameter('additionalFieldsJson', i) as string;
|
|
|
|
if (additionalFieldsJson !== '') {
|
|
|
|
if (validateJSON(additionalFieldsJson) !== undefined) {
|
|
|
|
Object.assign(body, JSON.parse(additionalFieldsJson));
|
|
|
|
} else {
|
|
throw new NodeOperationError(this.getNode(), '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[];
|
|
}
|
|
|
|
// Contact specific properties
|
|
if (resource === 'contact') {
|
|
|
|
if (additionalFields.leadScore) {
|
|
body.lead_score = additionalFields.leadScore as string;
|
|
}
|
|
|
|
if (additionalFields.firstName) {
|
|
properties.push({
|
|
type: 'SYSTEM',
|
|
name: 'first_name',
|
|
value: additionalFields.firstName as string,
|
|
} as IDataObject);
|
|
}
|
|
if (additionalFields.lastName) {
|
|
properties.push({
|
|
type: 'SYSTEM',
|
|
name: 'last_name',
|
|
value: additionalFields.lastName as string,
|
|
} as IDataObject);
|
|
}
|
|
if (additionalFields.company) {
|
|
properties.push({
|
|
type: 'SYSTEM',
|
|
name: 'company',
|
|
value: additionalFields.company as string,
|
|
} as IDataObject);
|
|
}
|
|
if (additionalFields.title) {
|
|
properties.push({
|
|
type: 'SYSTEM',
|
|
name: 'title',
|
|
value: additionalFields.title as string,
|
|
} as IDataObject);
|
|
}
|
|
if (additionalFields.emailOptions) {
|
|
//@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);
|
|
});
|
|
}
|
|
if (additionalFields.addressOptions) {
|
|
//@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);
|
|
});
|
|
}
|
|
|
|
if (additionalFields.phoneOptions) {
|
|
//@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);
|
|
});
|
|
}
|
|
} else if (resource === 'company') {
|
|
if (additionalFields.email) {
|
|
properties.push({
|
|
type: 'SYSTEM',
|
|
name: 'email',
|
|
value: additionalFields.email as string,
|
|
} as IDataObject);
|
|
}
|
|
|
|
if (additionalFields.address) {
|
|
properties.push({
|
|
type: 'SYSTEM',
|
|
name: 'address',
|
|
value: additionalFields.address as string,
|
|
} as IDataObject);
|
|
}
|
|
|
|
if (additionalFields.phone) {
|
|
properties.push({
|
|
type: 'SYSTEM',
|
|
name: 'phone',
|
|
value: additionalFields.phone as string,
|
|
} as IDataObject);
|
|
}
|
|
|
|
}
|
|
|
|
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);
|
|
});
|
|
}
|
|
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);
|
|
});
|
|
}
|
|
body.properties = properties;
|
|
}
|
|
|
|
Object.assign(contactUpdatePayload, body);
|
|
|
|
responseData = await agileCrmApiRequestUpdate.call(this, 'PUT', '', contactUpdatePayload);
|
|
|
|
}
|
|
|
|
} else if (resource === 'deal') {
|
|
|
|
if (operation === 'get') {
|
|
const dealId = this.getNodeParameter('dealId', i) as string;
|
|
|
|
const endpoint = `api/opportunity/${dealId}`;
|
|
responseData = await agileCrmApiRequest.call(this, 'GET', endpoint, {});
|
|
|
|
} else if (operation === 'delete') {
|
|
const contactId = this.getNodeParameter('dealId', i) as string;
|
|
|
|
const endpoint = `api/opportunity/${contactId}`;
|
|
responseData = await agileCrmApiRequest.call(this, 'DELETE', endpoint, {});
|
|
|
|
} else if (operation === 'getAll') {
|
|
const returnAll = this.getNodeParameter('returnAll', i) as boolean;
|
|
|
|
if (returnAll) {
|
|
const endpoint = 'api/opportunity';
|
|
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, {});
|
|
}
|
|
|
|
} 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;
|
|
|
|
if (additionalFieldsJson !== '') {
|
|
if (validateJSON(additionalFieldsJson) !== undefined) {
|
|
Object.assign(body, JSON.parse(additionalFieldsJson));
|
|
} else {
|
|
throw new NodeOperationError(this.getNode(), 'Additional fields must be a valid JSON');
|
|
}
|
|
}
|
|
|
|
} else {
|
|
const additionalFields = this.getNodeParameter('additionalFields', i) as IDataObject;
|
|
|
|
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;
|
|
|
|
if (additionalFields.contactIds) {
|
|
body.contactIds = additionalFields.contactIds as string[];
|
|
}
|
|
|
|
if (additionalFields.customData) {
|
|
// @ts-ignore
|
|
body.customData = additionalFields.customData.customProperty as IDealCustomProperty[];
|
|
}
|
|
|
|
}
|
|
|
|
const endpoint = 'api/opportunity';
|
|
responseData = await agileCrmApiRequest.call(this, 'POST', endpoint, body);
|
|
|
|
} 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;
|
|
|
|
if (additionalFieldsJson !== '') {
|
|
|
|
if (validateJSON(additionalFieldsJson) !== undefined) {
|
|
|
|
Object.assign(body, JSON.parse(additionalFieldsJson));
|
|
|
|
} else {
|
|
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;
|
|
|
|
if (additionalFields.expectedValue) {
|
|
body.expected_value = additionalFields.expectedValue as number;
|
|
}
|
|
|
|
if (additionalFields.name) {
|
|
body.name = additionalFields.name as string;
|
|
}
|
|
|
|
if (additionalFields.probability) {
|
|
body.probability = additionalFields.probability as number;
|
|
}
|
|
|
|
if (additionalFields.contactIds) {
|
|
body.contactIds = additionalFields.contactIds as string[];
|
|
}
|
|
|
|
if (additionalFields.customData) {
|
|
// @ts-ignore
|
|
body.customData = additionalFields.customData.customProperty as IDealCustomProperty[];
|
|
}
|
|
|
|
}
|
|
|
|
const endpoint = 'api/opportunity/partial-update';
|
|
responseData = await agileCrmApiRequest.call(this, 'PUT', endpoint, body);
|
|
}
|
|
}
|
|
|
|
if (Array.isArray(responseData)) {
|
|
returnData.push.apply(returnData, responseData as IDataObject[]);
|
|
} else {
|
|
returnData.push(responseData as IDataObject);
|
|
}
|
|
|
|
}
|
|
|
|
return [this.helpers.returnJsonArray(returnData)];
|
|
}
|
|
|
|
}
|