Refactor user verification and data handling
All checks were successful
Build and Push Docker Images / Build and Push Docker Images (push) Successful in 55s

- Updated `package.json` to add a new script for Prisma database setup.
- Replaced `usePerson` hook with `getNationalPerson` function in `lib/person.ts` for improved national data fetching.
- Refactored imports in `auth-actions.ts`, `user-actions.ts`, and `verify/page.tsx` to use the new `getNationalPerson` function.
- Enhanced device notification logic in `check-devices/route.ts` to correctly handle payment data.
- Improved error handling in `devices-to-pay.tsx` for better user feedback.

These changes streamline user verification processes and enhance data integrity across the application.
This commit is contained in:
i701 2025-01-10 21:58:13 +05:00
parent fcf4f37561
commit a3f0759731
8 changed files with 37 additions and 34 deletions

View File

@ -2,7 +2,7 @@
import { authClient } from "@/lib/auth-client"; import { authClient } from "@/lib/auth-client";
import prisma from "@/lib/db"; import prisma from "@/lib/db";
import VerifyUserDetails from "@/lib/person"; import { VerifyUserDetails } from "@/lib/person";
import { signUpFormSchema } from "@/lib/schemas"; import { signUpFormSchema } from "@/lib/schemas";
import { headers } from "next/headers"; import { headers } from "next/headers";
// import type { User } from "@prisma/client"; // import type { User } from "@prisma/client";

View File

@ -1,8 +1,7 @@
"use server"; "use server";
import usePerson from "@/hooks/use-person";
import prisma from "@/lib/db"; import prisma from "@/lib/db";
import VerifyUserDetails from "@/lib/person"; import { VerifyUserDetails } from "@/lib/person";
import { revalidatePath } from "next/cache"; import { revalidatePath } from "next/cache";
import { redirect } from "next/navigation"; import { redirect } from "next/navigation";
import { CreateClient } from "./ninja/client"; import { CreateClient } from "./ninja/client";

View File

@ -2,7 +2,7 @@ import InputReadOnly from '@/components/input-read-only';
import { Badge } from '@/components/ui/badge'; import { Badge } from '@/components/ui/badge';
import UserRejectDialog from '@/components/user/user-reject-dialog'; import UserRejectDialog from '@/components/user/user-reject-dialog';
import { UserVerifyDialog } from '@/components/user/user-verify-dialog'; import { UserVerifyDialog } from '@/components/user/user-verify-dialog';
import usePerson from '@/hooks/use-person'; import { getNationalPerson } from '@/lib/person';
import prisma from '@/lib/db'; import prisma from '@/lib/db';
import Image from 'next/image'; import Image from 'next/image';
@ -28,7 +28,7 @@ export default async function VerifyUserPage({
} }
}) })
const nationalData = await usePerson({ idCard: dbUser?.id_card ?? "" }) const nationalData = await getNationalPerson({ idCard: dbUser?.id_card ?? "" })
return ( return (
<div> <div>
<div className='flex items-center justify-between text-gray-500 text-2xl font-bold title-bg py-4 px-2 mb-4'> <div className='flex items-center justify-between text-gray-500 text-2xl font-bold title-bg py-4 px-2 mb-4'>

View File

@ -21,8 +21,12 @@ export async function GET(request: Request) {
blocked: false, blocked: false,
}, },
include: { include: {
payment: true, payments: true,
User: true, User: {
include: {
devices: true,
},
},
}, },
}); });
let devicesNeedingNotification = 0; let devicesNeedingNotification = 0;
@ -31,7 +35,7 @@ export async function GET(request: Request) {
for (const device of devices) { for (const device of devices) {
let expiryDate = new Date(); let expiryDate = new Date();
const payment = device.payment; const payment = device.payments[0];
expiryDate = addMonths( expiryDate = addMonths(
payment?.paidAt || new Date(), payment?.paidAt || new Date(),
payment?.numberOfMonths || 0, payment?.numberOfMonths || 0,
@ -43,8 +47,8 @@ export async function GET(request: Request) {
const currentDate = new Date(); const currentDate = new Date();
console.log("device name -> ", device.name); console.log("device name -> ", device.name);
console.log("paid date -> ", device.payment?.paidAt); console.log("paid date -> ", device.payments[0]?.paidAt);
console.log("no of months paid -> ", device.payment?.numberOfMonths); console.log("no of months paid -> ", device.payments[0]?.numberOfMonths);
console.log("calculated expire date -> ", expiryDate); console.log("calculated expire date -> ", expiryDate);
console.log("notification threshold -> ", notificationThreshold); console.log("notification threshold -> ", notificationThreshold);
console.log("current date -> ", currentDate); console.log("current date -> ", currentDate);
@ -60,7 +64,7 @@ export async function GET(request: Request) {
if (device.User?.phoneNumber) { if (device.User?.phoneNumber) {
await sendNotifySms( await sendNotifySms(
new Date(expiryDate), new Date(expiryDate),
device.User.phoneNumber, device.User?.phoneNumber ?? "",
device.name, device.name,
); );
devicesNeedingNotification++; devicesNeedingNotification++;

View File

@ -110,8 +110,8 @@ export default function DevicesToPay({
case res?.success === true: case res?.success === true:
toast.success(res.message); toast.success(res.message);
break; break;
case res.success === false: case res?.success === false:
toast.error(res.message); toast.error(res?.message);
break; break;
default: default:
toast.error("Unexpected error occurred."); toast.error("Unexpected error occurred.");

View File

@ -1,16 +0,0 @@
import type { TNationalPerson } from "@/lib/types";
export default async function usePerson({
idCard,
}: { idCard: string }): Promise<TNationalPerson> {
const nationalInformation = await fetch(
`${process.env.PERSON_VERIFY_API_BASE}/api/person/${idCard}`,
{
next: {
revalidate: 60,
},
},
);
const nationalData = (await nationalInformation.json()) as TNationalPerson;
return nationalData;
}

View File

@ -1,10 +1,25 @@
"use server"; "use server";
import usePerson from "@/hooks/use-person"; import type { TNationalPerson } from "@/lib/types";
import type { User } from "@prisma/client"; import type { User } from "@prisma/client";
export default async function VerifyUserDetails({ user }: { user: User }) { export async function getNationalPerson({
idCard,
}: { idCard: string }): Promise<TNationalPerson> {
const nationalInformation = await fetch(
`${process.env.PERSON_VERIFY_API_BASE}/api/person/${idCard}`,
{
next: {
revalidate: 60,
},
},
);
const nationalData = (await nationalInformation.json()) as TNationalPerson;
return nationalData;
}
export async function VerifyUserDetails({ user }: { user: User }) {
const phoneNumber = user.phoneNumber.slice(4); const phoneNumber = user.phoneNumber.slice(4);
const nationalData = await usePerson({ idCard: user.id_card ?? "" }); const nationalData = await getNationalPerson({ idCard: user.id_card ?? "" });
const dob = new Date(nationalData.dob); const dob = new Date(nationalData.dob);
const age = new Date().getFullYear() - dob.getFullYear(); const age = new Date().getFullYear() - dob.getFullYear();

View File

@ -66,5 +66,6 @@
"tailwindcss": "^3.4.17", "tailwindcss": "^3.4.17",
"ts-node": "^10.9.2", "ts-node": "^10.9.2",
"typescript": "^5.7.2" "typescript": "^5.7.2"
} },
"packageManager": "yarn@1.22.22+sha512.a6b2f7906b721bba3d67d4aff083df04dad64c399707841b7acf00f6b133b7ac24255f2652fa22ae3534329dc6180534e98d17432037ff6fd140556e2bb3137e"
} }