From 08220195a25ef1a61739ff7919f2f3bc30abd8ea Mon Sep 17 00:00:00 2001 From: Rupenieks Date: Tue, 5 May 2020 17:44:02 +0200 Subject: [PATCH] Deal Get / Getall / Delete / update / create --- .../nodes/AgileCrm/AgileCrm.node.ts | 94 +++++++ .../nodes/AgileCrm/DealDescription.ts | 260 ++++++++---------- .../nodes/AgileCrm/DealInterface.ts | 19 ++ .../nodes/AgileCrm/GenericFunctions.ts | 1 + 4 files changed, 236 insertions(+), 138 deletions(-) create mode 100644 packages/nodes-base/nodes/AgileCrm/DealInterface.ts diff --git a/packages/nodes-base/nodes/AgileCrm/AgileCrm.node.ts b/packages/nodes-base/nodes/AgileCrm/AgileCrm.node.ts index dbdf00ee1..50d0cc548 100644 --- a/packages/nodes-base/nodes/AgileCrm/AgileCrm.node.ts +++ b/packages/nodes-base/nodes/AgileCrm/AgileCrm.node.ts @@ -23,6 +23,7 @@ import { import { agileCrmApiRequest, validateJSON, agileCrmApiRequestUpdate} from './GenericFunctions'; import { IContact, IProperty, IContactUpdate } from './ContactInterface'; +import { IDeal } from './DealInterface'; export class AgileCrm implements INodeType { @@ -483,6 +484,99 @@ export class AgileCrm implements INodeType { responseData = await agileCrmApiRequest.call(this, 'GET', endpoint, {}); } } + + 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 Error('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[]; + } + + } + + let endpoint = 'api/opportunity' + responseData = await agileCrmApiRequest.call(this, 'POST', endpoint, body); + } + + 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 Error('Additional fields must be a 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[]; + } + + } + + let endpoint = 'api/opportunity/partial-update' + responseData = await agileCrmApiRequest.call(this, 'PUT', endpoint, body); + } } diff --git a/packages/nodes-base/nodes/AgileCrm/DealDescription.ts b/packages/nodes-base/nodes/AgileCrm/DealDescription.ts index 232eb0a79..4b947bda6 100644 --- a/packages/nodes-base/nodes/AgileCrm/DealDescription.ts +++ b/packages/nodes-base/nodes/AgileCrm/DealDescription.ts @@ -116,7 +116,7 @@ export const dealFields = [ { displayName: 'Close Date', name: 'closeDate', - type: 'string', + type: 'dateTime', required: true, displayOptions: { show: { @@ -126,6 +126,9 @@ export const dealFields = [ operation: [ 'create', ], + jsonParameters: [ + false, + ], }, }, default: '', @@ -148,6 +151,9 @@ export const dealFields = [ operation: [ 'create', ], + jsonParameters: [ + false, + ], }, }, default: 1, @@ -166,6 +172,9 @@ export const dealFields = [ operation: [ 'create', ], + jsonParameters: [ + false, + ], }, }, default: '', @@ -188,6 +197,9 @@ export const dealFields = [ operation: [ 'create', ], + jsonParameters: [ + false, + ], }, }, default: 50, @@ -206,6 +218,9 @@ export const dealFields = [ operation: [ 'create', ], + jsonParameters: [ + false, + ], }, }, default: '', @@ -324,14 +339,34 @@ export const dealFields = [ ] }, - +/* -------------------------------------------------------------------------- */ +/* deal:delete */ +/* -------------------------------------------------------------------------- */ +{ + displayName: 'Deal ID', + name: 'dealId', + type: 'string', + required: true, + displayOptions: { + show: { + resource: [ + 'deal', + ], + operation: [ + 'delete', + ], + }, + }, + default: '', + description: 'Unique identifier for a particular deal', +}, /* -------------------------------------------------------------------------- */ /* deal:update */ /* -------------------------------------------------------------------------- */ { - displayName: 'Close Date', - name: 'closeDate', + displayName: 'Deal ID', + name: 'dealId', type: 'string', required: true, displayOptions: { @@ -345,88 +380,9 @@ export const dealFields = [ }, }, default: '', - description: 'Closing date of deal.', - }, - { - displayName: 'Expected Value', - name: 'expectedValue', - type: 'number', - required: true, - typeOptions: { - minValue: 0, - maxValue: 10000 - }, - displayOptions: { - show: { - resource: [ - 'deal', - ], - operation: [ - 'update', - ], - }, - }, - default: '', - description: 'Expected Value of deal.', - }, - { - displayName: 'Milestone', - name: 'milestone', - type: 'string', - required: true, - displayOptions: { - show: { - resource: [ - 'deal', - ], - operation: [ - 'update', - ], - }, - }, - default: '', - description: 'Milestone of deal.', - }, - { - displayName: 'Probability', - name: 'probability', - type: 'number', - required: true, - typeOptions: { - minValue: 0, - maxValue: 100 - }, - displayOptions: { - show: { - resource: [ - 'deal', - ], - operation: [ - 'update', - ], - }, - }, - default: 50, - description: 'Expected Value of deal.', - }, - { - displayName: 'Name', - name: 'name', - type: 'string', - required: true, - displayOptions: { - show: { - resource: [ - 'deal', - ], - operation: [ - 'update', - ], - }, - }, - default: '', - description: 'Name of deal.', + description: 'Unique identifier for a particular deal', }, + { displayName: 'JSON Parameters', name: 'jsonParameters', @@ -469,39 +425,11 @@ export const dealFields = [ description: `Object of values to set as described here.`, }, { - displayName: 'Contact IDs', - name: 'contactIds', - type: 'string', - typeOptions: { - multipleValues: true, - multipleValueButtonText: 'Add Contact ID', - }, - displayOptions: { - show: { - resource: [ - 'deal', - ], - operation: [ - 'update', - ], - jsonParameters: [ - false, - ], - }, - }, - default: [], - placeholder: 'Id', - description: 'Id numbers of contacts.', - }, - { - displayName: 'Custom Data', - name: 'customData', - type: 'fixedCollection', - required: false, - description: 'Custom Data.', - typeOptions: { - multipleValues: true, - }, + displayName: 'Additional Fields', + name: 'additionalFields', + type: 'collection', + placeholder: 'Add Field', + default: {}, displayOptions: { show: { resource: [ @@ -517,30 +445,86 @@ export const dealFields = [ }, options: [ { - displayName: 'Data Properties', - name: 'customDataProperties', - values: [ + displayName: 'Expected Value', + name: 'expectedValue', + type: 'number', + required: false, + typeOptions: { + minValue: 0, + maxValue: 10000 + }, + default: '', + description: 'Expected Value of deal.', + }, + { + displayName: 'Probability', + name: 'probability', + type: 'number', + required: false, + typeOptions: { + minValue: 0, + maxValue: 100 + }, + default: 50, + description: 'Expected Value of deal.', + }, + { + displayName: 'Name', + name: 'name', + type: 'string', + required: false, + default: '', + description: 'Name of deal.', + }, + { + displayName: 'Contact Ids', + name: 'contactIds', + type: 'string', + typeOptions: { + multipleValues: true, + multipleValueButtonText: 'Add ID', + }, + default: [], + placeholder: 'ID', + description: 'Unique contact identifiers.', + }, + { + displayName: 'Custom Data', + name: 'customData', + type: 'fixedCollection', + required: false, + description: 'Custom Data', + typeOptions: { + multipleValues: true, + }, + options: [ { - displayName: 'Name', - name: 'name', - type: 'string', - required: true, - default: "", - placeholder: 'name', - description: 'Name of property', + displayName: 'Property', + name: 'customProperty', + values: [ + { + displayName: 'Name', + name: 'name', + type: 'string', + required: true, + default: "", + placeholder: '', + description: 'Property name.' + }, + { + displayName: 'Value', + name: 'value', + type: 'string', + required: false, + default: "", + placeholder: '', + description: 'Property value.', + } + ] }, - { - displayName: 'Value', - name: 'value', - type: 'string', - required: true, - default: "", - placeholder: '', - description: 'Value of property', - } + ] }, - ] }, diff --git a/packages/nodes-base/nodes/AgileCrm/DealInterface.ts b/packages/nodes-base/nodes/AgileCrm/DealInterface.ts new file mode 100644 index 000000000..797fddcb7 --- /dev/null +++ b/packages/nodes-base/nodes/AgileCrm/DealInterface.ts @@ -0,0 +1,19 @@ +import { + IDataObject, + } from 'n8n-workflow'; + + export interface IDealCustomProperty { + name: string; + value: string; +} + +export interface IDeal { + id?: number, + expected_value?: number, + probability?: number, + name?: string, + close_date?: number, + milestone?: string, + contactIds?: string[], + customData?: IDealCustomProperty[] +} \ No newline at end of file diff --git a/packages/nodes-base/nodes/AgileCrm/GenericFunctions.ts b/packages/nodes-base/nodes/AgileCrm/GenericFunctions.ts index eb7497f2f..897a17a3f 100644 --- a/packages/nodes-base/nodes/AgileCrm/GenericFunctions.ts +++ b/packages/nodes-base/nodes/AgileCrm/GenericFunctions.ts @@ -35,6 +35,7 @@ export async function agileCrmApiRequest(this: IHookFunctions | IExecuteFunction if(method !== "GET" && method !== "DELETE"){ options.body = body; } + try { return await this.helpers.request!(options);