138 lines
3.5 KiB
JavaScript
138 lines
3.5 KiB
JavaScript
/**
|
|
* Copyright (c) 2026 Niolla
|
|
* All rights reserved.
|
|
*
|
|
* This source code is proprietary and confidential.
|
|
* Unauthorized copying, modification, distribution, or use
|
|
* of this file, via any medium, is strictly prohibited.
|
|
*/
|
|
|
|
// app/controllers/notification.controller.js
|
|
|
|
const db = require("../models");
|
|
const Notification = db.notification;
|
|
const UserNotification = db.userNotification;
|
|
|
|
const log = require("../utils/consoleLog.utill").log;
|
|
|
|
// Controller for managing notifications
|
|
exports.createNotification = async (req, res) => {
|
|
try {
|
|
const { notificationHeadline, notificationDescription, notificationType } =
|
|
req.body;
|
|
|
|
const id = Date.now().toString(); // Generate a unique ID based on the current timestamp
|
|
const notification_id = `notif_${id}`; // Prefix the ID with "notif_"
|
|
|
|
// Create a new notification
|
|
const newNotification = await Notification.create({
|
|
notification_id,
|
|
notificationHeadline,
|
|
notificationDescription,
|
|
notificationType,
|
|
dateCreated: new Date(),
|
|
});
|
|
|
|
res.status(201).json({
|
|
success: true,
|
|
message: "Notification created successfully",
|
|
notification: newNotification,
|
|
});
|
|
} catch (error) {
|
|
log("Error creating notification:", error.message);
|
|
res.status(500).json({
|
|
success: false,
|
|
message: "Internal server error",
|
|
error: error.message
|
|
});
|
|
}
|
|
};
|
|
|
|
// Mark a notification as read for a user
|
|
exports.markAsRead = async (req, res) => {
|
|
try {
|
|
const { userId, notificationId } = req.params;
|
|
|
|
// Find the user notification entry
|
|
const userNotification = await UserNotification.findOne({
|
|
where: { user_id: userId, notification_id: notificationId },
|
|
});
|
|
|
|
if (!userNotification) {
|
|
return res.status(404).json({ success: false, error: "User notification not found" });
|
|
}
|
|
|
|
// Mark the notification as read
|
|
userNotification.isRead = true;
|
|
await userNotification.save();
|
|
res.status(200).json({
|
|
success: true,
|
|
message: "Notification marked as read",
|
|
});
|
|
}
|
|
|
|
catch (error) {
|
|
log("Error marking notification as read:", error.message);
|
|
res.status(500).json({
|
|
success: false,
|
|
message: "Internal server error",
|
|
error: error.message
|
|
});
|
|
}
|
|
};
|
|
|
|
// Get announcements for all users
|
|
exports.getAnnouncements = async (req, res) => {
|
|
try {
|
|
|
|
// Fetch all announcements
|
|
const announcements = await Notification.findAll({
|
|
where: { notificationType: "ANNOUNCEMENT", isActive: true },
|
|
});
|
|
|
|
res.status(200).json({
|
|
success: true,
|
|
announcements,
|
|
});
|
|
|
|
} catch (error) {
|
|
log("Error fetching announcements:", error.message);
|
|
res.status(500).json({
|
|
success: false,
|
|
message: "Internal server error",
|
|
error: error.message
|
|
});
|
|
}
|
|
}
|
|
|
|
// Get all notifications for a user
|
|
exports.getUserNotifications = async (req, res) => {
|
|
try {
|
|
const { userId } = req.params;
|
|
|
|
// Fetch notifications for the user
|
|
const notifications = await UserNotification.findAll({
|
|
where: { user_id: userId, isRead: false },
|
|
include: [
|
|
{
|
|
model: Notification,
|
|
as: "notification",
|
|
},
|
|
],
|
|
});
|
|
|
|
res.status(200).json({
|
|
success: true,
|
|
notifications,
|
|
});
|
|
}
|
|
|
|
catch (error) {
|
|
log("Error fetching user notifications:", error.message);
|
|
res.status(500).json({
|
|
success: false,
|
|
message: "Internal server error",
|
|
error: error.message
|
|
});
|
|
}
|
|
}; |