mirror of
https://github.com/i701/sarlink-portal.git
synced 2025-07-15 11:05:50 +00:00
132 lines
3.5 KiB
TypeScript
132 lines
3.5 KiB
TypeScript
"use server";
|
|
|
|
import { revalidatePath } from "next/cache";
|
|
import { redirect } from "next/navigation";
|
|
import { getServerSession } from "next-auth";
|
|
import { authOptions } from "@/app/auth";
|
|
import type { RejectUserFormState } from "@/components/user/user-reject-dialog";
|
|
import type { ApiError } from "@/lib/backend-types";
|
|
import type { User, UserProfile } from "@/lib/types/user";
|
|
import { handleApiResponse } from "@/utils/tryCatch";
|
|
|
|
export async function VerifyUser(_userId: string) {
|
|
// const user = await prisma.user.findUnique({
|
|
// where: {
|
|
// id: userId,
|
|
// },
|
|
// include: {Rejectuser
|
|
// atoll: true,
|
|
// island: true,
|
|
// },
|
|
// });
|
|
// if (!user) {
|
|
// throw new Error("User not found");
|
|
// }
|
|
// const isValidPerson = await VerifyUserDetails({ user });
|
|
// if (!isValidPerson)
|
|
// throw new Error("The user details does not match national data.");
|
|
// if (isValidPerson) {
|
|
// await prisma.user.update({
|
|
// where: {
|
|
// id: userId,
|
|
// },
|
|
// data: {
|
|
// verified: true,
|
|
// },
|
|
// });
|
|
// const ninjaClient = 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 getProfile() {
|
|
const session = await getServerSession(authOptions);
|
|
const response = await fetch(
|
|
`${process.env.SARLINK_API_BASE_URL}/api/auth/profile/`,
|
|
{
|
|
method: "GET",
|
|
headers: {
|
|
"Content-Type": "application/json",
|
|
Authorization: `Token ${session?.apiToken}`,
|
|
},
|
|
},
|
|
);
|
|
|
|
return handleApiResponse<User>(response, "getProfile");
|
|
}
|
|
|
|
|
|
|
|
export async function getProfileById(userId: string) {
|
|
const session = await getServerSession(authOptions);
|
|
const response = await fetch(
|
|
`${process.env.SARLINK_API_BASE_URL}/api/auth/users/${userId}/`,
|
|
{
|
|
method: "GET",
|
|
headers: {
|
|
"Content-Type": "application/json",
|
|
Authorization: `Token ${session?.apiToken}`,
|
|
},
|
|
},
|
|
);
|
|
|
|
return handleApiResponse<UserProfile>(response, "getProfilebyId");
|
|
}
|
|
|
|
export async function rejectUser(
|
|
_prevState: RejectUserFormState,
|
|
formData: FormData
|
|
): Promise<RejectUserFormState> {
|
|
const userId = formData.get("userId") as string;
|
|
const rejection_details = formData.get("rejection_details") as string;
|
|
const session = await getServerSession(authOptions);
|
|
const response = await fetch(
|
|
`${process.env.SARLINK_API_BASE_URL}/api/auth/users/${userId}/reject/`,
|
|
{
|
|
method: "DELETE",
|
|
headers: {
|
|
"Content-Type": "application/json",
|
|
Authorization: `Token ${session?.apiToken}`,
|
|
},
|
|
body: JSON.stringify({ rejection_details: rejection_details }),
|
|
},
|
|
);
|
|
|
|
if (!response.ok) {
|
|
const errorData = await response.json();
|
|
throw new Error(errorData.message || errorData.detail || "Failed to reject user");
|
|
}
|
|
|
|
// Handle 204 No Content response (successful deletion)
|
|
if (response.status === 204) {
|
|
revalidatePath("/users");
|
|
redirect("/users");
|
|
}
|
|
|
|
revalidatePath("/users");
|
|
const error = await response.json()
|
|
return {
|
|
message: (error as ApiError).message || (error as ApiError).detail || "An unexpected error occurred.",
|
|
fieldErrors: {},
|
|
payload: formData
|
|
};
|
|
}
|