From d8bbb352287d0d96ad9a1ea99395ab742bf3be11 Mon Sep 17 00:00:00 2001 From: Mahen Date: Sat, 21 Feb 2026 10:35:44 +0700 Subject: [PATCH] refactor: chagen get product count by userId --- src/app/api/product/route.ts | 11 +++++++---- src/services/product.service.ts | 18 ++++++++++++++++++ 2 files changed, 25 insertions(+), 4 deletions(-) create mode 100644 src/services/product.service.ts diff --git a/src/app/api/product/route.ts b/src/app/api/product/route.ts index 271f7d3..7efb77d 100644 --- a/src/app/api/product/route.ts +++ b/src/app/api/product/route.ts @@ -1,11 +1,14 @@ -import prisma from "@/lib/prisma"; +import { withAuth } from "@/lib/withAuth"; +import { productService } from "@/src/services/product.service"; import { NextResponse } from "next/server"; export const dynamic = "force-dynamic"; -export async function GET() { +export const GET = withAuth(async (_req, _context, session) => { try { - const count = await prisma.product.count(); + const email = session.user?.email as string; + + const count = await productService(email); return NextResponse.json({ count }, { status: 200 }); } catch (error) { @@ -16,4 +19,4 @@ export async function GET() { { status: 500 }, ); } -} +}); diff --git a/src/services/product.service.ts b/src/services/product.service.ts new file mode 100644 index 0000000..e2b85c8 --- /dev/null +++ b/src/services/product.service.ts @@ -0,0 +1,18 @@ +import prisma from "@/lib/prisma"; + +export const productService = async (email: string) => { + const user = await prisma.user.findUnique({ + where: { email }, + select: { id: true }, + }); + + if (!user) return null; + + const totalProducts = await prisma.analysis.count({ + where: { + userId: user.id, + }, + }); + + return totalProducts; +};