50 lines
1.4 KiB
TypeScript
50 lines
1.4 KiB
TypeScript
"use server";
|
|
|
|
import { getServerSession } from "next-auth";
|
|
import { revalidatePath } from "next/cache";
|
|
import { authOptions } from "@/lib/auth";
|
|
import { isAdminEmail } from "@/lib/admin";
|
|
import { userService } from "@/server/services/user.service";
|
|
|
|
export async function suspendUserAction(userId: string, reason: string) {
|
|
const session = await getServerSession(authOptions);
|
|
if (!session?.user) {
|
|
return { error: "Kamu harus login terlebih dahulu" };
|
|
}
|
|
if (!isAdminEmail(session.user.email)) {
|
|
return { error: "Hanya admin yang bisa melakukan aksi ini" };
|
|
}
|
|
|
|
try {
|
|
await userService.suspendUser({
|
|
userId,
|
|
adminId: session.user.id,
|
|
reason,
|
|
});
|
|
revalidatePath("/admin/users");
|
|
revalidatePath(`/admin/users/${userId}`);
|
|
return { success: true as const };
|
|
} catch (err) {
|
|
return { error: (err as Error).message };
|
|
}
|
|
}
|
|
|
|
export async function unsuspendUserAction(userId: string) {
|
|
const session = await getServerSession(authOptions);
|
|
if (!session?.user) {
|
|
return { error: "Kamu harus login terlebih dahulu" };
|
|
}
|
|
if (!isAdminEmail(session.user.email)) {
|
|
return { error: "Hanya admin yang bisa melakukan aksi ini" };
|
|
}
|
|
|
|
try {
|
|
await userService.unsuspendUser({ userId, adminId: session.user.id });
|
|
revalidatePath("/admin/users");
|
|
revalidatePath(`/admin/users/${userId}`);
|
|
return { success: true as const };
|
|
} catch (err) {
|
|
return { error: (err as Error).message };
|
|
}
|
|
}
|