updated the announcement apis
This commit is contained in:
parent
1d1330dfe3
commit
c2b01a7ed5
@ -46,13 +46,31 @@ export const getAnnouncements = async (req, res) => {
|
||||
// Get announcements for a specific role (RDs, PDs, SCs, or TMs)
|
||||
// Controller to get announcements for a specific role
|
||||
export const getAnnouncementsByRole = async (req, res) => {
|
||||
try {
|
||||
// Extract role from the URL path, e.g., 'RDs', 'PDs', etc.
|
||||
const role = req.path.split("/").pop();
|
||||
console.log("role");
|
||||
const { page = 1, rowsPerPage = 5 } = req.query; // Extract pagination params
|
||||
|
||||
const announcements = await AnnouncemntModal.find({ sentTo: role });
|
||||
res.status(200).json(announcements);
|
||||
try {
|
||||
// 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) {
|
||||
console.error(err);
|
||||
res.status(500).json({ error: "Server error" });
|
||||
|
Loading…
Reference in New Issue
Block a user