skalara-web/app/api/projects/[id]/route.ts
2023-07-30 02:13:35 -04:00

54 lines
1.3 KiB
TypeScript

import { createRouteHandlerClient } from "@supabase/auth-helpers-nextjs";
import { Database } from "@/types/supabase";
import prisma from "@/lib/prisma";
import { NextRequest, NextResponse } from "next/server";
import { revalidatePath } from "next/cache";
import { cookies } from "next/headers";
export async function GET(
req: NextRequest,
{ params }: { params: { id: string } }
) {
try {
const supabase = createRouteHandlerClient<Database>({ cookies });
const {
data: { session },
} = await supabase.auth.getSession();
if (!session) {
throw new Error("Unauthorized");
}
const project = await prisma.project.findUnique({
where: { id: Number(params.id) },
include: { Task: true },
});
if (!project) {
throw new Error("Project not found");
}
const { Task, ...projectData } = project;
const res = {
...projectData,
id: String(project.id),
tasks: Task.map((task) => ({
...task,
id: String(task.id),
projectId: String(task.projectId),
})),
};
const path = req.nextUrl.searchParams.get("path") || "/";
revalidatePath(path);
return NextResponse.json(res, { status: 200 });
} catch (err: any) {
return NextResponse.json({ message: err.message }, { status: 401 });
} finally {
await prisma.$disconnect();
}
}