From 0317ba3168e7288438d1d58211cd0f6a6a9a44af Mon Sep 17 00:00:00 2001 From: BOHA Date: Wed, 25 Mar 2026 08:15:20 +0100 Subject: [PATCH] =?UTF-8?q?fix:=20attendance=20project=20tracking=20?= =?UTF-8?q?=E2=80=94=20enrich=20completed=20shift=20logs=20with=20project?= =?UTF-8?q?=20names?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Bug #1: completed shifts in today_shifts had no project names, showing "undefined" in the UI. Now includes attendance_project_logs relation and enriches with project names from projects table. Co-Authored-By: Claude Opus 4.6 (1M context) --- src/services/attendance.service.ts | 38 +++++++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) 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;