n8n-nodes-mautic-advanced
Version:
Enhanced n8n node for Mautic with comprehensive API coverage including tags, campaigns, categories, and advanced contact management
138 lines (137 loc) • 6.17 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.executeNotificationOperation = void 0;
const n8n_workflow_1 = require("n8n-workflow");
const ApiHelpers_1 = require("../utils/ApiHelpers");
const DataHelpers_1 = require("../utils/DataHelpers");
async function executeNotificationOperation(context, operation, i) {
let responseData;
try {
switch (operation) {
case 'create':
responseData = await createNotification(context, i);
break;
case 'update':
responseData = await updateNotification(context, i);
break;
case 'get':
responseData = await getNotification(context, i);
break;
case 'getAll':
responseData = await getAllNotifications(context, i);
break;
case 'delete':
responseData = await deleteNotification(context, i);
break;
default:
throw new n8n_workflow_1.NodeOperationError(context.getNode(), `Operation '${operation}' is not supported for Notification resource.`, { itemIndex: i });
}
return context.helpers.returnJsonArray((0, DataHelpers_1.wrapSingleItem)(responseData));
}
catch (error) {
return (0, ApiHelpers_1.handleApiError)(context, error, operation, 'Notification');
}
}
exports.executeNotificationOperation = executeNotificationOperation;
async function createNotification(context, itemIndex) {
const name = (0, ApiHelpers_1.getRequiredParam)(context, 'name', itemIndex);
const heading = (0, ApiHelpers_1.getRequiredParam)(context, 'heading', itemIndex);
const message = (0, ApiHelpers_1.getRequiredParam)(context, 'message', itemIndex);
const url = (0, ApiHelpers_1.getOptionalParam)(context, 'url', itemIndex, '');
const isPublished = (0, ApiHelpers_1.getOptionalParam)(context, 'isPublished', itemIndex, false);
const language = (0, ApiHelpers_1.getOptionalParam)(context, 'language', itemIndex, '');
const publishUp = (0, ApiHelpers_1.getOptionalParam)(context, 'publishUp', itemIndex, '');
const publishDown = (0, ApiHelpers_1.getOptionalParam)(context, 'publishDown', itemIndex, '');
const additionalFields = (0, ApiHelpers_1.getOptionalParam)(context, 'additionalFields', itemIndex, {});
const body = {
name,
heading,
message,
};
if (url) {
body.url = url;
}
if (isPublished !== undefined) {
body.isPublished = isPublished;
}
if (language) {
body.language = language;
}
if (publishUp) {
body.publishUp = publishUp;
}
if (publishDown) {
body.publishDown = publishDown;
}
if (additionalFields.category) {
body.category = additionalFields.category;
}
const endpoint = '/notifications/new';
const response = await (0, ApiHelpers_1.makeApiRequest)(context, 'POST', endpoint, body);
return response.notification;
}
async function updateNotification(context, itemIndex) {
const notificationId = (0, ApiHelpers_1.getRequiredParam)(context, 'notificationId', itemIndex);
const createIfNotFound = (0, ApiHelpers_1.getOptionalParam)(context, 'createIfNotFound', itemIndex, false);
const updateFields = (0, ApiHelpers_1.getOptionalParam)(context, 'updateFields', itemIndex, {});
const body = {};
if (updateFields.name) {
body.name = updateFields.name;
}
if (updateFields.heading) {
body.heading = updateFields.heading;
}
if (updateFields.message) {
body.message = updateFields.message;
}
if (updateFields.url !== undefined) {
body.url = updateFields.url;
}
if (updateFields.isPublished !== undefined) {
body.isPublished = updateFields.isPublished;
}
if (updateFields.language !== undefined) {
body.language = updateFields.language;
}
if (updateFields.publishUp !== undefined) {
body.publishUp = updateFields.publishUp;
}
if (updateFields.publishDown !== undefined) {
body.publishDown = updateFields.publishDown;
}
if (updateFields.category !== undefined) {
body.category = updateFields.category;
}
const endpoint = `/notifications/${notificationId}/edit`;
const method = createIfNotFound ? 'PUT' : 'PATCH';
const response = await (0, ApiHelpers_1.makeApiRequest)(context, method, endpoint, body);
return response.notification;
}
async function getNotification(context, itemIndex) {
const notificationId = (0, ApiHelpers_1.getRequiredParam)(context, 'notificationId', itemIndex);
const endpoint = `/notifications/${notificationId}`;
const response = await (0, ApiHelpers_1.makeApiRequest)(context, 'GET', endpoint);
return (0, DataHelpers_1.convertNumericStrings)(response.notification);
}
async function getAllNotifications(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 = (0, DataHelpers_1.buildQueryFromOptions)(options);
if (returnAll) {
const result = await (0, ApiHelpers_1.makePaginatedRequest)(context, 'notifications', 'GET', '/notifications', {}, query);
return (0, DataHelpers_1.convertNumericStrings)(result);
}
else {
query.limit = limit;
const response = await (0, ApiHelpers_1.makeApiRequest)(context, 'GET', '/notifications', {}, query);
const data = Object.values(response.notifications || {});
return (0, DataHelpers_1.convertNumericStrings)(data);
}
}
async function deleteNotification(context, itemIndex) {
const notificationId = (0, ApiHelpers_1.getRequiredParam)(context, 'notificationId', itemIndex);
const endpoint = `/notifications/${notificationId}/delete`;
const response = await (0, ApiHelpers_1.makeApiRequest)(context, 'DELETE', endpoint);
return response.notification;
}