mirror of
https://github.com/n8n-io/n8n.git
synced 2025-03-05 20:50:17 -08:00
fix(InvoiceNinja Node): added support for v5
This commit is contained in:
parent
8f25da52b1
commit
2f4649cdf4
|
@ -1,4 +1,10 @@
|
||||||
import { ICredentialType, INodeProperties } from 'n8n-workflow';
|
import {
|
||||||
|
ICredentialDataDecryptedObject,
|
||||||
|
ICredentialTestRequest,
|
||||||
|
ICredentialType,
|
||||||
|
IHttpRequestOptions,
|
||||||
|
INodeProperties,
|
||||||
|
} from 'n8n-workflow';
|
||||||
|
|
||||||
export class InvoiceNinjaApi implements ICredentialType {
|
export class InvoiceNinjaApi implements ICredentialType {
|
||||||
name = 'invoiceNinjaApi';
|
name = 'invoiceNinjaApi';
|
||||||
|
@ -9,7 +15,8 @@ export class InvoiceNinjaApi implements ICredentialType {
|
||||||
displayName: 'URL',
|
displayName: 'URL',
|
||||||
name: 'url',
|
name: 'url',
|
||||||
type: 'string',
|
type: 'string',
|
||||||
default: 'https://app.invoiceninja.com',
|
default: '',
|
||||||
|
hint: 'Default URL for v4 is https://app.invoiceninja.com, for v5 it is https://invoicing.co',
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
displayName: 'API Token',
|
displayName: 'API Token',
|
||||||
|
@ -17,5 +24,42 @@ export class InvoiceNinjaApi implements ICredentialType {
|
||||||
type: 'string',
|
type: 'string',
|
||||||
default: '',
|
default: '',
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
displayName: 'Secret',
|
||||||
|
name: 'secret',
|
||||||
|
type: 'string',
|
||||||
|
default: '',
|
||||||
|
hint: 'This is optional, enter only if you did set a secret in your app and only if you are using v5',
|
||||||
|
},
|
||||||
];
|
];
|
||||||
|
test: ICredentialTestRequest = {
|
||||||
|
request: {
|
||||||
|
baseURL: '={{$credentials?.url}}',
|
||||||
|
url: '/api/v1/clients',
|
||||||
|
method: 'GET',
|
||||||
|
},
|
||||||
|
};
|
||||||
|
async authenticate(
|
||||||
|
credentials: ICredentialDataDecryptedObject,
|
||||||
|
requestOptions: IHttpRequestOptions,
|
||||||
|
): Promise<IHttpRequestOptions> {
|
||||||
|
const VERSION_5_TOKEN_LENGTH = 64;
|
||||||
|
const { apiToken, secret } = credentials;
|
||||||
|
const tokenLength = (apiToken as string).length;
|
||||||
|
|
||||||
|
if (tokenLength < VERSION_5_TOKEN_LENGTH) {
|
||||||
|
requestOptions.headers = {
|
||||||
|
Accept: 'application/json',
|
||||||
|
'X-Ninja-Token': apiToken,
|
||||||
|
};
|
||||||
|
} else {
|
||||||
|
requestOptions.headers = {
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
'X-API-TOKEN': apiToken,
|
||||||
|
'X-Requested-With': 'XMLHttpRequest',
|
||||||
|
'X-API-SECRET': secret || '',
|
||||||
|
};
|
||||||
|
}
|
||||||
|
return requestOptions;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,51 +7,60 @@ import {
|
||||||
ILoadOptionsFunctions,
|
ILoadOptionsFunctions,
|
||||||
} from 'n8n-core';
|
} from 'n8n-core';
|
||||||
|
|
||||||
import { IDataObject, NodeApiError, NodeOperationError } from 'n8n-workflow';
|
import { IDataObject, JsonObject, NodeApiError, NodeOperationError } from 'n8n-workflow';
|
||||||
|
|
||||||
import { get } from 'lodash';
|
import { get } from 'lodash';
|
||||||
|
|
||||||
|
export const eventID: { [key: string]: string } = {
|
||||||
|
create_client: '1',
|
||||||
|
create_invoice: '2',
|
||||||
|
create_quote: '3',
|
||||||
|
create_payment: '4',
|
||||||
|
create_vendor: '5',
|
||||||
|
};
|
||||||
|
|
||||||
export async function invoiceNinjaApiRequest(
|
export async function invoiceNinjaApiRequest(
|
||||||
this: IHookFunctions | IExecuteFunctions | IExecuteSingleFunctions | ILoadOptionsFunctions,
|
this: IHookFunctions | IExecuteFunctions | IExecuteSingleFunctions | ILoadOptionsFunctions,
|
||||||
method: string,
|
method: string,
|
||||||
endpoint: string,
|
endpoint: string,
|
||||||
// tslint:disable-next-line:no-any
|
body: IDataObject = {},
|
||||||
body: any = {},
|
|
||||||
query?: IDataObject,
|
query?: IDataObject,
|
||||||
uri?: string,
|
uri?: string,
|
||||||
// tslint:disable-next-line:no-any
|
) {
|
||||||
): Promise<any> {
|
|
||||||
const credentials = await this.getCredentials('invoiceNinjaApi');
|
const credentials = await this.getCredentials('invoiceNinjaApi');
|
||||||
|
|
||||||
const baseUrl = credentials!.url || 'https://app.invoiceninja.com';
|
if (credentials === undefined) {
|
||||||
|
throw new NodeOperationError(this.getNode(), 'No credentials got returned!');
|
||||||
|
}
|
||||||
|
|
||||||
|
const version = this.getNodeParameter('apiVersion', 0) as string;
|
||||||
|
|
||||||
|
const defaultUrl = version === 'v4' ? 'https://app.invoiceninja.com' : 'https://invoicing.co';
|
||||||
|
const baseUrl = credentials!.url || defaultUrl;
|
||||||
|
|
||||||
const options: OptionsWithUri = {
|
const options: OptionsWithUri = {
|
||||||
headers: {
|
|
||||||
Accept: 'application/json',
|
|
||||||
'X-Ninja-Token': credentials.apiToken,
|
|
||||||
},
|
|
||||||
method,
|
method,
|
||||||
qs: query,
|
qs: query,
|
||||||
uri: uri || `${baseUrl}/api/v1${endpoint}`,
|
uri: uri || `${baseUrl}/api/v1${endpoint}`,
|
||||||
body,
|
body,
|
||||||
json: true,
|
json: true,
|
||||||
};
|
};
|
||||||
|
|
||||||
try {
|
try {
|
||||||
return await this.helpers.request!(options);
|
return await this.helpers.requestWithAuthentication.call(this, 'invoiceNinjaApi', options);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
throw new NodeApiError(this.getNode(), error);
|
throw new NodeApiError(this.getNode(), error as JsonObject);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function invoiceNinjaApiRequestAllItems(
|
export async function invoiceNinjaApiRequestAllItems(
|
||||||
this: IExecuteFunctions | ILoadOptionsFunctions,
|
this: IExecuteFunctions | ILoadOptionsFunctions | IHookFunctions,
|
||||||
propertyName: string,
|
propertyName: string,
|
||||||
method: string,
|
method: string,
|
||||||
endpoint: string,
|
endpoint: string,
|
||||||
// tslint:disable-next-line:no-any
|
body: IDataObject = {},
|
||||||
body: any = {},
|
|
||||||
query: IDataObject = {},
|
query: IDataObject = {},
|
||||||
// tslint:disable-next-line:no-any
|
) {
|
||||||
): Promise<any> {
|
|
||||||
const returnData: IDataObject[] = [];
|
const returnData: IDataObject[] = [];
|
||||||
|
|
||||||
let responseData;
|
let responseData;
|
||||||
|
|
|
@ -43,7 +43,7 @@ export class InvoiceNinja implements INodeType {
|
||||||
name: 'invoiceNinja',
|
name: 'invoiceNinja',
|
||||||
icon: 'file:invoiceNinja.svg',
|
icon: 'file:invoiceNinja.svg',
|
||||||
group: ['output'],
|
group: ['output'],
|
||||||
version: 1,
|
version: [1, 2],
|
||||||
subtitle: '={{$parameter["operation"] + ": " + $parameter["resource"]}}',
|
subtitle: '={{$parameter["operation"] + ": " + $parameter["resource"]}}',
|
||||||
description: 'Consume Invoice Ninja API',
|
description: 'Consume Invoice Ninja API',
|
||||||
defaults: {
|
defaults: {
|
||||||
|
@ -58,6 +58,50 @@ export class InvoiceNinja implements INodeType {
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
properties: [
|
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: 'Resource',
|
displayName: 'Resource',
|
||||||
name: 'resource',
|
name: 'resource',
|
||||||
|
@ -114,8 +158,8 @@ export class InvoiceNinja implements INodeType {
|
||||||
const returnData: INodePropertyOptions[] = [];
|
const returnData: INodePropertyOptions[] = [];
|
||||||
const clients = await invoiceNinjaApiRequestAllItems.call(this, 'data', 'GET', '/clients');
|
const clients = await invoiceNinjaApiRequestAllItems.call(this, 'data', 'GET', '/clients');
|
||||||
for (const client of clients) {
|
for (const client of clients) {
|
||||||
const clientName = client.display_name;
|
const clientName = client.display_name as string;
|
||||||
const clientId = client.id;
|
const clientId = client.id as string;
|
||||||
returnData.push({
|
returnData.push({
|
||||||
name: clientName,
|
name: clientName,
|
||||||
value: clientId,
|
value: clientId,
|
||||||
|
@ -134,8 +178,8 @@ export class InvoiceNinja implements INodeType {
|
||||||
'/projects',
|
'/projects',
|
||||||
);
|
);
|
||||||
for (const project of projects) {
|
for (const project of projects) {
|
||||||
const projectName = project.name;
|
const projectName = project.name as string;
|
||||||
const projectId = project.id;
|
const projectId = project.id as string;
|
||||||
returnData.push({
|
returnData.push({
|
||||||
name: projectName,
|
name: projectName,
|
||||||
value: projectId,
|
value: projectId,
|
||||||
|
@ -154,8 +198,8 @@ export class InvoiceNinja implements INodeType {
|
||||||
'/invoices',
|
'/invoices',
|
||||||
);
|
);
|
||||||
for (const invoice of invoices) {
|
for (const invoice of invoices) {
|
||||||
const invoiceName = invoice.invoice_number;
|
const invoiceName = (invoice.invoice_number || invoice.number) as string;
|
||||||
const invoiceId = invoice.id;
|
const invoiceId = invoice.id as string;
|
||||||
returnData.push({
|
returnData.push({
|
||||||
name: invoiceName,
|
name: invoiceName,
|
||||||
value: invoiceId,
|
value: invoiceId,
|
||||||
|
@ -183,8 +227,8 @@ export class InvoiceNinja implements INodeType {
|
||||||
const returnData: INodePropertyOptions[] = [];
|
const returnData: INodePropertyOptions[] = [];
|
||||||
const vendors = await invoiceNinjaApiRequestAllItems.call(this, 'data', 'GET', '/vendors');
|
const vendors = await invoiceNinjaApiRequestAllItems.call(this, 'data', 'GET', '/vendors');
|
||||||
for (const vendor of vendors) {
|
for (const vendor of vendors) {
|
||||||
const vendorName = vendor.name;
|
const vendorName = vendor.name as string;
|
||||||
const vendorId = vendor.id;
|
const vendorId = vendor.id as string;
|
||||||
returnData.push({
|
returnData.push({
|
||||||
name: vendorName,
|
name: vendorName,
|
||||||
value: vendorId,
|
value: vendorId,
|
||||||
|
@ -203,8 +247,8 @@ export class InvoiceNinja implements INodeType {
|
||||||
'/expense_categories',
|
'/expense_categories',
|
||||||
);
|
);
|
||||||
for (const category of categories) {
|
for (const category of categories) {
|
||||||
const categoryName = category.name;
|
const categoryName = category.name as string;
|
||||||
const categoryId = category.id;
|
const categoryId = category.id as string;
|
||||||
returnData.push({
|
returnData.push({
|
||||||
name: categoryName,
|
name: categoryName,
|
||||||
value: categoryId,
|
value: categoryId,
|
||||||
|
@ -219,10 +263,14 @@ export class InvoiceNinja implements INodeType {
|
||||||
const items = this.getInputData();
|
const items = this.getInputData();
|
||||||
const returnData: INodeExecutionData[] = [];
|
const returnData: INodeExecutionData[] = [];
|
||||||
const length = items.length;
|
const length = items.length;
|
||||||
let responseData;
|
|
||||||
const qs: IDataObject = {};
|
const qs: IDataObject = {};
|
||||||
|
|
||||||
|
let responseData;
|
||||||
|
|
||||||
const resource = this.getNodeParameter('resource', 0) as string;
|
const resource = this.getNodeParameter('resource', 0) as string;
|
||||||
const operation = this.getNodeParameter('operation', 0) as string;
|
const operation = this.getNodeParameter('operation', 0) as string;
|
||||||
|
const apiVersion = this.getNodeParameter('apiVersion', 0) as string;
|
||||||
|
|
||||||
for (let i = 0; i < length; i++) {
|
for (let i = 0; i < length; i++) {
|
||||||
//Routes: https://github.com/invoiceninja/invoiceninja/blob/ff455c8ed9fd0c0326956175ecd509efa8bad263/routes/api.php
|
//Routes: https://github.com/invoiceninja/invoiceninja/blob/ff455c8ed9fd0c0326956175ecd509efa8bad263/routes/api.php
|
||||||
try {
|
try {
|
||||||
|
@ -291,7 +339,12 @@ export class InvoiceNinja implements INodeType {
|
||||||
body.postal_code = billingAddressValue.postalCode as string;
|
body.postal_code = billingAddressValue.postalCode as string;
|
||||||
body.country_id = parseInt(billingAddressValue.countryCode as string, 10);
|
body.country_id = parseInt(billingAddressValue.countryCode as string, 10);
|
||||||
}
|
}
|
||||||
responseData = await invoiceNinjaApiRequest.call(this, 'POST', '/clients', body);
|
responseData = await invoiceNinjaApiRequest.call(
|
||||||
|
this,
|
||||||
|
'POST',
|
||||||
|
'/clients',
|
||||||
|
body as IDataObject,
|
||||||
|
);
|
||||||
responseData = responseData.data;
|
responseData = responseData.data;
|
||||||
}
|
}
|
||||||
if (operation === 'get') {
|
if (operation === 'get') {
|
||||||
|
@ -429,14 +482,28 @@ export class InvoiceNinja implements INodeType {
|
||||||
}
|
}
|
||||||
body.invoice_items = items;
|
body.invoice_items = items;
|
||||||
}
|
}
|
||||||
responseData = await invoiceNinjaApiRequest.call(this, 'POST', '/invoices', body);
|
responseData = await invoiceNinjaApiRequest.call(
|
||||||
|
this,
|
||||||
|
'POST',
|
||||||
|
'/invoices',
|
||||||
|
body as IDataObject,
|
||||||
|
);
|
||||||
responseData = responseData.data;
|
responseData = responseData.data;
|
||||||
}
|
}
|
||||||
if (operation === 'email') {
|
if (operation === 'email') {
|
||||||
const invoiceId = this.getNodeParameter('invoiceId', i) as string;
|
const invoiceId = this.getNodeParameter('invoiceId', i) as string;
|
||||||
responseData = await invoiceNinjaApiRequest.call(this, 'POST', '/email_invoice', {
|
if (apiVersion === 'v4') {
|
||||||
id: invoiceId,
|
responseData = await invoiceNinjaApiRequest.call(this, 'POST', '/email_invoice', {
|
||||||
});
|
id: invoiceId,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
if (apiVersion === 'v5') {
|
||||||
|
responseData = await invoiceNinjaApiRequest.call(
|
||||||
|
this,
|
||||||
|
'GET',
|
||||||
|
`/invoices/${invoiceId}/email`,
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if (operation === 'get') {
|
if (operation === 'get') {
|
||||||
const invoiceId = this.getNodeParameter('invoiceId', i) as string;
|
const invoiceId = this.getNodeParameter('invoiceId', i) as string;
|
||||||
|
@ -526,7 +593,12 @@ export class InvoiceNinja implements INodeType {
|
||||||
}
|
}
|
||||||
body.time_log = JSON.stringify(logs);
|
body.time_log = JSON.stringify(logs);
|
||||||
}
|
}
|
||||||
responseData = await invoiceNinjaApiRequest.call(this, 'POST', '/tasks', body);
|
responseData = await invoiceNinjaApiRequest.call(
|
||||||
|
this,
|
||||||
|
'POST',
|
||||||
|
'/tasks',
|
||||||
|
body as IDataObject,
|
||||||
|
);
|
||||||
responseData = responseData.data;
|
responseData = responseData.data;
|
||||||
}
|
}
|
||||||
if (operation === 'get') {
|
if (operation === 'get') {
|
||||||
|
@ -575,10 +647,14 @@ export class InvoiceNinja implements INodeType {
|
||||||
if (operation === 'create') {
|
if (operation === 'create') {
|
||||||
const additionalFields = this.getNodeParameter('additionalFields', i) as IDataObject;
|
const additionalFields = this.getNodeParameter('additionalFields', i) as IDataObject;
|
||||||
const invoice = this.getNodeParameter('invoice', i) as number;
|
const invoice = this.getNodeParameter('invoice', i) as number;
|
||||||
|
const client = (
|
||||||
|
await invoiceNinjaApiRequest.call(this, 'GET', `/invoices/${invoice}`, {}, qs)
|
||||||
|
).data?.client_id as string;
|
||||||
const amount = this.getNodeParameter('amount', i) as number;
|
const amount = this.getNodeParameter('amount', i) as number;
|
||||||
const body: IPayment = {
|
const body: IPayment = {
|
||||||
invoice_id: invoice,
|
invoice_id: invoice,
|
||||||
amount,
|
amount,
|
||||||
|
client_id: client,
|
||||||
};
|
};
|
||||||
if (additionalFields.paymentType) {
|
if (additionalFields.paymentType) {
|
||||||
body.payment_type_id = additionalFields.paymentType as number;
|
body.payment_type_id = additionalFields.paymentType as number;
|
||||||
|
@ -589,7 +665,12 @@ export class InvoiceNinja implements INodeType {
|
||||||
if (additionalFields.privateNotes) {
|
if (additionalFields.privateNotes) {
|
||||||
body.private_notes = additionalFields.privateNotes as string;
|
body.private_notes = additionalFields.privateNotes as string;
|
||||||
}
|
}
|
||||||
responseData = await invoiceNinjaApiRequest.call(this, 'POST', '/payments', body);
|
responseData = await invoiceNinjaApiRequest.call(
|
||||||
|
this,
|
||||||
|
'POST',
|
||||||
|
'/payments',
|
||||||
|
body as IDataObject,
|
||||||
|
);
|
||||||
responseData = responseData.data;
|
responseData = responseData.data;
|
||||||
}
|
}
|
||||||
if (operation === 'get') {
|
if (operation === 'get') {
|
||||||
|
@ -693,7 +774,12 @@ export class InvoiceNinja implements INodeType {
|
||||||
if (additionalFields.vendor) {
|
if (additionalFields.vendor) {
|
||||||
body.vendor_id = additionalFields.vendor as number;
|
body.vendor_id = additionalFields.vendor as number;
|
||||||
}
|
}
|
||||||
responseData = await invoiceNinjaApiRequest.call(this, 'POST', '/expenses', body);
|
responseData = await invoiceNinjaApiRequest.call(
|
||||||
|
this,
|
||||||
|
'POST',
|
||||||
|
'/expenses',
|
||||||
|
body as IDataObject,
|
||||||
|
);
|
||||||
responseData = responseData.data;
|
responseData = responseData.data;
|
||||||
}
|
}
|
||||||
if (operation === 'get') {
|
if (operation === 'get') {
|
||||||
|
@ -735,6 +821,7 @@ export class InvoiceNinja implements INodeType {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (resource === 'quote') {
|
if (resource === 'quote') {
|
||||||
|
const resourceEndpoint = apiVersion === 'v4' ? '/invoices' : '/quotes';
|
||||||
if (operation === 'create') {
|
if (operation === 'create') {
|
||||||
const additionalFields = this.getNodeParameter('additionalFields', i) as IDataObject;
|
const additionalFields = this.getNodeParameter('additionalFields', i) as IDataObject;
|
||||||
const body: IQuote = {
|
const body: IQuote = {
|
||||||
|
@ -825,14 +912,28 @@ export class InvoiceNinja implements INodeType {
|
||||||
}
|
}
|
||||||
body.invoice_items = items;
|
body.invoice_items = items;
|
||||||
}
|
}
|
||||||
responseData = await invoiceNinjaApiRequest.call(this, 'POST', '/invoices', body);
|
responseData = await invoiceNinjaApiRequest.call(
|
||||||
|
this,
|
||||||
|
'POST',
|
||||||
|
resourceEndpoint,
|
||||||
|
body as IDataObject,
|
||||||
|
);
|
||||||
responseData = responseData.data;
|
responseData = responseData.data;
|
||||||
}
|
}
|
||||||
if (operation === 'email') {
|
if (operation === 'email') {
|
||||||
const quoteId = this.getNodeParameter('quoteId', i) as string;
|
const quoteId = this.getNodeParameter('quoteId', i) as string;
|
||||||
responseData = await invoiceNinjaApiRequest.call(this, 'POST', '/email_invoice', {
|
if (apiVersion === 'v4') {
|
||||||
id: quoteId,
|
responseData = await invoiceNinjaApiRequest.call(this, 'POST', '/email_invoice', {
|
||||||
});
|
id: quoteId,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
if (apiVersion === 'v5') {
|
||||||
|
responseData = await invoiceNinjaApiRequest.call(
|
||||||
|
this,
|
||||||
|
'GET',
|
||||||
|
`/quotes/${quoteId}/email`,
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if (operation === 'get') {
|
if (operation === 'get') {
|
||||||
const quoteId = this.getNodeParameter('quoteId', i) as string;
|
const quoteId = this.getNodeParameter('quoteId', i) as string;
|
||||||
|
@ -843,7 +944,7 @@ export class InvoiceNinja implements INodeType {
|
||||||
responseData = await invoiceNinjaApiRequest.call(
|
responseData = await invoiceNinjaApiRequest.call(
|
||||||
this,
|
this,
|
||||||
'GET',
|
'GET',
|
||||||
`/invoices/${quoteId}`,
|
`${resourceEndpoint}/${quoteId}`,
|
||||||
{},
|
{},
|
||||||
qs,
|
qs,
|
||||||
);
|
);
|
||||||
|
@ -878,7 +979,7 @@ export class InvoiceNinja implements INodeType {
|
||||||
responseData = await invoiceNinjaApiRequest.call(
|
responseData = await invoiceNinjaApiRequest.call(
|
||||||
this,
|
this,
|
||||||
'DELETE',
|
'DELETE',
|
||||||
`/invoices/${quoteId}`,
|
`${resourceEndpoint}/${quoteId}`,
|
||||||
);
|
);
|
||||||
responseData = responseData.data;
|
responseData = responseData.data;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,8 +1,12 @@
|
||||||
import { IHookFunctions, IWebhookFunctions } from 'n8n-core';
|
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 {
|
export class InvoiceNinjaTrigger implements INodeType {
|
||||||
description: INodeTypeDescription = {
|
description: INodeTypeDescription = {
|
||||||
|
@ -10,7 +14,7 @@ export class InvoiceNinjaTrigger implements INodeType {
|
||||||
name: 'invoiceNinjaTrigger',
|
name: 'invoiceNinjaTrigger',
|
||||||
icon: 'file:invoiceNinja.svg',
|
icon: 'file:invoiceNinja.svg',
|
||||||
group: ['trigger'],
|
group: ['trigger'],
|
||||||
version: 1,
|
version: [1, 2],
|
||||||
description: 'Starts the workflow when Invoice Ninja events occur',
|
description: 'Starts the workflow when Invoice Ninja events occur',
|
||||||
defaults: {
|
defaults: {
|
||||||
name: 'Invoice Ninja Trigger',
|
name: 'Invoice Ninja Trigger',
|
||||||
|
@ -32,6 +36,50 @@ export class InvoiceNinjaTrigger implements INodeType {
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
properties: [
|
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',
|
displayName: 'Event',
|
||||||
name: 'event',
|
name: 'event',
|
||||||
|
@ -68,35 +116,84 @@ export class InvoiceNinjaTrigger implements INodeType {
|
||||||
webhookMethods = {
|
webhookMethods = {
|
||||||
default: {
|
default: {
|
||||||
async checkExists(this: IHookFunctions): Promise<boolean> {
|
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;
|
return false;
|
||||||
},
|
},
|
||||||
async create(this: IHookFunctions): Promise<boolean> {
|
async create(this: IHookFunctions): Promise<boolean> {
|
||||||
const webhookUrl = this.getNodeWebhookUrl('default');
|
const webhookUrl = this.getNodeWebhookUrl('default');
|
||||||
|
const webhookData = this.getWorkflowStaticData('node');
|
||||||
const event = this.getNodeParameter('event') as string;
|
const event = this.getNodeParameter('event') as string;
|
||||||
|
const apiVersion = this.getNodeParameter('apiVersion', 0) as string;
|
||||||
|
|
||||||
const endpoint = '/hooks';
|
let responseData;
|
||||||
|
|
||||||
const body = {
|
if (apiVersion === 'v4') {
|
||||||
target_url: webhookUrl,
|
const endpoint = '/hooks';
|
||||||
event,
|
|
||||||
};
|
|
||||||
|
|
||||||
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
|
// Required data is missing so was not successful
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
const webhookData = this.getWorkflowStaticData('node');
|
|
||||||
webhookData.webhookId = responseData.id as string;
|
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
},
|
},
|
||||||
async delete(this: IHookFunctions): Promise<boolean> {
|
async delete(this: IHookFunctions): Promise<boolean> {
|
||||||
const webhookData = this.getWorkflowStaticData('node');
|
const webhookData = this.getWorkflowStaticData('node');
|
||||||
|
|
||||||
|
const apiVersion = this.getNodeParameter('apiVersion', 0) as string;
|
||||||
|
const hooksEndpoint = apiVersion === 'v4' ? '/hooks' : '/webhooks';
|
||||||
|
|
||||||
if (webhookData.webhookId !== undefined) {
|
if (webhookData.webhookId !== undefined) {
|
||||||
const endpoint = `/hooks/${webhookData.webhookId}`;
|
const endpoint = `${hooksEndpoint}/${webhookData.webhookId}`;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await invoiceNinjaApiRequest.call(this, 'DELETE', endpoint);
|
await invoiceNinjaApiRequest.call(this, 'DELETE', endpoint);
|
||||||
|
|
|
@ -4,4 +4,5 @@ export interface IPayment {
|
||||||
payment_type_id?: number;
|
payment_type_id?: number;
|
||||||
transaction_reference?: string;
|
transaction_reference?: string;
|
||||||
private_notes?: string;
|
private_notes?: string;
|
||||||
|
client_id?: string;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue