From 0c093f1303fc2939725051a0527f89971330db82 Mon Sep 17 00:00:00 2001 From: i701 Date: Sun, 20 Apr 2025 12:36:24 +0500 Subject: [PATCH] feat: add age validation in signup and update payment verification logic --- actions/auth-actions.ts | 9 +++ actions/payment.ts | 110 ++++---------------------------- components/auth/signup-form.tsx | 5 ++ components/device-cart.tsx | 92 +------------------------- components/devices-to-pay.tsx | 8 +-- 5 files changed, 31 insertions(+), 193 deletions(-) diff --git a/actions/auth-actions.ts b/actions/auth-actions.ts index da7cfeb..d6f6ead 100644 --- a/actions/auth-actions.ts +++ b/actions/auth-actions.ts @@ -121,6 +121,15 @@ export async function signup(_actionState: ActionState, formData: FormData) { errors: parsedData.error.flatten(), }; } + const age = + new Date().getFullYear() - new Date(parsedData.data.dob).getFullYear(); + if (age < 18) { + return { + message: "You must be at least 18 years old to register.", + payload: formData, + db_error: "dob", + }; + } const idCardExists = await checkIdOrPhone({ id_card: parsedData.data.id_card, diff --git a/actions/payment.ts b/actions/payment.ts index 77702bb..7bcbd89 100644 --- a/actions/payment.ts +++ b/actions/payment.ts @@ -115,20 +115,18 @@ export async function cancelPayment({ id }: { id: string }) { return { message: "Payment successfully canceled." }; } -type UpdatePayment = Pick< - Payment, - "id" | "paid" | "paid_at" | "method" | "number_of_months" ->; -export async function updatePayment({ - id, - method, - paid, - paid_at, - number_of_months, -}: UpdatePayment) { +type UpdatePayment = { + id: string; + method: "TRANSFER" | "WALLET"; + benefName?: string; + accountNo?: string; + absAmount?: string; + time?: string; +}; +export async function verifyPayment({ id, method }: UpdatePayment) { const session = await getServerSession(authOptions); const response = await fetch( - `${process.env.SARLINK_API_BASE_URL}/api/billing/payment/${id}/update/`, + `${process.env.SARLINK_API_BASE_URL}/api/billing/payment/${id}/verify/`, { method: "PUT", headers: { @@ -137,58 +135,11 @@ export async function updatePayment({ }, body: JSON.stringify({ method, - paid, - paid_at, - number_of_months, }), }, ); - - if (!response.ok) { - const errorData = (await response.json()) as ApiError; - const errorMessage = - errorData.message || errorData.detail || "An error occurred."; - const error = new Error(errorMessage); - (error as ApiError & { details?: ApiError }).details = errorData; // Attach the errorData to the error object - throw error; - } - const payment = (await response.json()) as Payment; - return payment; -} - -type TUpdateWalletBalance = Pick; -export async function updateWalletBalance({ - id, - wallet_balance, -}: TUpdateWalletBalance) { - const session = await getServerSession(authOptions); - console.log("wallet bal in server action", wallet_balance); - const response = await fetch( - `${process.env.SARLINK_API_BASE_URL}/api/auth/update-wallet/${id}/`, - { - method: "PUT", - headers: { - "Content-Type": "application/json", - Authorization: `Token ${session?.apiToken}`, - }, - body: JSON.stringify({ - wallet_balance: Number.parseFloat(wallet_balance?.toFixed(2) ?? "0"), - }), - }, - ); - - if (!response.ok) { - const errorData = (await response.json()) as ApiError; - const errorMessage = - errorData.message || errorData.detail || "An error occurred."; - const error = new Error(errorMessage); - (error as ApiError & { details?: ApiError }).details = errorData; // Attach the errorData to the error object - throw error; - } - const message = (await response.json()) as { - message: "Wallet balance updated successfully."; - }; - return message; + revalidatePath("/payments/[paymentsId]"); + return handleApiResponse(response, "updatePayment"); } type VerifyPaymentType = { @@ -217,43 +168,6 @@ export async function getProfile() { return handleApiResponse(response, "getProfile"); } -export async function processWalletPayment({ - payment, - amount, -}: { payment: Payment | undefined; amount: number }) { - await checkSession(); - const session = await getServerSession(authOptions); - const walletBalance = session?.user?.wallet_balance ?? 0; - console.log("processing wallet payment >>>", walletBalance); - if (!payment) return; - const [updatePaymentError, _] = await tryCatch( - updatePayment({ - id: payment.id, - method: "WALLET", - paid: true, - paid_at: new Date().toISOString(), - number_of_months: payment.number_of_months, - }), - ); - if (updatePaymentError) { - throw new Error(updatePaymentError.message); - } - console.log("Wallet balance before update:", walletBalance); - const updated_balance = walletBalance - amount; - if (!session?.user?.id) return; - const [walletUpdateError, response] = await tryCatch( - updateWalletBalance({ - id: session?.user?.id, - wallet_balance: Number.parseFloat(updated_balance?.toFixed(2) ?? "0"), - }), - ); - if (walletUpdateError) { - throw new Error(walletUpdateError.message); - } - revalidatePath("/payments/[paymentsId]", "page"); - return response; -} - type VerifyPaymentResponse = | { success: boolean; diff --git a/components/auth/signup-form.tsx b/components/auth/signup-form.tsx index dee0d0f..64b67e7 100644 --- a/components/auth/signup-form.tsx +++ b/components/auth/signup-form.tsx @@ -230,6 +230,11 @@ export default function SignUpForm() { {actionState?.errors?.fieldErrors?.dob} )} + {actionState?.db_error === "dob" && ( + + {actionState?.message} + + )}