refactor: chagen get product count by userId

This commit is contained in:
Mahen 2026-02-21 10:35:44 +07:00
parent c2c5d433ce
commit d8bbb35228
2 changed files with 25 additions and 4 deletions

View File

@ -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"; import { NextResponse } from "next/server";
export const dynamic = "force-dynamic"; export const dynamic = "force-dynamic";
export async function GET() { export const GET = withAuth(async (_req, _context, session) => {
try { 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 }); return NextResponse.json({ count }, { status: 200 });
} catch (error) { } catch (error) {
@ -16,4 +19,4 @@ export async function GET() {
{ status: 500 }, { status: 500 },
); );
} }
} });

View File

@ -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;
};