import db from "@/lib/db"; import { NextResponse } from "next/server"; import { withReadAuth, withTeamLeadAuth } from "@/lib/middleware/auth"; async function getContractHandler(req, { params }) { const { id } = await params; const contract = db .prepare( ` SELECT * FROM contracts WHERE contract_id = ? ` ) .get(id); if (!contract) { return NextResponse.json({ error: "Contract not found" }, { status: 404 }); } return NextResponse.json(contract); } async function deleteContractHandler(req, { params }) { const { id } = params; try { // Check if there are any projects linked to this contract const linkedProjects = db .prepare("SELECT COUNT(*) as count FROM projects WHERE contract_id = ?") .get(id); if (linkedProjects.count > 0) { return NextResponse.json( { error: "Nie można usunąć umowy z przypisanymi projektami" }, { status: 400 } ); } // Delete the contract const result = db .prepare("DELETE FROM contracts WHERE contract_id = ?") .run(id); if (result.changes === 0) { return NextResponse.json( { error: "Contract not found" }, { status: 404 } ); } return NextResponse.json({ success: true }); } catch (error) { console.error("Error deleting contract:", error); return NextResponse.json( { error: "Internal server error" }, { status: 500 } ); } } // Protected routes - require authentication export const GET = withReadAuth(getContractHandler); export const DELETE = withTeamLeadAuth(deleteContractHandler);