From a86e304d2248b8f34d7864c330660825a3b665e5 Mon Sep 17 00:00:00 2001
From: ricardo <ricardoespinoza105@gmail.com>
Date: Sun, 29 Mar 2020 22:08:00 -0400
Subject: [PATCH] :zap: Improved getall operations

---
 .../HelpScout/ConversationDescription.ts      | 40 +++++++++++++++++
 .../nodes/HelpScout/CustomerDescription.ts    | 40 +++++++++++++++++
 .../nodes/HelpScout/GenericFunctions.ts       |  5 ++-
 .../nodes/HelpScout/HelpScout.node.ts         | 36 ++++++++++++++--
 .../nodes/HelpScout/MailboxDescription.ts     | 43 +++++++++++++++++++
 .../nodes/HelpScout/ThreadDescription.ts      | 40 +++++++++++++++++
 6 files changed, 199 insertions(+), 5 deletions(-)

diff --git a/packages/nodes-base/nodes/HelpScout/ConversationDescription.ts b/packages/nodes-base/nodes/HelpScout/ConversationDescription.ts
index 5d882ac80f..30b46368a7 100644
--- a/packages/nodes-base/nodes/HelpScout/ConversationDescription.ts
+++ b/packages/nodes-base/nodes/HelpScout/ConversationDescription.ts
@@ -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',
diff --git a/packages/nodes-base/nodes/HelpScout/CustomerDescription.ts b/packages/nodes-base/nodes/HelpScout/CustomerDescription.ts
index 584108cf77..5f353d0bd5 100644
--- a/packages/nodes-base/nodes/HelpScout/CustomerDescription.ts
+++ b/packages/nodes-base/nodes/HelpScout/CustomerDescription.ts
@@ -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',
diff --git a/packages/nodes-base/nodes/HelpScout/GenericFunctions.ts b/packages/nodes-base/nodes/HelpScout/GenericFunctions.ts
index fb04b669e6..ab933856c9 100644
--- a/packages/nodes-base/nodes/HelpScout/GenericFunctions.ts
+++ b/packages/nodes-base/nodes/HelpScout/GenericFunctions.ts
@@ -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 &&
diff --git a/packages/nodes-base/nodes/HelpScout/HelpScout.node.ts b/packages/nodes-base/nodes/HelpScout/HelpScout.node.ts
index 190b276cb8..ce1f02d648 100644
--- a/packages/nodes-base/nodes/HelpScout/HelpScout.node.ts
+++ b/packages/nodes-base/nodes/HelpScout/HelpScout.node.ts
@@ -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)) {
diff --git a/packages/nodes-base/nodes/HelpScout/MailboxDescription.ts b/packages/nodes-base/nodes/HelpScout/MailboxDescription.ts
index 27063c05ed..d9aff61100 100644
--- a/packages/nodes-base/nodes/HelpScout/MailboxDescription.ts
+++ b/packages/nodes-base/nodes/HelpScout/MailboxDescription.ts
@@ -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[];
diff --git a/packages/nodes-base/nodes/HelpScout/ThreadDescription.ts b/packages/nodes-base/nodes/HelpScout/ThreadDescription.ts
index 4374e68453..eb2039745c 100644
--- a/packages/nodes-base/nodes/HelpScout/ThreadDescription.ts
+++ b/packages/nodes-base/nodes/HelpScout/ThreadDescription.ts
@@ -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[];