From 6c50c84ab1fc52c150ab4d6a4486f1c03464cb5e Mon Sep 17 00:00:00 2001 From: Rodrigo Correia Date: Thu, 16 Sep 2021 14:09:19 -0300 Subject: [PATCH 1/3] Pipedrive - Get Activities from Deal Id --- .../nodes/Pipedrive/Pipedrive.node.ts | 126 +++++++++++++++++- 1 file changed, 125 insertions(+), 1 deletion(-) diff --git a/packages/nodes-base/nodes/Pipedrive/Pipedrive.node.ts b/packages/nodes-base/nodes/Pipedrive/Pipedrive.node.ts index 97b712e61..1ada18b0c 100644 --- a/packages/nodes-base/nodes/Pipedrive/Pipedrive.node.ts +++ b/packages/nodes-base/nodes/Pipedrive/Pipedrive.node.ts @@ -118,6 +118,10 @@ export class Pipedrive implements INodeType { name: 'Deal', value: 'deal', }, + { + name: 'Deal Activity', + value: 'dealActivity', + }, { name: 'Deal Product', value: 'dealProduct', @@ -250,6 +254,27 @@ export class Pipedrive implements INodeType { description: 'The operation to perform.', }, + { + displayName: 'Operation', + name: 'operation', + type: 'options', + displayOptions: { + show: { + resource: [ + 'dealActivity', + ], + }, + }, + options: [ + { + name: 'Get All', + value: 'getAll', + description: 'Get all products in a deal', + }, + ], + default: 'getAll', + }, + { displayName: 'Operation', name: 'operation', @@ -3423,6 +3448,76 @@ export class Pipedrive implements INodeType { description: 'How many results to return.', }, + // ---------------------------------- + // dealActivities:getAll + // ---------------------------------- + { + displayName: 'Deal ID', + name: 'dealId', + type: 'options', + default: '', + typeOptions: { + loadOptionsMethod: 'getDeals', + }, + required: true, + displayOptions: { + show: { + operation: [ + 'getAll', + ], + resource: [ + 'dealActivity', + ], + }, + }, + description: 'The ID of the deal whose products to retrieve', + }, + { + displayName: 'Additional Fields', + name: 'additionalFields', + type: 'collection', + placeholder: 'Add Field', + displayOptions: { + show: { + operation: [ + 'getAll', + ], + resource: [ + 'dealActivity', + ], + }, + }, + default: {}, + options: [ + { + displayName: 'Done', + name: 'done', + type: 'options', + options: [ + { + name: 'Not done', + value: '0', + }, + { + name: 'Done', + value: '1', + }, + ], + default: '0', + description: 'Whether the activity is done or not.', + }, + { + displayName: 'Exclude Activity Ids', + name: 'exclude', + type: 'string', + typeOptions: { + rows: 3, + }, + default: '', + description: 'A comma separated Activity Ids, to exclude from result. Ex. 4, 9, 11, ...', + }, + ], + }, // ---------------------------------------- // lead: getAll // ---------------------------------------- @@ -4387,6 +4482,35 @@ export class Pipedrive implements INodeType { } + } else if (resource === 'dealActivity') { + + if (operation === 'getAll') { + // ---------------------------------- + // dealActivity: getAll + // ---------------------------------- + + requestMethod = 'GET'; + const dealId = this.getNodeParameter('dealId', i) as string; + + returnAll = this.getNodeParameter('returnAll', i) as boolean; + + if (returnAll === false) { + qs.limit = this.getNodeParameter('limit', i) as number; + } + + const additionalFields = this.getNodeParameter('additionalFields', i) as IDataObject; + + if (additionalFields.exclude) { + qs.exclude = (additionalFields.exclude as string); + } + + if (additionalFields.done) { + qs.done = parseInt(additionalFields.done as string); + } + + endpoint = `/deals/${dealId}/activities`; + + } } else if (resource === 'dealProduct') { if (operation === 'add') { @@ -4982,7 +5106,7 @@ export class Pipedrive implements INodeType { returnData.push(responseData.data as IDataObject); } } - } catch (error) { + } catch (error: any) { if (this.continueOnFail()) { if (resource === 'file' && operation === 'download') { items[i].json = { error: error.message }; From 9e2298eb73046dce25dbef6c7fecd1dd1629c250 Mon Sep 17 00:00:00 2001 From: Jan Oberhauser Date: Sat, 9 Oct 2021 14:20:04 -0500 Subject: [PATCH 2/3] :zap: Use shared sort function --- .../nodes/Pipedrive/GenericFunctions.ts | 14 +++ .../nodes/Pipedrive/Pipedrive.node.ts | 115 +++--------------- 2 files changed, 31 insertions(+), 98 deletions(-) diff --git a/packages/nodes-base/nodes/Pipedrive/GenericFunctions.ts b/packages/nodes-base/nodes/Pipedrive/GenericFunctions.ts index b9efb550e..89b6338bb 100644 --- a/packages/nodes-base/nodes/Pipedrive/GenericFunctions.ts +++ b/packages/nodes-base/nodes/Pipedrive/GenericFunctions.ts @@ -6,6 +6,7 @@ import { import { IDataObject, ILoadOptionsFunctions, + INodePropertyOptions, NodeApiError, NodeOperationError, } from 'n8n-workflow'; @@ -261,3 +262,16 @@ export function pipedriveResolveCustomProperties(customProperties: ICustomProper } } + + +export function sortOptionParameters(optionParameters: INodePropertyOptions[]): INodePropertyOptions[] { + optionParameters.sort((a, b) => { + const aName = a.name.toLowerCase(); + const bName = b.name.toLowerCase(); + if (aName < bName) { return -1; } + if (aName > bName) { return 1; } + return 0; + }); + + return optionParameters; +} diff --git a/packages/nodes-base/nodes/Pipedrive/Pipedrive.node.ts b/packages/nodes-base/nodes/Pipedrive/Pipedrive.node.ts index 1ada18b0c..f3d234523 100644 --- a/packages/nodes-base/nodes/Pipedrive/Pipedrive.node.ts +++ b/packages/nodes-base/nodes/Pipedrive/Pipedrive.node.ts @@ -19,6 +19,7 @@ import { pipedriveEncodeCustomProperties, pipedriveGetCustomProperties, pipedriveResolveCustomProperties, + sortOptionParameters, } from './GenericFunctions'; import { @@ -3902,15 +3903,7 @@ export class Pipedrive implements INodeType { }); } - returnData.sort((a, b) => { - const aName = a.name.toLowerCase(); - const bName = b.name.toLowerCase(); - if (aName < bName) { return -1; } - if (aName > bName) { return 1; } - return 0; - }); - - return returnData; + return sortOptionParameters(returnData); }, // Get all Filters to display them to user so that he can // select them easily @@ -3931,15 +3924,7 @@ export class Pipedrive implements INodeType { }); } - returnData.sort((a, b) => { - const aName = a.name.toLowerCase(); - const bName = b.name.toLowerCase(); - if (aName < bName) { return -1; } - if (aName > bName) { return 1; } - return 0; - }); - - return returnData; + return sortOptionParameters(returnData); }, // Get all Organizations to display them to user so that he can // select them easily @@ -3953,15 +3938,7 @@ export class Pipedrive implements INodeType { }); } - returnData.sort((a, b) => { - const aName = a.name.toLowerCase(); - const bName = b.name.toLowerCase(); - if (aName < bName) { return -1; } - if (aName > bName) { return 1; } - return 0; - }); - - return returnData; + return sortOptionParameters(returnData); }, // Get all Organizations to display them to user so that he can // select them easily @@ -3977,15 +3954,7 @@ export class Pipedrive implements INodeType { } } - returnData.sort((a, b) => { - const aName = a.name.toLowerCase(); - const bName = b.name.toLowerCase(); - if (aName < bName) { return -1; } - if (aName > bName) { return 1; } - return 0; - }); - - return returnData; + return sortOptionParameters(returnData); }, // Get all Deals to display them to user so that he can // select them easily @@ -3993,7 +3962,7 @@ export class Pipedrive implements INodeType { const { data } = await pipedriveApiRequest.call(this, 'GET', '/deals', {}) as { data: Array<{ id: string; title: string; }> }; - return data.map(({ id, title }) => ({ value: id, name: title })); + return sortOptionParameters(data.map(({ id, title }) => ({ value: id, name: title }))); }, // Get all Products to display them to user so that he can // select them easily @@ -4001,7 +3970,7 @@ export class Pipedrive implements INodeType { const { data } = await pipedriveApiRequest.call(this, 'GET', '/products', {}) as { data: Array<{ id: string; name: string; }> }; - return data.map(({ id, name }) => ({ value: id, name })); + return sortOptionParameters(data.map(({ id, name }) => ({ value: id, name }))); }, // Get all Products related to a deal and display them to user so that he can // select them easily @@ -4011,7 +3980,7 @@ export class Pipedrive implements INodeType { const { data } = await pipedriveApiRequest.call(this, 'GET', `/deals/${dealId}/products`, {}) as { data: Array<{ id: string; name: string; }> }; - return data.map(({ id, name }) => ({ value: id, name })); + return sortOptionParameters(data.map(({ id, name }) => ({ value: id, name }))); }, // Get all Stages to display them to user so that he can // select them easily @@ -4025,15 +3994,7 @@ export class Pipedrive implements INodeType { }); } - returnData.sort((a, b) => { - const aName = a.name.toLowerCase(); - const bName = b.name.toLowerCase(); - if (aName < bName) { return -1; } - if (aName > bName) { return 1; } - return 0; - }); - - return returnData; + return sortOptionParameters(returnData); }, // Get all the Organization Custom Fields to display them to user so that he can // select them easily @@ -4049,15 +4010,7 @@ export class Pipedrive implements INodeType { } } - returnData.sort((a, b) => { - const aName = a.name.toLowerCase(); - const bName = b.name.toLowerCase(); - if (aName < bName) { return -1; } - if (aName > bName) { return 1; } - return 0; - }); - - return returnData; + return sortOptionParameters(returnData); }, // Get all the Deal Custom Fields to display them to user so that he can // select them easily @@ -4073,15 +4026,7 @@ export class Pipedrive implements INodeType { } } - returnData.sort((a, b) => { - const aName = a.name.toLowerCase(); - const bName = b.name.toLowerCase(); - if (aName < bName) { return -1; } - if (aName > bName) { return 1; } - return 0; - }); - - return returnData; + return sortOptionParameters(returnData); }, // Get all the Person Custom Fields to display them to user so that he can // select them easily @@ -4097,15 +4042,7 @@ export class Pipedrive implements INodeType { } } - returnData.sort((a, b) => { - const aName = a.name.toLowerCase(); - const bName = b.name.toLowerCase(); - if (aName < bName) { return -1; } - if (aName > bName) { return 1; } - return 0; - }); - - return returnData; + return sortOptionParameters(returnData); }, // Get all the person labels to display them to user so that he can // select them easily @@ -4126,13 +4063,7 @@ export class Pipedrive implements INodeType { } } - returnData.sort((a, b) => { - const aName = a.name.toLowerCase(); - const bName = b.name.toLowerCase(); - if (aName < bName) { return -1; } - if (aName > bName) { return 1; } - return 0; - }); + sortOptionParameters(returnData); if (operation === 'update') { returnData.push({ @@ -4161,13 +4092,7 @@ export class Pipedrive implements INodeType { } } - returnData.sort((a, b) => { - const aName = a.name.toLowerCase(); - const bName = b.name.toLowerCase(); - if (aName < bName) { return -1; } - if (aName > bName) { return 1; } - return 0; - }); + sortOptionParameters(returnData); if (operation === 'update') { returnData.push({ @@ -4185,7 +4110,7 @@ export class Pipedrive implements INodeType { data: Array<{ id: string; name: string; }> }; - return data.map(({ id, name }) => ({ value: id, name })); + return sortOptionParameters(data.map(({ id, name }) => ({ value: id, name }))); }, // Get all the lead labels to display them to user so that he can @@ -4195,7 +4120,7 @@ export class Pipedrive implements INodeType { data: Array<{ id: string; name: string; }> }; - return data.map(({ id, name }) => ({ value: id, name })); + return sortOptionParameters(data.map(({ id, name }) => ({ value: id, name }))); }, // Get all the labels to display them to user so that he can @@ -4217,13 +4142,7 @@ export class Pipedrive implements INodeType { } } - returnData.sort((a, b) => { - const aName = a.name.toLowerCase(); - const bName = b.name.toLowerCase(); - if (aName < bName) { return -1; } - if (aName > bName) { return 1; } - return 0; - }); + sortOptionParameters(returnData); if (operation === 'update') { returnData.push({ From 8a39e92348720dd3c7f0307766a3b78d981a93c0 Mon Sep 17 00:00:00 2001 From: Jan Oberhauser Date: Sat, 9 Oct 2021 14:32:51 -0500 Subject: [PATCH 3/3] :zap: Some fixes and improvements --- .../nodes/Pipedrive/Pipedrive.node.ts | 30 ++++++------------- 1 file changed, 9 insertions(+), 21 deletions(-) diff --git a/packages/nodes-base/nodes/Pipedrive/Pipedrive.node.ts b/packages/nodes-base/nodes/Pipedrive/Pipedrive.node.ts index f3d234523..175d62876 100644 --- a/packages/nodes-base/nodes/Pipedrive/Pipedrive.node.ts +++ b/packages/nodes-base/nodes/Pipedrive/Pipedrive.node.ts @@ -270,7 +270,7 @@ export class Pipedrive implements INodeType { { name: 'Get All', value: 'getAll', - description: 'Get all products in a deal', + description: 'Get all activities of a deal', }, ], default: 'getAll', @@ -3471,7 +3471,7 @@ export class Pipedrive implements INodeType { ], }, }, - description: 'The ID of the deal whose products to retrieve', + description: 'The ID of the deal whose activity to retrieve', }, { displayName: 'Additional Fields', @@ -3493,32 +3493,20 @@ export class Pipedrive implements INodeType { { displayName: 'Done', name: 'done', - type: 'options', - options: [ - { - name: 'Not done', - value: '0', - }, - { - name: 'Done', - value: '1', - }, - ], - default: '0', + type: 'boolean', + default: false, description: 'Whether the activity is done or not.', }, { - displayName: 'Exclude Activity Ids', + displayName: 'Exclude Activity IDs', name: 'exclude', type: 'string', - typeOptions: { - rows: 3, - }, default: '', description: 'A comma separated Activity Ids, to exclude from result. Ex. 4, 9, 11, ...', }, ], }, + // ---------------------------------------- // lead: getAll // ---------------------------------------- @@ -4423,8 +4411,8 @@ export class Pipedrive implements INodeType { qs.exclude = (additionalFields.exclude as string); } - if (additionalFields.done) { - qs.done = parseInt(additionalFields.done as string); + if (additionalFields && additionalFields.done !== undefined) { + qs.done = additionalFields.done === true ? 1 : 0; } endpoint = `/deals/${dealId}/activities`; @@ -5025,7 +5013,7 @@ export class Pipedrive implements INodeType { returnData.push(responseData.data as IDataObject); } } - } catch (error: any) { + } catch (error) { if (this.continueOnFail()) { if (resource === 'file' && operation === 'download') { items[i].json = { error: error.message };