From bc717218191ec83ed5b814f4fdf9669216455e51 Mon Sep 17 00:00:00 2001 From: ricardo Date: Tue, 18 Aug 2020 13:46:38 -0400 Subject: [PATCH] :zap: Improvements --- .../nodes-base/nodes/ERPNext/DocumentDescription.ts | 1 + packages/nodes-base/nodes/ERPNext/ERPNext.node.ts | 10 ++++++++-- packages/nodes-base/nodes/ERPNext/GenericFunctions.ts | 11 ++++++++--- 3 files changed, 17 insertions(+), 5 deletions(-) diff --git a/packages/nodes-base/nodes/ERPNext/DocumentDescription.ts b/packages/nodes-base/nodes/ERPNext/DocumentDescription.ts index c1ef809846..8abc99b9db 100644 --- a/packages/nodes-base/nodes/ERPNext/DocumentDescription.ts +++ b/packages/nodes-base/nodes/ERPNext/DocumentDescription.ts @@ -235,6 +235,7 @@ export const documentFields = [ displayName: 'Properties', name: 'properties', type: 'fixedCollection', + placeholder: 'Add Property', description: 'Properties of request body.', default: {}, typeOptions: { diff --git a/packages/nodes-base/nodes/ERPNext/ERPNext.node.ts b/packages/nodes-base/nodes/ERPNext/ERPNext.node.ts index 0291af6bc4..7d41f5644a 100644 --- a/packages/nodes-base/nodes/ERPNext/ERPNext.node.ts +++ b/packages/nodes-base/nodes/ERPNext/ERPNext.node.ts @@ -20,7 +20,6 @@ import { erpNextApiRequest, erpNextApiRequestAllItems } from './GenericFunctions'; -import { filter } from 'rhea'; export class ERPNext implements INodeType { description: INodeTypeDescription = { @@ -107,12 +106,13 @@ export class ERPNext implements INodeType { '/api/resource/DocType', {}, ); + for (const type of types) { const typeName = type.name; const typeId = type.name; returnData.push({ name: typeName, - value: typeId + value: encodeURI(typeId) }); } return returnData; @@ -132,6 +132,7 @@ export class ERPNext implements INodeType { {}, ); for (const field of data.fields) { + //field.reqd wheater is required or not const fieldName = field.label; const fieldId = field.fieldname; returnData.push({ @@ -226,6 +227,11 @@ export class ERPNext implements INodeType { if (properties) { const fieldsValues = (properties as IDataObject).customProperty as IDataObject[]; + if (Array.isArray(fieldsValues) && fieldsValues.length === 0) { + throw new Error( + `At least one property has to be defined`, + ); + } for (const fieldValue of fieldsValues) { body[fieldValue.field as string] = fieldValue.value; } diff --git a/packages/nodes-base/nodes/ERPNext/GenericFunctions.ts b/packages/nodes-base/nodes/ERPNext/GenericFunctions.ts index c8d0eb6f8e..a714484e6e 100644 --- a/packages/nodes-base/nodes/ERPNext/GenericFunctions.ts +++ b/packages/nodes-base/nodes/ERPNext/GenericFunctions.ts @@ -35,8 +35,6 @@ export async function erpNextApiRequest(this: IExecuteFunctions | IWebhookFuncti options = Object.assign({}, options, option); - console.log(options); - options.headers!['Authorization'] = `token ${credentials.apiKey}:${credentials.apiSecret}`; if (Object.keys(body).length === 0) { @@ -45,6 +43,13 @@ export async function erpNextApiRequest(this: IExecuteFunctions | IWebhookFuncti try { return await this.helpers.request!(options); } catch (error) { + + if (error.statusCode === 307) { + throw new Error( + `ARPNext error response [${error.statusCode}]: Make sure the subdomain is correct` + ); + } + let errorMessages; if (error.response && error.response.body && error.response.body._server_messages) { const errors = JSON.parse(error.response.body._server_messages); @@ -70,7 +75,7 @@ export async function erpNextApiRequestAllItems(this: IHookFunctions | IExecuteF returnData.push.apply(returnData, responseData[propertyName]); query!.limit_start += query!.limit_page_lengt - 1; } while ( - responseData.data.length !== 0 + responseData.data.length > 0 ); return returnData;