n8n-nodes-mautic-advanced
Version:
Enhanced n8n node for Mautic with comprehensive API coverage including tags, campaigns, categories, and advanced contact management
175 lines (174 loc) • 7.34 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.executeThemeOperation = void 0;
const n8n_workflow_1 = require("n8n-workflow");
const ApiHelpers_1 = require("../utils/ApiHelpers");
const DataHelpers_1 = require("../utils/DataHelpers");
const authenticatedRequest_1 = require("../utils/authenticatedRequest");
async function executeThemeOperation(context, operation, i) {
try {
let responseData;
switch (operation) {
case 'create':
responseData = await createTheme(context, i);
break;
case 'get':
// getTheme returns INodeExecutionData[] directly with binary data
return await getTheme(context, i);
case 'getAll':
responseData = await getAllThemes(context, i);
break;
case 'delete':
responseData = await deleteTheme(context, i);
break;
default:
throw new n8n_workflow_1.NodeOperationError(context.getNode(), `Operation '${operation}' is not supported for Theme resource.`, { itemIndex: i });
}
return context.helpers.returnJsonArray((0, DataHelpers_1.wrapSingleItem)(responseData));
}
catch (error) {
return (0, ApiHelpers_1.handleApiError)(context, error, operation, 'Theme');
}
}
exports.executeThemeOperation = executeThemeOperation;
async function createTheme(context, itemIndex) {
const filePropertyName = (0, ApiHelpers_1.getRequiredParam)(context, 'file', itemIndex);
const items = context.getInputData();
const binaryData = items[itemIndex].binary?.[filePropertyName];
if (!binaryData) {
throw new n8n_workflow_1.NodeOperationError(context.getNode(), `No binary data found for property '${filePropertyName}'. Please provide a zip file.`, { itemIndex });
}
const authenticationMethod = context.getNodeParameter('authentication', 0, 'credentials');
const options = {
headers: {},
method: 'POST',
uri: '/api/themes/new',
formData: {
file: {
value: Buffer.from(binaryData.data, 'base64'),
options: {
filename: binaryData.fileName || 'theme.zip',
contentType: binaryData.mimeType || 'application/zip',
},
},
},
};
try {
const returnData = await (0, authenticatedRequest_1.requestMauticAuthenticated)(context, authenticationMethod, options);
if (returnData.errors) {
throw new n8n_workflow_1.NodeOperationError(context.getNode(), 'Theme creation failed', {
itemIndex,
description: returnData,
});
}
return returnData;
}
catch (error) {
if (error instanceof n8n_workflow_1.NodeApiError || error instanceof n8n_workflow_1.NodeOperationError) {
throw error;
}
throw new n8n_workflow_1.NodeOperationError(context.getNode(), `Failed to create theme: ${error?.message || 'Unknown error'}`, { itemIndex });
}
}
async function getTheme(context, itemIndex) {
const themeName = (0, ApiHelpers_1.getRequiredParam)(context, 'themeName', itemIndex);
const authenticationMethod = context.getNodeParameter('authentication', 0, 'credentials');
const options = {
headers: {},
method: 'GET',
uri: `/api/themes/${themeName}`,
encoding: null,
json: false,
};
try {
const response = await (0, authenticatedRequest_1.requestMauticAuthenticated)(context, authenticationMethod, options);
const binaryData = await context.helpers.prepareBinaryData(response, `${themeName}.zip`);
return [
{
json: {
themeName,
success: true,
},
binary: {
theme: binaryData,
},
},
];
}
catch (error) {
if (error instanceof n8n_workflow_1.NodeApiError || error instanceof n8n_workflow_1.NodeOperationError) {
throw error;
}
throw new n8n_workflow_1.NodeOperationError(context.getNode(), `Failed to get theme: ${error?.message || 'Unknown error'}`, { itemIndex });
}
}
async function getAllThemes(context, itemIndex) {
const returnAll = (0, ApiHelpers_1.getOptionalParam)(context, 'returnAll', itemIndex, false);
const limit = (0, ApiHelpers_1.getOptionalParam)(context, 'limit', itemIndex, 50);
const options = (0, ApiHelpers_1.getOptionalParam)(context, 'options', itemIndex, {});
const query = {};
if (options.search)
query.search = options.search;
if (options.orderBy)
query.orderBy = options.orderBy;
if (options.orderByDir)
query.orderByDir = options.orderByDir;
const authenticationMethod = context.getNodeParameter('authentication', 0, 'credentials');
const requestOptions = {
headers: {},
method: 'GET',
uri: '/api/themes',
qs: query,
json: true,
};
try {
const response = await (0, authenticatedRequest_1.requestMauticAuthenticated)(context, authenticationMethod, requestOptions);
if (response.errors) {
throw new n8n_workflow_1.NodeOperationError(context.getNode(), 'Failed to get themes', {
itemIndex,
description: response,
});
}
const themes = response.themes || {};
const themesArray = Object.entries(themes).map(([name, config]) => ({
name,
...config,
}));
if (!returnAll && limit) {
return (0, DataHelpers_1.convertNumericStrings)(themesArray.slice(0, limit));
}
return (0, DataHelpers_1.convertNumericStrings)(themesArray);
}
catch (error) {
if (error instanceof n8n_workflow_1.NodeApiError || error instanceof n8n_workflow_1.NodeOperationError) {
throw error;
}
throw new n8n_workflow_1.NodeOperationError(context.getNode(), `Failed to get themes: ${error?.message || 'Unknown error'}`, { itemIndex });
}
}
async function deleteTheme(context, itemIndex) {
const themeName = (0, ApiHelpers_1.getRequiredParam)(context, 'themeName', itemIndex);
const authenticationMethod = context.getNodeParameter('authentication', 0, 'credentials');
const options = {
headers: {},
method: 'DELETE',
uri: `/api/themes/${themeName}/delete`,
json: true,
};
try {
const response = await (0, authenticatedRequest_1.requestMauticAuthenticated)(context, authenticationMethod, options);
if (response.errors) {
throw new n8n_workflow_1.NodeOperationError(context.getNode(), 'Theme deletion failed', {
itemIndex,
description: response,
});
}
return response || { success: true };
}
catch (error) {
if (error instanceof n8n_workflow_1.NodeApiError || error instanceof n8n_workflow_1.NodeOperationError) {
throw error;
}
throw new n8n_workflow_1.NodeOperationError(context.getNode(), `Failed to delete theme: ${error?.message || 'Unknown error'}`, { itemIndex });
}
}