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

625 lines
18 KiB
TypeScript
Raw Normal View History

import type {
IExecuteFunctions,
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,
2020-04-30 05:03:36 -07:00
} from 'n8n-workflow';
import { jsonParse, NodeOperationError } from 'n8n-workflow';
2020-04-30 05:03:36 -07:00
import { contactFields, contactOperations } from './ContactDescription';
2020-05-05 04:59:45 -07:00
import { companyFields, companyOperations } from './CompanyDescription';
2020-05-05 04:59:45 -07:00
import { dealFields, dealOperations } from './DealDescription';
2020-05-05 07:03:53 -07:00
import type { IContact, IContactUpdate } from './ContactInterface';
import {
agileCrmApiRequest,
agileCrmApiRequestAllItems,
agileCrmApiRequestUpdate,
getFilterRules,
simplifyResponse,
validateJSON,
} from './GenericFunctions';
2020-04-30 05:03:36 -07:00
import type { IDeal } from './DealInterface';
import type { 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',
refactor: Apply more `eslint-plugin-n8n-nodes-base` rules (#3534) * :zap: Update `lintfix` script * :zap: Run baseline `lintfix` * :fire: Remove unneeded exceptions (#3538) * :fire: Remove exceptions for `node-param-default-wrong-for-simplify` * :fire: Remove exceptions for `node-param-placeholder-miscased-id` * :zap: Update version * :shirt: Apply `node-param-placeholder-missing` (#3542) * :shirt: Apply `filesystem-wrong-cred-filename` (#3543) * :shirt: Apply `node-param-description-missing-from-dynamic-options` (#3545) Co-authored-by: Iván Ovejero <ivov.src@gmail.com> * :shirt: Apply `node-class-description-empty-string` (#3546) * :shirt: Apply `node-class-description-icon-not-svg` (#3548) * :shirt: Apply `filesystem-wrong-node-filename` (#3549) Co-authored-by: Iván Ovejero <ivov.src@gmail.com> * :shirt: Expand lintings to credentials (#3550) * :shirt: Apply `node-param-multi-options-type-unsorted-items` (#3552) * :zap: fix * :zap: Minor fixes Co-authored-by: Michael Kret <michael.k@radency.com> * :shirt: Apply `node-param-description-wrong-for-dynamic-multi-options` (#3541) * :zap: Add new lint rule, node-param-description-wrong-for-dynamic-multi-options * :zap: Fix with updated linting rules * :zap: Minor fixes Co-authored-by: Iván Ovejero <ivov.src@gmail.com> * :shirt: Apply `node-param-description-boolean-without-whether` (#3553) * :zap: fix * Update packages/nodes-base/nodes/Clockify/ProjectDescription.ts Co-authored-by: Iván Ovejero <ivov.src@gmail.com> * :shirt: Apply node-param-display-name-wrong-for-dynamic-multi-options (#3537) * :shirt: Add exceptions * :shirt: Add exception * :pencil2: Alphabetize rules * :zap: Restore `lintfix` command Co-authored-by: agobrech <45268029+agobrech@users.noreply.github.com> Co-authored-by: Omar Ajoue <krynble@gmail.com> Co-authored-by: Michael Kret <michael.k@radency.com> Co-authored-by: brianinoa <54530642+brianinoa@users.noreply.github.com> Co-authored-by: Michael Kret <88898367+michael-radency@users.noreply.github.com>
2022-06-20 07:54:01 -07:00
// eslint-disable-next-line n8n-nodes-base/node-class-description-icon-not-svg
2020-05-06 01:27:01 -07:00
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: {
feat(editor): Node creator actions (#4696) * WIP: Node Actions List UI * WIP: Recommended Actions and preseting of fields * WIP: Resource category * :art: Moved actions categorisation to the server * :label: Add missing INodeAction type * :sparkles: Improve SSR categorisation, fix adding of mixed actions * :recycle: Refactor CategorizedItems to composition api, style fixes * WIP: Adding multiple nodes * :recycle: Refactor rest of the NodeCreator component to composition API, conver globalLinkActions to composable * :sparkles: Allow actions dragging, fix search and refactor passing of actions to categorized items * :lipstick: Fix node actions title * Migrate to the pinia store, add posthog feature and various fixes * :bug: Fix filtering of trigger actions when not merged * fix: N8N-5439 — Do not use simple node item when at NodeHelperPanel root * :bug: Design review fixes * :bug: Fix disabling of merged actions * Fix trigger root filtering * :sparkles: Allow for custom node actions parser, introduce hubspot parser * :bug: Fix initial node params validation, fix position of second added node * :bug: Introduce operations category, removed canvas node names overrride, fix API actions display and prevent dragging of action nodes * :sparkles: Prevent NDV auto-open feature flag * :bug: Inject recommened action for trigger nodes without actions * Refactored NodeCreatorNode to Storybook, change filtering of merged nodes for the trigger helper panel, minor fixes * Improve rendering of app nodes and animation * Cleanup, any only enable accordion transition on triggerhelperpanel * Hide node creator scrollbars in Firefox * Minor styles fixes * Do not copy the array in rendering method * Removed unused props * Fix memory leak * Fix categorisation of regular nodes with a single resource * Implement telemetry calls for node actions * Move categorization to FE * Fix client side actions categorisation * Skip custom action show * Only load tooltip for NodeIcon if necessary * Fix lodash startCase import * Remove lodash.startcase * Cleanup * Fix node creator autofocus on "tab" * Prevent posthog getFeatureFlag from crashing * Debugging preview env search issues * Remove logs * Make sure the pre-filled params are update not overwritten * Get rid of transition in itemiterator * WIP: Rough version of NodeActions keyboard navigation, replace nodeCreator composable with Pinia store module * Rewrite to add support for ActionItem to ItemIterator and make CategorizedItems accept items props * Fix category item counter & cleanup * Add APIHint to actions search no-result, clean up NodeCreatorNode * Improve node actions no results message * Remove logging, fix filtering of recommended placeholder category * Remove unused NodeActions component and node merging feature falg * Do not show regular nodes without actions * Make sure to add manual trigger when adding http node via actions hint * Fixed api hint footer line height * Prevent pointer-events od NodeIcon img and remove "this" from template * Address PR points * Fix e2e specs * Make sure canvas ia loaded * Make sure canvas ia loaded before opening nodeCreator in e2e spec * Fix flaky workflows tags e2e getter * Imrpove node creator click outside UX, add manual node to regular nodes added from trigger panel * Add manual trigger node if dragging regular from trigger panel
2022-12-09 01:56:36 -08:00
name: 'Agile CRM',
2020-04-30 05:03:36 -07:00
},
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',
refactor: Apply more nodelinting rules (#3324) * :pencil2: Alphabetize lint rules * :fire: Remove duplicates * :zap: Update `lintfix` script * :shirt: Apply `node-param-operation-without-no-data-expression` (#3329) * :shirt: Apply `node-param-operation-without-no-data-expression` * :shirt: Add exceptions * :shirt: Apply `node-param-description-weak` (#3328) Co-authored-by: Iván Ovejero <ivov.src@gmail.com> * :shirt: Apply `node-param-option-value-duplicate` (#3331) * :shirt: Apply `node-param-description-miscased-json` (#3337) * :shirt: Apply `node-param-display-name-excess-inner-whitespace` (#3335) Co-authored-by: Iván Ovejero <ivov.src@gmail.com> * :shirt: Apply `node-param-type-options-missing-from-limit` (#3336) * Rule workig as intended * :pencil2: Uncomment rules Co-authored-by: Iván Ovejero <ivov.src@gmail.com> * :shirt: Apply `node-param-option-name-duplicate` (#3338) Co-authored-by: Iván Ovejero <ivov.src@gmail.com> * :shirt: Apply `node-param-description-wrong-for-simplify` (#3334) * :zap: fix * :zap: exceptions * :zap: changed rule ignoring from file to line * :shirt: Apply `node-param-resource-without-no-data-expression` (#3339) * :shirt: Apply `node-param-display-name-untrimmed` (#3341) * :shirt: Apply `node-param-display-name-miscased-id` (#3340) Co-authored-by: Iván Ovejero <ivov.src@gmail.com> * :shirt: Apply `node-param-resource-with-plural-option` (#3342) * :shirt: Apply `node-param-description-wrong-for-upsert` (#3333) * :zap: fix * :zap: replaced record with contact in description * :zap: fix Co-authored-by: Iván Ovejero <ivov.src@gmail.com> * :shirt: Apply `node-param-option-description-identical-to-name` (#3343) * :shirt: Apply `node-param-option-name-containing-star` (#3347) * :shirt: Apply `node-param-display-name-wrong-for-update-fields` (#3348) * :shirt: Apply `node-param-option-name-wrong-for-get-all` (#3345) * :zap: fix * :zap: exceptions * :shirt: Apply node-param-display-name-wrong-for-simplify (#3344) * Rule working as intended * Uncomented other rules * :shirt: Undo and add exceptions Co-authored-by: Iván Ovejero <ivov.src@gmail.com> * :zap: Alphabetize lint rules * :zap: Restore `lintfix` script Co-authored-by: Michael Kret <88898367+michael-radency@users.noreply.github.com> Co-authored-by: agobrech <45268029+agobrech@users.noreply.github.com>
2022-05-20 14:47:24 -07:00
noDataExpression: true,
2020-05-06 01:27:01 -07:00
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',
},
// 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);
const operation = this.getNodeParameter('operation', 0);
2020-04-30 05:03:36 -07:00
2020-05-11 11:22:31 -07:00
for (let i = 0; i < items.length; i++) {
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-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);
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.',
{ itemIndex: i },
);
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, jsonParse(filterJsonRules));
2020-05-05 04:59:45 -07:00
} else {
throw new NodeOperationError(this.getNode(), 'Filter (JSON) must be a valid json', {
itemIndex: i,
});
}
}
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);
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') {
const jsonParameters = this.getNodeParameter('jsonParameters', i);
2020-04-30 05:03:36 -07:00
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, jsonParse(additionalFieldsJson));
2020-04-30 05:03:36 -07:00
} else {
throw new NodeOperationError(
this.getNode(),
'Additional fields must be a valid JSON',
{ itemIndex: i },
);
2020-04-30 05:03:36 -07:00
}
}
} else {
const additionalFields = this.getNodeParameter('additionalFields', i);
2020-04-30 05:03:36 -07:00
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) => {
2020-05-05 04:59:45 -07:00
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) => {
2020-05-05 04:59:45 -07:00
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) => {
2020-05-05 04:59:45 -07:00
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 };
const jsonParameters = this.getNodeParameter('jsonParameters', i);
2020-05-05 02:08:30 -07:00
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, jsonParse(additionalFieldsJson));
2020-05-05 02:08:30 -07:00
} else {
throw new NodeOperationError(
this.getNode(),
'Additional fields must be a valid JSON',
{ itemIndex: i },
);
2020-05-05 02:08:30 -07:00
}
}
} else {
const additionalFields = this.getNodeParameter('additionalFields', i);
2020-05-05 02:08:30 -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.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) => {
2020-05-05 04:59:45 -07:00
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) => {
2020-05-05 04:59:45 -07:00
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) => {
2020-05-05 04:59:45 -07:00
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) => {
2020-05-05 02:08:30 -07:00
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) => {
2020-05-05 02:08:30 -07:00
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') {
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);
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);
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);
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, jsonParse(additionalFieldsJson));
} else {
throw new NodeOperationError(
this.getNode(),
'Additional fields must be a valid JSON',
{ itemIndex: i },
);
}
}
} else {
const additionalFields = this.getNodeParameter('additionalFields', i);
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);
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, jsonParse(additionalFieldsJson));
} else {
throw new NodeOperationError(
this.getNode(),
'Additional fields must be valid JSON',
{ itemIndex: i },
);
}
}
} else {
const additionalFields = this.getNodeParameter('additionalFields', i);
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)];
}
}