mirror of
https://github.com/i701/sarlink-portal.git
synced 2025-07-28 17:20:23 +00:00
add admin checks for admin pages and run biome formating 🔨
All checks were successful
Build and Push Docker Images / Build and Push Docker Images (push) Successful in 11m8s
All checks were successful
Build and Push Docker Images / Build and Push Docker Images (push) Successful in 11m8s
This commit is contained in:
@ -9,37 +9,45 @@ import type { ApiError } from "@/lib/backend-types";
|
||||
import type { User } from "@/lib/types/user";
|
||||
import { handleApiResponse } from "@/utils/tryCatch";
|
||||
|
||||
type VerifyUserResponse = {
|
||||
"ok": boolean,
|
||||
"mismatch_fields": string[] | null,
|
||||
"error": string | null,
|
||||
"detail": string | null
|
||||
} | {
|
||||
"message": boolean,
|
||||
};
|
||||
type VerifyUserResponse =
|
||||
| {
|
||||
ok: boolean;
|
||||
mismatch_fields: string[] | null;
|
||||
error: string | null;
|
||||
detail: string | null;
|
||||
}
|
||||
| {
|
||||
message: boolean;
|
||||
};
|
||||
export async function verifyUser(userId: string) {
|
||||
const session = await getServerSession(authOptions);
|
||||
if (!session?.apiToken) {
|
||||
return { ok: false, error: 'Not authenticated' } as const;
|
||||
return { ok: false, error: "Not authenticated" } as const;
|
||||
}
|
||||
|
||||
try {
|
||||
const r = await fetch(
|
||||
`${process.env.SARLINK_API_BASE_URL}/api/auth/users/${userId}/verify/`,
|
||||
{
|
||||
method: 'PUT',
|
||||
method: "PUT",
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
"Content-Type": "application/json",
|
||||
Authorization: `Token ${session.apiToken}`,
|
||||
},
|
||||
},
|
||||
);
|
||||
const body = (await r.json().catch(() => ({}))) as VerifyUserResponse &
|
||||
{ message?: string; detail?: string };
|
||||
const body = (await r.json().catch(() => ({}))) as VerifyUserResponse & {
|
||||
message?: string;
|
||||
detail?: string;
|
||||
};
|
||||
|
||||
if (!r.ok) {
|
||||
const msg = body?.message || body?.detail || 'User verification failed';
|
||||
return { ok: false, error: msg, mismatch_fields: body?.mismatch_fields || null } as const;
|
||||
const msg = body?.message || body?.detail || "User verification failed";
|
||||
return {
|
||||
ok: false,
|
||||
error: msg,
|
||||
mismatch_fields: body?.mismatch_fields || null,
|
||||
} as const;
|
||||
}
|
||||
|
||||
return { ok: true, data: body } as const;
|
||||
@ -66,7 +74,7 @@ export async function getProfile() {
|
||||
|
||||
export async function rejectUser(
|
||||
_prevState: RejectUserFormState,
|
||||
formData: FormData
|
||||
formData: FormData,
|
||||
): Promise<RejectUserFormState> {
|
||||
const userId = formData.get("userId") as string;
|
||||
const rejection_details = formData.get("rejection_details") as string;
|
||||
@ -85,7 +93,9 @@ export async function rejectUser(
|
||||
|
||||
if (!response.ok) {
|
||||
const errorData = await response.json();
|
||||
throw new Error(errorData.message || errorData.detail || "Failed to reject user");
|
||||
throw new Error(
|
||||
errorData.message || errorData.detail || "Failed to reject user",
|
||||
);
|
||||
}
|
||||
|
||||
// Handle 204 No Content response (successful deletion)
|
||||
@ -95,11 +105,14 @@ export async function rejectUser(
|
||||
}
|
||||
|
||||
revalidatePath("/users");
|
||||
const error = await response.json()
|
||||
const error = await response.json();
|
||||
return {
|
||||
message: (error as ApiError).message || (error as ApiError).detail || "An unexpected error occurred.",
|
||||
message:
|
||||
(error as ApiError).message ||
|
||||
(error as ApiError).detail ||
|
||||
"An unexpected error occurred.",
|
||||
fieldErrors: {},
|
||||
payload: formData
|
||||
payload: formData,
|
||||
};
|
||||
}
|
||||
|
||||
@ -116,10 +129,9 @@ export type UpdateUserFormState = {
|
||||
payload?: FormData;
|
||||
};
|
||||
|
||||
|
||||
export async function updateUser(
|
||||
_prevState: UpdateUserFormState,
|
||||
formData: FormData
|
||||
formData: FormData,
|
||||
): Promise<UpdateUserFormState> {
|
||||
const userId = formData.get("userId") as string;
|
||||
const data: Record<string, string | number | boolean> = {};
|
||||
@ -128,7 +140,7 @@ export async function updateUser(
|
||||
data[key] = typeof value === "number" ? value : String(value);
|
||||
}
|
||||
}
|
||||
console.log("data in update user action", data)
|
||||
console.log("data in update user action", data);
|
||||
|
||||
const session = await getServerSession(authOptions);
|
||||
const response = await fetch(
|
||||
@ -142,18 +154,21 @@ export async function updateUser(
|
||||
body: JSON.stringify(data),
|
||||
},
|
||||
);
|
||||
console.log("response in update user action", response)
|
||||
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.",
|
||||
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;
|
||||
const updatedUser = (await response.json()) as User;
|
||||
revalidatePath("/users/[userId]/update", "page");
|
||||
revalidatePath("/users/[userId]/verify", "page");
|
||||
return {
|
||||
@ -164,7 +179,7 @@ export async function updateUser(
|
||||
|
||||
export async function updateUserAgreement(
|
||||
_prevState: UpdateUserFormState,
|
||||
formData: FormData
|
||||
formData: FormData,
|
||||
): Promise<UpdateUserFormState> {
|
||||
const userId = formData.get("userId") as string;
|
||||
// Remove userId from formData before sending to API
|
||||
@ -174,7 +189,7 @@ export async function updateUserAgreement(
|
||||
apiFormData.append(key, value);
|
||||
}
|
||||
}
|
||||
console.log({ apiFormData })
|
||||
console.log({ apiFormData });
|
||||
const session = await getServerSession(authOptions);
|
||||
const response = await fetch(
|
||||
`${process.env.SARLINK_API_BASE_URL}/api/auth/users/${userId}/agreement/`,
|
||||
@ -186,17 +201,20 @@ export async function updateUserAgreement(
|
||||
body: apiFormData,
|
||||
},
|
||||
);
|
||||
console.log("response in update user agreement action", response)
|
||||
console.log("response in update user agreement action", response);
|
||||
if (!response.ok) {
|
||||
const errorData = await response.json();
|
||||
return {
|
||||
message: errorData.message || errorData.detail || "An error occurred while updating the user agreement.",
|
||||
message:
|
||||
errorData.message ||
|
||||
errorData.detail ||
|
||||
"An error occurred while updating the user agreement.",
|
||||
fieldErrors: errorData.field_errors || {},
|
||||
payload: formData,
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
const updatedUserAgreement = await response.json() as { agreement: string };
|
||||
const updatedUserAgreement = (await response.json()) as { agreement: string };
|
||||
revalidatePath("/users/[userId]/update", "page");
|
||||
revalidatePath("/users/[userId]/verify", "page");
|
||||
revalidatePath("/users/[userId]/agreement", "page");
|
||||
@ -205,4 +223,4 @@ export async function updateUserAgreement(
|
||||
...updatedUserAgreement,
|
||||
message: "User agreement updated successfully",
|
||||
};
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user