updated the announcement apis

This commit is contained in:
ROSHAN GARG 2024-10-16 12:47:30 +05:30
parent 1d1330dfe3
commit c2b01a7ed5

View File

@ -46,13 +46,31 @@ export const getAnnouncements = async (req, res) => {
// Get announcements for a specific role (RDs, PDs, SCs, or TMs) // Get announcements for a specific role (RDs, PDs, SCs, or TMs)
// Controller to get announcements for a specific role // Controller to get announcements for a specific role
export const getAnnouncementsByRole = async (req, res) => { export const getAnnouncementsByRole = async (req, res) => {
try { const { page = 1, rowsPerPage = 5 } = req.query; // Extract pagination params
// Extract role from the URL path, e.g., 'RDs', 'PDs', etc.
const role = req.path.split("/").pop();
console.log("role");
const announcements = await AnnouncemntModal.find({ sentTo: role }); try {
res.status(200).json(announcements); // Extract role from the URL path
const role = req.path.split("/").pop();
// Calculate pagination parameters
const skip = (page - 1) * rowsPerPage;
const limit = parseInt(rowsPerPage, 10);
// Get total count of announcements for the given role
const totalAnnouncements = await AnnouncemntModal.countDocuments({
sentTo: role,
});
// Fetch announcements with pagination for the given role
const announcements = await AnnouncemntModal.find({ sentTo: role })
.skip(skip)
.limit(limit)
.sort({ createdAt: -1 });
res.status(200).json({
announcements,
totalAnnouncements,
});
} catch (err) { } catch (err) {
console.error(err); console.error(err);
res.status(500).json({ error: "Server error" }); res.status(500).json({ error: "Server error" });