From a3f07597318f7148a2cc1260087692d4ac7d988e Mon Sep 17 00:00:00 2001 From: i701 Date: Fri, 10 Jan 2025 21:58:13 +0500 Subject: [PATCH] Refactor user verification and data handling - 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. --- actions/auth-actions.ts | 2 +- actions/user-actions.ts | 3 +-- .../users/[userId]/verify/page.tsx | 4 ++-- app/api/check-devices/route.ts | 16 ++++++++------ components/devices-to-pay.tsx | 4 ++-- hooks/use-person.ts | 16 -------------- lib/person.ts | 21 ++++++++++++++++--- package.json | 5 +++-- 8 files changed, 37 insertions(+), 34 deletions(-) delete mode 100644 hooks/use-person.ts diff --git a/actions/auth-actions.ts b/actions/auth-actions.ts index d84b7ae..d5519cb 100644 --- a/actions/auth-actions.ts +++ b/actions/auth-actions.ts @@ -2,7 +2,7 @@ import { authClient } from "@/lib/auth-client"; import prisma from "@/lib/db"; -import VerifyUserDetails from "@/lib/person"; +import { VerifyUserDetails } from "@/lib/person"; import { signUpFormSchema } from "@/lib/schemas"; import { headers } from "next/headers"; // import type { User } from "@prisma/client"; diff --git a/actions/user-actions.ts b/actions/user-actions.ts index 2c6c34e..3a96b32 100644 --- a/actions/user-actions.ts +++ b/actions/user-actions.ts @@ -1,8 +1,7 @@ "use server"; -import usePerson from "@/hooks/use-person"; import prisma from "@/lib/db"; -import VerifyUserDetails from "@/lib/person"; +import { VerifyUserDetails } from "@/lib/person"; import { revalidatePath } from "next/cache"; import { redirect } from "next/navigation"; import { CreateClient } from "./ninja/client"; diff --git a/app/(dashboard)/users/[userId]/verify/page.tsx b/app/(dashboard)/users/[userId]/verify/page.tsx index 640fb98..930b187 100644 --- a/app/(dashboard)/users/[userId]/verify/page.tsx +++ b/app/(dashboard)/users/[userId]/verify/page.tsx @@ -2,7 +2,7 @@ import InputReadOnly from '@/components/input-read-only'; import { Badge } from '@/components/ui/badge'; import UserRejectDialog from '@/components/user/user-reject-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 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 (
diff --git a/app/api/check-devices/route.ts b/app/api/check-devices/route.ts index ea2bba4..fbef047 100644 --- a/app/api/check-devices/route.ts +++ b/app/api/check-devices/route.ts @@ -21,8 +21,12 @@ export async function GET(request: Request) { blocked: false, }, include: { - payment: true, - User: true, + payments: true, + User: { + include: { + devices: true, + }, + }, }, }); let devicesNeedingNotification = 0; @@ -31,7 +35,7 @@ export async function GET(request: Request) { for (const device of devices) { let expiryDate = new Date(); - const payment = device.payment; + const payment = device.payments[0]; expiryDate = addMonths( payment?.paidAt || new Date(), payment?.numberOfMonths || 0, @@ -43,8 +47,8 @@ export async function GET(request: Request) { const currentDate = new Date(); console.log("device name -> ", device.name); - console.log("paid date -> ", device.payment?.paidAt); - console.log("no of months paid -> ", device.payment?.numberOfMonths); + console.log("paid date -> ", device.payments[0]?.paidAt); + console.log("no of months paid -> ", device.payments[0]?.numberOfMonths); console.log("calculated expire date -> ", expiryDate); console.log("notification threshold -> ", notificationThreshold); console.log("current date -> ", currentDate); @@ -60,7 +64,7 @@ export async function GET(request: Request) { if (device.User?.phoneNumber) { await sendNotifySms( new Date(expiryDate), - device.User.phoneNumber, + device.User?.phoneNumber ?? "", device.name, ); devicesNeedingNotification++; diff --git a/components/devices-to-pay.tsx b/components/devices-to-pay.tsx index b102cdc..e6ce684 100644 --- a/components/devices-to-pay.tsx +++ b/components/devices-to-pay.tsx @@ -110,8 +110,8 @@ export default function DevicesToPay({ case res?.success === true: toast.success(res.message); break; - case res.success === false: - toast.error(res.message); + case res?.success === false: + toast.error(res?.message); break; default: toast.error("Unexpected error occurred."); diff --git a/hooks/use-person.ts b/hooks/use-person.ts deleted file mode 100644 index 4b97c5d..0000000 --- a/hooks/use-person.ts +++ /dev/null @@ -1,16 +0,0 @@ -import type { TNationalPerson } from "@/lib/types"; - -export default async function usePerson({ - idCard, -}: { idCard: string }): Promise { - 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; -} diff --git a/lib/person.ts b/lib/person.ts index c3f1144..e34e76c 100644 --- a/lib/person.ts +++ b/lib/person.ts @@ -1,10 +1,25 @@ "use server"; -import usePerson from "@/hooks/use-person"; +import type { TNationalPerson } from "@/lib/types"; import type { User } from "@prisma/client"; -export default async function VerifyUserDetails({ user }: { user: User }) { +export async function getNationalPerson({ + idCard, +}: { idCard: string }): Promise { + 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 nationalData = await usePerson({ idCard: user.id_card ?? "" }); + const nationalData = await getNationalPerson({ idCard: user.id_card ?? "" }); const dob = new Date(nationalData.dob); const age = new Date().getFullYear() - dob.getFullYear(); diff --git a/package.json b/package.json index 3449071..4e308d6 100644 --- a/package.json +++ b/package.json @@ -9,7 +9,7 @@ "lint": "next lint", "studio": "bunx prisma studio", "push": "bunx prisma db push", - "db-setup": "bunx prisma migrate deploy && bunx prisma generate && bunx prisma db push" + "db-setup": "bunx prisma migrate deploy && bunx prisma generate && bunx prisma db push" }, "prisma": { "seed": "ts-node --compiler-options {\"module\":\"CommonJS\"} prisma/seed.ts" @@ -66,5 +66,6 @@ "tailwindcss": "^3.4.17", "ts-node": "^10.9.2", "typescript": "^5.7.2" - } + }, + "packageManager": "yarn@1.22.22+sha512.a6b2f7906b721bba3d67d4aff083df04dad64c399707841b7acf00f6b133b7ac24255f2652fa22ae3534329dc6180534e98d17432037ff6fd140556e2bb3137e" }