@@ -12,7 +12,8 @@ export async function GET(req: Request) {
|
|||||||
const q = searchParams.get("q") ?? "";
|
const q = searchParams.get("q") ?? "";
|
||||||
const status = searchParams.get("status");
|
const status = searchParams.get("status");
|
||||||
|
|
||||||
const projects = await db.project.findMany({
|
const [projects, shotGroups] = await Promise.all([
|
||||||
|
db.project.findMany({
|
||||||
where: {
|
where: {
|
||||||
AND: [
|
AND: [
|
||||||
q
|
q
|
||||||
@@ -30,9 +31,32 @@ export async function GET(req: Request) {
|
|||||||
include: {
|
include: {
|
||||||
client: { select: { id: true, company: true } },
|
client: { select: { id: true, company: true } },
|
||||||
producer: { select: { id: true, name: true, image: true } },
|
producer: { select: { id: true, name: true, image: true } },
|
||||||
_count: { select: { shots: true } },
|
|
||||||
},
|
},
|
||||||
});
|
}),
|
||||||
|
// Single query to get shot counts grouped by project + status
|
||||||
|
db.shot.groupBy({
|
||||||
|
by: ["projectId", "status"],
|
||||||
|
_count: { id: true },
|
||||||
|
}),
|
||||||
|
]);
|
||||||
|
|
||||||
return NextResponse.json({ projects });
|
// Build shotStats map keyed by projectId
|
||||||
|
const statsMap = new Map<string, { total: number; approved: number; inProgress: number }>();
|
||||||
|
for (const row of shotGroups) {
|
||||||
|
const existing = statsMap.get(row.projectId) ?? { total: 0, approved: 0, inProgress: 0 };
|
||||||
|
existing.total += row._count.id;
|
||||||
|
if (row.status === "COMPLETE") {
|
||||||
|
existing.approved += row._count.id;
|
||||||
|
} else if (row.status !== "WAITING") {
|
||||||
|
existing.inProgress += row._count.id;
|
||||||
|
}
|
||||||
|
statsMap.set(row.projectId, existing);
|
||||||
|
}
|
||||||
|
|
||||||
|
const projectsWithStats = projects.map((p) => ({
|
||||||
|
...p,
|
||||||
|
shotStats: statsMap.get(p.id) ?? { total: 0, approved: 0, inProgress: 0 },
|
||||||
|
}));
|
||||||
|
|
||||||
|
return NextResponse.json({ projects: projectsWithStats });
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user