feat: add admin delete
This commit is contained in:
@@ -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, {
|
||||
|
||||
@@ -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
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user