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 {
OptionsWithUrl,
} from 'request';
} from 'request';
import {
IExecuteFunctions,
@ -12,7 +12,7 @@ import {
import {
IDataObject,
} from 'n8n-workflow';
} from 'n8n-workflow';
import {
createHmac,
@ -20,7 +20,7 @@ import {
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}`;
@ -43,7 +43,6 @@ export async function unleashedApiRequest(this: IHookFunctions | IExecuteFunctio
const credentials = this.getCredentials('unleashedSoftwareApi');
if (credentials === undefined) {
throw new Error('No credentials got returned!');
}
@ -57,13 +56,9 @@ export async function unleashedApiRequest(this: IHookFunctions | IExecuteFunctio
});
try {
return await this.helpers.request!(options);
} catch (error) {
if (error.response && error.response.body && 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
const returnData: IDataObject[] = [];
let responseData;
let pageNumber = 1;
query.pageSize = 1000;
do {
responseData = await unleashedApiRequest.call(this, method, endpoint, body, query, pageNumber);
returnData.push.apply(returnData, responseData[propertyName]);
pageNumber++;
} while (
@ -96,8 +87,8 @@ export async function unleashedApiRequestAllItems(this: IExecuteFunctions | ILoa
//.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
//so we need to convert all of the fields that has it.
export function convertNETDates(item: {[key: string]: any}){
Object.keys(item).forEach( path => {
export function convertNETDates(item: { [key: string]: any }) { // tslint:disable-line:no-any
Object.keys(item).forEach(path => {
const type = typeof item[path] as string;
if (type === '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 = [
/* ------------------------------------------------------------------------- */
/* salesOrder:getAll */
/* ------------------------------------------------------------------------- */
/* ------------------------------------------------------------------------- */
/* salesOrder:getAll */
/* ------------------------------------------------------------------------- */
{
displayName: 'Return All',
name: 'returnAll',
@ -72,30 +72,6 @@ export const salesOrderFields = [
default: 100,
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',
name: 'filters',
@ -119,72 +95,72 @@ export const salesOrderFields = [
type: 'string',
default: '',
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',
name: 'customerCode',
type: 'string',
default: '',
description: 'Returns orders that start with the specific customer code.'
description: 'Returns orders that start with the specific customer code.',
},
{
displayName: 'End Date',
name: 'endDate',
type: 'dateTime',
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',
name: 'modifiedSince',
type: 'dateTime',
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',
name: 'orderNumber',
type: 'string',
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',
name: 'orderStatus',
type: 'multiOptions',
options: [
{
name: 'Completed',
value: 'Completed'
},
{
name: 'Backordered',
value: 'Backordered'
value: 'Backordered',
},
{
name: 'Parked',
value: 'Parked'
},
{
name: 'Placed',
value: 'Placed'
name: 'Completed',
value: 'Completed',
},
{
name: 'Deleted',
value: 'Deleted'
}
value: 'Deleted',
},
{
name: 'Parked',
value: 'Parked',
},
{
name: 'Placed',
value: 'Placed',
},
],
default: [],
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',
name: 'startDate',
type: 'dateTime',
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 = [
/* ------------------------------------------------------------------------- */
/* stockOnHand:get */
/* ------------------------------------------------------------------------- */
/* ------------------------------------------------------------------------- */
/* stockOnHand:get */
/* ------------------------------------------------------------------------- */
{
displayName: 'Product ID',
name: 'productId',
@ -53,9 +52,9 @@ export const stockOnHandFields = [
},
default: '',
},
/* ------------------------------------------------------------------------- */
/* stockOnHand:getAll */
/* ------------------------------------------------------------------------- */
/* ------------------------------------------------------------------------- */
/* stockOnHand:getAll */
/* ------------------------------------------------------------------------- */
{
displayName: 'Return All',
name: 'returnAll',
@ -97,30 +96,6 @@ export const stockOnHandFields = [
default: 100,
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',
name: 'filters',
@ -143,49 +118,49 @@ export const stockOnHandFields = [
name: 'asAtDate',
type: 'dateTime',
default: '',
description: 'Returns the stock on hand for a specific date.'
description: 'Returns the stock on hand for a specific date.',
},
{
displayName: 'Is Assembled',
name: 'IsAssembled',
type: 'boolean',
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',
name: 'modifiedSince',
type: 'dateTime',
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',
name: 'orderBy',
type: 'string',
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',
name: 'productId',
type: 'string',
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',
name: 'warehouseCode',
type: 'string',
default: '',
description: 'Returns stock on hand for a specific warehouse code.'
description: 'Returns stock on hand for a specific warehouse code.',
},
{
displayName: 'Warehouse Name',
name: 'warehouseName',
type: 'string',
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[][]> {
const items = this.getInputData();
const returnData: IDataObject[] = [];
const length = items.length;
const qs: IDataObject = {};
let responseData;
for (let i = 0; i < length; i++) {
const resource = this.getNodeParameter('resource', 0) as string;
const operation = this.getNodeParameter('operation', 0) as string;
//https://apidocs.unleashedsoftware.com/SalesOrders
@ -98,45 +93,32 @@ export class UnleashedSoftware implements INodeType {
if (operation === 'getAll') {
const returnAll = this.getNodeParameter('returnAll', i) as boolean;
const filters = this.getNodeParameter('filters', i) as IDataObject;
if (filters.startDate) {
filters.startDate = moment(filters.startDate as string).format('YYYY-MM-DD');
}
if (filters.endDate) {
filters.endDate = moment(filters.endDate as string).format('YYYY-MM-DD');
}
if (filters.modifiedSince) {
filters.modifiedSince = moment(filters.modifiedSince as string).format('YYYY-MM-DD');
}
if (filters.orderStatus) {
filters.orderStatus = (filters.orderStatus as string[]).join(',');
}
Object.assign(qs, filters);
if (returnAll) {
responseData = await unleashedApiRequestAllItems.call(this, 'Items', 'GET', '/SalesOrders', {}, qs);
} else {
const limit = this.getNodeParameter('limit', i) as number;
qs.pageSize = limit;
const pageNumber = this.getNodeParameter('page', i) as number;
responseData = await unleashedApiRequest.call(this, 'GET', `/SalesOrders`, {}, qs, pageNumber);
responseData = await unleashedApiRequest.call(this, 'GET', `/SalesOrders`, {}, qs, 1);
responseData = responseData.Items;
}
@ -148,42 +130,30 @@ export class UnleashedSoftware implements INodeType {
if (resource === 'stockOnHand') {
if (operation === 'getAll') {
const returnAll = this.getNodeParameter('returnAll', i) as boolean;
const filters = this.getNodeParameter('filters', i) as IDataObject;
if (filters.asAtDate) {
filters.asAtDate = moment(filters.asAtDate as string).format('YYYY-MM-DD');
}
if (filters.modifiedSince) {
filters.modifiedSince = moment(filters.modifiedSince as string).format('YYYY-MM-DD');
}
if (filters.orderBy) {
filters.orderBy = (filters.orderBy as string).trim();
}
Object.assign(qs, filters);
if (returnAll) {
responseData = await unleashedApiRequestAllItems.call(this, 'Items', 'GET', '/StockOnHand', {}, qs);
} else {
const limit = this.getNodeParameter('limit', i) as number;
qs.pageSize = limit;
const pageNumber = this.getNodeParameter('page', i) as number;
responseData = await unleashedApiRequest.call(this, 'GET', `/StockOnHand`, {}, qs, pageNumber);
responseData = await unleashedApiRequest.call(this, 'GET', `/StockOnHand`, {}, qs, 1);
responseData = responseData.Items;
}
@ -191,21 +161,15 @@ export class UnleashedSoftware implements INodeType {
}
if (operation === 'get') {
const productId = this.getNodeParameter('productId', i) as string;
responseData = await unleashedApiRequest.call(this, 'GET', `/StockOnHand/${productId}`);
convertNETDates(responseData);
}
}
if (Array.isArray(responseData)) {
returnData.push.apply(returnData, responseData as IDataObject[]);
} else {
returnData.push(responseData as IDataObject);
}
}
@ -213,7 +177,3 @@ export class UnleashedSoftware implements INodeType {
return [this.helpers.returnJsonArray(returnData)];
}
}