fix: attendance project tracking — enrich completed shift logs with project names

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) <noreply@anthropic.com>
This commit is contained in:
BOHA
2026-03-25 08:15:20 +01:00
parent 87dbde5c59
commit 0317ba3168

View File

@@ -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<number>();
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<number, string>();
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;