From af95e53cdcda4f28437e484958472929180baea2 Mon Sep 17 00:00:00 2001 From: oiov Date: Thu, 1 Aug 2024 19:47:34 +0800 Subject: [PATCH] feat: add admin delete --- app/api/user/admin/update/route.ts | 39 +++++++++++++++++++++--------- components/forms/user-form.tsx | 4 +-- lib/dto/user.ts | 5 ++-- 3 files changed, 32 insertions(+), 16 deletions(-) diff --git a/app/api/user/admin/update/route.ts b/app/api/user/admin/update/route.ts index d52c31e..820f4a8 100644 --- a/app/api/user/admin/update/route.ts +++ b/app/api/user/admin/update/route.ts @@ -1,16 +1,33 @@ +import { checkUserStatus, updateUser } from "@/lib/dto/user"; +import { getCurrentUser } from "@/lib/session"; + export async function POST(req: Request) { try { - // const user = checkUserStatus(await getCurrentUser()); - // if (user instanceof Response) return user; - // if (user.role !== "ADMIN") { - // return Response.json("Unauthorized", { - // status: 401, - // statusText: "Unauthorized", - // }); - // } - // const body = await req.json(); - // const { id, ...data } = body; - // const result = await updateUserById(id, data); + const user = checkUserStatus(await getCurrentUser()); + if (user instanceof Response) return user; + if (user.role !== "ADMIN") { + return Response.json("Unauthorized", { + status: 401, + statusText: "Unauthorized", + }); + } + + const { id, data } = await req.json(); + + const res = await updateUser(id, { + name: data.name, + email: data.email, + role: data.role, + active: data.active, + team: data.team, + image: data.image, + }); + if (!res?.id) { + return Response.json("An error occurred", { + status: 400, + statusText: "An error occurred", + }); + } return Response.json("success"); } catch (error) { return Response.json(error?.statusText || error, { diff --git a/components/forms/user-form.tsx b/components/forms/user-form.tsx index cda1caa..55edb59 100644 --- a/components/forms/user-form.tsx +++ b/components/forms/user-form.tsx @@ -75,14 +75,13 @@ export function UserForm({ if (type === "edit") { const response = await fetch("/api/user/admin/update", { method: "POST", - body: JSON.stringify({ data }), + body: JSON.stringify({ id: initData?.id, data }), }); if (!response.ok || response.status !== 200) { toast.error("Update Failed", { description: response.statusText, }); } else { - const res = await response.json(); toast.success(`Update successfully!`); setShowForm(false); onRefresh(); @@ -103,7 +102,6 @@ export function UserForm({ description: response.statusText, }); } else { - await response.json(); toast.success(`Success`); setShowForm(false); onRefresh(); diff --git a/lib/dto/user.ts b/lib/dto/user.ts index 2d2399e..417f239 100644 --- a/lib/dto/user.ts +++ b/lib/dto/user.ts @@ -2,7 +2,8 @@ import { User, UserRole } from "@prisma/client"; import { prisma } from "@/lib/db"; -export interface UpdateUserForm extends Omit {} +export interface UpdateUserForm + extends Omit {} export const getUserByEmail = async (email: string) => { try { @@ -68,7 +69,7 @@ export const updateUser = async (userId: string, data: UpdateUserForm) => { where: { id: userId, }, - data: data, + data, }); return session; } catch (error) {