Improved getall operations

This commit is contained in:
ricardo 2020-03-29 22:08:00 -04:00
parent 0834defedb
commit a86e304d22
6 changed files with 199 additions and 5 deletions

View file

@ -411,6 +411,46 @@ export const conversationFields = [
/* -------------------------------------------------------------------------- */
/* conversation:getAll */
/* -------------------------------------------------------------------------- */
{
displayName: 'Return All',
name: 'returnAll',
type: 'boolean',
displayOptions: {
show: {
operation: [
'getAll',
],
resource: [
'conversation',
],
},
},
default: false,
description: 'If all results should be returned or only up to a given limit.',
},
{
displayName: 'Limit',
name: 'limit',
type: 'number',
displayOptions: {
show: {
operation: [
'getAll',
],
resource: [
'conversation',
],
returnAll: [
false,
],
},
},
typeOptions: {
minValue: 1,
},
default: 50,
description: 'How many results to return.',
},
{
displayName: 'Options',
name: 'options',

View file

@ -567,6 +567,46 @@ export const customerFields = [
/* -------------------------------------------------------------------------- */
/* customer:getAll */
/* -------------------------------------------------------------------------- */
{
displayName: 'Return All',
name: 'returnAll',
type: 'boolean',
displayOptions: {
show: {
operation: [
'getAll',
],
resource: [
'customer',
],
},
},
default: false,
description: 'If all results should be returned or only up to a given limit.',
},
{
displayName: 'Limit',
name: 'limit',
type: 'number',
displayOptions: {
show: {
operation: [
'getAll',
],
resource: [
'customer',
],
returnAll: [
false,
],
},
},
typeOptions: {
minValue: 1,
},
default: 50,
description: 'How many results to return.',
},
{
displayName: 'Options',
name: 'options',

View file

@ -12,6 +12,7 @@ import {
import {
get,
} from 'lodash';
import { queryResult } from 'pg-promise';
export async function helpscoutApiRequest(this: IExecuteFunctions | IExecuteSingleFunctions | ILoadOptionsFunctions | IHookFunctions, method: string, resource: string, body: any = {}, qs: IDataObject = {}, uri?: string, option: IDataObject = {}): Promise<any> { // tslint:disable-line:no-any
let options: OptionsWithUri = {
@ -53,13 +54,15 @@ export async function helpscoutApiRequestAllItems(this: IExecuteFunctions | ILoa
const returnData: IDataObject[] = [];
let responseData;
query.size = 50;
let uri;
do {
responseData = await helpscoutApiRequest.call(this, method, endpoint, body, query, uri);
uri = get(responseData, '_links.next.href');
returnData.push.apply(returnData, get(responseData, propertyName));
if (query.limit && query.limit <= returnData.length) {
return returnData;
}
} while (
responseData['_links'] !== undefined &&
responseData['_links'].next !== undefined &&

View file

@ -241,9 +241,16 @@ export class HelpScout implements INodeType {
}
//https://developer.helpscout.com/mailbox-api/endpoints/conversations/list
if (operation === 'getAll') {
const returnAll = this.getNodeParameter('returnAll', i) as boolean;
const options = this.getNodeParameter('options', i) as IDataObject;
Object.assign(qs, options);
responseData = await helpscoutApiRequestAllItems.call(this, '_embedded.conversations', 'GET', '/v2/conversations', {}, qs);
if (returnAll) {
responseData = await helpscoutApiRequestAllItems.call(this, '_embedded.conversations', 'GET', '/v2/conversations', {}, qs);
} else {
qs.limit = this.getNodeParameter('limit', i) as number;
responseData = await helpscoutApiRequestAllItems.call(this, '_embedded.conversations', 'GET', '/v2/conversations', {}, qs);
responseData = responseData.splice(0, qs.limit);
}
}
}
if (resource === 'customer') {
@ -307,9 +314,16 @@ export class HelpScout implements INodeType {
}
//https://developer.helpscout.com/mailbox-api/endpoints/customers/list
if (operation === 'getAll') {
const returnAll = this.getNodeParameter('returnAll', i) as boolean;
const options = this.getNodeParameter('options', i) as IDataObject;
Object.assign(qs, options);
responseData = await helpscoutApiRequestAllItems.call(this, '_embedded.customers', 'GET', '/v2/customers', {}, qs);
if (returnAll) {
responseData = await helpscoutApiRequestAllItems.call(this, '_embedded.customers', 'GET', '/v2/customers', {}, qs);
} else {
qs.limit = this.getNodeParameter('limit', i) as number;
responseData = await helpscoutApiRequestAllItems.call(this, '_embedded.customers', 'GET', '/v2/customers', {}, qs);
responseData = responseData.splice(0, qs.limit);
}
}
//https://developer.helpscout.com/mailbox-api/endpoints/customers/overwrite/
if (operation === 'update') {
@ -335,7 +349,14 @@ export class HelpScout implements INodeType {
}
//https://developer.helpscout.com/mailbox-api/endpoints/mailboxes/list
if (operation === 'getAll') {
responseData = await helpscoutApiRequestAllItems.call(this, '_embedded.mailboxes', 'GET', '/v2/mailboxes', {}, qs);
const returnAll = this.getNodeParameter('returnAll', i) as boolean;
if (returnAll) {
responseData = await helpscoutApiRequestAllItems.call(this, '_embedded.mailboxes', 'GET', '/v2/mailboxes', {}, qs);
} else {
qs.limit = this.getNodeParameter('limit', i) as number;
responseData = await helpscoutApiRequestAllItems.call(this, '_embedded.mailboxes', 'GET', '/v2/mailboxes', {}, qs);
responseData = responseData.splice(0, qs.limit);
}
}
}
if (resource === 'thread') {
@ -396,8 +417,15 @@ export class HelpScout implements INodeType {
}
//https://developer.helpscout.com/mailbox-api/endpoints/conversations/threads/list
if (operation === 'getAll') {
const returnAll = this.getNodeParameter('returnAll', i) as boolean;
const conversationId = this.getNodeParameter('conversationId', i) as string;
responseData = await helpscoutApiRequestAllItems.call(this, '_embedded.threads', 'GET', `/v2/conversations/${conversationId}/threads`);
if (returnAll) {
responseData = await helpscoutApiRequestAllItems.call(this, '_embedded.threads', 'GET', `/v2/conversations/${conversationId}/threads`);
} else {
qs.limit = this.getNodeParameter('limit', i) as number;
responseData = await helpscoutApiRequestAllItems.call(this, '_embedded.threads', 'GET', `/v2/conversations/${conversationId}/threads`, {}, qs);
responseData = responseData.splice(0, qs.limit);
}
}
}
if (Array.isArray(responseData)) {

View file

@ -51,4 +51,47 @@ export const mailboxFields = [
},
},
},
/* -------------------------------------------------------------------------- */
/* mailbox:getAll */
/* -------------------------------------------------------------------------- */
{
displayName: 'Return All',
name: 'returnAll',
type: 'boolean',
displayOptions: {
show: {
operation: [
'getAll',
],
resource: [
'mailbox',
],
},
},
default: false,
description: 'If all results should be returned or only up to a given limit.',
},
{
displayName: 'Limit',
name: 'limit',
type: 'number',
displayOptions: {
show: {
operation: [
'getAll',
],
resource: [
'mailbox',
],
returnAll: [
false,
],
},
},
typeOptions: {
minValue: 1,
},
default: 50,
description: 'How many results to return.',
},
] as INodeProperties[];

View file

@ -254,4 +254,44 @@ export const threadFields = [
},
description: 'conversation ID',
},
{
displayName: 'Return All',
name: 'returnAll',
type: 'boolean',
displayOptions: {
show: {
operation: [
'getAll',
],
resource: [
'thread',
],
},
},
default: false,
description: 'If all results should be returned or only up to a given limit.',
},
{
displayName: 'Limit',
name: 'limit',
type: 'number',
displayOptions: {
show: {
operation: [
'getAll',
],
resource: [
'thread',
],
returnAll: [
false,
],
},
},
typeOptions: {
minValue: 1,
},
default: 50,
description: 'How many results to return.',
},
] as INodeProperties[];