n8n-nodes-base
Version:
Base nodes of n8n
190 lines • 7.11 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.InvoiceNinjaTrigger = void 0;
const n8n_workflow_1 = require("n8n-workflow");
const GenericFunctions_1 = require("./GenericFunctions");
class InvoiceNinjaTrigger {
description = {
displayName: 'Invoice Ninja Trigger',
name: 'invoiceNinjaTrigger',
icon: 'file:invoiceNinja.svg',
group: ['trigger'],
version: [1, 2],
description: 'Starts the workflow when Invoice Ninja events occur',
defaults: {
name: 'Invoice Ninja Trigger',
},
inputs: [],
outputs: [n8n_workflow_1.NodeConnectionTypes.Main],
credentials: [
{
name: 'invoiceNinjaApi',
required: true,
},
],
webhooks: [
{
name: 'default',
httpMethod: 'POST',
responseMode: 'onReceived',
path: 'webhook',
},
],
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',
type: 'options',
options: [
{
name: 'Client Created',
value: 'create_client',
},
{
name: 'Invoice Created',
value: 'create_invoice',
},
{
name: 'Payment Created',
value: 'create_payment',
},
{
name: 'Quote Created',
value: 'create_quote',
},
{
name: 'Vendor Created',
value: 'create_vendor',
},
],
default: '',
required: true,
},
],
};
webhookMethods = {
default: {
async checkExists() {
const webhookData = this.getWorkflowStaticData('node');
const webhookUrl = this.getNodeWebhookUrl('default');
const event = this.getNodeParameter('event');
const apiVersion = this.getNodeParameter('apiVersion', 0);
if (webhookData.webhookId === undefined) {
return false;
}
if (apiVersion === 'v5') {
const registeredWebhooks = await GenericFunctions_1.invoiceNinjaApiRequestAllItems.call(this, 'data', 'GET', '/webhooks');
for (const webhook of registeredWebhooks) {
if (webhook.target_url === webhookUrl &&
webhook.is_deleted === false &&
webhook.event_id === GenericFunctions_1.eventID[event]) {
webhookData.webhookId = webhook.id;
return true;
}
}
}
return false;
},
async create() {
const webhookUrl = this.getNodeWebhookUrl('default');
const webhookData = this.getWorkflowStaticData('node');
const event = this.getNodeParameter('event');
const apiVersion = this.getNodeParameter('apiVersion', 0);
let responseData;
if (apiVersion === 'v4') {
const endpoint = '/hooks';
const body = {
target_url: webhookUrl,
event,
};
responseData = await GenericFunctions_1.invoiceNinjaApiRequest.call(this, 'POST', endpoint, body);
webhookData.webhookId = responseData.id;
}
if (apiVersion === 'v5') {
const endpoint = '/webhooks';
const body = {
target_url: webhookUrl,
event_id: GenericFunctions_1.eventID[event],
};
responseData = await GenericFunctions_1.invoiceNinjaApiRequest.call(this, 'POST', endpoint, body);
webhookData.webhookId = responseData.data.id;
}
if (webhookData.webhookId === undefined) {
// Required data is missing so was not successful
return false;
}
return true;
},
async delete() {
const webhookData = this.getWorkflowStaticData('node');
const apiVersion = this.getNodeParameter('apiVersion', 0);
const hooksEndpoint = apiVersion === 'v4' ? '/hooks' : '/webhooks';
if (webhookData.webhookId !== undefined) {
const endpoint = `${hooksEndpoint}/${webhookData.webhookId}`;
try {
await GenericFunctions_1.invoiceNinjaApiRequest.call(this, 'DELETE', endpoint);
}
catch (error) {
return false;
}
// Remove from the static workflow data so that it is clear
// that no webhooks are registered anymore
delete webhookData.webhookId;
}
return true;
},
},
};
async webhook() {
const bodyData = this.getBodyData();
return {
workflowData: [this.helpers.returnJsonArray(bodyData)],
};
}
}
exports.InvoiceNinjaTrigger = InvoiceNinjaTrigger;
//# sourceMappingURL=InvoiceNinjaTrigger.node.js.map