This commit is contained in:
alma 2025-05-11 22:08:22 +02:00
parent 984d4488c0
commit 7465efad5a
2 changed files with 33 additions and 43 deletions

View File

@ -8,14 +8,18 @@ import { IntegrationService } from '@/lib/services/integration-service';
import { N8nService } from '@/lib/services/n8n-service';
// Helper function to check authentication
async function checkAuth(request: Request) {
async function checkAuth(request: Request, body?: any) {
// Check for service account API key first
const apiKey = request.headers.get('x-api-key');
console.log('API key from header:', apiKey);
console.log('API key from env:', process.env.N8N_API_KEY);
console.log('Keys match:', apiKey === process.env.N8N_API_KEY);
if (apiKey === process.env.N8N_API_KEY) {
// For service account, use a default system user ID
// For service account, use the creatorId from the request body
if (body?.creatorId) {
return { authorized: true, userId: body.creatorId };
}
// Fallback to system user if no creatorId provided
return { authorized: true, userId: process.env.SYSTEM_USER_ID || 'system' };
}
@ -124,31 +128,17 @@ export async function GET(request: Request) {
// POST endpoint to create a new mission
export async function POST(request: Request) {
try {
const { authorized, userId } = await checkAuth(request);
// Parse the request body first
const body = await request.json();
// Pass the body to checkAuth
const { authorized, userId } = await checkAuth(request, body);
if (!authorized || !userId) {
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
}
// Parse the request body
const body = await request.json();
const {
name,
logo,
oddScope,
niveau,
intention,
missionType,
donneurDOrdre,
projection,
services,
participation,
profils,
guardians,
volunteers
} = body;
// Validate required fields
if (!name || !niveau || !intention || !missionType || !donneurDOrdre || !projection) {
if (!body.name || !body.niveau || !body.intention || !body.missionType || !body.donneurDOrdre || !body.projection) {
return NextResponse.json({
error: 'Missing required fields',
required: {
@ -160,12 +150,12 @@ export async function POST(request: Request) {
projection: true
},
received: {
name: !!name,
niveau: !!niveau,
intention: !!intention,
missionType: !!missionType,
donneurDOrdre: !!donneurDOrdre,
projection: !!projection
name: !!body.name,
niveau: !!body.niveau,
intention: !!body.intention,
missionType: !!body.missionType,
donneurDOrdre: !!body.donneurDOrdre,
projection: !!body.projection
}
}, { status: 400 });
}
@ -173,25 +163,25 @@ export async function POST(request: Request) {
// Create the mission in the database first
const mission = await prisma.mission.create({
data: {
name,
logo,
oddScope: oddScope || [],
niveau,
intention,
missionType,
donneurDOrdre,
projection,
services: services || [],
participation,
profils: profils || [],
name: body.name,
logo: body.logo,
oddScope: body.oddScope || [],
niveau: body.niveau,
intention: body.intention,
missionType: body.missionType,
donneurDOrdre: body.donneurDOrdre,
projection: body.projection,
services: body.services || [],
participation: body.participation,
profils: body.profils || [],
creatorId: userId
}
});
// Add guardians if provided
if (guardians) {
if (body.guardians) {
const guardianRoles = ['gardien-temps', 'gardien-parole', 'gardien-memoire'];
const guardianEntries = Object.entries(guardians)
const guardianEntries = Object.entries(body.guardians)
.filter(([role, userId]) => guardianRoles.includes(role) && userId)
.map(([role, userId]) => ({
role,
@ -207,8 +197,8 @@ export async function POST(request: Request) {
}
// Add volunteers if provided
if (volunteers && volunteers.length > 0) {
const volunteerEntries = volunteers.map((userId: string) => ({
if (body.volunteers && body.volunteers.length > 0) {
const volunteerEntries = body.volunteers.map((userId: string) => ({
role: 'volontaire',
userId,
missionId: mission.id