correction de l'update du
workflow
This commit is contained in:
66
src/app/api/projects/[id]/workflow/route.ts
Normal file
66
src/app/api/projects/[id]/workflow/route.ts
Normal file
@@ -0,0 +1,66 @@
|
||||
export const dynamic = 'force-dynamic';
|
||||
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
import { auth } from '@/lib/auth';
|
||||
import getDB from '@/lib/prisma';
|
||||
|
||||
// PUT /api/projects/[id]/workflow — Sync workflow (nodes + connections)
|
||||
export async function PUT(
|
||||
request: NextRequest,
|
||||
{ params }: { params: Promise<{ id: string }> }
|
||||
) {
|
||||
const session = await auth();
|
||||
if (!session?.user?.id) {
|
||||
return NextResponse.json({ error: 'Non autorisé' }, { status: 401 });
|
||||
}
|
||||
|
||||
const { id } = await params;
|
||||
const prisma = getDB();
|
||||
|
||||
// Verify ownership
|
||||
const project = await prisma.project.findFirst({
|
||||
where: { id, userId: session.user.id },
|
||||
});
|
||||
if (!project) {
|
||||
return NextResponse.json({ error: 'Projet non trouvé' }, { status: 404 });
|
||||
}
|
||||
|
||||
const { nodes, connections } = await request.json();
|
||||
|
||||
// Replace all nodes and connections in a transaction
|
||||
await prisma.$transaction(async (tx) => {
|
||||
// Delete existing
|
||||
await tx.plotConnection.deleteMany({ where: { projectId: id } });
|
||||
await tx.plotNode.deleteMany({ where: { projectId: id } });
|
||||
|
||||
// Create new nodes
|
||||
if (nodes && nodes.length > 0) {
|
||||
await tx.plotNode.createMany({
|
||||
data: nodes.map((n: any) => ({
|
||||
id: n.id,
|
||||
projectId: id,
|
||||
x: n.x,
|
||||
y: n.y,
|
||||
title: n.title || '',
|
||||
description: n.description || '',
|
||||
color: n.color || '#ffffff',
|
||||
type: n.type || 'story',
|
||||
})),
|
||||
});
|
||||
}
|
||||
|
||||
// Create new connections
|
||||
if (connections && connections.length > 0) {
|
||||
await tx.plotConnection.createMany({
|
||||
data: connections.map((c: any) => ({
|
||||
id: c.id,
|
||||
projectId: id,
|
||||
source: c.source,
|
||||
target: c.target,
|
||||
})),
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
return NextResponse.json({ success: true });
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
@@ -146,12 +146,23 @@ export const useProjects = (user: UserProfile | null) => {
|
||||
setProjects(prev => prev.map(p => p.id === id ? { ...p, ...data } : p));
|
||||
|
||||
try {
|
||||
// Persist project fields
|
||||
const payload: any = {};
|
||||
if (data.title !== undefined) payload.title = data.title;
|
||||
if (data.author !== undefined) payload.author = data.author;
|
||||
if (data.settings !== undefined) payload.settings = data.settings;
|
||||
if (data.styleGuide !== undefined) payload.styleGuide = data.styleGuide;
|
||||
await api.projects.update(id, payload);
|
||||
if (Object.keys(payload).length > 0) {
|
||||
await api.projects.update(id, payload);
|
||||
}
|
||||
|
||||
// Persist workflow (nodes + connections) separately
|
||||
if (data.workflow) {
|
||||
await api.projects.syncWorkflow(id, {
|
||||
nodes: data.workflow.nodes || [],
|
||||
connections: data.workflow.connections || [],
|
||||
});
|
||||
}
|
||||
} catch (err) {
|
||||
console.error("Failed to update project", err);
|
||||
}
|
||||
|
||||
@@ -73,6 +73,13 @@ const api = {
|
||||
method: 'DELETE',
|
||||
});
|
||||
},
|
||||
|
||||
async syncWorkflow(id: string, data: { nodes: any[]; connections: any[] }) {
|
||||
return api.request(`/projects/${id}/workflow`, {
|
||||
method: 'PUT',
|
||||
body: JSON.stringify(data),
|
||||
});
|
||||
},
|
||||
},
|
||||
|
||||
// --- CHAPTERS ---
|
||||
|
||||
Reference in New Issue
Block a user