n8n
This commit is contained in:
parent
984d4488c0
commit
7465efad5a
@ -8,14 +8,18 @@ import { IntegrationService } from '@/lib/services/integration-service';
|
|||||||
import { N8nService } from '@/lib/services/n8n-service';
|
import { N8nService } from '@/lib/services/n8n-service';
|
||||||
|
|
||||||
// Helper function to check authentication
|
// Helper function to check authentication
|
||||||
async function checkAuth(request: Request) {
|
async function checkAuth(request: Request, body?: any) {
|
||||||
// Check for service account API key first
|
// Check for service account API key first
|
||||||
const apiKey = request.headers.get('x-api-key');
|
const apiKey = request.headers.get('x-api-key');
|
||||||
console.log('API key from header:', apiKey);
|
console.log('API key from header:', apiKey);
|
||||||
console.log('API key from env:', process.env.N8N_API_KEY);
|
console.log('API key from env:', process.env.N8N_API_KEY);
|
||||||
console.log('Keys match:', apiKey === process.env.N8N_API_KEY);
|
console.log('Keys match:', apiKey === process.env.N8N_API_KEY);
|
||||||
if (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' };
|
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
|
// POST endpoint to create a new mission
|
||||||
export async function POST(request: Request) {
|
export async function POST(request: Request) {
|
||||||
try {
|
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) {
|
if (!authorized || !userId) {
|
||||||
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
|
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
|
// 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({
|
return NextResponse.json({
|
||||||
error: 'Missing required fields',
|
error: 'Missing required fields',
|
||||||
required: {
|
required: {
|
||||||
@ -160,12 +150,12 @@ export async function POST(request: Request) {
|
|||||||
projection: true
|
projection: true
|
||||||
},
|
},
|
||||||
received: {
|
received: {
|
||||||
name: !!name,
|
name: !!body.name,
|
||||||
niveau: !!niveau,
|
niveau: !!body.niveau,
|
||||||
intention: !!intention,
|
intention: !!body.intention,
|
||||||
missionType: !!missionType,
|
missionType: !!body.missionType,
|
||||||
donneurDOrdre: !!donneurDOrdre,
|
donneurDOrdre: !!body.donneurDOrdre,
|
||||||
projection: !!projection
|
projection: !!body.projection
|
||||||
}
|
}
|
||||||
}, { status: 400 });
|
}, { status: 400 });
|
||||||
}
|
}
|
||||||
@ -173,25 +163,25 @@ export async function POST(request: Request) {
|
|||||||
// Create the mission in the database first
|
// Create the mission in the database first
|
||||||
const mission = await prisma.mission.create({
|
const mission = await prisma.mission.create({
|
||||||
data: {
|
data: {
|
||||||
name,
|
name: body.name,
|
||||||
logo,
|
logo: body.logo,
|
||||||
oddScope: oddScope || [],
|
oddScope: body.oddScope || [],
|
||||||
niveau,
|
niveau: body.niveau,
|
||||||
intention,
|
intention: body.intention,
|
||||||
missionType,
|
missionType: body.missionType,
|
||||||
donneurDOrdre,
|
donneurDOrdre: body.donneurDOrdre,
|
||||||
projection,
|
projection: body.projection,
|
||||||
services: services || [],
|
services: body.services || [],
|
||||||
participation,
|
participation: body.participation,
|
||||||
profils: profils || [],
|
profils: body.profils || [],
|
||||||
creatorId: userId
|
creatorId: userId
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// Add guardians if provided
|
// Add guardians if provided
|
||||||
if (guardians) {
|
if (body.guardians) {
|
||||||
const guardianRoles = ['gardien-temps', 'gardien-parole', 'gardien-memoire'];
|
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)
|
.filter(([role, userId]) => guardianRoles.includes(role) && userId)
|
||||||
.map(([role, userId]) => ({
|
.map(([role, userId]) => ({
|
||||||
role,
|
role,
|
||||||
@ -207,8 +197,8 @@ export async function POST(request: Request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Add volunteers if provided
|
// Add volunteers if provided
|
||||||
if (volunteers && volunteers.length > 0) {
|
if (body.volunteers && body.volunteers.length > 0) {
|
||||||
const volunteerEntries = volunteers.map((userId: string) => ({
|
const volunteerEntries = body.volunteers.map((userId: string) => ({
|
||||||
role: 'volontaire',
|
role: 'volontaire',
|
||||||
userId,
|
userId,
|
||||||
missionId: mission.id
|
missionId: mission.id
|
||||||
|
|||||||
BIN
node_modules/prisma/engines/a9055b89e58b4b5bfb59600785423b1db3d0e75d/libquery_engine-darwin-arm64.dylib.node
generated
vendored
Executable file
BIN
node_modules/prisma/engines/a9055b89e58b4b5bfb59600785423b1db3d0e75d/libquery_engine-darwin-arm64.dylib.node
generated
vendored
Executable file
Binary file not shown.
Loading…
Reference in New Issue
Block a user