From 1ef7089986b5b1e04250ac3f2266b882d078438f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?F=C3=A9lix=20MARQUET?= Date: Thu, 20 Nov 2025 18:14:02 +0100 Subject: [PATCH] =?UTF-8?q?feat:=20Ajouter=20la=20fonctionnalit=C3=A9=20de?= =?UTF-8?q?=20s=C3=A9lection=20d'utilisateurs=20pour=20l'ajout=20d'heures?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/admin/page.tsx | 35 ++++++++++++++++++++++++++++++++ app/api/hours/route.ts | 46 +++++++++++++++++++++++++++++++----------- 2 files changed, 69 insertions(+), 12 deletions(-) diff --git a/app/admin/page.tsx b/app/admin/page.tsx index 2c1aeaa..0f6f873 100644 --- a/app/admin/page.tsx +++ b/app/admin/page.tsx @@ -94,6 +94,7 @@ export default function AdminPage() { const [confirmResetPassword, setConfirmResetPassword] = useState(false); const [users, setUsers] = useState([]); const [confirmPasswordChange, setConfirmPasswordChange] = useState(false); + const [selectedUserIds, setSelectedUserIds] = useState([]); const { refetchSettings } = useSettings(); useEffect(() => { @@ -223,6 +224,7 @@ export default function AdminPage() { date: dateString, duration: totalMinutes, reason, + userIds: selectedUserIds.length > 0 ? selectedUserIds : undefined, }), }); if (res.ok) { @@ -230,6 +232,7 @@ export default function AdminPage() { setHoursInput(''); setMinutesInput(''); setReason(''); + setSelectedUserIds([]); fetchHours(); toast.success('Heure ajoutée avec succès'); } else { @@ -385,6 +388,38 @@ export default function AdminPage() {
+
+ +
+ {users.map((user) => ( +
+ { + if (e.target.checked) { + setSelectedUserIds([...selectedUserIds, user.id]); + } else { + setSelectedUserIds( + selectedUserIds.filter((id) => id !== user.id), + ); + } + }} + className="h-4 w-4 rounded border-gray-300 text-primary focus:ring-primary" + /> + +
+ ))} +
+
diff --git a/app/api/hours/route.ts b/app/api/hours/route.ts index 4f53e4e..727770e 100644 --- a/app/api/hours/route.ts +++ b/app/api/hours/route.ts @@ -65,7 +65,7 @@ export async function POST(request: NextRequest) { return NextResponse.json({ error: 'Accès refusé' }, { status: 403 }); } - const { date, duration, reason } = await request.json(); + const { date, duration, reason, userIds } = await request.json(); if (!date || !duration || !reason) { return NextResponse.json( @@ -74,17 +74,39 @@ export async function POST(request: NextRequest) { ); } - const userId = session.user.id; + let targetUserIds = [session.user.id]; + let status = 'PENDING'; + let validatedById = undefined; - const hour = await prisma.hour.create({ - data: { - id: uuidv4(), - date: new Date(date), - duration, - reason, - userId, - }, - }); + if (userIds && Array.isArray(userIds) && userIds.length > 0) { + if (session.user.role === 'ADMIN' || session.user.role === 'SUPER_ADMIN') { + targetUserIds = userIds; + status = 'VALIDATED'; + validatedById = session.user.id; + } else { + return NextResponse.json( + { error: "Non autorisé à ajouter des heures pour d'autres utilisateurs" }, + { status: 403 }, + ); + } + } - return NextResponse.json(hour); + const createdHours = []; + + for (const uid of targetUserIds) { + const hour = await prisma.hour.create({ + data: { + id: uuidv4(), + date: new Date(date), + duration, + reason, + userId: uid, + status: status as any, + validatedById, + }, + }); + createdHours.push(hour); + } + + return NextResponse.json(createdHours); }