| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| import { NextResponse } from "next/server"; |
| import { applyHeadingChanges } from "@/lib/docx-service"; |
| import fileStore from "@/lib/file-store"; |
|
|
| export async function POST(request) { |
| try { |
| const body = await request.json(); |
| const { sessionId, headingIndices, headingFontSize = 18 } = body; |
|
|
| if (!sessionId) { |
| return NextResponse.json( |
| { error: "sessionId is required" }, |
| { status: 400 } |
| ); |
| } |
|
|
| if (!Array.isArray(headingIndices)) { |
| return NextResponse.json( |
| { error: "headingIndices must be an array of paragraph indices" }, |
| { status: 400 } |
| ); |
| } |
|
|
| const session = fileStore.get(sessionId); |
| if (!session) { |
| return NextResponse.json( |
| { error: "Session not found or expired. Please re-upload the file." }, |
| { status: 404 } |
| ); |
| } |
|
|
| |
| const headingChanges = headingIndices.map((index) => ({ |
| index, |
| headingFontSize, |
| bold: true, |
| })); |
|
|
| |
| const enrichedBuffer = applyHeadingChanges( |
| session.originalBuffer, |
| headingChanges |
| ); |
|
|
| |
| const updatedClassifications = (session.paragraphs || []).map((p) => ({ |
| index: p.index, |
| text: p.text, |
| isHeading: headingIndices.includes(p.index), |
| })); |
|
|
| fileStore.update(sessionId, { |
| enrichedBuffer: enrichedBuffer, |
| headingIndices: headingIndices, |
| classifications: updatedClassifications, |
| headingFontSize: headingFontSize, |
| }); |
|
|
| return NextResponse.json({ |
| sessionId, |
| classifications: updatedClassifications, |
| headingsApplied: headingIndices.length, |
| headingFontSize, |
| }); |
| } catch (err) { |
| console.error("Apply changes error:", err); |
| return NextResponse.json( |
| { error: "Failed to apply changes: " + err.message }, |
| { status: 500 } |
| ); |
| } |
| } |
|
|