Merge branch 'main' of https://git.cnapp.co.in/gitadmin/api
This commit is contained in:
commit
9a4e7912b8
@ -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" });
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
import { getStartAndEndOfDay } from "../Task/TaskController.js";
|
||||||
import { Notification } from "./notificationModal.js";
|
import { Notification } from "./notificationModal.js";
|
||||||
|
|
||||||
export const getNotification = async (req, res) => {
|
export const getNotification = async (req, res) => {
|
||||||
@ -54,3 +55,39 @@ export const getSingleNotification = async (req, res) => {
|
|||||||
.json({ return_message: "Something went wrong", error });
|
.json({ return_message: "Something went wrong", error });
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
export const getNotificationByDates = async (req, res) => {
|
||||||
|
try {
|
||||||
|
const filter = { added_for: req.user._id }; // Default filter by user ID
|
||||||
|
|
||||||
|
// Get the date from the query and parse it
|
||||||
|
const { Date: queryDate } = req.query;
|
||||||
|
let notificationDate = queryDate
|
||||||
|
? new Date(queryDate.split("/").reverse().join("-")) // Convert DD/MM/YYYY to YYYY-MM-DD
|
||||||
|
: new Date(); // Default to today's date
|
||||||
|
|
||||||
|
// Get the start and end of the day
|
||||||
|
const { startOfDay, endOfDay } = getStartAndEndOfDay(notificationDate);
|
||||||
|
|
||||||
|
// Find notifications filtered by user and date range
|
||||||
|
const notifications = await Notification.find({
|
||||||
|
...filter,
|
||||||
|
createdAt: { $gte: startOfDay, $lte: endOfDay },
|
||||||
|
}).sort({ createdAt: -1 });
|
||||||
|
|
||||||
|
if (notifications.length > 0) {
|
||||||
|
return res.status(200).json({
|
||||||
|
success: true,
|
||||||
|
notifications,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return res
|
||||||
|
.status(404)
|
||||||
|
.json({ success: false, message: "No notifications found" });
|
||||||
|
} catch (error) {
|
||||||
|
res.status(500).json({
|
||||||
|
success: false,
|
||||||
|
message: error.message,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
||||||
|
@ -1,6 +1,9 @@
|
|||||||
import express from "express";
|
import express from "express";
|
||||||
import { isAuthenticatedSalesCoOrdinator } from "../../middlewares/SalesCoOrdinatorAuth.js";
|
import { isAuthenticatedSalesCoOrdinator } from "../../middlewares/SalesCoOrdinatorAuth.js";
|
||||||
import { getNotification } from "./notificationController.js";
|
import {
|
||||||
|
getNotification,
|
||||||
|
getNotificationByDates,
|
||||||
|
} from "./notificationController.js";
|
||||||
import { isAuthenticatedTerritoryManager } from "../../middlewares/TerritoryManagerAuth.js";
|
import { isAuthenticatedTerritoryManager } from "../../middlewares/TerritoryManagerAuth.js";
|
||||||
import { isAuthenticatedUser } from "../../middlewares/auth.js";
|
import { isAuthenticatedUser } from "../../middlewares/auth.js";
|
||||||
import { isAuthenticatedRD } from "../../middlewares/rdAuth.js";
|
import { isAuthenticatedRD } from "../../middlewares/rdAuth.js";
|
||||||
@ -9,7 +12,7 @@ const router = express.Router();
|
|||||||
|
|
||||||
router
|
router
|
||||||
.route("/get-notification-sc")
|
.route("/get-notification-sc")
|
||||||
.get(isAuthenticatedSalesCoOrdinator, getNotification);
|
.get(isAuthenticatedSalesCoOrdinator, getNotificationByDates);
|
||||||
|
|
||||||
router
|
router
|
||||||
.route("/get-notification-sc/:id")
|
.route("/get-notification-sc/:id")
|
||||||
@ -17,13 +20,17 @@ router
|
|||||||
|
|
||||||
router
|
router
|
||||||
.route("/get-notification-tm")
|
.route("/get-notification-tm")
|
||||||
.get(isAuthenticatedTerritoryManager, getNotification);
|
.get(isAuthenticatedTerritoryManager, getNotificationByDates);
|
||||||
|
|
||||||
router
|
router
|
||||||
.route("/get-notification-tm/:id")
|
.route("/get-notification-tm/:id")
|
||||||
.get(isAuthenticatedTerritoryManager, getNotification);
|
.get(isAuthenticatedTerritoryManager, getNotification);
|
||||||
|
|
||||||
router.route("/get-notification-pd").get(isAuthenticatedUser, getNotification);
|
router
|
||||||
router.route("/get-notification-rd").get(isAuthenticatedRD, getNotification);
|
.route("/get-notification-pd")
|
||||||
|
.get(isAuthenticatedUser, getNotificationByDates);
|
||||||
|
router
|
||||||
|
.route("/get-notification-rd")
|
||||||
|
.get(isAuthenticatedRD, getNotificationByDates);
|
||||||
|
|
||||||
export default router;
|
export default router;
|
||||||
|
@ -168,7 +168,7 @@ export const getTasksByStatus = async (req, res) => {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
const getStartAndEndOfDay = (date) => {
|
export const getStartAndEndOfDay = (date) => {
|
||||||
const startOfDay = new Date(date);
|
const startOfDay = new Date(date);
|
||||||
startOfDay.setUTCHours(0, 0, 0, 0); // Start of the day UTC
|
startOfDay.setUTCHours(0, 0, 0, 0); // Start of the day UTC
|
||||||
|
|
||||||
@ -213,7 +213,10 @@ export const getTasksByDates = async (req, res) => {
|
|||||||
createdAt: { $gte: startOfDay, $lte: endOfDay },
|
createdAt: { $gte: startOfDay, $lte: endOfDay },
|
||||||
})
|
})
|
||||||
.populate({
|
.populate({
|
||||||
path: req.userType === "SalesCoOrdinator" ? "taskAssignedBy" : "taskAssignedTo", // Change path based on user type
|
path:
|
||||||
|
req.userType === "SalesCoOrdinator"
|
||||||
|
? "taskAssignedBy"
|
||||||
|
: "taskAssignedTo", // Change path based on user type
|
||||||
select: "name mobileNumber email",
|
select: "name mobileNumber email",
|
||||||
})
|
})
|
||||||
.sort({ createdAt: -1 });
|
.sort({ createdAt: -1 });
|
||||||
|
Loading…
Reference in New Issue
Block a user