fix(api): repair root src/routes/pets.ts visit-count query (GRO-1945)
CI / Test (pull_request) Successful in 11s
CI / Lint & Typecheck (pull_request) Successful in 14s
CI / Build & Push Docker Images (pull_request) Successful in 1m4s

Use sql\`count(*)::int\` instead of selecting appointments.id, which was
causing TS2339 under noUncheckedIndexedAccess (arr[0] is T | undefined).

Import sql from @groombook/db. Use countRow?.count ?? 0 to stay
noUncheckedIndexedAccess-safe.

Matches the working implementation in apps/api/src/routes/pets.ts:365.

Co-Authored-By: Paperclip <noreply@paperclip.ing>
This commit is contained in:
Flea Flicker
2026-05-30 03:21:28 +00:00
parent 280c699d0d
commit a14bb5e17d
+6 -5
View File
@@ -12,6 +12,7 @@ import {
appointments,
staff,
services,
sql,
} from "@groombook/db";
import type { AppEnv } from "../middleware/rbac.js";
import {
@@ -153,11 +154,11 @@ petsRouter.get("/:id/profile-summary", async (c) => {
.limit(10);
// Visit count (completed appointments)
const [{ count }] = await db
.select({ count: appointments.id })
const [countRow] = await db
.select({ count: sql<number>`count(*)::int` })
.from(appointments)
.where(and(eq(appointments.petId, petId), eq(appointments.status, "completed")))
.limit(1);
.where(and(eq(appointments.petId, petId), eq(appointments.status, "completed")));
const visitCount = countRow?.count ?? 0;
// Upcoming appointment (next scheduled or confirmed)
const [upcoming] = await db
@@ -195,7 +196,7 @@ petsRouter.get("/:id/profile-summary", async (c) => {
serviceName: h.serviceName,
staffName: h.staffName,
})),
visitCount: Number(count ?? 0),
visitCount,
upcomingAppointment: upcoming
? {
id: upcoming.id,