feat: add admin delete

This commit is contained in:
oiov
2024-08-01 19:47:34 +08:00
parent 0b2060177c
commit af95e53cdc
3 changed files with 32 additions and 16 deletions
+28 -11
View File
@@ -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, {
+1 -3
View File
@@ -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();
+3 -2
View File
@@ -2,7 +2,8 @@ import { User, UserRole } from "@prisma/client";
import { prisma } from "@/lib/db";
export interface UpdateUserForm extends Omit<User, "id" | "createdAt"> {}
export interface UpdateUserForm
extends Omit<User, "id" | "createdAt" | "updatedAt" | "emailVerified"> {}
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) {