Add user rejection and device addition functionalities

- Implemented Rejectuser function to delete a user and send rejection details via SMS.
- Added SendUserRejectionDetailSMS function for SMS notifications.
- Introduced AddDevice function to create new devices associated with users.
- Updated user-actions.ts to include new functionalities and improve user management.
- Refactored auth-guard.ts to ensure proper session handling for admin access.
This commit is contained in:
i701 2024-12-01 23:16:54 +05:00
parent 3f8bb4e70a
commit 2cbf9fb773
2 changed files with 70 additions and 4 deletions

View File

@ -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;
}

View File

@ -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");
}