fix(InvoiceNinja Node): added support for v5

This commit is contained in:
Michael Kret
2022-10-21 19:45:54 +03:00
committed by GitHub
parent 8f25da52b1
commit 2f4649cdf4
5 changed files with 311 additions and 59 deletions

View File

@@ -1,8 +1,12 @@
import { IHookFunctions, IWebhookFunctions } from 'n8n-core';
import { INodeType, INodeTypeDescription, IWebhookResponseData } from 'n8n-workflow';
import { IDataObject, INodeType, INodeTypeDescription, IWebhookResponseData } from 'n8n-workflow';
import { invoiceNinjaApiRequest } from './GenericFunctions';
import {
eventID,
invoiceNinjaApiRequest,
invoiceNinjaApiRequestAllItems,
} from './GenericFunctions';
export class InvoiceNinjaTrigger implements INodeType {
description: INodeTypeDescription = {
@@ -10,7 +14,7 @@ export class InvoiceNinjaTrigger implements INodeType {
name: 'invoiceNinjaTrigger',
icon: 'file:invoiceNinja.svg',
group: ['trigger'],
version: 1,
version: [1, 2],
description: 'Starts the workflow when Invoice Ninja events occur',
defaults: {
name: 'Invoice Ninja Trigger',
@@ -32,6 +36,50 @@ export class InvoiceNinjaTrigger implements INodeType {
},
],
properties: [
{
displayName: 'API Version',
name: 'apiVersion',
type: 'options',
isNodeSetting: true,
displayOptions: {
show: {
'@version': [1],
},
},
options: [
{
name: 'Version 4',
value: 'v4',
},
{
name: 'Version 5',
value: 'v5',
},
],
default: 'v4',
},
{
displayName: 'API Version',
name: 'apiVersion',
type: 'options',
isNodeSetting: true,
displayOptions: {
show: {
'@version': [2],
},
},
options: [
{
name: 'Version 4',
value: 'v4',
},
{
name: 'Version 5',
value: 'v5',
},
],
default: 'v5',
},
{
displayName: 'Event',
name: 'event',
@@ -68,35 +116,84 @@ export class InvoiceNinjaTrigger implements INodeType {
webhookMethods = {
default: {
async checkExists(this: IHookFunctions): Promise<boolean> {
const webhookData = this.getWorkflowStaticData('node');
const webhookUrl = this.getNodeWebhookUrl('default') as string;
const event = this.getNodeParameter('event') as string;
const apiVersion = this.getNodeParameter('apiVersion', 0) as string;
if (webhookData.webhookId === undefined) {
return false;
}
if (apiVersion === 'v5') {
const registeredWebhooks = (await invoiceNinjaApiRequestAllItems.call(
this,
'data',
'GET',
'/webhooks',
)) as IDataObject[];
for (const webhook of registeredWebhooks) {
if (
webhook.target_url === webhookUrl &&
webhook.is_deleted === false &&
webhook.event_id === eventID[event]
) {
webhookData.webhookId = webhook.id;
return true;
}
}
}
return false;
},
async create(this: IHookFunctions): Promise<boolean> {
const webhookUrl = this.getNodeWebhookUrl('default');
const webhookData = this.getWorkflowStaticData('node');
const event = this.getNodeParameter('event') as string;
const apiVersion = this.getNodeParameter('apiVersion', 0) as string;
const endpoint = '/hooks';
let responseData;
const body = {
target_url: webhookUrl,
event,
};
if (apiVersion === 'v4') {
const endpoint = '/hooks';
const responseData = await invoiceNinjaApiRequest.call(this, 'POST', endpoint, body);
const body = {
target_url: webhookUrl,
event,
};
if (responseData.id === undefined) {
responseData = await invoiceNinjaApiRequest.call(this, 'POST', endpoint, body);
webhookData.webhookId = responseData.id as string;
}
if (apiVersion === 'v5') {
const endpoint = '/webhooks';
const body = {
target_url: webhookUrl,
event_id: eventID[event],
};
responseData = await invoiceNinjaApiRequest.call(this, 'POST', endpoint, body);
webhookData.webhookId = responseData.data.id as string;
}
if (webhookData.webhookId === undefined) {
// Required data is missing so was not successful
return false;
}
const webhookData = this.getWorkflowStaticData('node');
webhookData.webhookId = responseData.id as string;
return true;
},
async delete(this: IHookFunctions): Promise<boolean> {
const webhookData = this.getWorkflowStaticData('node');
const apiVersion = this.getNodeParameter('apiVersion', 0) as string;
const hooksEndpoint = apiVersion === 'v4' ? '/hooks' : '/webhooks';
if (webhookData.webhookId !== undefined) {
const endpoint = `/hooks/${webhookData.webhookId}`;
const endpoint = `${hooksEndpoint}/${webhookData.webhookId}`;
try {
await invoiceNinjaApiRequest.call(this, 'DELETE', endpoint);