skalara-web/app/api/projects/[id]/tasks/route.ts
2023-08-03 00:00:15 -04:00

132 lines
3.4 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 POST(
// 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 { description, status, priority, dueDate, tags } = await req.json();
// const newTask = await prisma.task.create({
// data: {
// description,
// status,
// priority,
// dueDate,
// tags,
// projectId: Number(params.id),
// },
// });
// await prisma.userProfile_Task.create({
// data: {
// userProfileId: session.user.id,
// taskId: newTask.id,
// },
// });
// const res = {
// ...newTask,
// id: String(newTask.id),
// projectId: String(newTask.projectId),
// };
// const path = req.nextUrl.searchParams.get("path") || "/";
// revalidatePath(path);
// return NextResponse.json(res, { status: 201 });
// } catch (err: any) {
// return NextResponse.json({ message: err.message }, { status: 500 });
// } finally {
// await prisma.$disconnect();
// }
// }
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 POST(
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 input = await req.json();
console.log(input);
let newTasks = [];
if (Array.isArray(input)) {
// Handle multiple tasks
for (const task of input) {
const newTask = await prisma.task.create({
data: {
description: task.task,
projectId: Number(params.id),
},
});
newTasks.push({
...newTask,
id: String(newTask.id),
projectId: String(newTask.projectId),
});
}
const path = req.nextUrl.searchParams.get("path") || "/";
revalidatePath(path);
return NextResponse.json(newTasks, { status: 201 });
} else {
// Handle single task
const newTask = await prisma.task.create({
data: {
description: input.description,
projectId: Number(params.id),
},
});
const res = {
...newTask,
id: String(newTask?.id),
projectId: String(newTask?.projectId),
};
console.log(res);
const path = req.nextUrl.searchParams.get("path") || "/";
revalidatePath(path);
return NextResponse.json(res, { status: 201 });
}
} catch (err: any) {
return NextResponse.json({ message: err.message }, { status: 500 });
} finally {
await prisma.$disconnect();
}
}