mirror of
https://github.com/i701/sarlink-portal.git
synced 2025-07-15 11:05:50 +00:00
172 lines
4.6 KiB
TypeScript
172 lines
4.6 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 } 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 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
|
|
};
|
|
}
|
|
|
|
export type UpdateUserFormState = {
|
|
message: string;
|
|
fieldErrors?: {
|
|
id_card?: string[];
|
|
first_name?: string[];
|
|
last_name?: string[];
|
|
dob?: string[];
|
|
mobile?: string[];
|
|
address?: string[];
|
|
};
|
|
payload?: FormData;
|
|
};
|
|
|
|
|
|
export async function updateUser(
|
|
_prevState: UpdateUserFormState,
|
|
formData: FormData
|
|
): Promise<UpdateUserFormState> {
|
|
const userId = formData.get("userId") as string;
|
|
const data: Record<string, string | number | boolean> = {};
|
|
for (const [key, value] of formData.entries()) {
|
|
if (value !== undefined && value !== "") {
|
|
data[key] = typeof value === "number" ? value : String(value);
|
|
}
|
|
}
|
|
console.log("data in update user action", data)
|
|
|
|
const session = await getServerSession(authOptions);
|
|
const response = await fetch(
|
|
`${process.env.SARLINK_API_BASE_URL}/api/auth/users/${userId}/update/`,
|
|
{
|
|
method: "PUT",
|
|
headers: {
|
|
"Content-Type": "application/json",
|
|
Authorization: `Token ${session?.apiToken}`,
|
|
},
|
|
body: JSON.stringify(data),
|
|
},
|
|
);
|
|
console.log("response in update user action", response)
|
|
|
|
if (!response.ok) {
|
|
const errorData = await response.json();
|
|
return {
|
|
message: errorData.message || errorData.detail || "An error occurred while updating the user.",
|
|
fieldErrors: errorData.field_errors || {},
|
|
payload: formData,
|
|
}
|
|
}
|
|
|
|
const updatedUser = await response.json() as User;
|
|
revalidatePath("/users/[userId]/update", "page");
|
|
revalidatePath("/users/[userId]/verify", "page");
|
|
return {
|
|
...updatedUser,
|
|
message: "User updated successfully",
|
|
};
|
|
} |