From 09ba55b20f1cd53d338c8a86761af2ea97a92d68 Mon Sep 17 00:00:00 2001 From: Sid-80 Date: Wed, 10 Jul 2024 19:47:15 +0530 Subject: [PATCH] feat: api for creating new team --- src/app/api/teams/create/route.ts | 33 +++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 src/app/api/teams/create/route.ts diff --git a/src/app/api/teams/create/route.ts b/src/app/api/teams/create/route.ts new file mode 100644 index 0000000..697ba29 --- /dev/null +++ b/src/app/api/teams/create/route.ts @@ -0,0 +1,33 @@ +import { mongoDB } from "@/lib/MongoDB"; +import { AuthMiddleware } from "@/Middleware/AuthMiddleware"; +import TeamModel from "@/models/team"; +import { ApiUser } from "@/types/types"; +import { NextResponse } from "next/server"; + +export const POST = async (req: Request) => { + + const result = await AuthMiddleware(req); + + if (result instanceof NextResponse) { + + try { + const { teamName } = await req.json(); + + await mongoDB(); + + const user: ApiUser = JSON.parse(req.headers.get("user") || "{}"); + + const team = await TeamModel.create({ + teamName, + createdBy:user._id, + teamMembers:[user._id] + }); + + return NextResponse.json({ status: 200 }); + } catch (err) { + return NextResponse.json(`Err : ${err}`, {status:500}); + } + } else { + return result; + } +};