Minor improvements to AcuityScheduling-Node

This commit is contained in:
Jan Oberhauser 2020-01-18 11:10:46 -06:00
parent bde233b9c4
commit c5ad3eecca
2 changed files with 41 additions and 6 deletions

View file

@ -17,7 +17,7 @@ import {
export class AcuitySchedulingTrigger implements INodeType {
description: INodeTypeDescription = {
displayName: 'Acuity Scheduling Trigger',
name: 'acuityScheduling',
name: 'acuitySchedulingTrigger',
icon: 'file:acuityScheduling.png',
group: ['trigger'],
version: 1,
@ -77,6 +77,13 @@ export class AcuitySchedulingTrigger implements INodeType {
},
],
},
{
displayName: 'Resolve Data',
name: 'resolveData',
type: 'boolean',
default: true,
description: 'By default does the webhook-data only contain the ID of the object.<br />If this option gets activated it will resolve the data automatically.',
},
],
};
// @ts-ignore
@ -127,10 +134,30 @@ export class AcuitySchedulingTrigger implements INodeType {
async webhook(this: IWebhookFunctions): Promise<IWebhookResponseData> {
const req = this.getRequestObject();
const resolveData = this.getNodeParameter('resolveData', false) as boolean;
if (resolveData === false) {
// Return the data as it got received
return {
workflowData: [
this.helpers.returnJsonArray(req.body),
],
};
}
// Resolve the data by requesting the information via API
const event = this.getNodeParameter('event', false) as string;
const eventType = event.split('.').shift();
const endpoint = `/${eventType}s/${req.body.id}`;
const responseData = await acuitySchedulingApiRequest.call(this, 'GET', endpoint, {});
return {
workflowData: [
this.helpers.returnJsonArray(req.body),
this.helpers.returnJsonArray(responseData),
],
};
}
}

View file

@ -14,12 +14,14 @@ export async function acuitySchedulingApiRequest(this: IHookFunctions | IExecute
throw new Error('No credentials got returned!');
}
const base64Key = Buffer.from(`${credentials.userId}:${credentials.apiKey}`).toString('base64');
let options: OptionsWithUri = {
const options: OptionsWithUri = {
headers: {
Authorization: `Basic ${base64Key}`,
'Content-Type': 'application/json',
},
auth: {
user: credentials.userId as string,
password: credentials.apiKey as string,
},
method,
qs,
body,
@ -29,6 +31,12 @@ export async function acuitySchedulingApiRequest(this: IHookFunctions | IExecute
try {
return await this.helpers.request!(options);
} catch (error) {
throw new Error('Acuity Scheduling Error: ' + error.message);
let errorMessage = error.message;
if (error.response.body && error.response.body.message) {
errorMessage = `[${error.response.body.status_code}] ${error.response.body.message}`;
}
throw new Error('Acuity Scheduling Error: ' + errorMessage);
}
}