diff --git a/src/services/attendance.service.ts b/src/services/attendance.service.ts index bd6c59a..fc81198 100644 --- a/src/services/attendance.service.ts +++ b/src/services/attendance.service.ts @@ -167,12 +167,15 @@ export async function getStatus(userId: number) { }); // 2) Today's completed shifts - const todayShifts = await prisma.attendance.findMany({ + const todayShiftsRaw = await prisma.attendance.findMany({ where: { user_id: userId, shift_date: { gte: todayStart, lte: todayEnd }, departure_time: { not: null }, }, + include: { + attendance_project_logs: { orderBy: { started_at: "asc" } }, + }, orderBy: { arrival_time: "asc" }, }); @@ -251,6 +254,39 @@ export async function getStatus(userId: number) { }; // 5) Project logs for ongoing shift + // Collect all project IDs from completed shifts for name lookup + const completedProjectIds = new Set(); + for (const shift of todayShiftsRaw) { + for (const log of shift.attendance_project_logs) { + completedProjectIds.add(log.project_id); + } + } + + // Fetch project names for completed shifts + const completedProjectNames = new Map(); + if (completedProjectIds.size > 0) { + const projects = await prisma.projects.findMany({ + where: { id: { in: [...completedProjectIds] } }, + select: { id: true, name: true, project_number: true }, + }); + for (const p of projects) { + completedProjectNames.set( + p.id, + p.project_number ? `${p.project_number} – ${p.name}` : p.name || "", + ); + } + } + + // Enrich today's completed shifts with project names + const todayShifts = todayShiftsRaw.map((shift) => ({ + ...shift, + project_logs: shift.attendance_project_logs.map((l) => ({ + ...l, + project_name: + completedProjectNames.get(l.project_id) || `Projekt #${l.project_id}`, + })), + })); + let projectLogs: Array<{ id: number; attendance_id: number;