Improvements to Pipedrive node (#707)

This commit is contained in:
Ricardo Espinoza 2020-06-29 09:38:50 -04:00 committed by GitHub
parent 4d5c166414
commit 13f71d3af0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 83 additions and 3 deletions

View file

@ -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<any>}
*/
export async function pipedriveApiRequest(this: IHookFunctions | IExecuteFunctions, method: string, endpoint: string, body: IDataObject, query?: IDataObject, formData?: IDataObject, downloadFile?: boolean): Promise<any> { // 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<any> { // 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) {

View file

@ -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<INodePropertyOptions[]> {
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<INodeExecutionData[][]> {
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 {