From 6ad81bbec6ef2be632f6b6db83a260ee02d77768 Mon Sep 17 00:00:00 2001 From: Louisa Best Date: Fri, 8 May 2026 16:06:48 +0930 Subject: [PATCH] fix(shifts): return assigned guard details --- .../src/controllers/shift.controller.js | 37 +++++++++++++------ 1 file changed, 25 insertions(+), 12 deletions(-) diff --git a/app-backend/src/controllers/shift.controller.js b/app-backend/src/controllers/shift.controller.js index 02716c9ef..d8907eab1 100644 --- a/app-backend/src/controllers/shift.controller.js +++ b/app-backend/src/controllers/shift.controller.js @@ -3,7 +3,6 @@ import Shift from '../models/Shift.js'; import Branch from '../models/Branch.js'; import Guard from '../models/Guard.js'; import Availability from '../models/Availability.js'; -import ShiftAttendance from '../models/ShiftAttendance.js'; import { ACTIONS } from "../middleware/logger.js"; @@ -526,9 +525,16 @@ export const listAvailableShifts = async (req, res) => { } const findQ = Shift.find(query) - .sort({ date: role === 'guard' ? 1 : -1, startTime: role === 'guard' ? 1 : -1, createdAt: -1 }) - .skip(skip).limit(limit) - .populate('createdBy', 'name'); + .sort({ + date: role === 'guard' ? 1 : -1, + startTime: role === 'guard' ? 1 : -1, + createdAt: -1, + }) + .skip(skip) + .limit(limit) + .populate('createdBy', 'name email') + .populate('guardIds', 'name email') + .populate('acceptedBy', 'name email'); if (role === 'employer' || role === 'admin') { findQ.populate('applicants', 'name email'); @@ -536,13 +542,20 @@ export const listAvailableShifts = async (req, res) => { const [docs, total] = await Promise.all([findQ.lean(), Shift.countDocuments(query)]); - const items = (role === 'employer' || role === 'admin') - ? docs.map(d => ({ - ...d, - applicantCount: Array.isArray(d.applicants) ? d.applicants.length : 0, - hasApplicants: Array.isArray(d.applicants) && d.applicants.length > 0, - })) - : docs; + const items = docs.map((shift) => { + const assignedGuards = shift.acceptedBy + ? [shift.acceptedBy] + : Array.isArray(shift.guardIds) + ? shift.guardIds + : []; + + return { + ...shift, + assignedGuards, + applicantCount: Array.isArray(shift.applicants) ? shift.applicants.length : 0, + hasApplicants: Array.isArray(shift.applicants) && shift.applicants.length > 0, + }; + }); res.json({ page, limit, total, items }); } catch (e) { @@ -566,7 +579,7 @@ export const applyForShift = async (req, res) => { const shift = await Shift.findById(id); if (!shift) return res.status(404).json({ message: 'Shift not found' }); if (shift.status !== 'open') { - return res.status(400).json({ message: 'Can only apply to open shifts' }); + return res.status(400).json({ message: 'Can only apply to open shifts' }); } if (['assigned', 'completed'].includes(shift.status)) {