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

This commit is contained in:
2025-07-25 13:31:12 +05:00
parent aedf7cdf7d
commit 9b2f2c1528
127 changed files with 6577 additions and 6334 deletions

View File

@@ -8,7 +8,7 @@ import type {
ApiResponse,
NewPayment,
Payment,
Topup
Topup,
} from "@/lib/backend-types";
import type { TopupResponse } from "@/lib/types";
import { handleApiResponse } from "@/utils/tryCatch";
@@ -24,7 +24,8 @@ export async function createPayment(data: NewPayment) {
const session = await getServerSession(authOptions);
console.log("data", data);
const response = await fetch(
`${process.env.SARLINK_API_BASE_URL // });
`${
process.env.SARLINK_API_BASE_URL // });
}/api/billing/payment/`,
{
method: "POST",
@@ -93,11 +94,17 @@ type GetPaymentProps = {
[key: string]: string | number | undefined; // Allow additional properties for flexibility
};
export async function getPayments(params: GetPaymentProps, allPayments = false) {
export async function getPayments(
params: GetPaymentProps,
allPayments = false,
) {
// Build query string from all defined params
const query = Object.entries(params)
.filter(([_, value]) => value !== undefined && value !== "")
.map(([key, value]) => `${encodeURIComponent(key)}=${encodeURIComponent(String(value))}`)
.map(
([key, value]) =>
`${encodeURIComponent(key)}=${encodeURIComponent(String(value))}`,
)
.join("&");
const session = await getServerSession(authOptions);
const response = await fetch(
@@ -122,12 +129,17 @@ export async function getPayments(params: GetPaymentProps, allPayments = false)
return data;
}
export async function getTopups(params: GenericGetResponseProps, all_topups = false) {
export async function getTopups(
params: GenericGetResponseProps,
all_topups = false,
) {
// Build query string from all defined params
const query = Object.entries(params)
.filter(([_, value]) => value !== undefined && value !== "")
.map(([key, value]) => `${encodeURIComponent(key)}=${encodeURIComponent(String(value))}`)
.map(
([key, value]) =>
`${encodeURIComponent(key)}=${encodeURIComponent(String(value))}`,
)
.join("&");
const session = await getServerSession(authOptions);
@@ -223,17 +235,17 @@ export type VerifyDevicePaymentState = {
success: boolean;
fieldErrors: Record<string, string>;
payload?: FormData;
}
};
export async function verifyDevicePayment(
_prevState: VerifyDevicePaymentState,
formData: FormData
formData: FormData,
): Promise<VerifyDevicePaymentState> {
const session = await getServerSession(authOptions);
// Get the payment ID and method from the form data
const paymentId = formData.get('paymentId') as string;
const method = formData.get('method') as "TRANSFER" | "WALLET";
const paymentId = formData.get("paymentId") as string;
const method = formData.get("method") as "TRANSFER" | "WALLET";
if (!paymentId) {
return {
@@ -266,14 +278,18 @@ export async function verifyDevicePayment(
},
);
const result = await handleApiResponse<Payment>(response, "verifyDevicePayment");
const result = await handleApiResponse<Payment>(
response,
"verifyDevicePayment",
);
revalidatePath("/payments/[paymentId]", "page");
return {
message: method === "WALLET"
? "Payment completed successfully using wallet!"
: "Payment verification successful!",
message:
method === "WALLET"
? "Payment completed successfully using wallet!"
: "Payment verification successful!",
success: true,
fieldErrors: {},
payment: result,
@@ -281,7 +297,8 @@ export async function verifyDevicePayment(
} catch (error: unknown) {
if (error instanceof Error) {
return {
message: error.message || "Payment verification failed. Please try again.",
message:
error.message || "Payment verification failed. Please try again.",
success: false,
fieldErrors: {},
};
@@ -295,7 +312,6 @@ export async function verifyDevicePayment(
}
}
export type VerifyTopupPaymentState = {
transaction?: {
sourceBank: string;
@@ -305,15 +321,15 @@ export type VerifyTopupPaymentState = {
success: boolean;
fieldErrors: Record<string, string>;
payload?: FormData;
}
};
export async function verifyTopupPayment(
_prevState: VerifyTopupPaymentState,
formData: FormData
formData: FormData,
): Promise<VerifyTopupPaymentState> {
const session = await getServerSession(authOptions);
// Get the topup ID from the form data or use a hidden input
const topupId = formData.get('topupId') as string;
const topupId = formData.get("topupId") as string;
if (!topupId) {
return {
@@ -335,7 +351,10 @@ export async function verifyTopupPayment(
},
);
const result = await handleApiResponse<TopupResponse>(response, "verifyTopupPayment");
const result = await handleApiResponse<TopupResponse>(
response,
"verifyTopupPayment",
);
revalidatePath("/top-ups/[topupId]", "page");
@@ -348,7 +367,8 @@ export async function verifyTopupPayment(
} catch (error: unknown) {
if (error instanceof Error) {
return {
message: error.message || "Please check your payment details and try again.",
message:
error.message || "Please check your payment details and try again.",
success: false,
fieldErrors: {},
};
@@ -361,5 +381,3 @@ export async function verifyTopupPayment(
}
}
}

View File

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