Small improvements to UnleashedSoftware-Node

This commit is contained in:
Jan Oberhauser 2020-08-10 18:19:56 +02:00
parent 3c29e5c14f
commit 3ef6f031ec
4 changed files with 47 additions and 146 deletions

View file

@ -1,7 +1,7 @@
import { import {
OptionsWithUrl, OptionsWithUrl,
} from 'request'; } from 'request';
import { import {
IExecuteFunctions, IExecuteFunctions,
@ -12,7 +12,7 @@ import {
import { import {
IDataObject, IDataObject,
} from 'n8n-workflow'; } from 'n8n-workflow';
import { import {
createHmac, createHmac,
@ -20,7 +20,7 @@ import {
import * as qs from 'qs'; import * as qs from 'qs';
export async function unleashedApiRequest(this: IHookFunctions | IExecuteFunctions | IExecuteSingleFunctions | ILoadOptionsFunctions, method: string, path: string, body: any = {}, query: IDataObject = {} , pageNumber?: number, headers?: object): Promise<any> { // tslint:disable-line:no-any export async function unleashedApiRequest(this: IHookFunctions | IExecuteFunctions | IExecuteSingleFunctions | ILoadOptionsFunctions, method: string, path: string, body: any = {}, query: IDataObject = {}, pageNumber?: number, headers?: object): Promise<any> { // tslint:disable-line:no-any
const paginatedPath = pageNumber ? `/${path}/${pageNumber}` : `/${path}`; const paginatedPath = pageNumber ? `/${path}/${pageNumber}` : `/${path}`;
@ -43,7 +43,6 @@ export async function unleashedApiRequest(this: IHookFunctions | IExecuteFunctio
const credentials = this.getCredentials('unleashedSoftwareApi'); const credentials = this.getCredentials('unleashedSoftwareApi');
if (credentials === undefined) { if (credentials === undefined) {
throw new Error('No credentials got returned!'); throw new Error('No credentials got returned!');
} }
@ -57,13 +56,9 @@ export async function unleashedApiRequest(this: IHookFunctions | IExecuteFunctio
}); });
try { try {
return await this.helpers.request!(options); return await this.helpers.request!(options);
} catch (error) { } catch (error) {
if (error.response && error.response.body && error.response.body.description) { if (error.response && error.response.body && error.response.body.description) {
throw new Error(`Unleashed Error response [${error.statusCode}]: ${error.response.body.description}`); throw new Error(`Unleashed Error response [${error.statusCode}]: ${error.response.body.description}`);
} }
@ -73,18 +68,14 @@ export async function unleashedApiRequest(this: IHookFunctions | IExecuteFunctio
export async function unleashedApiRequestAllItems(this: IExecuteFunctions | ILoadOptionsFunctions, propertyName: string, method: string, endpoint: string, body: any = {}, query: IDataObject = {}): Promise<any> { // tslint:disable-line:no-any export async function unleashedApiRequestAllItems(this: IExecuteFunctions | ILoadOptionsFunctions, propertyName: string, method: string, endpoint: string, body: any = {}, query: IDataObject = {}): Promise<any> { // tslint:disable-line:no-any
const returnData: IDataObject[] = []; const returnData: IDataObject[] = [];
let responseData; let responseData;
let pageNumber = 1; let pageNumber = 1;
query.pageSize = 1000; query.pageSize = 1000;
do { do {
responseData = await unleashedApiRequest.call(this, method, endpoint, body, query, pageNumber); responseData = await unleashedApiRequest.call(this, method, endpoint, body, query, pageNumber);
returnData.push.apply(returnData, responseData[propertyName]); returnData.push.apply(returnData, responseData[propertyName]);
pageNumber++; pageNumber++;
} while ( } while (
@ -96,8 +87,8 @@ export async function unleashedApiRequestAllItems(this: IExecuteFunctions | ILoa
//.NET code is serializing dates in the following format: "/Date(1586833770780)/" //.NET code is serializing dates in the following format: "/Date(1586833770780)/"
//which is useless on JS side and could not treated as a date for other nodes //which is useless on JS side and could not treated as a date for other nodes
//so we need to convert all of the fields that has it. //so we need to convert all of the fields that has it.
export function convertNETDates(item: {[key: string]: any}){ export function convertNETDates(item: { [key: string]: any }) { // tslint:disable-line:no-any
Object.keys(item).forEach( path => { Object.keys(item).forEach(path => {
const type = typeof item[path] as string; const type = typeof item[path] as string;
if (type === 'string') { if (type === 'string') {
const value = item[path] as string; const value = item[path] as string;
@ -110,4 +101,3 @@ export function convertNETDates(item: {[key: string]: any}){
} }
}); });
} }

View file

@ -28,9 +28,9 @@ export const salesOrderOperations = [
export const salesOrderFields = [ export const salesOrderFields = [
/* ------------------------------------------------------------------------- */ /* ------------------------------------------------------------------------- */
/* salesOrder:getAll */ /* salesOrder:getAll */
/* ------------------------------------------------------------------------- */ /* ------------------------------------------------------------------------- */
{ {
displayName: 'Return All', displayName: 'Return All',
name: 'returnAll', name: 'returnAll',
@ -72,30 +72,6 @@ export const salesOrderFields = [
default: 100, default: 100,
description: 'How many results to return.', description: 'How many results to return.',
}, },
{
displayName: 'Page',
name: 'page',
type: 'number',
displayOptions: {
show: {
operation: [
'getAll',
],
resource: [
'salesOrder',
],
returnAll: [
false,
],
},
},
typeOptions: {
minValue: 1,
maxValue: 100000,
},
default: 1,
description: 'Page number.',
},
{ {
displayName: 'Filters', displayName: 'Filters',
name: 'filters', name: 'filters',
@ -119,72 +95,72 @@ export const salesOrderFields = [
type: 'string', type: 'string',
default: '', default: '',
placeholder: 'XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX', placeholder: 'XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX',
description: 'Only returns orders for a specified Customer GUID. The CustomerId can be specified as a list of comma-separated GUIDs' description: 'Only returns orders for a specified Customer GUID. The CustomerId can be specified as a list of comma-separated GUIDs',
}, },
{ {
displayName: 'Customer Code', displayName: 'Customer Code',
name: 'customerCode', name: 'customerCode',
type: 'string', type: 'string',
default: '', default: '',
description: 'Returns orders that start with the specific customer code.' description: 'Returns orders that start with the specific customer code.',
}, },
{ {
displayName: 'End Date', displayName: 'End Date',
name: 'endDate', name: 'endDate',
type: 'dateTime', type: 'dateTime',
default: '', default: '',
description: 'Returns orders with order date before the specified date. UTC.' description: 'Returns orders with order date before the specified date. UTC.',
}, },
{ {
displayName: 'Modified Since', displayName: 'Modified Since',
name: 'modifiedSince', name: 'modifiedSince',
type: 'dateTime', type: 'dateTime',
default: '', default: '',
description: 'Returns orders created or edited after a specified date, must be UTC format.' description: 'Returns orders created or edited after a specified date, must be UTC format.',
}, },
{ {
displayName: 'Order Number', displayName: 'Order Number',
name: 'orderNumber', name: 'orderNumber',
type: 'string', type: 'string',
default: '', default: '',
description: 'Returns a single order with the specified order number. If set, it overrides all other filters.' description: 'Returns a single order with the specified order number. If set, it overrides all other filters.',
}, },
{ {
displayName: 'Order Status', displayName: 'Order Status',
name: 'orderStatus', name: 'orderStatus',
type: 'multiOptions', type: 'multiOptions',
options: [ options: [
{
name: 'Completed',
value: 'Completed'
},
{ {
name: 'Backordered', name: 'Backordered',
value: 'Backordered' value: 'Backordered',
}, },
{ {
name: 'Parked', name: 'Completed',
value: 'Parked' value: 'Completed',
},
{
name: 'Placed',
value: 'Placed'
}, },
{ {
name: 'Deleted', name: 'Deleted',
value: 'Deleted' value: 'Deleted',
} },
{
name: 'Parked',
value: 'Parked',
},
{
name: 'Placed',
value: 'Placed',
},
], ],
default: [], default: [],
required: false, required: false,
description: 'Returns orders with the specified status. If no orderStatus filter is specified, then we exclude “Deleted” by default.' description: 'Returns orders with the specified status. If no orderStatus filter is specified, then we exclude "Deleted" by default.',
}, },
{ {
displayName: 'Start Date', displayName: 'Start Date',
name: 'startDate', name: 'startDate',
type: 'dateTime', type: 'dateTime',
default: '', default: '',
description: 'Returns orders with order date after the specified date. UTC.' description: 'Returns orders with order date after the specified date. UTC.',
}, },
], ],
}, },

View file

@ -33,10 +33,9 @@ export const stockOnHandOperations = [
export const stockOnHandFields = [ export const stockOnHandFields = [
/* ------------------------------------------------------------------------- */
/* ------------------------------------------------------------------------- */ /* stockOnHand:get */
/* stockOnHand:get */ /* ------------------------------------------------------------------------- */
/* ------------------------------------------------------------------------- */
{ {
displayName: 'Product ID', displayName: 'Product ID',
name: 'productId', name: 'productId',
@ -53,9 +52,9 @@ export const stockOnHandFields = [
}, },
default: '', default: '',
}, },
/* ------------------------------------------------------------------------- */ /* ------------------------------------------------------------------------- */
/* stockOnHand:getAll */ /* stockOnHand:getAll */
/* ------------------------------------------------------------------------- */ /* ------------------------------------------------------------------------- */
{ {
displayName: 'Return All', displayName: 'Return All',
name: 'returnAll', name: 'returnAll',
@ -97,30 +96,6 @@ export const stockOnHandFields = [
default: 100, default: 100,
description: 'How many results to return.', description: 'How many results to return.',
}, },
{
displayName: 'Page',
name: 'page',
type: 'number',
displayOptions: {
show: {
operation: [
'getAll',
],
resource: [
'stockOnHand',
],
returnAll: [
false,
],
},
},
typeOptions: {
minValue: 1,
maxValue: 100000,
},
default: 1,
description: 'Page number.',
},
{ {
displayName: 'Filters', displayName: 'Filters',
name: 'filters', name: 'filters',
@ -143,49 +118,49 @@ export const stockOnHandFields = [
name: 'asAtDate', name: 'asAtDate',
type: 'dateTime', type: 'dateTime',
default: '', default: '',
description: 'Returns the stock on hand for a specific date.' description: 'Returns the stock on hand for a specific date.',
}, },
{ {
displayName: 'Is Assembled', displayName: 'Is Assembled',
name: 'IsAssembled', name: 'IsAssembled',
type: 'boolean', type: 'boolean',
default: '', default: '',
description: 'If set to True, the AvailableQty will also include the quantity that can be assembled.' description: 'If set to True, the AvailableQty will also include the quantity that can be assembled.',
}, },
{ {
displayName: 'Modified Since', displayName: 'Modified Since',
name: 'modifiedSince', name: 'modifiedSince',
type: 'dateTime', type: 'dateTime',
default: '', default: '',
description: 'Returns stock on hand values modified after a specific date.' description: 'Returns stock on hand values modified after a specific date.',
}, },
{ {
displayName: 'Order By', displayName: 'Order By',
name: 'orderBy', name: 'orderBy',
type: 'string', type: 'string',
default: '', default: '',
description: 'Orders the list by a specific column, by default the list is ordered by productCode.' description: 'Orders the list by a specific column, by default the list is ordered by productCode.',
}, },
{ {
displayName: 'Product ID', displayName: 'Product ID',
name: 'productId', name: 'productId',
type: 'string', type: 'string',
default: '', default: '',
description: 'Returns products with the specific Product Guid. You can enter multiple product Ids separated by commas.' description: 'Returns products with the specific Product Guid. You can enter multiple product Ids separated by commas.',
}, },
{ {
displayName: 'Warehouse Code', displayName: 'Warehouse Code',
name: 'warehouseCode', name: 'warehouseCode',
type: 'string', type: 'string',
default: '', default: '',
description: 'Returns stock on hand for a specific warehouse code.' description: 'Returns stock on hand for a specific warehouse code.',
}, },
{ {
displayName: 'Warehouse Name', displayName: 'Warehouse Name',
name: 'warehouseName', name: 'warehouseName',
type: 'string', type: 'string',
default: '', default: '',
description: 'Returns stock on hand for a specific warehouse name.' description: 'Returns stock on hand for a specific warehouse name.',
}, },
], ],
}, },

View file

@ -77,19 +77,14 @@ export class UnleashedSoftware implements INodeType {
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> { async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
const items = this.getInputData(); const items = this.getInputData();
const returnData: IDataObject[] = []; const returnData: IDataObject[] = [];
const length = items.length; const length = items.length;
const qs: IDataObject = {}; const qs: IDataObject = {};
let responseData; let responseData;
for (let i = 0; i < length; i++) { for (let i = 0; i < length; i++) {
const resource = this.getNodeParameter('resource', 0) as string; const resource = this.getNodeParameter('resource', 0) as string;
const operation = this.getNodeParameter('operation', 0) as string; const operation = this.getNodeParameter('operation', 0) as string;
//https://apidocs.unleashedsoftware.com/SalesOrders //https://apidocs.unleashedsoftware.com/SalesOrders
@ -98,45 +93,32 @@ export class UnleashedSoftware implements INodeType {
if (operation === 'getAll') { if (operation === 'getAll') {
const returnAll = this.getNodeParameter('returnAll', i) as boolean; const returnAll = this.getNodeParameter('returnAll', i) as boolean;
const filters = this.getNodeParameter('filters', i) as IDataObject; const filters = this.getNodeParameter('filters', i) as IDataObject;
if (filters.startDate) { if (filters.startDate) {
filters.startDate = moment(filters.startDate as string).format('YYYY-MM-DD'); filters.startDate = moment(filters.startDate as string).format('YYYY-MM-DD');
} }
if (filters.endDate) { if (filters.endDate) {
filters.endDate = moment(filters.endDate as string).format('YYYY-MM-DD'); filters.endDate = moment(filters.endDate as string).format('YYYY-MM-DD');
} }
if (filters.modifiedSince) { if (filters.modifiedSince) {
filters.modifiedSince = moment(filters.modifiedSince as string).format('YYYY-MM-DD'); filters.modifiedSince = moment(filters.modifiedSince as string).format('YYYY-MM-DD');
} }
if (filters.orderStatus) { if (filters.orderStatus) {
filters.orderStatus = (filters.orderStatus as string[]).join(','); filters.orderStatus = (filters.orderStatus as string[]).join(',');
} }
Object.assign(qs, filters); Object.assign(qs, filters);
if (returnAll) { if (returnAll) {
responseData = await unleashedApiRequestAllItems.call(this, 'Items', 'GET', '/SalesOrders', {}, qs); responseData = await unleashedApiRequestAllItems.call(this, 'Items', 'GET', '/SalesOrders', {}, qs);
} else { } else {
const limit = this.getNodeParameter('limit', i) as number; const limit = this.getNodeParameter('limit', i) as number;
qs.pageSize = limit; qs.pageSize = limit;
responseData = await unleashedApiRequest.call(this, 'GET', `/SalesOrders`, {}, qs, 1);
const pageNumber = this.getNodeParameter('page', i) as number;
responseData = await unleashedApiRequest.call(this, 'GET', `/SalesOrders`, {}, qs, pageNumber);
responseData = responseData.Items; responseData = responseData.Items;
} }
@ -148,42 +130,30 @@ export class UnleashedSoftware implements INodeType {
if (resource === 'stockOnHand') { if (resource === 'stockOnHand') {
if (operation === 'getAll') { if (operation === 'getAll') {
const returnAll = this.getNodeParameter('returnAll', i) as boolean; const returnAll = this.getNodeParameter('returnAll', i) as boolean;
const filters = this.getNodeParameter('filters', i) as IDataObject; const filters = this.getNodeParameter('filters', i) as IDataObject;
if (filters.asAtDate) { if (filters.asAtDate) {
filters.asAtDate = moment(filters.asAtDate as string).format('YYYY-MM-DD'); filters.asAtDate = moment(filters.asAtDate as string).format('YYYY-MM-DD');
} }
if (filters.modifiedSince) { if (filters.modifiedSince) {
filters.modifiedSince = moment(filters.modifiedSince as string).format('YYYY-MM-DD'); filters.modifiedSince = moment(filters.modifiedSince as string).format('YYYY-MM-DD');
} }
if (filters.orderBy) { if (filters.orderBy) {
filters.orderBy = (filters.orderBy as string).trim(); filters.orderBy = (filters.orderBy as string).trim();
} }
Object.assign(qs, filters); Object.assign(qs, filters);
if (returnAll) { if (returnAll) {
responseData = await unleashedApiRequestAllItems.call(this, 'Items', 'GET', '/StockOnHand', {}, qs); responseData = await unleashedApiRequestAllItems.call(this, 'Items', 'GET', '/StockOnHand', {}, qs);
} else { } else {
const limit = this.getNodeParameter('limit', i) as number; const limit = this.getNodeParameter('limit', i) as number;
qs.pageSize = limit; qs.pageSize = limit;
responseData = await unleashedApiRequest.call(this, 'GET', `/StockOnHand`, {}, qs, 1);
const pageNumber = this.getNodeParameter('page', i) as number;
responseData = await unleashedApiRequest.call(this, 'GET', `/StockOnHand`, {}, qs, pageNumber);
responseData = responseData.Items; responseData = responseData.Items;
} }
@ -191,21 +161,15 @@ export class UnleashedSoftware implements INodeType {
} }
if (operation === 'get') { if (operation === 'get') {
const productId = this.getNodeParameter('productId', i) as string; const productId = this.getNodeParameter('productId', i) as string;
responseData = await unleashedApiRequest.call(this, 'GET', `/StockOnHand/${productId}`); responseData = await unleashedApiRequest.call(this, 'GET', `/StockOnHand/${productId}`);
convertNETDates(responseData); convertNETDates(responseData);
} }
} }
if (Array.isArray(responseData)) { if (Array.isArray(responseData)) {
returnData.push.apply(returnData, responseData as IDataObject[]); returnData.push.apply(returnData, responseData as IDataObject[]);
} else { } else {
returnData.push(responseData as IDataObject); returnData.push(responseData as IDataObject);
} }
} }
@ -213,7 +177,3 @@ export class UnleashedSoftware implements INodeType {
return [this.helpers.returnJsonArray(returnData)]; return [this.helpers.returnJsonArray(returnData)];
} }
} }