mirror of
https://github.com/i701/sarlink-portal.git
synced 2025-07-16 21:35:50 +00:00
feat(user-verification): implement user verification functionality and update dialog UI ✨
All checks were successful
Build and Push Docker Images / Build and Push Docker Images (push) Successful in 6m54s
All checks were successful
Build and Push Docker Images / Build and Push Docker Images (push) Successful in 6m54s
This commit is contained in:
@ -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() {
|
||||
|
@ -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 (
|
||||
<AlertDialog>
|
||||
<AlertDialogTrigger asChild>
|
||||
<Dialog open={open} onOpenChange={setOpen}>
|
||||
<DialogTrigger asChild>
|
||||
<Button
|
||||
variant={user.verified ? "secondary" : "default"}
|
||||
disabled={disabled || user.verified}
|
||||
@ -30,11 +29,11 @@ export function UserVerifyDialog({ user }: { user: UserProfile }) {
|
||||
{user.verified ? <CheckCheck size={16} /> : <Check size={16} />}
|
||||
{user.verified ? "Verified" : "Verify"}
|
||||
</Button>
|
||||
</AlertDialogTrigger>
|
||||
<AlertDialogContent>
|
||||
<AlertDialogHeader>
|
||||
<AlertDialogTitle className="text-muted-foreground">Verify User</AlertDialogTitle>
|
||||
<AlertDialogDescription>
|
||||
</DialogTrigger>
|
||||
<DialogContent>
|
||||
<DialogHeader>
|
||||
<DialogTitle className="text-muted-foreground">Verify User</DialogTitle>
|
||||
<DialogDescription>
|
||||
Are you sure you want to verify the following user?
|
||||
<span className="inline-block my-4">
|
||||
<li>
|
||||
@ -52,31 +51,35 @@ export function UserVerifyDialog({ user }: { user: UserProfile }) {
|
||||
</li>
|
||||
<li>Phone Number: {user.mobile}</li>
|
||||
</span>
|
||||
</AlertDialogDescription>
|
||||
</AlertDialogHeader>
|
||||
<AlertDialogFooter>
|
||||
<AlertDialogCancel>Cancel</AlertDialogCancel>
|
||||
<AlertDialogAction
|
||||
</DialogDescription>
|
||||
</DialogHeader>
|
||||
<DialogFooter>
|
||||
<Button
|
||||
disabled={disabled}
|
||||
onClick={() => {
|
||||
onClick={async () => {
|
||||
setOpen(true);
|
||||
setDisabled(true);
|
||||
toast.promise(VerifyUser(String(userId)), {
|
||||
loading: "Verifying...",
|
||||
success: () => {
|
||||
setDisabled(false);
|
||||
return "User Verified!";
|
||||
},
|
||||
error: (error: Error) => {
|
||||
setDisabled(false);
|
||||
return error.message || "Something went wrong";
|
||||
},
|
||||
const res = await verifyUser(String(userId));
|
||||
if (res.ok) toast.success('User Verified!');
|
||||
else toast.warning(res.error, {
|
||||
description: <div>
|
||||
<p className="italic">The following fields do not match</p>
|
||||
<ul className="list-disc list-inside p-2">
|
||||
{res.mismatch_fields?.map((field) => (
|
||||
<li key={field}>{field}</li>
|
||||
))}
|
||||
</ul>
|
||||
</div>,
|
||||
closeButton: true,
|
||||
duration: 10000,
|
||||
});
|
||||
setDisabled(false);
|
||||
}}
|
||||
>
|
||||
{disabled ? "Verifying..." : "Verify"}
|
||||
</AlertDialogAction>
|
||||
</AlertDialogFooter>
|
||||
</AlertDialogContent>
|
||||
</AlertDialog>
|
||||
</Button>
|
||||
</DialogFooter>
|
||||
</DialogContent>
|
||||
</Dialog>
|
||||
);
|
||||
}
|
||||
|
Reference in New Issue
Block a user