2024-08-29 06:55:53 -07:00
|
|
|
import {
|
|
|
|
type IHookFunctions,
|
|
|
|
type IWebhookFunctions,
|
|
|
|
type INodeType,
|
|
|
|
type INodeTypeDescription,
|
|
|
|
type IWebhookResponseData,
|
|
|
|
NodeConnectionType,
|
2023-03-09 09:13:15 -08:00
|
|
|
} from 'n8n-workflow';
|
2020-03-04 09:05:54 -08:00
|
|
|
|
2022-10-21 09:45:54 -07:00
|
|
|
import {
|
|
|
|
eventID,
|
|
|
|
invoiceNinjaApiRequest,
|
|
|
|
invoiceNinjaApiRequestAllItems,
|
|
|
|
} from './GenericFunctions';
|
2020-03-04 09:05:54 -08:00
|
|
|
|
|
|
|
export class InvoiceNinjaTrigger implements INodeType {
|
|
|
|
description: INodeTypeDescription = {
|
|
|
|
displayName: 'Invoice Ninja Trigger',
|
|
|
|
name: 'invoiceNinjaTrigger',
|
2021-04-02 05:37:13 -07:00
|
|
|
icon: 'file:invoiceNinja.svg',
|
2020-03-04 09:05:54 -08:00
|
|
|
group: ['trigger'],
|
2022-10-21 09:45:54 -07:00
|
|
|
version: [1, 2],
|
2021-07-03 05:40:16 -07:00
|
|
|
description: 'Starts the workflow when Invoice Ninja events occur',
|
2020-03-04 09:05:54 -08:00
|
|
|
defaults: {
|
|
|
|
name: 'Invoice Ninja Trigger',
|
|
|
|
},
|
|
|
|
inputs: [],
|
2024-08-29 06:55:53 -07:00
|
|
|
outputs: [NodeConnectionType.Main],
|
2020-03-04 09:05:54 -08:00
|
|
|
credentials: [
|
|
|
|
{
|
2020-03-16 15:26:27 -07:00
|
|
|
name: 'invoiceNinjaApi',
|
2020-03-04 09:05:54 -08:00
|
|
|
required: true,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
webhooks: [
|
|
|
|
{
|
|
|
|
name: 'default',
|
|
|
|
httpMethod: 'POST',
|
|
|
|
responseMode: 'onReceived',
|
|
|
|
path: 'webhook',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
properties: [
|
2022-10-21 09:45:54 -07:00
|
|
|
{
|
|
|
|
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',
|
|
|
|
},
|
2020-03-04 09:05:54 -08:00
|
|
|
{
|
|
|
|
displayName: 'Event',
|
|
|
|
name: 'event',
|
|
|
|
type: 'options',
|
|
|
|
options: [
|
|
|
|
{
|
2020-03-16 15:26:27 -07:00
|
|
|
name: 'Client Created',
|
2020-03-04 09:05:54 -08:00
|
|
|
value: 'create_client',
|
|
|
|
},
|
|
|
|
{
|
2020-03-16 15:26:27 -07:00
|
|
|
name: 'Invoice Created',
|
2020-03-04 09:05:54 -08:00
|
|
|
value: 'create_invoice',
|
|
|
|
},
|
|
|
|
{
|
2020-03-16 15:26:27 -07:00
|
|
|
name: 'Payment Created',
|
2020-03-04 09:05:54 -08:00
|
|
|
value: 'create_payment',
|
|
|
|
},
|
|
|
|
{
|
2020-03-16 15:26:27 -07:00
|
|
|
name: 'Quote Created',
|
2020-03-04 09:05:54 -08:00
|
|
|
value: 'create_quote',
|
|
|
|
},
|
|
|
|
{
|
2020-03-16 15:26:27 -07:00
|
|
|
name: 'Vendor Created',
|
2020-03-04 09:05:54 -08:00
|
|
|
value: 'create_vendor',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
default: '',
|
|
|
|
required: true,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
};
|
|
|
|
|
|
|
|
webhookMethods = {
|
|
|
|
default: {
|
|
|
|
async checkExists(this: IHookFunctions): Promise<boolean> {
|
2022-10-21 09:45:54 -07:00
|
|
|
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') {
|
2022-12-02 12:54:28 -08:00
|
|
|
const registeredWebhooks = await invoiceNinjaApiRequestAllItems.call(
|
2022-10-21 09:45:54 -07:00
|
|
|
this,
|
|
|
|
'data',
|
|
|
|
'GET',
|
|
|
|
'/webhooks',
|
2022-12-02 12:54:28 -08:00
|
|
|
);
|
2022-10-21 09:45:54 -07:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-04 09:05:54 -08:00
|
|
|
return false;
|
|
|
|
},
|
|
|
|
async create(this: IHookFunctions): Promise<boolean> {
|
|
|
|
const webhookUrl = this.getNodeWebhookUrl('default');
|
2022-10-21 09:45:54 -07:00
|
|
|
const webhookData = this.getWorkflowStaticData('node');
|
2020-03-04 09:05:54 -08:00
|
|
|
const event = this.getNodeParameter('event') as string;
|
2022-10-21 09:45:54 -07:00
|
|
|
const apiVersion = this.getNodeParameter('apiVersion', 0) as string;
|
2020-03-04 09:05:54 -08:00
|
|
|
|
2022-10-21 09:45:54 -07:00
|
|
|
let responseData;
|
2020-03-04 09:05:54 -08:00
|
|
|
|
2022-10-21 09:45:54 -07:00
|
|
|
if (apiVersion === 'v4') {
|
|
|
|
const endpoint = '/hooks';
|
2020-03-04 09:05:54 -08:00
|
|
|
|
2022-10-21 09:45:54 -07:00
|
|
|
const body = {
|
|
|
|
target_url: webhookUrl,
|
|
|
|
event,
|
|
|
|
};
|
2020-03-04 09:05:54 -08:00
|
|
|
|
2022-10-21 09:45:54 -07:00
|
|
|
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) {
|
2020-03-04 09:05:54 -08:00
|
|
|
// Required data is missing so was not successful
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
async delete(this: IHookFunctions): Promise<boolean> {
|
|
|
|
const webhookData = this.getWorkflowStaticData('node');
|
2022-10-21 09:45:54 -07:00
|
|
|
|
|
|
|
const apiVersion = this.getNodeParameter('apiVersion', 0) as string;
|
|
|
|
const hooksEndpoint = apiVersion === 'v4' ? '/hooks' : '/webhooks';
|
|
|
|
|
2020-03-04 09:05:54 -08:00
|
|
|
if (webhookData.webhookId !== undefined) {
|
2022-10-21 09:45:54 -07:00
|
|
|
const endpoint = `${hooksEndpoint}/${webhookData.webhookId}`;
|
2020-03-04 09:05:54 -08:00
|
|
|
|
|
|
|
try {
|
2020-03-16 15:26:27 -07:00
|
|
|
await invoiceNinjaApiRequest.call(this, 'DELETE', endpoint);
|
2021-04-16 09:33:36 -07:00
|
|
|
} catch (error) {
|
2020-03-04 09:05:54 -08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove from the static workflow data so that it is clear
|
2023-03-03 09:49:19 -08:00
|
|
|
// that no webhooks are registered anymore
|
2020-03-04 09:05:54 -08:00
|
|
|
delete webhookData.webhookId;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
async webhook(this: IWebhookFunctions): Promise<IWebhookResponseData> {
|
|
|
|
const bodyData = this.getBodyData();
|
|
|
|
return {
|
2022-08-17 08:50:24 -07:00
|
|
|
workflowData: [this.helpers.returnJsonArray(bodyData)],
|
2020-03-04 09:05:54 -08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|