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:
@@ -167,12 +167,15 @@ export async function getStatus(userId: number) {
|
|||||||
});
|
});
|
||||||
|
|
||||||
// 2) Today's completed shifts
|
// 2) Today's completed shifts
|
||||||
const todayShifts = await prisma.attendance.findMany({
|
const todayShiftsRaw = await prisma.attendance.findMany({
|
||||||
where: {
|
where: {
|
||||||
user_id: userId,
|
user_id: userId,
|
||||||
shift_date: { gte: todayStart, lte: todayEnd },
|
shift_date: { gte: todayStart, lte: todayEnd },
|
||||||
departure_time: { not: null },
|
departure_time: { not: null },
|
||||||
},
|
},
|
||||||
|
include: {
|
||||||
|
attendance_project_logs: { orderBy: { started_at: "asc" } },
|
||||||
|
},
|
||||||
orderBy: { arrival_time: "asc" },
|
orderBy: { arrival_time: "asc" },
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -251,6 +254,39 @@ export async function getStatus(userId: number) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
// 5) Project logs for ongoing shift
|
// 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<{
|
let projectLogs: Array<{
|
||||||
id: number;
|
id: number;
|
||||||
attendance_id: number;
|
attendance_id: number;
|
||||||
|
|||||||
Reference in New Issue
Block a user