Files
wr.do/actions/update-user-role.ts
2024-07-29 11:34:02 +08:00

45 lines
916 B
TypeScript

"use server";
import { revalidatePath } from "next/cache";
import { auth } from "@/auth";
import { UserRole } from "@prisma/client";
import { prisma } from "@/lib/db";
import { userRoleSchema } from "@/lib/validations/user";
export type FormData = {
role: UserRole;
};
export async function updateUserRole(userId: string, data: FormData) {
try {
const session = await auth();
if (
!session?.user ||
session?.user.id !== userId ||
session.user.role !== "ADMIN"
) {
throw new Error("Unauthorized");
}
const { role } = userRoleSchema.parse(data);
// Update the user role.
await prisma.user.update({
where: {
id: userId,
},
data: {
role: role,
},
});
revalidatePath("/dashboard/settings");
return { status: "success" };
} catch (error) {
// console.log(error)
return { status: "error" };
}
}