fix: garden container plant count
This commit is contained in:
@@ -5,6 +5,12 @@ import { db } from "@/lib/db";
|
||||
import { getCurrentSession } from "@/lib/session";
|
||||
import { gardenCareLogs, gardenCareSchedules, gardenContainers, gardenPlants } from "../schema";
|
||||
|
||||
function toCount(value: number | string | bigint | null | undefined): number {
|
||||
if (value == null) return 0;
|
||||
const n = Number(value);
|
||||
return Number.isFinite(n) ? n : 0;
|
||||
}
|
||||
|
||||
export type ContainerDto = {
|
||||
id: string;
|
||||
householdId: string;
|
||||
@@ -34,29 +40,43 @@ export type PlantSummaryDto = {
|
||||
export async function listContainers(): Promise<ContainerDto[]> {
|
||||
const { household } = await getCurrentSession();
|
||||
|
||||
const rows = await db
|
||||
.select({
|
||||
id: gardenContainers.id,
|
||||
householdId: gardenContainers.householdId,
|
||||
name: gardenContainers.name,
|
||||
type: gardenContainers.type,
|
||||
locationNotes: gardenContainers.locationNotes,
|
||||
coverImageUrl: gardenContainers.coverImageUrl,
|
||||
images: gardenContainers.images,
|
||||
createdAt: gardenContainers.createdAt,
|
||||
updatedAt: gardenContainers.updatedAt,
|
||||
plantCount: sql<number>`count(${gardenPlants.id})::int`,
|
||||
})
|
||||
.from(gardenContainers)
|
||||
.leftJoin(gardenPlants, eq(gardenPlants.containerId, gardenContainers.id))
|
||||
.where(eq(gardenContainers.householdId, household.id))
|
||||
.groupBy(gardenContainers.id)
|
||||
.orderBy(gardenContainers.name);
|
||||
const [rows, countRows] = await Promise.all([
|
||||
db
|
||||
.select({
|
||||
id: gardenContainers.id,
|
||||
householdId: gardenContainers.householdId,
|
||||
name: gardenContainers.name,
|
||||
type: gardenContainers.type,
|
||||
locationNotes: gardenContainers.locationNotes,
|
||||
coverImageUrl: gardenContainers.coverImageUrl,
|
||||
images: gardenContainers.images,
|
||||
createdAt: gardenContainers.createdAt,
|
||||
updatedAt: gardenContainers.updatedAt,
|
||||
})
|
||||
.from(gardenContainers)
|
||||
.where(eq(gardenContainers.householdId, household.id))
|
||||
.orderBy(gardenContainers.name),
|
||||
db
|
||||
.select({
|
||||
containerId: gardenPlants.containerId,
|
||||
plantCount: sql<number>`count(*)::int`,
|
||||
})
|
||||
.from(gardenPlants)
|
||||
.where(eq(gardenPlants.householdId, household.id))
|
||||
.groupBy(gardenPlants.containerId),
|
||||
]);
|
||||
|
||||
const plantCountByContainer = new Map<string, number>();
|
||||
for (const row of countRows) {
|
||||
if (row.containerId) {
|
||||
plantCountByContainer.set(row.containerId, toCount(row.plantCount));
|
||||
}
|
||||
}
|
||||
|
||||
return rows.map((r) => ({
|
||||
...r,
|
||||
images: r.images ?? [],
|
||||
plantCount: r.plantCount ?? 0,
|
||||
plantCount: plantCountByContainer.get(r.id) ?? 0,
|
||||
createdAt: r.createdAt.toISOString(),
|
||||
updatedAt: r.updatedAt.toISOString(),
|
||||
}));
|
||||
@@ -76,7 +96,6 @@ export async function getContainer(id: string): Promise<ContainerDetailDto | nul
|
||||
images: gardenContainers.images,
|
||||
createdAt: gardenContainers.createdAt,
|
||||
updatedAt: gardenContainers.updatedAt,
|
||||
plantCount: sql<number>`(select count(*)::int from garden_plants where container_id = ${gardenContainers.id})`,
|
||||
})
|
||||
.from(gardenContainers)
|
||||
.where(and(eq(gardenContainers.id, id), eq(gardenContainers.householdId, household.id)))
|
||||
@@ -105,7 +124,7 @@ export async function getContainer(id: string): Promise<ContainerDetailDto | nul
|
||||
locationNotes: container.locationNotes,
|
||||
coverImageUrl: container.coverImageUrl,
|
||||
images: container.images ?? [],
|
||||
plantCount: container.plantCount ?? 0,
|
||||
plantCount: plants.length,
|
||||
createdAt: container.createdAt.toISOString(),
|
||||
updatedAt: container.updatedAt.toISOString(),
|
||||
plants,
|
||||
@@ -505,8 +524,8 @@ export async function getGardenOverviewStats(householdId: string): Promise<Garde
|
||||
.where(eq(gardenContainers.householdId, householdId)),
|
||||
]);
|
||||
|
||||
const plantCount = plantRow[0]?.count ?? 0;
|
||||
const containerCount = containerRow[0]?.count ?? 0;
|
||||
const plantCount = toCount(plantRow[0]?.count);
|
||||
const containerCount = toCount(containerRow[0]?.count);
|
||||
|
||||
const overdueRows = await db
|
||||
.select({ count: sql<number>`count(*)::int` })
|
||||
@@ -518,7 +537,7 @@ export async function getGardenOverviewStats(householdId: string): Promise<Garde
|
||||
lte(gardenCareSchedules.nextDueAt, sql`now()`),
|
||||
),
|
||||
);
|
||||
const overdueCount = overdueRows[0]?.count ?? 0;
|
||||
const overdueCount = toCount(overdueRows[0]?.count);
|
||||
|
||||
const nextRows = await db
|
||||
.select({
|
||||
|
||||
Reference in New Issue
Block a user