From dc3b5f9bf9a486cab45b38bace3fdcc136a0a83a Mon Sep 17 00:00:00 2001 From: i701 Date: Wed, 16 Jul 2025 02:03:03 +0500 Subject: [PATCH] =?UTF-8?q?feat(user-verification):=20implement=20user=20v?= =?UTF-8?q?erification=20functionality=20and=20update=20dialog=20UI=20?= =?UTF-8?q?=E2=9C=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- actions/user-actions.ts | 83 ++++++++++++-------------- components/user/user-verify-dialog.tsx | 81 +++++++++++++------------ 2 files changed, 79 insertions(+), 85 deletions(-) diff --git a/actions/user-actions.ts b/actions/user-actions.ts index bfad649..1fda4e7 100644 --- a/actions/user-actions.ts +++ b/actions/user-actions.ts @@ -9,52 +9,43 @@ 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"); +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; + } + + try { + const r = await fetch( + `${process.env.SARLINK_API_BASE_URL}/api/auth/users/${userId}/verify/`, + { + method: 'PUT', + headers: { + 'Content-Type': 'application/json', + Authorization: `Token ${session.apiToken}`, + }, + }, + ); + 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; + } + + return { ok: true, data: body } as const; + } catch (err) { + return { ok: false, error: (err as Error).message } as const; + } } export async function getProfile() { diff --git a/components/user/user-verify-dialog.tsx b/components/user/user-verify-dialog.tsx index 89638fa..2a161c5 100644 --- a/components/user/user-verify-dialog.tsx +++ b/components/user/user-verify-dialog.tsx @@ -2,27 +2,26 @@ import { Check, CheckCheck } from "lucide-react"; import { useState } from "react"; import { toast } from "sonner"; -import { VerifyUser } from "@/actions/user-actions"; -import { - AlertDialog, - AlertDialogAction, - AlertDialogCancel, - AlertDialogContent, - AlertDialogDescription, - AlertDialogFooter, - AlertDialogHeader, - AlertDialogTitle, - AlertDialogTrigger, -} from "@/components/ui/alert-dialog"; +import { verifyUser } from "@/actions/user-actions"; import { Button } from "@/components/ui/button"; +import { + Dialog, + DialogContent, + DialogDescription, + DialogFooter, + DialogHeader, + DialogTitle, + DialogTrigger, +} from "@/components/ui/dialog"; import type { UserProfile } from "@/lib/types/user"; export function UserVerifyDialog({ user }: { user: UserProfile }) { const userId = user.id; const [disabled, setDisabled] = useState(false); + const [open, setOpen] = useState(false); return ( - - + + - - - - Verify User - + + + + Verify User + Are you sure you want to verify the following user?
  • @@ -52,31 +51,35 @@ export function UserVerifyDialog({ user }: { user: UserProfile }) {
  • Phone Number: {user.mobile}
  • -
    -
    - - Cancel - + + + + + + ); }