11import { NextResponse } from "next/server"
2+ import { withPlanningAccess , withPlanningEditor } from "@/lib/api/with-auth"
23import { db } from "@/lib/db/config"
34import { schedules } from "@/lib/db/schema/schedules"
45import { eq , and } from "drizzle-orm"
@@ -47,7 +48,7 @@ function getDateFromWeekKeyAndDay(weekKey: string, day: string): string | null {
4748}
4849
4950// GET /api/schedules?weekKey=2024-W1
50- export async function GET ( request : Request ) {
51+ export const GET = withPlanningAccess ( async ( request ) => {
5152 try {
5253 const { searchParams } = new URL ( request . url )
5354 const weekKey = searchParams . get ( "weekKey" )
@@ -66,15 +67,16 @@ export async function GET(request: Request) {
6667 console . error ( "Error fetching schedules:" , error )
6768 return NextResponse . json ( { error : "Internal Server Error" } , { status : 500 } )
6869 }
69- }
70+ } )
7071
7172// POST /api/schedules
72- export async function POST ( request : Request ) {
73+ export const POST = withPlanningEditor ( async ( request , { user } ) => {
7374 try {
7475 const body = await request . json ( )
7576 const { employeeId, weekKey, day, timeSlots } = body
76- const userId = request . headers . get ( 'x-user-id' ) || 'unknown' ;
77- const userEmail = request . headers . get ( 'x-user-email' ) || 'unknown' ;
77+ // Identité authentifiée pour l'audit (les headers x-user-* étaient falsifiables)
78+ const userId = user . id ;
79+ const userEmail = user . email ;
7880
7981 // Récupérer les jours fériés
8082 const holidays = await getFrenchHolidays ( ) ;
@@ -148,17 +150,17 @@ export async function POST(request: Request) {
148150 console . error ( "Error creating/updating schedule:" , error )
149151 return NextResponse . json ( { error : "Internal Server Error" } , { status : 500 } )
150152 }
151- }
153+ } )
152154
153155// DELETE /api/schedules?employeeId=123&weekKey=2024-W1&day=monday
154- export async function DELETE ( request : Request ) {
156+ export const DELETE = withPlanningEditor ( async ( request , { user } ) => {
155157 try {
156158 const { searchParams } = new URL ( request . url )
157159 const employeeId = searchParams . get ( "employeeId" )
158160 const weekKey = searchParams . get ( "weekKey" )
159161 const day = searchParams . get ( "day" )
160- const userId = request . headers . get ( 'x- user-id' ) || 'unknown' ;
161- const userEmail = request . headers . get ( 'x- user- email' ) || 'unknown' ;
162+ const userId = user . id ;
163+ const userEmail = user . email ;
162164
163165 if ( ! employeeId || ! weekKey || ! day ) {
164166 return NextResponse . json ( { error : "Missing required parameters" } , { status : 400 } )
@@ -202,4 +204,4 @@ export async function DELETE(request: Request) {
202204 console . error ( "Error deleting schedule:" , error )
203205 return NextResponse . json ( { error : "Internal Server Error" } , { status : 500 } )
204206 }
205- }
207+ } )
0 commit comments