50 lines
1.6 KiB
TypeScript
50 lines
1.6 KiB
TypeScript
import { NextRequest, NextResponse } from "next/server";
|
|
import { getServerSession } from "next-auth/next";
|
|
import { authOptions } from "@/app/api/auth/options";
|
|
import { runCalendarSyncJob } from "@/lib/services/calendar-sync-job";
|
|
import { logger } from "@/lib/logger";
|
|
|
|
/**
|
|
* Trigger calendar sync job manually (admin only or cron)
|
|
* POST /api/calendars/sync/job
|
|
*/
|
|
export async function POST(req: NextRequest) {
|
|
try {
|
|
const session = await getServerSession(authOptions);
|
|
|
|
// Check for API key in header (for cron jobs)
|
|
const apiKey = req.headers.get('x-api-key');
|
|
const isCronRequest = apiKey === process.env.CALENDAR_SYNC_API_KEY;
|
|
|
|
if (!session?.user?.id && !isCronRequest) {
|
|
return NextResponse.json({ error: "Non authentifié" }, { status: 401 });
|
|
}
|
|
|
|
// If authenticated, check if user is admin
|
|
if (session?.user?.id && !session.user.role?.includes('ROLE_Admin') && !isCronRequest) {
|
|
return NextResponse.json({ error: "Non autorisé" }, { status: 403 });
|
|
}
|
|
|
|
logger.info('Manual calendar sync job triggered', {
|
|
userId: session?.user?.id,
|
|
isCronRequest,
|
|
});
|
|
|
|
// Run sync job
|
|
await runCalendarSyncJob();
|
|
|
|
return NextResponse.json({ success: true, message: "Synchronisation terminée" });
|
|
} catch (error) {
|
|
logger.error('Error running calendar sync job', {
|
|
error: error instanceof Error ? error.message : String(error),
|
|
});
|
|
return NextResponse.json(
|
|
{
|
|
error: "Erreur lors de la synchronisation",
|
|
details: error instanceof Error ? error.message : String(error),
|
|
},
|
|
{ status: 500 }
|
|
);
|
|
}
|
|
}
|