From 13f71d3af0308e8a95e3db90d0681d0e344fda3b Mon Sep 17 00:00:00 2001 From: Ricardo Espinoza Date: Mon, 29 Jun 2020 09:38:50 -0400 Subject: [PATCH] :zap: Improvements to Pipedrive node (#707) --- .../nodes/Pipedrive/GenericFunctions.ts | 9 ++- .../nodes/Pipedrive/Pipedrive.node.ts | 77 +++++++++++++++++++ 2 files changed, 83 insertions(+), 3 deletions(-) diff --git a/packages/nodes-base/nodes/Pipedrive/GenericFunctions.ts b/packages/nodes-base/nodes/Pipedrive/GenericFunctions.ts index 32e8194359..33bf215b12 100644 --- a/packages/nodes-base/nodes/Pipedrive/GenericFunctions.ts +++ b/packages/nodes-base/nodes/Pipedrive/GenericFunctions.ts @@ -5,10 +5,12 @@ import { import { IDataObject, + ILoadOptionsFunctions, } from 'n8n-workflow'; -import { OptionsWithUri } from 'request'; - +import { + OptionsWithUri, +} from 'request'; export interface ICustomInterface { name: string; @@ -33,7 +35,7 @@ export interface ICustomProperties { * @param {object} body * @returns {Promise} */ -export async function pipedriveApiRequest(this: IHookFunctions | IExecuteFunctions, method: string, endpoint: string, body: IDataObject, query?: IDataObject, formData?: IDataObject, downloadFile?: boolean): Promise { // tslint:disable-line:no-any +export async function pipedriveApiRequest(this: IHookFunctions | IExecuteFunctions | ILoadOptionsFunctions, method: string, endpoint: string, body: IDataObject, query?: IDataObject, formData?: IDataObject, downloadFile?: boolean): Promise { // tslint:disable-line:no-any const credentials = this.getCredentials('pipedriveApi'); if (credentials === undefined) { throw new Error('No credentials got returned!'); @@ -66,6 +68,7 @@ export async function pipedriveApiRequest(this: IHookFunctions | IExecuteFunctio } try { + //@ts-ignore const responseData = await this.helpers.request(options); if (downloadFile === true) { diff --git a/packages/nodes-base/nodes/Pipedrive/Pipedrive.node.ts b/packages/nodes-base/nodes/Pipedrive/Pipedrive.node.ts index 8ae9c42630..a5cd93ab9f 100644 --- a/packages/nodes-base/nodes/Pipedrive/Pipedrive.node.ts +++ b/packages/nodes-base/nodes/Pipedrive/Pipedrive.node.ts @@ -1,12 +1,14 @@ import { BINARY_ENCODING, IExecuteFunctions, + ILoadOptionsFunctions, } from 'n8n-core'; import { IDataObject, INodeTypeDescription, INodeExecutionData, INodeType, + INodePropertyOptions, } from 'n8n-workflow'; import { @@ -2047,9 +2049,68 @@ export class Pipedrive implements INodeType { description: 'How many results to return.', }, + // ---------------------------------- + // person:getAll + // ---------------------------------- + { + displayName: 'Additional Fields', + name: 'additionalFields', + type: 'collection', + placeholder: 'Add Field', + displayOptions: { + show: { + operation: [ + 'getAll', + ], + resource: [ + 'person', + ], + }, + }, + default: {}, + options: [ + { + displayName: 'Filter ID', + name: 'filterId', + type: 'options', + typeOptions: { + loadOptionsMethod: 'getFilters', + }, + default: '', + description: 'ID of the filter to use.', + }, + { + displayName: 'First Char', + name: 'firstChar', + type: 'string', + default: '', + description: 'If supplied, only persons whose name starts with the specified letter will be returned ', + }, + ], + }, ], }; + methods = { + loadOptions: { + // Get all the filters to display them to user so that he can + // select them easily + async getFilters(this: ILoadOptionsFunctions): Promise { + const returnData: INodePropertyOptions[] = []; + const { data } = await pipedriveApiRequest.call(this, 'GET', '/filters', {}, { type: 'people' }); + for (const filter of data) { + const filterName = filter.name; + const filterId = filter.id; + returnData.push({ + name: filterName, + value: filterId, + }); + } + return returnData; + }, + } + }; + async execute(this: IExecuteFunctions): Promise { const items = this.getInputData(); @@ -2453,6 +2514,16 @@ export class Pipedrive implements INodeType { qs.limit = this.getNodeParameter('limit', i) as number; } + const additionalFields = this.getNodeParameter('additionalFields', i) as IDataObject; + + if (additionalFields.filterId) { + qs.filter_id = additionalFields.filterId as string; + } + + if (additionalFields.firstChar) { + qs.first_char = additionalFields.firstChar as string; + } + endpoint = `/persons`; } else if (operation === 'update') { @@ -2499,6 +2570,7 @@ export class Pipedrive implements INodeType { } responseData = await pipedriveApiRequest.call(this, requestMethod, endpoint, body, qs, formData, downloadFile); + } if (resource === 'file' && operation === 'download') { @@ -2520,6 +2592,11 @@ export class Pipedrive implements INodeType { items[i].binary![binaryPropertyName] = await this.helpers.prepareBinaryData(responseData.data); } else { + + if (responseData.data === null) { + responseData.data = []; + } + if (Array.isArray(responseData.data)) { returnData.push.apply(returnData, responseData.data as IDataObject[]); } else {