mirror of
https://github.com/SkalaraAI/skalara-web.git
synced 2025-04-07 05:50:18 -04:00
59 lines
1.4 KiB
TypeScript
59 lines
1.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();
|
|
}
|
|
}
|