tududi/backend/modules/notifications/controller.js
Chris ea6d2b3ee8
fix(notifications): Add missing test notification endpoint (Issue #1002) (#1047)
The frontend test notification feature was calling /api/test-notifications/trigger,
but this endpoint didn't exist in the backend, causing a 404 HTML response that
failed JSON parsing with the error "JSON.parse: unexpected character at line 1 column 1".

Changes:
- Added triggerTestNotification service method to handle test notification creation
- Added triggerTestNotification controller method to handle the API request
- Registered POST /test-notifications/trigger route
- Implemented type mapping for all notification types (task_due_soon, task_overdue,
  defer_until, project_due_soon, project_overdue)
- Test notifications respect user notification preferences and send to enabled channels
- Returns notification details with sources (in-app, telegram) sent to

Fixes #1002
2026-04-18 17:51:27 +03:00

107 lines
2.8 KiB
JavaScript

'use strict';
const notificationsService = require('./service');
const { UnauthorizedError } = require('../../shared/errors');
const { getAuthenticatedUserId } = require('../../utils/request-utils');
function requireUserId(req) {
const userId = getAuthenticatedUserId(req);
if (!userId) {
throw new UnauthorizedError('Authentication required');
}
return userId;
}
const notificationsController = {
async getAll(req, res, next) {
try {
const userId = requireUserId(req);
const result = await notificationsService.getAll(userId, req.query);
res.json(result);
} catch (error) {
next(error);
}
},
async getUnreadCount(req, res, next) {
try {
const userId = requireUserId(req);
const result = await notificationsService.getUnreadCount(userId);
res.json(result);
} catch (error) {
next(error);
}
},
async markAsRead(req, res, next) {
try {
const userId = requireUserId(req);
const result = await notificationsService.markAsRead(
userId,
req.params.id
);
res.json(result);
} catch (error) {
next(error);
}
},
async markAsUnread(req, res, next) {
try {
const userId = requireUserId(req);
const result = await notificationsService.markAsUnread(
userId,
req.params.id
);
res.json(result);
} catch (error) {
next(error);
}
},
async markAllAsRead(req, res, next) {
try {
const userId = requireUserId(req);
const result = await notificationsService.markAllAsRead(userId);
res.json(result);
} catch (error) {
next(error);
}
},
async dismiss(req, res, next) {
try {
const userId = requireUserId(req);
const result = await notificationsService.dismiss(
userId,
req.params.id
);
res.json(result);
} catch (error) {
next(error);
}
},
async triggerTestNotification(req, res, next) {
try {
const userId = requireUserId(req);
const { type } = req.body;
if (!type) {
return res
.status(400)
.json({ error: 'Notification type is required' });
}
const result = await notificationsService.triggerTestNotification(
userId,
type
);
res.json(result);
} catch (error) {
next(error);
}
},
};
module.exports = notificationsController;