UNPKG

n8n-nodes-fatture-in-cloud

Version:

n8n community node for Fatture in Cloud integration using the official TypeScript SDK

242 lines 14.7 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.SettingsModule = void 0; const n8n_workflow_1 = require("n8n-workflow"); const fattureincloud_ts_sdk_1 = require("@fattureincloud/fattureincloud-ts-sdk"); const SettingsResource_1 = require("../resources/SettingsResource"); class SettingsModule { constructor() { this.resource = SettingsResource_1.SettingsResource; } async execute(context, operation, itemIndex) { var _a; const credentials = await context.getCredentials('fattureInCloudOAuth2Api'); let accessToken; if ((_a = credentials.oauthTokenData) === null || _a === void 0 ? void 0 : _a.access_token) { accessToken = credentials.oauthTokenData.access_token; } else if (credentials.access_token) { accessToken = credentials.access_token; } else if (credentials.accessToken) { accessToken = credentials.accessToken; } else { throw new n8n_workflow_1.NodeOperationError(context.getNode(), 'Access token not found in credentials. Please re-authenticate your Fatture in Cloud connection.', { itemIndex }); } const config = new fattureincloud_ts_sdk_1.Configuration({ accessToken, }); const settingsApi = new fattureincloud_ts_sdk_1.SettingsApi(config); const companyId = context.getNodeParameter('companyId', itemIndex); const settingsType = context.getNodeParameter('settingsType', itemIndex); let responseData; switch (operation) { case 'create': switch (settingsType) { case 'payment_accounts': const accountName = context.getNodeParameter('name', itemIndex); const accountType = context.getNodeParameter('type', itemIndex, 'standard'); const iban = context.getNodeParameter('iban', itemIndex, ''); if (!accountName || accountName.trim() === '') { throw new n8n_workflow_1.NodeOperationError(context.getNode(), 'Name is required for payment account.', { itemIndex }); } const paymentAccountData = { name: accountName.trim(), type: accountType, }; if (iban.trim()) { paymentAccountData.iban = iban.trim(); } const createAccountRequest = { data: paymentAccountData, }; const createAccountResponse = await settingsApi.createPaymentAccount(companyId, createAccountRequest); responseData = createAccountResponse.data; break; case 'payment_methods': const methodName = context.getNodeParameter('name', itemIndex); if (!methodName || methodName.trim() === '') { throw new n8n_workflow_1.NodeOperationError(context.getNode(), 'Name is required for payment method.', { itemIndex }); } const paymentMethodData = { name: methodName.trim(), }; const createMethodRequest = { data: paymentMethodData, }; const createMethodResponse = await settingsApi.createPaymentMethod(companyId, createMethodRequest); responseData = createMethodResponse.data; break; case 'vat_types': const vatValue = context.getNodeParameter('vatValue', itemIndex, 22); const vatDescription = context.getNodeParameter('vatDescription', itemIndex, ''); const vatNotes = context.getNodeParameter('vatNotes', itemIndex, ''); const eInvoice = context.getNodeParameter('eInvoice', itemIndex, true); const vatTypeData = { value: vatValue, e_invoice: eInvoice, }; if (vatDescription.trim()) { vatTypeData.description = vatDescription.trim(); } if (vatNotes.trim()) { vatTypeData.notes = vatNotes.trim(); } const createVatRequest = { data: vatTypeData, }; const createVatResponse = await settingsApi.createVatType(companyId, createVatRequest); responseData = createVatResponse.data; break; default: throw new n8n_workflow_1.NodeOperationError(context.getNode(), `Create operation not supported for settings type: ${settingsType}`, { itemIndex }); } break; case 'get': switch (settingsType) { case 'tax_profile': const taxProfileResponse = await settingsApi.getTaxProfile(companyId); responseData = taxProfileResponse.data; break; case 'payment_accounts': const accountId = context.getNodeParameter('itemId', itemIndex); if (!accountId) { throw new n8n_workflow_1.NodeOperationError(context.getNode(), 'Item ID is required.', { itemIndex }); } const additionalFields = context.getNodeParameter('additionalFields', itemIndex, {}); const accountResponse = await settingsApi.getPaymentAccount(companyId, accountId, additionalFields.fields, additionalFields.fieldset); responseData = accountResponse.data; break; case 'payment_methods': const methodId = context.getNodeParameter('itemId', itemIndex); if (!methodId) { throw new n8n_workflow_1.NodeOperationError(context.getNode(), 'Item ID is required.', { itemIndex }); } const methodAdditionalFields = context.getNodeParameter('additionalFields', itemIndex, {}); const methodResponse = await settingsApi.getPaymentMethod(companyId, methodId, methodAdditionalFields.fields, methodAdditionalFields.fieldset); responseData = methodResponse.data; break; case 'vat_types': const vatId = context.getNodeParameter('itemId', itemIndex); if (!vatId) { throw new n8n_workflow_1.NodeOperationError(context.getNode(), 'Item ID is required.', { itemIndex }); } const vatResponse = await settingsApi.getVatType(companyId, vatId); responseData = vatResponse.data; break; default: throw new n8n_workflow_1.NodeOperationError(context.getNode(), `Get operation not supported for settings type: ${settingsType}`, { itemIndex }); } break; case 'update': switch (settingsType) { case 'payment_accounts': const updateAccountId = context.getNodeParameter('itemId', itemIndex); const updateAccountName = context.getNodeParameter('name', itemIndex); const updateAccountType = context.getNodeParameter('type', itemIndex, 'standard'); const updateIban = context.getNodeParameter('iban', itemIndex, ''); if (!updateAccountId) { throw new n8n_workflow_1.NodeOperationError(context.getNode(), 'Item ID is required.', { itemIndex }); } if (!updateAccountName || updateAccountName.trim() === '') { throw new n8n_workflow_1.NodeOperationError(context.getNode(), 'Name is required for payment account.', { itemIndex }); } const updatePaymentAccountData = { name: updateAccountName.trim(), type: updateAccountType, }; if (updateIban.trim()) { updatePaymentAccountData.iban = updateIban.trim(); } const modifyAccountRequest = { data: updatePaymentAccountData, }; const updateAccountResponse = await settingsApi.modifyPaymentAccount(companyId, updateAccountId, modifyAccountRequest); responseData = updateAccountResponse.data; break; case 'payment_methods': const updateMethodId = context.getNodeParameter('itemId', itemIndex); const updateMethodName = context.getNodeParameter('name', itemIndex); if (!updateMethodId) { throw new n8n_workflow_1.NodeOperationError(context.getNode(), 'Item ID is required.', { itemIndex }); } if (!updateMethodName || updateMethodName.trim() === '') { throw new n8n_workflow_1.NodeOperationError(context.getNode(), 'Name is required for payment method.', { itemIndex }); } const updatePaymentMethodData = { name: updateMethodName.trim(), }; const modifyMethodRequest = { data: updatePaymentMethodData, }; const updateMethodResponse = await settingsApi.modifyPaymentMethod(companyId, updateMethodId, modifyMethodRequest); responseData = updateMethodResponse.data; break; case 'vat_types': const updateVatId = context.getNodeParameter('itemId', itemIndex); const updateVatValue = context.getNodeParameter('vatValue', itemIndex, 22); const updateVatDescription = context.getNodeParameter('vatDescription', itemIndex, ''); const updateVatNotes = context.getNodeParameter('vatNotes', itemIndex, ''); const updateEInvoice = context.getNodeParameter('eInvoice', itemIndex, true); if (!updateVatId) { throw new n8n_workflow_1.NodeOperationError(context.getNode(), 'Item ID is required.', { itemIndex }); } const updateVatTypeData = { value: updateVatValue, e_invoice: updateEInvoice, }; if (updateVatDescription.trim()) { updateVatTypeData.description = updateVatDescription.trim(); } if (updateVatNotes.trim()) { updateVatTypeData.notes = updateVatNotes.trim(); } const modifyVatRequest = { data: updateVatTypeData, }; const updateVatResponse = await settingsApi.modifyVatType(companyId, updateVatId, modifyVatRequest); responseData = updateVatResponse.data; break; default: throw new n8n_workflow_1.NodeOperationError(context.getNode(), `Update operation not supported for settings type: ${settingsType}`, { itemIndex }); } break; case 'delete': switch (settingsType) { case 'payment_accounts': const deleteAccountId = context.getNodeParameter('itemId', itemIndex); if (!deleteAccountId) { throw new n8n_workflow_1.NodeOperationError(context.getNode(), 'Item ID is required.', { itemIndex }); } await settingsApi.deletePaymentAccount(companyId, deleteAccountId); responseData = { success: true, message: 'Payment account deleted successfully' }; break; case 'payment_methods': const deleteMethodId = context.getNodeParameter('itemId', itemIndex); if (!deleteMethodId) { throw new n8n_workflow_1.NodeOperationError(context.getNode(), 'Item ID is required.', { itemIndex }); } await settingsApi.deletePaymentMethod(companyId, deleteMethodId); responseData = { success: true, message: 'Payment method deleted successfully' }; break; case 'vat_types': const deleteVatId = context.getNodeParameter('itemId', itemIndex); if (!deleteVatId) { throw new n8n_workflow_1.NodeOperationError(context.getNode(), 'Item ID is required.', { itemIndex }); } await settingsApi.deleteVatType(companyId, deleteVatId); responseData = { success: true, message: 'VAT type deleted successfully' }; break; default: throw new n8n_workflow_1.NodeOperationError(context.getNode(), `Delete operation not supported for settings type: ${settingsType}`, { itemIndex }); } break; default: throw new n8n_workflow_1.NodeOperationError(context.getNode(), `Unknown operation: ${operation}`, { itemIndex }); } return responseData; } } exports.SettingsModule = SettingsModule; //# sourceMappingURL=SettingsModule.js.map