W n8n route
This commit is contained in:
parent
2be19ea579
commit
3f56b98ae5
@ -20,24 +20,21 @@ interface MissionCreateInput {
|
||||
guardians?: Record<string, string>;
|
||||
volunteers?: string[];
|
||||
creatorId?: string;
|
||||
config?: {
|
||||
N8N_API_KEY: string;
|
||||
MISSION_API_URL: string;
|
||||
};
|
||||
logo?: {
|
||||
data: string;
|
||||
name?: string;
|
||||
type?: string;
|
||||
} | null;
|
||||
attachments?: Array<{
|
||||
data: string;
|
||||
name?: string;
|
||||
type?: string;
|
||||
}>;
|
||||
leantimeProjectId?: string | null;
|
||||
outlineCollectionId?: string | null;
|
||||
rocketChatChannelId?: string | null;
|
||||
gitRepositoryId?: string | null;
|
||||
giteaRepositoryUrl?: string | null;
|
||||
penpotProjectId?: string | null;
|
||||
status?: string;
|
||||
createdAt?: Date;
|
||||
updatedAt?: Date;
|
||||
}
|
||||
|
||||
interface MissionUserInput {
|
||||
@ -47,6 +44,7 @@ interface MissionUserInput {
|
||||
}
|
||||
|
||||
interface MissionResponse {
|
||||
id: string;
|
||||
name: string;
|
||||
oddScope: string[];
|
||||
niveau: string;
|
||||
@ -64,20 +62,17 @@ interface MissionResponse {
|
||||
rocketChatChannelId: string | null;
|
||||
giteaRepositoryUrl: string | null;
|
||||
penpotProjectId: string | null;
|
||||
createdAt: Date;
|
||||
updatedAt: Date;
|
||||
}
|
||||
|
||||
// Helper function to check authentication
|
||||
async function checkAuth(request: Request) {
|
||||
const apiKey = request.headers.get('x-api-key');
|
||||
if (apiKey === process.env.N8N_API_KEY) {
|
||||
return { authorized: true, userId: 'system' };
|
||||
}
|
||||
|
||||
const session = await getServerSession(authOptions);
|
||||
if (!session?.user?.id) {
|
||||
return { authorized: false, userId: null };
|
||||
}
|
||||
return { authorized: true, userId: session.user.id };
|
||||
return {
|
||||
authorized: !!session?.user,
|
||||
userId: session?.user?.id
|
||||
};
|
||||
}
|
||||
|
||||
// GET endpoint to list missions
|
||||
@ -159,11 +154,10 @@ export async function POST(request: Request) {
|
||||
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
|
||||
}
|
||||
|
||||
const requestId = request.headers.get('x-request-id');
|
||||
const body = await request.json() as MissionCreateInput;
|
||||
|
||||
// Validate required fields
|
||||
const requiredFields = ['name', 'niveau', 'intention', 'missionType', 'donneurDOrdre', 'projection'];
|
||||
const requiredFields = ['name', 'oddScope'];
|
||||
const missingFields = requiredFields.filter(field => !body[field as keyof MissionCreateInput]);
|
||||
|
||||
if (missingFields.length > 0) {
|
||||
@ -179,7 +173,7 @@ export async function POST(request: Request) {
|
||||
// Get the creator ID from the appropriate source
|
||||
const creatorId = isN8nRequest ? body.creatorId : userId;
|
||||
|
||||
// Verify creator exists for both n8n and non-n8n requests
|
||||
// Verify creator exists
|
||||
const creator = await prisma.user.findUnique({
|
||||
where: { id: creatorId }
|
||||
});
|
||||
@ -217,24 +211,6 @@ export async function POST(request: Request) {
|
||||
});
|
||||
}
|
||||
|
||||
// Check for mission creation in progress
|
||||
const missionInProgress = await prisma.mission.findFirst({
|
||||
where: {
|
||||
name: body.name,
|
||||
createdAt: {
|
||||
gte: new Date(Date.now() - 5 * 60 * 1000) // Within last 5 minutes
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
if (missionInProgress) {
|
||||
return NextResponse.json({
|
||||
error: 'Mission creation already in progress',
|
||||
details: 'Please wait a few minutes and try again',
|
||||
code: 'MISSION_CREATION_IN_PROGRESS'
|
||||
}, { status: 409 });
|
||||
}
|
||||
|
||||
// Handle n8n workflow
|
||||
if (!isN8nRequest) {
|
||||
const n8nService = new N8nService();
|
||||
@ -259,6 +235,7 @@ export async function POST(request: Request) {
|
||||
name: body.logo.name || 'logo.png',
|
||||
type: body.logo.type || 'image/png'
|
||||
} : null,
|
||||
attachments: body.attachments || [],
|
||||
config: {
|
||||
N8N_API_KEY: process.env.N8N_API_KEY,
|
||||
MISSION_API_URL: process.env.NEXT_PUBLIC_API_URL || 'https://api.slm-lab.net/api'
|
||||
@ -266,25 +243,6 @@ export async function POST(request: Request) {
|
||||
};
|
||||
|
||||
try {
|
||||
// Log the data we're sending (without sensitive info)
|
||||
console.log('Sending data to n8n workflow:', {
|
||||
name: n8nData.name,
|
||||
creatorId: n8nData.creatorId,
|
||||
oddScope: n8nData.oddScope,
|
||||
niveau: n8nData.niveau,
|
||||
intention: n8nData.intention?.substring(0, 100) + '...',
|
||||
missionType: n8nData.missionType,
|
||||
donneurDOrdre: n8nData.donneurDOrdre,
|
||||
projection: n8nData.projection,
|
||||
services: n8nData.services,
|
||||
participation: n8nData.participation,
|
||||
profils: n8nData.profils,
|
||||
hasGuardians: Object.keys(n8nData.guardians).length > 0,
|
||||
volunteersCount: n8nData.volunteers.length,
|
||||
hasConfig: true,
|
||||
configKeys: Object.keys(n8nData.config)
|
||||
});
|
||||
|
||||
const workflowResult = await n8nService.triggerMissionCreation(n8nData);
|
||||
|
||||
if (!workflowResult.success) {
|
||||
@ -296,12 +254,10 @@ export async function POST(request: Request) {
|
||||
}, { status: 500 });
|
||||
}
|
||||
|
||||
// Process the workflow result
|
||||
const results = workflowResult.results || {};
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
status: 'success',
|
||||
message: 'Mission created successfully',
|
||||
message: 'Mission creation initiated',
|
||||
mission: {
|
||||
name: n8nData.name,
|
||||
oddScope: n8nData.oddScope,
|
||||
@ -313,65 +269,15 @@ export async function POST(request: Request) {
|
||||
services: n8nData.services,
|
||||
profils: n8nData.profils,
|
||||
participation: n8nData.participation,
|
||||
creatorId: n8nData.creatorId,
|
||||
logo: n8nData.logo,
|
||||
leantimeProjectId: results.leantimeProjectId,
|
||||
outlineCollectionId: results.outlineCollectionId,
|
||||
rocketChatChannelId: results.rocketChatChannelId,
|
||||
giteaRepositoryUrl: results.giteaRepositoryUrl,
|
||||
penpotProjectId: results.penpotProjectId
|
||||
},
|
||||
integrationStatus: {
|
||||
gitRepo: !!results.giteaRepositoryUrl,
|
||||
leantimeProject: !!results.leantimeProjectId,
|
||||
docCollection: !!results.outlineCollectionId,
|
||||
rocketChatChannel: !!results.rocketChatChannelId
|
||||
},
|
||||
resourceStatus: {
|
||||
gitRepo: !!results.giteaRepositoryUrl,
|
||||
leantimeProject: !!results.leantimeProjectId,
|
||||
docCollection: !!results.outlineCollectionId,
|
||||
rocketChatChannel: !!results.rocketChatChannelId
|
||||
creatorId: n8nData.creatorId
|
||||
}
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('Error triggering n8n workflow:', error);
|
||||
|
||||
// Check if it's an n8n workflow error
|
||||
if (error instanceof Error && error.message.includes('HTTP error! status: 500')) {
|
||||
// Try to parse the error message for more details
|
||||
const errorMatch = error.message.match(/body: ({.*})/);
|
||||
let errorDetails = 'The mission creation process encountered an error';
|
||||
|
||||
if (errorMatch) {
|
||||
try {
|
||||
const errorBody = JSON.parse(errorMatch[1]);
|
||||
errorDetails = errorBody.message || errorDetails;
|
||||
} catch (e) {
|
||||
console.error('Failed to parse error body:', e);
|
||||
}
|
||||
}
|
||||
|
||||
return NextResponse.json(
|
||||
{
|
||||
error: 'Mission creation workflow failed',
|
||||
details: errorDetails,
|
||||
code: 'WORKFLOW_ERROR',
|
||||
originalError: error.message
|
||||
},
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
|
||||
// Handle other types of errors
|
||||
return NextResponse.json(
|
||||
{
|
||||
error: 'Failed to create mission resources',
|
||||
details: error instanceof Error ? error.message : 'The mission creation process failed. Please try again later.',
|
||||
code: 'WORKFLOW_ERROR'
|
||||
},
|
||||
{ status: 500 }
|
||||
);
|
||||
return NextResponse.json({
|
||||
error: 'Failed to initiate mission creation',
|
||||
details: error instanceof Error ? error.message : String(error)
|
||||
}, { status: 500 });
|
||||
}
|
||||
}
|
||||
|
||||
@ -436,47 +342,32 @@ export async function POST(request: Request) {
|
||||
}
|
||||
}
|
||||
|
||||
// Format response to match workflow output
|
||||
const missionResponse: MissionResponse = {
|
||||
name: mission.name,
|
||||
oddScope: mission.oddScope,
|
||||
niveau: mission.niveau,
|
||||
intention: mission.intention,
|
||||
missionType: mission.missionType,
|
||||
donneurDOrdre: mission.donneurDOrdre,
|
||||
projection: mission.projection,
|
||||
services: mission.services,
|
||||
profils: mission.profils,
|
||||
participation: mission.participation || 'default',
|
||||
creatorId: mission.creatorId,
|
||||
logo: mission.logo,
|
||||
leantimeProjectId: (mission as any).leantimeProjectId,
|
||||
outlineCollectionId: (mission as any).outlineCollectionId,
|
||||
rocketChatChannelId: (mission as any).rocketChatChannelId,
|
||||
giteaRepositoryUrl: (mission as any).giteaRepositoryUrl,
|
||||
penpotProjectId: (mission as any).penpotProjectId
|
||||
};
|
||||
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
status: 'success',
|
||||
message: 'Mission created successfully',
|
||||
mission: missionResponse,
|
||||
integrationStatus: {
|
||||
gitRepo: !!missionResponse.giteaRepositoryUrl,
|
||||
leantimeProject: !!missionResponse.leantimeProjectId,
|
||||
docCollection: !!missionResponse.outlineCollectionId,
|
||||
rocketChatChannel: !!missionResponse.rocketChatChannelId
|
||||
},
|
||||
resourceStatus: {
|
||||
gitRepo: false,
|
||||
leantimeProject: true,
|
||||
docCollection: true,
|
||||
rocketChatChannel: true
|
||||
},
|
||||
errors: [
|
||||
'Git repository creation failed: Unknown error'
|
||||
]
|
||||
mission: {
|
||||
id: (mission as any).id,
|
||||
name: (mission as any).name,
|
||||
oddScope: (mission as any).oddScope,
|
||||
niveau: (mission as any).niveau,
|
||||
intention: (mission as any).intention,
|
||||
missionType: (mission as any).missionType,
|
||||
donneurDOrdre: (mission as any).donneurDOrdre,
|
||||
projection: (mission as any).projection,
|
||||
services: (mission as any).services,
|
||||
profils: (mission as any).profils,
|
||||
participation: (mission as any).participation,
|
||||
creatorId: (mission as any).creatorId,
|
||||
logo: (mission as any).logo,
|
||||
leantimeProjectId: (mission as any).leantimeProjectId,
|
||||
outlineCollectionId: (mission as any).outlineCollectionId,
|
||||
rocketChatChannelId: (mission as any).rocketChatChannelId,
|
||||
giteaRepositoryUrl: (mission as any).giteaRepositoryUrl,
|
||||
penpotProjectId: (mission as any).penpotProjectId,
|
||||
createdAt: (mission as any).createdAt,
|
||||
updatedAt: (mission as any).updatedAt
|
||||
} as MissionResponse
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('Error creating mission:', error);
|
||||
|
||||
Loading…
Reference in New Issue
Block a user