n8n-nodes-alegra-unofficial
Version:
Unofficial Alegra node for n8n - Community integration with Alegra's accounting platform. NOT OFFICIALLY SUPPORTED BY ALEGRA.
370 lines • 16.3 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.handleBankAccountOperations = exports.handleTaxOperations = exports.handleEstimateOperations = exports.handlePaymentOperations = exports.handleItemOperations = exports.handleInvoiceOperations = exports.handleContactOperations = exports.alegraApiRequestAllItems = exports.alegraApiRequest = void 0;
const n8n_workflow_1 = require("n8n-workflow");
async function alegraApiRequest(method, endpoint, body = {}, qs = {}, uri) {
const credentials = await this.getCredentials('alegraApi');
const options = {
method,
qs,
body,
url: uri || `${credentials.environment}/api/v1${endpoint}`,
json: true,
};
try {
return await this.helpers.requestWithAuthentication.call(this, 'alegraApi', options);
}
catch (error) {
throw new n8n_workflow_1.NodeApiError(this.getNode(), error);
}
}
exports.alegraApiRequest = alegraApiRequest;
async function alegraApiRequestAllItems(method, endpoint, body = {}, query = {}) {
const returnData = [];
let responseData;
query.limit = 30;
query.start = 0;
do {
responseData = await alegraApiRequest.call(this, method, endpoint, body, query);
returnData.push(...responseData);
query.start += query.limit;
} while (responseData.length === query.limit);
return returnData;
}
exports.alegraApiRequestAllItems = alegraApiRequestAllItems;
async function handleContactOperations(operation, itemIndex) {
const resource = 'contact';
if (operation === 'create') {
const name = this.getNodeParameter('name', itemIndex);
const type = this.getNodeParameter('type', itemIndex);
const identificationType = this.getNodeParameter('identificationType', itemIndex);
const identificationNumber = this.getNodeParameter('identificationNumber', itemIndex);
const body = {
name,
type,
identification: {
type: identificationType,
number: identificationNumber,
},
};
const additionalFields = this.getNodeParameter('additionalFields', itemIndex);
Object.assign(body, additionalFields);
return await alegraApiRequest.call(this, 'POST', '/contacts', body);
}
if (operation === 'get') {
const contactId = this.getNodeParameter('contactId', itemIndex);
return await alegraApiRequest.call(this, 'GET', `/contacts/${contactId}`);
}
if (operation === 'getAll') {
const returnAll = this.getNodeParameter('returnAll', itemIndex, false);
const filters = this.getNodeParameter('filters', itemIndex, {});
const options = this.getNodeParameter('options', itemIndex, {});
const qs = {
...filters,
...options,
};
if (returnAll) {
return await alegraApiRequestAllItems.call(this, 'GET', '/contacts', {}, qs);
}
else {
qs.limit = this.getNodeParameter('limit', itemIndex, 30);
return await alegraApiRequest.call(this, 'GET', '/contacts', {}, qs);
}
}
if (operation === 'update') {
const contactId = this.getNodeParameter('contactId', itemIndex);
const updateFields = this.getNodeParameter('updateFields', itemIndex);
return await alegraApiRequest.call(this, 'PUT', `/contacts/${contactId}`, updateFields);
}
if (operation === 'delete') {
const contactId = this.getNodeParameter('contactId', itemIndex);
return await alegraApiRequest.call(this, 'DELETE', `/contacts/${contactId}`);
}
}
exports.handleContactOperations = handleContactOperations;
async function handleInvoiceOperations(operation, itemIndex) {
const resource = 'invoice';
if (operation === 'create') {
const date = this.getNodeParameter('date', itemIndex);
const dueDate = this.getNodeParameter('dueDate', itemIndex);
const client = this.getNodeParameter('client', itemIndex);
const items = this.getNodeParameter('items', itemIndex);
const paymentForm = this.getNodeParameter('paymentForm', itemIndex);
const paymentMethod = this.getNodeParameter('paymentMethod', itemIndex);
const generateStamp = this.getNodeParameter('generateStamp', itemIndex);
const body = {
date,
dueDate,
client,
items: items.item || [],
paymentForm,
paymentMethod,
stamp: {
generateStamp,
},
};
const additionalFields = this.getNodeParameter('additionalFields', itemIndex, {});
Object.assign(body, additionalFields);
return await alegraApiRequest.call(this, 'POST', '/invoices', body);
}
if (operation === 'get') {
const invoiceId = this.getNodeParameter('invoiceId', itemIndex);
return await alegraApiRequest.call(this, 'GET', `/invoices/${invoiceId}`);
}
if (operation === 'getAll') {
const returnAll = this.getNodeParameter('returnAll', itemIndex, false);
const filters = this.getNodeParameter('filters', itemIndex, {});
const options = this.getNodeParameter('options', itemIndex, {});
const qs = {
...filters,
...options,
};
if (returnAll) {
return await alegraApiRequestAllItems.call(this, 'GET', '/invoices', {}, qs);
}
else {
qs.limit = this.getNodeParameter('limit', itemIndex, 30);
return await alegraApiRequest.call(this, 'GET', '/invoices', {}, qs);
}
}
if (operation === 'update') {
const invoiceId = this.getNodeParameter('invoiceId', itemIndex);
const updateFields = this.getNodeParameter('updateFields', itemIndex);
return await alegraApiRequest.call(this, 'PUT', `/invoices/${invoiceId}`, updateFields);
}
if (operation === 'delete') {
const invoiceId = this.getNodeParameter('invoiceId', itemIndex);
return await alegraApiRequest.call(this, 'DELETE', `/invoices/${invoiceId}`);
}
if (operation === 'send') {
const invoiceId = this.getNodeParameter('invoiceId', itemIndex);
const emails = this.getNodeParameter('emails', itemIndex);
const body = {
emails,
sendCopyToUser: this.getNodeParameter('sendCopyToUser', itemIndex, false),
};
return await alegraApiRequest.call(this, 'POST', `/invoices/${invoiceId}/email`, body);
}
}
exports.handleInvoiceOperations = handleInvoiceOperations;
async function handleItemOperations(operation, itemIndex) {
const resource = 'item';
if (operation === 'create') {
const name = this.getNodeParameter('name', itemIndex);
const type = this.getNodeParameter('type', itemIndex);
const price = this.getNodeParameter('price', itemIndex);
const body = {
name,
type,
price: [{ price }],
};
const additionalFields = this.getNodeParameter('additionalFields', itemIndex, {});
Object.assign(body, additionalFields);
return await alegraApiRequest.call(this, 'POST', '/items', body);
}
if (operation === 'get') {
const itemId = this.getNodeParameter('itemId', itemIndex);
return await alegraApiRequest.call(this, 'GET', `/items/${itemId}`);
}
if (operation === 'getAll') {
const returnAll = this.getNodeParameter('returnAll', itemIndex, false);
const filters = this.getNodeParameter('filters', itemIndex, {});
const options = this.getNodeParameter('options', itemIndex, {});
const qs = {
...filters,
...options,
};
if (returnAll) {
return await alegraApiRequestAllItems.call(this, 'GET', '/items', {}, qs);
}
else {
qs.limit = this.getNodeParameter('limit', itemIndex, 30);
return await alegraApiRequest.call(this, 'GET', '/items', {}, qs);
}
}
if (operation === 'update') {
const itemId = this.getNodeParameter('itemId', itemIndex);
const updateFields = this.getNodeParameter('updateFields', itemIndex);
return await alegraApiRequest.call(this, 'PUT', `/items/${itemId}`, updateFields);
}
if (operation === 'delete') {
const itemId = this.getNodeParameter('itemId', itemIndex);
return await alegraApiRequest.call(this, 'DELETE', `/items/${itemId}`);
}
}
exports.handleItemOperations = handleItemOperations;
async function handlePaymentOperations(operation, itemIndex) {
const resource = 'payment';
if (operation === 'create') {
const date = this.getNodeParameter('date', itemIndex);
const bankAccount = this.getNodeParameter('bankAccount', itemIndex);
const paymentMethod = this.getNodeParameter('paymentMethod', itemIndex);
const invoices = this.getNodeParameter('invoices', itemIndex);
const body = {
date,
bankAccount,
paymentMethod,
invoices: invoices.invoice || [],
};
const additionalFields = this.getNodeParameter('additionalFields', itemIndex, {});
Object.assign(body, additionalFields);
return await alegraApiRequest.call(this, 'POST', '/payments', body);
}
if (operation === 'get') {
const paymentId = this.getNodeParameter('paymentId', itemIndex);
return await alegraApiRequest.call(this, 'GET', `/payments/${paymentId}`);
}
if (operation === 'getAll') {
const returnAll = this.getNodeParameter('returnAll', itemIndex, false);
const filters = this.getNodeParameter('filters', itemIndex, {});
const options = this.getNodeParameter('options', itemIndex, {});
const qs = {
...filters,
...options,
};
if (returnAll) {
return await alegraApiRequestAllItems.call(this, 'GET', '/payments', {}, qs);
}
else {
qs.limit = this.getNodeParameter('limit', itemIndex, 30);
return await alegraApiRequest.call(this, 'GET', '/payments', {}, qs);
}
}
if (operation === 'delete') {
const paymentId = this.getNodeParameter('paymentId', itemIndex);
return await alegraApiRequest.call(this, 'DELETE', `/payments/${paymentId}`);
}
}
exports.handlePaymentOperations = handlePaymentOperations;
async function handleEstimateOperations(operation, itemIndex) {
const resource = 'estimate';
if (operation === 'create') {
const date = this.getNodeParameter('date', itemIndex);
const dueDate = this.getNodeParameter('dueDate', itemIndex);
const client = this.getNodeParameter('client', itemIndex);
const items = this.getNodeParameter('items', itemIndex);
const body = {
date,
dueDate,
client,
items: items.item || [],
};
const additionalFields = this.getNodeParameter('additionalFields', itemIndex, {});
Object.assign(body, additionalFields);
return await alegraApiRequest.call(this, 'POST', '/estimates', body);
}
if (operation === 'get') {
const estimateId = this.getNodeParameter('estimateId', itemIndex);
return await alegraApiRequest.call(this, 'GET', `/estimates/${estimateId}`);
}
if (operation === 'getAll') {
const returnAll = this.getNodeParameter('returnAll', itemIndex, false);
const filters = this.getNodeParameter('filters', itemIndex, {});
const options = this.getNodeParameter('options', itemIndex, {});
const qs = {
...filters,
...options,
};
if (returnAll) {
return await alegraApiRequestAllItems.call(this, 'GET', '/estimates', {}, qs);
}
else {
qs.limit = this.getNodeParameter('limit', itemIndex, 30);
return await alegraApiRequest.call(this, 'GET', '/estimates', {}, qs);
}
}
if (operation === 'update') {
const estimateId = this.getNodeParameter('estimateId', itemIndex);
const updateFields = this.getNodeParameter('updateFields', itemIndex);
return await alegraApiRequest.call(this, 'PUT', `/estimates/${estimateId}`, updateFields);
}
if (operation === 'delete') {
const estimateId = this.getNodeParameter('estimateId', itemIndex);
return await alegraApiRequest.call(this, 'DELETE', `/estimates/${estimateId}`);
}
if (operation === 'send') {
const estimateId = this.getNodeParameter('estimateId', itemIndex);
const emails = this.getNodeParameter('emails', itemIndex);
const body = {
emails,
sendCopyToUser: this.getNodeParameter('sendCopyToUser', itemIndex, false),
};
return await alegraApiRequest.call(this, 'POST', `/estimates/${estimateId}/email`, body);
}
}
exports.handleEstimateOperations = handleEstimateOperations;
async function handleTaxOperations(operation, itemIndex) {
const resource = 'tax';
if (operation === 'get') {
const taxId = this.getNodeParameter('taxId', itemIndex);
return await alegraApiRequest.call(this, 'GET', `/taxes/${taxId}`);
}
if (operation === 'getAll') {
const returnAll = this.getNodeParameter('returnAll', itemIndex, false);
const options = this.getNodeParameter('options', itemIndex, {});
const qs = {
...options,
};
if (returnAll) {
return await alegraApiRequestAllItems.call(this, 'GET', '/taxes', {}, qs);
}
else {
qs.limit = this.getNodeParameter('limit', itemIndex, 30);
return await alegraApiRequest.call(this, 'GET', '/taxes', {}, qs);
}
}
}
exports.handleTaxOperations = handleTaxOperations;
async function handleBankAccountOperations(operation, itemIndex) {
const resource = 'bankAccount';
if (operation === 'create') {
const name = this.getNodeParameter('name', itemIndex);
const type = this.getNodeParameter('type', itemIndex);
const initialBalance = this.getNodeParameter('initialBalance', itemIndex);
const initialBalanceDate = this.getNodeParameter('initialBalanceDate', itemIndex);
const body = {
name,
type,
initialBalance,
initialBalanceDate,
};
const additionalFields = this.getNodeParameter('additionalFields', itemIndex, {});
Object.assign(body, additionalFields);
return await alegraApiRequest.call(this, 'POST', '/bank-accounts', body);
}
if (operation === 'get') {
const bankAccountId = this.getNodeParameter('bankAccountId', itemIndex);
return await alegraApiRequest.call(this, 'GET', `/bank-accounts/${bankAccountId}`);
}
if (operation === 'getAll') {
const returnAll = this.getNodeParameter('returnAll', itemIndex, false);
const options = this.getNodeParameter('options', itemIndex, {});
const qs = {
...options,
};
if (returnAll) {
return await alegraApiRequestAllItems.call(this, 'GET', '/bank-accounts', {}, qs);
}
else {
qs.limit = this.getNodeParameter('limit', itemIndex, 30);
return await alegraApiRequest.call(this, 'GET', '/bank-accounts', {}, qs);
}
}
if (operation === 'transfer') {
const fromBankAccount = this.getNodeParameter('fromBankAccount', itemIndex);
const toBankAccount = this.getNodeParameter('toBankAccount', itemIndex);
const amount = this.getNodeParameter('amount', itemIndex);
const date = this.getNodeParameter('date', itemIndex);
const body = {
fromBankAccount,
toBankAccount,
amount,
date,
};
const additionalFields = this.getNodeParameter('additionalFields', itemIndex, {});
Object.assign(body, additionalFields);
return await alegraApiRequest.call(this, 'POST', '/bank-transfers', body);
}
}
exports.handleBankAccountOperations = handleBankAccountOperations;
//# sourceMappingURL=GenericFunctions.js.map