diff --git a/actions/user-actions.ts b/actions/user-actions.ts index 7c2c0d1..5097c58 100644 --- a/actions/user-actions.ts +++ b/actions/user-actions.ts @@ -2,6 +2,7 @@ import prisma from "@/lib/db"; import { revalidatePath } from "next/cache"; +import { redirect } from "next/navigation"; import { CreateClient } from "./ninja/client"; export async function VerifyUser(userId: string) { @@ -46,3 +47,67 @@ export async function VerifyUser(userId: string) { }); revalidatePath("/users"); } + +export async function Rejectuser({ + userId, + reason, +}: { userId: string; reason: string }) { + const user = await prisma.user.findUnique({ + where: { + id: userId, + }, + }); + if (!user) { + throw new Error("User not found"); + } + await prisma.user.delete({ + where: { + id: userId, + }, + }); + await SendUserRejectionDetailSMS({ + details: reason, + phoneNumber: user.phoneNumber, + }); + revalidatePath("/users"); + redirect("/users"); +} + +export const SendUserRejectionDetailSMS = async ({ + details, + phoneNumber, +}: { + details: string; + phoneNumber: string; +}) => { + const respose = await fetch("https://smsapi.sarlink.link/send", { + method: "POST", + headers: { + "Content-Type": "application/json", + }, + body: JSON.stringify({ + api_key: process.env.SMS_API_KEY, + number: phoneNumber, + text: details, + }), + }); + const data = await respose.json(); + console.log(data); + return data; +}; + +export async function AddDevice({ + name, + mac_address, + user_id, +}: { name: string; mac_address: string; user_id: string }) { + const newDevice = await prisma.device.create({ + data: { + name: name, + mac: mac_address, + userId: user_id, + }, + }); + revalidatePath("/devices"); + return newDevice; +} diff --git a/lib/auth-guard.ts b/lib/auth-guard.ts index 9880ee7..720e6aa 100644 --- a/lib/auth-guard.ts +++ b/lib/auth-guard.ts @@ -1,11 +1,12 @@ +"use server"; import { auth } from "@/lib/auth"; -import { redirect } from "next/navigation"; import { headers } from "next/headers"; +import { redirect } from "next/navigation"; -const session = await auth.api.getSession({ - headers: await headers(), -}); export async function AdminAuthGuard() { + const session = await auth.api.getSession({ + headers: await headers(), + }); if (session?.user.role !== "ADMIN") { return redirect("/login"); }