mirror of
https://github.com/BreizhHardware/Site-comptage-heure.git
synced 2026-03-18 21:30:40 +01:00
feat: Ajouter la fonctionnalité de sélection d'utilisateurs pour l'ajout d'heures
This commit is contained in:
@@ -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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user