phosphat-report-app/app/routes/api.worker-shifts.ts

52 lines
1.5 KiB
TypeScript

import type { LoaderFunctionArgs } from "@remix-run/node";
import { json } from "@remix-run/node";
import { requireAuthLevel } from "~/utils/auth.server";
import { prisma } from "~/utils/db.server";
export const loader = async ({ request }: LoaderFunctionArgs) => {
await requireAuthLevel(request, 2); // Only supervisors and admins
const url = new URL(request.url);
const workerId = url.searchParams.get('workerId');
const dateFrom = url.searchParams.get('dateFrom');
const dateTo = url.searchParams.get('dateTo');
if (!workerId) {
return json({ error: "Worker ID is required" }, { status: 400 });
}
// Build where clause for reports
const whereClause: any = {
shiftWorkers: {
some: {
workerId: parseInt(workerId)
}
}
};
// Add date filters if provided
if (dateFrom || dateTo) {
whereClause.createdDate = {};
if (dateFrom) {
whereClause.createdDate.gte = new Date(dateFrom + 'T00:00:00.000Z');
}
if (dateTo) {
whereClause.createdDate.lte = new Date(dateTo + 'T23:59:59.999Z');
}
}
// Fetch shifts where this worker was assigned
const shifts = await prisma.report.findMany({
where: whereClause,
orderBy: { createdDate: 'desc' },
include: {
employee: { select: { name: true } },
area: { select: { name: true } },
dredgerLocation: { select: { name: true, class: true } },
reclamationLocation: { select: { name: true } }
}
});
return json({ shifts });
};