Add search operation to resource person

This commit is contained in:
ricardo 2020-07-09 16:23:17 -04:00
parent c152c0f959
commit 5adec0a299
2 changed files with 125 additions and 4 deletions

View file

@ -48,6 +48,9 @@ export async function pipedriveApiRequest(this: IHookFunctions | IExecuteFunctio
query.api_token = credentials.apiToken;
const options: OptionsWithUri = {
headers: {
"Accept": "application/json",
},
method,
qs: query,
uri: `https://api.pipedrive.com/v1${endpoint}`,
@ -93,7 +96,7 @@ export async function pipedriveApiRequest(this: IHookFunctions | IExecuteFunctio
if (error.response && error.response.body && error.response.body.error) {
// Try to return the error prettier
let errorMessage = `Pipedrive error response [${error.statusCode}]: ${error.response.body.error}`;
let errorMessage = `Pipedrive error response [${error.statusCode}]: ${error.response.body.error.message}`;
if (error.response.body.error_info) {
errorMessage += ` - ${error.response.body.error_info}`;
}
@ -124,7 +127,7 @@ export async function pipedriveApiRequestAllItems(this: IHookFunctions | IExecut
if (query === undefined) {
query = {};
}
query.limit = 500;
query.limit = 100;
query.start = 0;
const returnData: IDataObject[] = [];
@ -133,7 +136,12 @@ export async function pipedriveApiRequestAllItems(this: IHookFunctions | IExecut
do {
responseData = await pipedriveApiRequest.call(this, method, endpoint, body, query);
returnData.push.apply(returnData, responseData.data);
// the search path returns data diferently
if (responseData.data.items) {
returnData.push.apply(returnData, responseData.data.items);
} else {
returnData.push.apply(returnData, responseData.data);
}
query.start = responseData.additionalData.pagination.next_start;
} while (

View file

@ -25,7 +25,6 @@ interface CustomProperty {
value: string;
}
/**
* Add the additional fields to the body
*
@ -362,6 +361,11 @@ export class Pipedrive implements INodeType {
value: 'getAll',
description: 'Get data of all persons',
},
{
name: 'Search',
value: 'search',
description: 'Search all persons',
},
{
name: 'Update',
value: 'update',
@ -2021,6 +2025,7 @@ export class Pipedrive implements INodeType {
show: {
operation: [
'getAll',
'search',
],
},
},
@ -2035,6 +2040,7 @@ export class Pipedrive implements INodeType {
show: {
operation: [
'getAll',
'search',
],
returnAll: [
false,
@ -2088,6 +2094,74 @@ export class Pipedrive implements INodeType {
},
],
},
// ----------------------------------
// person:search
// ----------------------------------
{
displayName: 'Term',
name: 'term',
type: 'string',
displayOptions: {
show: {
operation: [
'search',
],
resource: [
'person',
],
},
},
default: '',
description: 'The search term to look for. Minimum 2 characters (or 1 if using exact_match).',
},
{
displayName: 'Additional Fields',
name: 'additionalFields',
type: 'collection',
placeholder: 'Add Field',
displayOptions: {
show: {
operation: [
'search',
],
resource: [
'person',
],
},
},
default: {},
options: [
{
displayName: 'Exact Match',
name: 'exactMatch',
type: 'boolean',
default: false,
description: 'When enabled, only full exact matches against the given term are returned. It is not case sensitive.',
},
{
displayName: 'Fields',
name: 'fields',
type: 'string',
default: '',
description: 'A comma-separated string array. The fields to perform the search from. Defaults to all of them.',
},
{
displayName: 'Include Fields',
name: 'includeFields',
type: 'string',
default: '',
description: 'Supports including optional fields in the results which are not provided by default.',
},
{
displayName: 'Organization ID',
name: 'organizationId',
type: 'string',
default: '',
description: 'Will filter Deals by the provided Organization ID.',
},
],
},
],
};
@ -2526,6 +2600,39 @@ export class Pipedrive implements INodeType {
endpoint = `/persons`;
} else if (operation === 'search') {
// ----------------------------------
// persons:search
// ----------------------------------
requestMethod = 'GET';
qs.term = this.getNodeParameter('term', 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.fields) {
qs.fields = additionalFields.fields as string;
}
if (additionalFields.exactMatch) {
qs.exact_match = additionalFields.exactMatch as boolean;
}
if (additionalFields.organizationId) {
qs.organization_id = parseInt(additionalFields.organizationId as string, 10);
}
if (additionalFields.includeFields) {
qs.include_fields = additionalFields.includeFields as string;
}
endpoint = `/persons/search`;
} else if (operation === 'update') {
// ----------------------------------
// person:update
@ -2562,7 +2669,9 @@ export class Pipedrive implements INodeType {
let responseData;
if (returnAll === true) {
responseData = await pipedriveApiRequestAllItems.call(this, requestMethod, endpoint, body, qs);
} else {
if (customProperties !== undefined) {
@ -2597,6 +2706,10 @@ export class Pipedrive implements INodeType {
responseData.data = [];
}
if (operation === 'search' && responseData.data && responseData.data.items) {
responseData.data = responseData.data.items;
}
if (Array.isArray(responseData.data)) {
returnData.push.apply(returnData, responseData.data as IDataObject[]);
} else {