From 1f5630586b8e07518ccefef08cf4ac8429b1c004 Mon Sep 17 00:00:00 2001 From: Sibunnayak Date: Thu, 18 Jul 2024 11:13:19 +0530 Subject: [PATCH] attendance section update --- app.js | 4 +- middlewares/SalesCoOrdinatorAuth.js | 22 +- resources/Attendance/AttandanceRoute.js | 102 +++++ resources/Attendance/AttendanceController.js | 383 ++++++++++++++++++ resources/Attendance/AttendanceModel.js | 68 ++++ .../AttandanceSalesCoordinatorController.js | 189 --------- .../AttandanceSalesCoordinatorModel.js | 31 -- .../AttandanceSalesCoordinatorRoute.js | 41 -- .../SalesCoOrdinatorController.js | 2 +- 9 files changed, 565 insertions(+), 277 deletions(-) create mode 100644 resources/Attendance/AttandanceRoute.js create mode 100644 resources/Attendance/AttendanceController.js create mode 100644 resources/Attendance/AttendanceModel.js delete mode 100644 resources/SalesCoOrdinatorAttandance/AttandanceSalesCoordinatorController.js delete mode 100644 resources/SalesCoOrdinatorAttandance/AttandanceSalesCoordinatorModel.js delete mode 100644 resources/SalesCoOrdinatorAttandance/AttandanceSalesCoordinatorRoute.js diff --git a/app.js b/app.js index db344c4..976a12c 100644 --- a/app.js +++ b/app.js @@ -170,7 +170,7 @@ import CouponRoute from "./resources/Affiliate&Coupon/Coupon/CouponRoute.js"; // import ShortUrlRouter from "./resources/Businesses/Short_Urls/ShortUrlRoute.js"; //support Ticket // attandance -import attandanceSalesCoordinator from "./resources/SalesCoOrdinatorAttandance/AttandanceSalesCoordinatorRoute.js" +import attendance from "./resources/Attendance/AttandanceRoute.js" app.use("/api/v1/", user); //Product @@ -227,7 +227,7 @@ app.use("/api/v1/coupon", CouponRoute); //Blog app.use("/api/v1/blog", BlogRoute); //attendance -app.use("/api/v1/salescoordinator", attandanceSalesCoordinator); +app.use("/api/v1/", attendance); //config specialty // app.use("/api/config/specialty", SpecialtiesRouter); diff --git a/middlewares/SalesCoOrdinatorAuth.js b/middlewares/SalesCoOrdinatorAuth.js index 771312d..4718dbe 100644 --- a/middlewares/SalesCoOrdinatorAuth.js +++ b/middlewares/SalesCoOrdinatorAuth.js @@ -6,23 +6,23 @@ export const isAuthenticatedSalesCoOrdinator = async (req, res, next) => { if (!req.headers.authorization) { return res.status(401).json({ success: false, - message: "Please Login to access this resource", + message: "Please login to access this resource", }); } - const getToken = req.headers; - // Remove Bearer from token - const fronttoken = getToken.authorization.slice(7); + const getToken = req.headers.authorization; + const token = getToken.slice(7); - const frontdecoded = jwt.verify(fronttoken, process.env.JWT_SECRET); - if (!frontdecoded) { + const decoded = jwt.verify(token, process.env.JWT_SECRET); + if (!decoded) { return res.status(400).json({ success: false, message: "Incorrect token", }); } - const salescoordinator = await SalesCoOrdinator.findById(frontdecoded.id); + const salescoordinator = await SalesCoOrdinator.findById(decoded.id); if (salescoordinator) { - req.salescoordinator = salescoordinator; + req.user = salescoordinator; + req.userType = 'SalesCoOrdinator'; return next(); } else { return res.status(401).json({ @@ -34,11 +34,7 @@ export const isAuthenticatedSalesCoOrdinator = async (req, res, next) => { if (error.name === "TokenExpiredError") { return res.status(401).json({ message: "Token has expired." }); } else if (error.name === "JsonWebTokenError") { - if (error.message === "invalid signature") { - return res.status(401).json({ message: "Invalid token!." }); - } else { - return res.status(401).json({ message: "Invalid token." }); - } + return res.status(401).json({ message: "Invalid token." }); } else { return res.status(500).json({ message: "An internal error occurred while verifying the token.", diff --git a/resources/Attendance/AttandanceRoute.js b/resources/Attendance/AttandanceRoute.js new file mode 100644 index 0000000..78c0339 --- /dev/null +++ b/resources/Attendance/AttandanceRoute.js @@ -0,0 +1,102 @@ +// import express from "express"; +// import { +// markAttendance, +// getAttendanceBySalesCoordinator, +// getTodayAttendance, +// AdmingetAttendanceBySalesCoordinator, +// } from "./AttandanceController.js"; +// import { isAuthenticatedSalesCoOrdinator } from "../../middlewares/SalesCoOrdinatorAuth.js"; +// import { authorizeRoles, isAuthenticatedUser } from "../../middlewares/auth.js"; + +// const router = express.Router(); + +// // Place more specific routes first + +// // Route to get today's attendance for admin +// router.get( +// "/attendance/today", +// isAuthenticatedUser, +// authorizeRoles("admin"), +// getTodayAttendance +// ); + +// // Route to mark attendance +// router.post("/attendance", isAuthenticatedSalesCoOrdinator, markAttendance); + +// // Route to get attendance for the logged-in sales coordinator +// router.get( +// "/attendance", +// isAuthenticatedSalesCoOrdinator, +// getAttendanceBySalesCoordinator +// ); + +// // Admin route to get attendance by sales coordinator ID +// router.get( +// "/attendance/:id", +// isAuthenticatedUser, +// authorizeRoles("admin"), +// AdmingetAttendanceBySalesCoordinator +// ); + +// export default router; + +import express from "express"; +import { + markAttendance, + getAttendanceByUser, + getTodayAttendance, + AdmingetAttendanceByUser, +} from "./AttendanceController.js"; +import { isAuthenticatedSalesCoOrdinator } from "../../middlewares/SalesCoOrdinatorAuth.js"; +// import { isAuthenticatedTerritoryManager } from "../../middlewares/TerritoryManagerAuth.js"; +import { authorizeRoles, isAuthenticatedUser } from "../../middlewares/auth.js"; + +const router = express.Router(); + +// Place more specific routes first + +// Route to get today's attendance for admin +router.get( + "/attendance/today", + isAuthenticatedUser, + authorizeRoles("admin"), + getTodayAttendance +); + +// Route to mark attendance for Sales Coordinators +router.post( + "/attendance/salescoordinator", + isAuthenticatedSalesCoOrdinator, + markAttendance +); + +// Route to mark attendance for Territory Managers +router.post( + "/attendance/territorymanager", + // isAuthenticatedTerritoryManager, + markAttendance +); + +// Route to get attendance for the logged-in sales coordinator +router.get( + "/attendance/salescoordinator", + isAuthenticatedSalesCoOrdinator, + getAttendanceByUser +); + +// Route to get attendance for the logged-in territory manager +router.get( + "/attendance/territorymanager", + // isAuthenticatedTerritoryManager, + getAttendanceByUser +); + +// Admin route to get attendance by user ID +router.get( + "/attendance/:id", + isAuthenticatedUser, + authorizeRoles("admin"), + AdmingetAttendanceByUser +); + +export default router; diff --git a/resources/Attendance/AttendanceController.js b/resources/Attendance/AttendanceController.js new file mode 100644 index 0000000..7fa3935 --- /dev/null +++ b/resources/Attendance/AttendanceController.js @@ -0,0 +1,383 @@ +// import { Attendance } from "./AttandanceModel.js"; + +// export const markAttendance = async (req, res) => { +// try { +// const { date, time, location, notes } = req.body; +// const salesCoordinatorId = req.user._id; + +// // Get the start and end of the day to check if the attendance is already marked +// const startOfDay = new Date(date); +// startOfDay.setHours(0, 0, 0, 0); + +// const endOfDay = new Date(date); +// endOfDay.setHours(23, 59, 59, 999); + +// // Check if the attendance record exists for today +// const existingAttendance = await AttendanceSalesCoOrdinator.findOne({ +// salesCoordinator: salesCoordinatorId, +// "records.date": { $gte: startOfDay, $lte: endOfDay }, +// }); + +// if (existingAttendance) { +// return res.status(400).json({ +// success: false, +// message: "Attendance for today is already marked.", +// }); +// } + +// // Check if attendance record exists for the sales coordinator +// let attendance = await AttendanceSalesCoOrdinator.findOne({ +// salesCoordinator: salesCoordinatorId, +// }); + +// if (!attendance) { +// // Create a new attendance record if it doesn't exist +// attendance = new AttendanceSalesCoOrdinator({ +// salesCoordinator: salesCoordinatorId, +// records: [], +// }); +// } + +// // Add the new attendance record to the array +// attendance.records.push({ date, time, location, notes }); +// await attendance.save(); + +// res.status(201).json({ +// success: true, +// message: "Attendance marked successfully", +// record: { date, time, location, notes }, +// }); +// } catch (error) { +// res.status(500).json({ +// success: false, +// message: error.message || "Something went wrong", +// }); +// } +// }; + +// export const getAttendanceBySalesCoordinator = async (req, res) => { +// try { +// const salesCoordinatorId = req.user._id; + +// const attendance = await AttendanceSalesCoOrdinator.findOne({ +// salesCoordinator: salesCoordinatorId, +// }).populate("salesCoordinator", "name email"); + +// if (!attendance) { +// return res.status(404).json({ +// success: false, +// message: "No attendance records found for this sales coordinator.", +// }); +// } + +// res.status(200).json({ +// success: true, +// attendance, +// }); +// } catch (error) { +// res.status(500).json({ +// success: false, +// message: error.message || "Something went wrong", +// }); +// } +// }; +// // admin +// export const AdmingetAttendanceBySalesCoordinator = async (req, res) => { +// try { +// const { id } = req.params; +// const { page = 1, show = 10 } = req.query; +// const limit = parseInt(show); +// const skip = (page - 1) * limit; + +// // Find attendance records for the given sales coordinator +// const attendanceDoc = await AttendanceSalesCoOrdinator.findOne({ +// salesCoordinator: id, +// }).populate("salesCoordinator", "name email"); + +// if (!attendanceDoc) { +// return res.status(404).json({ +// success: false, +// message: "No attendance records found for this sales coordinator.", +// }); +// } + +// // Pagination and slicing records +// const totalData = attendanceDoc.records.length; +// const paginatedRecords = attendanceDoc.records.slice(skip, skip + limit); + +// res.status(200).json({ +// success: true, +// salesCoordinator: attendanceDoc.salesCoordinator, +// attendance: paginatedRecords, +// total_data: totalData, +// }); +// } catch (error) { +// res.status(500).json({ +// success: false, +// message: error.message || "Something went wrong", +// }); +// } +// }; + +// export const getTodayAttendance = async (req, res) => { +// try { +// const today = new Date(); +// today.setHours(0, 0, 0, 0); + +// const startOfDay = new Date(today); +// const endOfDay = new Date(today); +// endOfDay.setHours(23, 59, 59, 999); + +// const { page = 1, show = 10 } = req.query; +// const limit = parseInt(show); +// const skip = (page - 1) * limit; + +// const attendances = await AttendanceSalesCoOrdinator.aggregate([ +// { $unwind: '$records' }, +// { +// $match: { +// 'records.date': { $gte: startOfDay, $lte: endOfDay } +// } +// }, +// { +// $lookup: { +// from: 'salescoordinators', +// localField: 'salesCoordinator', +// foreignField: '_id', +// as: 'salesCoordinator' +// } +// }, +// { $unwind: '$salesCoordinator' }, +// { +// $facet: { +// totalData: [{ $count: 'count' }], +// attendance: [ +// { $skip: skip }, +// { $limit: limit }, +// { +// $project: { +// salesCoordinator: { +// id: '$salesCoordinator._id', +// name: '$salesCoordinator.name', +// email: '$salesCoordinator.email' +// }, +// date: '$records.date', +// time: '$records.time', +// location: '$records.location', +// note: '$records.notes' +// } +// } +// ] +// } +// } +// ]); + +// const totalData = attendances[0].totalData[0] ? attendances[0].totalData[0].count : 0; + +// res.status(200).json({ +// success: true, +// attendance: attendances[0].attendance, +// total_data: totalData +// }); +// } catch (error) { +// res.status(500).json({ +// success: false, +// message: error.message || 'Something went wrong' +// }); +// } +// }; + +import { Attendance } from "./AttendanceModel.js"; + +// Mark attendance +export const markAttendance = async (req, res) => { + try { + const { date, time, location, notes } = req.body; + const userId = req.user._id; + const userType = req.userType; + + // Get the start and end of the day to check if the attendance is already marked + const startOfDay = new Date(date); + startOfDay.setHours(0, 0, 0, 0); + + const endOfDay = new Date(date); + endOfDay.setHours(23, 59, 59, 999); + + // Check if the attendance record exists for today + const existingAttendance = await Attendance.findOne({ + userId, + userType, + "records.date": { $gte: startOfDay, $lte: endOfDay }, + }); + + if (existingAttendance) { + return res.status(400).json({ + success: false, + message: "Attendance for today is already marked.", + }); + } + + // Check if attendance record exists for the user + let attendance = await Attendance.findOne({ userId, userType }); + + if (!attendance) { + // Create a new attendance record if it doesn't exist + attendance = new Attendance({ + userId, + userType, + records: [], + }); + } + + // Add the new attendance record to the array + attendance.records.push({ date, time, location, notes }); + await attendance.save(); + + res.status(201).json({ + success: true, + message: "Attendance marked successfully", + record: { date, time, location, notes }, + }); + } catch (error) { + res.status(500).json({ + success: false, + message: error.message || "Something went wrong", + }); + } +}; + +// Get attendance by user ID +export const getAttendanceByUser = async (req, res) => { + try { + const userId = req.user._id; + + const attendance = await Attendance.findOne({ + userId, + }).populate("userId", "name email"); + + if (!attendance) { + return res.status(404).json({ + success: false, + message: "No attendance records found for this user.", + }); + } + + res.status(200).json({ + success: true, + attendance, + }); + } catch (error) { + res.status(500).json({ + success: false, + message: error.message || "Something went wrong", + }); + } +}; + +// Admin route to get attendance by user ID +export const AdmingetAttendanceByUser = async (req, res) => { + try { + const { id } = req.params; + const { page = 1, show = 10 } = req.query; + const limit = parseInt(show); + const skip = (page - 1) * limit; + + // Find attendance records for the given user + const attendanceDoc = await Attendance.findOne({ + userId: id, + }).populate("userId", "name email"); + + if (!attendanceDoc) { + return res.status(404).json({ + success: false, + message: "No attendance records found for this user.", + }); + } + + // Pagination and slicing records + const totalData = attendanceDoc.records.length; + const paginatedRecords = attendanceDoc.records.slice(skip, skip + limit); + + res.status(200).json({ + success: true, + user: attendanceDoc.userId, + attendance: paginatedRecords, + total_data: totalData, + }); + } catch (error) { + res.status(500).json({ + success: false, + message: error.message || "Something went wrong", + }); + } +}; + +// Get today's attendance for admin +export const getTodayAttendance = async (req, res) => { + try { + const today = new Date(); + today.setHours(0, 0, 0, 0); + + const startOfDay = new Date(today); + const endOfDay = new Date(today); + endOfDay.setHours(23, 59, 59, 999); + + const { page = 1, show = 10 } = req.query; + const limit = parseInt(show); + const skip = (page - 1) * limit; + + const attendances = await Attendance.aggregate([ + { $unwind: '$records' }, + { + $match: { + 'records.date': { $gte: startOfDay, $lte: endOfDay } + } + }, + { + $lookup: { + from: 'users', + localField: 'userId', + foreignField: '_id', + as: 'user' + } + }, + { $unwind: '$user' }, + { + $facet: { + totalData: [{ $count: 'count' }], + attendance: [ + { $skip: skip }, + { $limit: limit }, + { + $project: { + user: { + id: '$user._id', + name: '$user.name', + email: '$user.email', + userType: '$userType' + }, + date: '$records.date', + time: '$records.time', + location: '$records.location', + notes: '$records.notes', + } + } + ] + } + } + ]); + + const totalData = attendances[0].totalData[0]?.count || 0; + + res.status(200).json({ + success: true, + total_data: totalData, + attendance: attendances[0].attendance + }); + } catch (error) { + res.status(500).json({ + success: false, + message: error.message || "Something went wrong", + }); + } +}; diff --git a/resources/Attendance/AttendanceModel.js b/resources/Attendance/AttendanceModel.js new file mode 100644 index 0000000..c2a1cb0 --- /dev/null +++ b/resources/Attendance/AttendanceModel.js @@ -0,0 +1,68 @@ +// import mongoose from 'mongoose'; + +// const attendanceRecordSchema = new mongoose.Schema({ +// date: { +// type: Date, +// required: true, +// }, +// time: { +// type: String, +// required: true, +// }, +// location: { +// type: String, +// required: true, +// }, +// notes: { +// type: String, +// }, +// }); + +// const attendanceSchema = new mongoose.Schema({ +// salesCoordinator: { +// type: mongoose.Schema.Types.ObjectId, +// ref: 'SalesCoOrdinator', +// required: true, +// unique: true, +// }, +// records: [attendanceRecordSchema], +// }, { timestamps: true, versionKey: false }); + +// export const AttendanceSalesCoOrdinator = mongoose.model('Attendance', attendanceSchema); +import mongoose from 'mongoose'; + +// Define attendance record schema +const attendanceRecordSchema = new mongoose.Schema({ + date: { + type: Date, + required: true, + }, + time: { + type: String, + required: true, + }, + location: { + type: String, + required: true, + }, + notes: { + type: String, + }, +}); + +// Define main attendance schema +const attendanceSchema = new mongoose.Schema({ + userId: { + type: mongoose.Schema.Types.ObjectId, + refPath: 'userType', + required: true, + }, + userType: { + type: String, + required: true, + enum: ['SalesCoOrdinator', 'TerritoryManager'], // Specify allowed user types + }, + records: [attendanceRecordSchema], +}, { timestamps: true, versionKey: false }); + +export const Attendance = mongoose.model('Attendance', attendanceSchema); diff --git a/resources/SalesCoOrdinatorAttandance/AttandanceSalesCoordinatorController.js b/resources/SalesCoOrdinatorAttandance/AttandanceSalesCoordinatorController.js deleted file mode 100644 index e749147..0000000 --- a/resources/SalesCoOrdinatorAttandance/AttandanceSalesCoordinatorController.js +++ /dev/null @@ -1,189 +0,0 @@ -import { AttendanceSalesCoOrdinator } from "./AttandanceSalesCoordinatorModel.js"; - -export const markAttendance = async (req, res) => { - try { - const { date, time, location, notes } = req.body; - const salesCoordinatorId = req.salescoordinator._id; - - // Get the start and end of the day to check if the attendance is already marked - const startOfDay = new Date(date); - startOfDay.setHours(0, 0, 0, 0); - - const endOfDay = new Date(date); - endOfDay.setHours(23, 59, 59, 999); - - // Check if the attendance record exists for today - const existingAttendance = await AttendanceSalesCoOrdinator.findOne({ - salesCoordinator: salesCoordinatorId, - "records.date": { $gte: startOfDay, $lte: endOfDay }, - }); - - if (existingAttendance) { - return res.status(400).json({ - success: false, - message: "Attendance for today is already marked.", - }); - } - - // Check if attendance record exists for the sales coordinator - let attendance = await AttendanceSalesCoOrdinator.findOne({ - salesCoordinator: salesCoordinatorId, - }); - - if (!attendance) { - // Create a new attendance record if it doesn't exist - attendance = new AttendanceSalesCoOrdinator({ - salesCoordinator: salesCoordinatorId, - records: [], - }); - } - - // Add the new attendance record to the array - attendance.records.push({ date, time, location, notes }); - await attendance.save(); - - res.status(201).json({ - success: true, - message: "Attendance marked successfully", - record: { date, time, location, notes }, - }); - } catch (error) { - res.status(500).json({ - success: false, - message: error.message || "Something went wrong", - }); - } -}; - -export const getAttendanceBySalesCoordinator = async (req, res) => { - try { - const salesCoordinatorId = req.salescoordinator._id; - - const attendance = await AttendanceSalesCoOrdinator.findOne({ - salesCoordinator: salesCoordinatorId, - }).populate("salesCoordinator", "name email"); - - if (!attendance) { - return res.status(404).json({ - success: false, - message: "No attendance records found for this sales coordinator.", - }); - } - - res.status(200).json({ - success: true, - attendance, - }); - } catch (error) { - res.status(500).json({ - success: false, - message: error.message || "Something went wrong", - }); - } -}; -// admin -export const AdmingetAttendanceBySalesCoordinator = async (req, res) => { - try { - const { id } = req.params; - const { page = 1, show = 10 } = req.query; - const limit = parseInt(show); - const skip = (page - 1) * limit; - - // Find attendance records for the given sales coordinator - const attendanceDoc = await AttendanceSalesCoOrdinator.findOne({ - salesCoordinator: id, - }).populate("salesCoordinator", "name email"); - - if (!attendanceDoc) { - return res.status(404).json({ - success: false, - message: "No attendance records found for this sales coordinator.", - }); - } - - // Pagination and slicing records - const totalData = attendanceDoc.records.length; - const paginatedRecords = attendanceDoc.records.slice(skip, skip + limit); - - res.status(200).json({ - success: true, - salesCoordinator: attendanceDoc.salesCoordinator, - attendance: paginatedRecords, - total_data: totalData, - }); - } catch (error) { - res.status(500).json({ - success: false, - message: error.message || "Something went wrong", - }); - } -}; - - -export const getTodayAttendance = async (req, res) => { - try { - const today = new Date(); - today.setHours(0, 0, 0, 0); - - const startOfDay = new Date(today); - const endOfDay = new Date(today); - endOfDay.setHours(23, 59, 59, 999); - - const { page = 1, show = 10 } = req.query; - const limit = parseInt(show); - const skip = (page - 1) * limit; - - const attendances = await AttendanceSalesCoOrdinator.aggregate([ - { $unwind: '$records' }, - { - $match: { - 'records.date': { $gte: startOfDay, $lte: endOfDay } - } - }, - { - $lookup: { - from: 'salescoordinators', - localField: 'salesCoordinator', - foreignField: '_id', - as: 'salesCoordinator' - } - }, - { $unwind: '$salesCoordinator' }, - { - $facet: { - totalData: [{ $count: 'count' }], - attendance: [ - { $skip: skip }, - { $limit: limit }, - { - $project: { - salesCoordinator: { - id: '$salesCoordinator._id', - name: '$salesCoordinator.name', - email: '$salesCoordinator.email' - }, - date: '$records.date', - time: '$records.time', - location: '$records.location', - note: '$records.notes' - } - } - ] - } - } - ]); - - const totalData = attendances[0].totalData[0] ? attendances[0].totalData[0].count : 0; - - res.status(200).json({ - success: true, - attendance: attendances[0].attendance, - total_data: totalData - }); - } catch (error) { - res.status(500).json({ - success: false, - message: error.message || 'Something went wrong' - }); - } -}; \ No newline at end of file diff --git a/resources/SalesCoOrdinatorAttandance/AttandanceSalesCoordinatorModel.js b/resources/SalesCoOrdinatorAttandance/AttandanceSalesCoordinatorModel.js deleted file mode 100644 index 0d39782..0000000 --- a/resources/SalesCoOrdinatorAttandance/AttandanceSalesCoordinatorModel.js +++ /dev/null @@ -1,31 +0,0 @@ -import mongoose from 'mongoose'; - -const attendanceRecordSchema = new mongoose.Schema({ - date: { - type: Date, - required: true, - }, - time: { - type: String, - required: true, - }, - location: { - type: String, - required: true, - }, - notes: { - type: String, - }, -}); - -const attendanceSchema = new mongoose.Schema({ - salesCoordinator: { - type: mongoose.Schema.Types.ObjectId, - ref: 'SalesCoOrdinator', - required: true, - unique: true, - }, - records: [attendanceRecordSchema], -}, { timestamps: true, versionKey: false }); - -export const AttendanceSalesCoOrdinator = mongoose.model('Attendance', attendanceSchema); diff --git a/resources/SalesCoOrdinatorAttandance/AttandanceSalesCoordinatorRoute.js b/resources/SalesCoOrdinatorAttandance/AttandanceSalesCoordinatorRoute.js deleted file mode 100644 index 0d96a41..0000000 --- a/resources/SalesCoOrdinatorAttandance/AttandanceSalesCoordinatorRoute.js +++ /dev/null @@ -1,41 +0,0 @@ -import express from "express"; -import { - markAttendance, - getAttendanceBySalesCoordinator, - getTodayAttendance, - AdmingetAttendanceBySalesCoordinator, -} from "./AttandanceSalesCoordinatorController.js"; -import { isAuthenticatedSalesCoOrdinator } from "../../middlewares/SalesCoOrdinatorAuth.js"; -import { authorizeRoles, isAuthenticatedUser } from "../../middlewares/auth.js"; - -const router = express.Router(); - -// Place more specific routes first - -// Route to get today's attendance for admin -router.get( - "/attendance/today", - isAuthenticatedUser, - authorizeRoles("admin"), - getTodayAttendance -); - -// Route to mark attendance -router.post("/attendance", isAuthenticatedSalesCoOrdinator, markAttendance); - -// Route to get attendance for the logged-in sales coordinator -router.get( - "/attendance", - isAuthenticatedSalesCoOrdinator, - getAttendanceBySalesCoordinator -); - -// Admin route to get attendance by sales coordinator ID -router.get( - "/attendance/:id", - isAuthenticatedUser, - authorizeRoles("admin"), - AdmingetAttendanceBySalesCoordinator -); - -export default router; diff --git a/resources/SalesCoOrdinators/SalesCoOrdinatorController.js b/resources/SalesCoOrdinators/SalesCoOrdinatorController.js index 6da7025..a32e162 100644 --- a/resources/SalesCoOrdinators/SalesCoOrdinatorController.js +++ b/resources/SalesCoOrdinators/SalesCoOrdinatorController.js @@ -383,7 +383,7 @@ export const verifyUpdatedMobileOtp = async (req, res) => { //getmyProfile export const getmyProfile = async (req, res) => { try { - const myData = await SalesCoOrdinator.findById(req.salescoordinator?._id); + const myData = await SalesCoOrdinator.findById(req.user?._id); if (myData) { return res.status(200).json({ success: true,