mirror of
https://github.com/i701/sarlink-portal.git
synced 2025-02-22 18:22:00 +00:00
- 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.
114 lines
2.2 KiB
TypeScript
114 lines
2.2 KiB
TypeScript
"use server";
|
|
|
|
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) {
|
|
const user = await prisma.user.findUnique({
|
|
where: {
|
|
id: userId,
|
|
},
|
|
include: {
|
|
atoll: true,
|
|
island: true,
|
|
},
|
|
});
|
|
if (!user) {
|
|
throw new Error("User not found");
|
|
}
|
|
await prisma.user.update({
|
|
where: {
|
|
id: userId,
|
|
},
|
|
data: {
|
|
verified: true,
|
|
},
|
|
});
|
|
await CreateClient({
|
|
group_settings_id: "",
|
|
address1: "",
|
|
city: user.atoll?.name || "",
|
|
state: user.island?.name || "",
|
|
postal_code: "",
|
|
country_id: "462",
|
|
address2: user.address || "",
|
|
contacts: {
|
|
first_name: user.name?.split(" ")[0] || "",
|
|
last_name: user.name?.split(" ")[1] || "",
|
|
email: user.email || "",
|
|
phone: user.phoneNumber || "",
|
|
send_email: false,
|
|
custom_value1: user.dob?.toISOString().split("T")[0] || "",
|
|
custom_value2: user.id_card || "",
|
|
custom_value3: "",
|
|
},
|
|
});
|
|
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;
|
|
}
|