188 lines
5.7 KiB
TypeScript
188 lines
5.7 KiB
TypeScript
import { NextResponse } from 'next/server';
|
|
import { prisma } from '@/lib/prisma';
|
|
|
|
/**
|
|
* POST /api/missions/mission-created
|
|
*
|
|
* Endpoint appelé par N8N après la création des intégrations externes.
|
|
* Reçoit les IDs des intégrations créées et met à jour la mission en base.
|
|
*
|
|
* Headers attendus:
|
|
* - Authorization: Bearer {keycloak_token} (optionnel, vérifié via x-api-key)
|
|
* - x-api-key: {N8N_API_KEY}
|
|
*
|
|
* Body attendu (format N8N):
|
|
* {
|
|
* name: string,
|
|
* creatorId: string,
|
|
* gitRepoUrl?: string,
|
|
* leantimeProjectId?: string,
|
|
* documentationCollectionId?: string,
|
|
* rocketchatChannelId?: string,
|
|
* // ... autres champs optionnels
|
|
* }
|
|
*/
|
|
export async function POST(request: Request) {
|
|
try {
|
|
console.log('=== Mission Created Webhook Received ===');
|
|
|
|
// Vérifier l'API key
|
|
const apiKey = request.headers.get('x-api-key');
|
|
const expectedApiKey = process.env.N8N_API_KEY;
|
|
|
|
if (!expectedApiKey) {
|
|
console.error('N8N_API_KEY not configured in environment');
|
|
return NextResponse.json(
|
|
{ error: 'Server configuration error' },
|
|
{ status: 500 }
|
|
);
|
|
}
|
|
|
|
if (apiKey !== expectedApiKey) {
|
|
console.error('Invalid API key:', {
|
|
received: apiKey ? 'present' : 'missing',
|
|
expected: expectedApiKey ? 'configured' : 'missing'
|
|
});
|
|
return NextResponse.json(
|
|
{ error: 'Unauthorized' },
|
|
{ status: 401 }
|
|
);
|
|
}
|
|
|
|
const body = await request.json();
|
|
console.log('Received mission-created data:', JSON.stringify(body, null, 2));
|
|
|
|
// Validation des champs requis
|
|
if (!body.name || !body.creatorId) {
|
|
console.error('Missing required fields:', {
|
|
hasName: !!body.name,
|
|
hasCreatorId: !!body.creatorId
|
|
});
|
|
return NextResponse.json(
|
|
{ error: 'Missing required fields: name and creatorId' },
|
|
{ status: 400 }
|
|
);
|
|
}
|
|
|
|
// Trouver la mission par name + creatorId
|
|
// On cherche la mission la plus récente avec ce nom et ce créateur
|
|
const mission = await prisma.mission.findFirst({
|
|
where: {
|
|
name: body.name,
|
|
creatorId: body.creatorId
|
|
},
|
|
orderBy: {
|
|
createdAt: 'desc' // Prendre la plus récente
|
|
}
|
|
});
|
|
|
|
if (!mission) {
|
|
console.error('Mission not found:', {
|
|
name: body.name,
|
|
creatorId: body.creatorId
|
|
});
|
|
return NextResponse.json(
|
|
{ error: 'Mission not found' },
|
|
{ status: 404 }
|
|
);
|
|
}
|
|
|
|
console.log('Found mission:', {
|
|
id: mission.id,
|
|
name: mission.name,
|
|
currentIntegrationIds: {
|
|
gitea: mission.giteaRepositoryUrl,
|
|
leantime: mission.leantimeProjectId,
|
|
outline: mission.outlineCollectionId,
|
|
rocketChat: mission.rocketChatChannelId
|
|
}
|
|
});
|
|
|
|
// Préparer les données de mise à jour
|
|
const updateData: {
|
|
giteaRepositoryUrl?: string | null;
|
|
leantimeProjectId?: string | null;
|
|
outlineCollectionId?: string | null;
|
|
rocketChatChannelId?: string | null;
|
|
} = {};
|
|
|
|
// Mapper les champs N8N vers notre schéma Prisma
|
|
if (body.gitRepoUrl !== undefined) {
|
|
updateData.giteaRepositoryUrl = body.gitRepoUrl || null;
|
|
console.log('Updating giteaRepositoryUrl:', body.gitRepoUrl);
|
|
}
|
|
|
|
if (body.leantimeProjectId !== undefined) {
|
|
// N8N peut retourner un number, on le convertit en string
|
|
updateData.leantimeProjectId = body.leantimeProjectId
|
|
? String(body.leantimeProjectId)
|
|
: null;
|
|
console.log('Updating leantimeProjectId:', updateData.leantimeProjectId);
|
|
}
|
|
|
|
if (body.documentationCollectionId !== undefined) {
|
|
updateData.outlineCollectionId = body.documentationCollectionId || null;
|
|
console.log('Updating outlineCollectionId:', updateData.outlineCollectionId);
|
|
}
|
|
|
|
if (body.rocketchatChannelId !== undefined) {
|
|
updateData.rocketChatChannelId = body.rocketchatChannelId || null;
|
|
console.log('Updating rocketChatChannelId:', updateData.rocketChatChannelId);
|
|
}
|
|
|
|
// Vérifier qu'il y a au moins un champ à mettre à jour
|
|
if (Object.keys(updateData).length === 0) {
|
|
console.warn('No integration IDs to update');
|
|
return NextResponse.json({
|
|
message: 'Mission found but no integration IDs provided',
|
|
mission: {
|
|
id: mission.id,
|
|
name: mission.name
|
|
}
|
|
});
|
|
}
|
|
|
|
// Mettre à jour la mission
|
|
const updatedMission = await prisma.mission.update({
|
|
where: { id: mission.id },
|
|
data: updateData
|
|
});
|
|
|
|
console.log('Mission updated successfully:', {
|
|
id: updatedMission.id,
|
|
name: updatedMission.name,
|
|
updatedFields: Object.keys(updateData),
|
|
newIntegrationIds: {
|
|
gitea: updatedMission.giteaRepositoryUrl,
|
|
leantime: updatedMission.leantimeProjectId,
|
|
outline: updatedMission.outlineCollectionId,
|
|
rocketChat: updatedMission.rocketChatChannelId
|
|
}
|
|
});
|
|
|
|
return NextResponse.json({
|
|
success: true,
|
|
message: 'Mission updated successfully',
|
|
mission: {
|
|
id: updatedMission.id,
|
|
name: updatedMission.name,
|
|
giteaRepositoryUrl: updatedMission.giteaRepositoryUrl,
|
|
leantimeProjectId: updatedMission.leantimeProjectId,
|
|
outlineCollectionId: updatedMission.outlineCollectionId,
|
|
rocketChatChannelId: updatedMission.rocketChatChannelId
|
|
}
|
|
});
|
|
|
|
} catch (error) {
|
|
console.error('Error in mission-created webhook:', error);
|
|
return NextResponse.json(
|
|
{
|
|
error: 'Failed to update mission',
|
|
details: error instanceof Error ? error.message : String(error)
|
|
},
|
|
{ status: 500 }
|
|
);
|
|
}
|
|
}
|
|
|