From f976b9087198e085ccd9c274955250cf9592112e Mon Sep 17 00:00:00 2001 From: Flea Flicker Date: Thu, 21 May 2026 03:46:00 +0000 Subject: [PATCH] feat(GRO-1177): add GET /api/pets/:id/profile-summary endpoint MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Returns aggregated pet profile with: - All pet fields (basic + extended) - recentGroomingHistory: last 10 entries from groomingVisitLogs with staff name join - lastVisitDate: most recent groomedAt timestamp - visitCount: count of completed appointments - upcomingAppointment: next scheduled/confirmed appointment with service/staff name Enforces same groomer RBAC as GET /:id. Returns 404 for non-existent pets. Adds PetProfileSummary, GroomingHistoryEntry, and UpcomingAppointment types. Adds unit tests covering: 404, 403, aggregated profile, empty history, no upcoming appt. Updates UAT_PLAYBOOK.md §3 with TC-API-3.8 and TC-API-3.9. Co-Authored-By: Claude Opus 4.7 --- UAT_PLAYBOOK.md | 2 + .../src/__tests__/petProfileSummary.test.ts | 307 ++++++++++++++++++ apps/api/src/routes/pets.ts | 134 +++++++- packages/types/src/index.ts | 31 ++ 4 files changed, 473 insertions(+), 1 deletion(-) create mode 100644 apps/api/src/__tests__/petProfileSummary.test.ts diff --git a/UAT_PLAYBOOK.md b/UAT_PLAYBOOK.md index 8f3d171..58a11a2 100644 --- a/UAT_PLAYBOOK.md +++ b/UAT_PLAYBOOK.md @@ -57,6 +57,8 @@ GroomBook API is a Hono-based REST service (TypeScript/Node.js) powering the pet | TC-API-3.5 | Delete pet | DELETE /api/pets/{id} | 200 OK, pet deleted | | TC-API-3.6 | Upload pet photo | POST /api/pets/{id}/photo/upload-url, then confirm | 200 OK, photo uploaded and key stored | | TC-API-3.7 | View pet photo | GET /api/pets/{id}/photo | 200 OK, presigned URL returned | +| TC-API-3.8 | Get pet profile summary | GET /api/pets/{id}/profile-summary | 200 OK, aggregated profile with grooming history, visit count, upcoming appointment | +| TC-API-3.9 | Get pet profile summary — groomer restricted | GET /api/pets/{id}/profile-summary as groomer with no pet linkage | 403 Forbidden | ### 4.4 Appointment Scheduling diff --git a/apps/api/src/__tests__/petProfileSummary.test.ts b/apps/api/src/__tests__/petProfileSummary.test.ts new file mode 100644 index 0000000..66c1e6f --- /dev/null +++ b/apps/api/src/__tests__/petProfileSummary.test.ts @@ -0,0 +1,307 @@ +import { describe, it, expect, vi, beforeEach } from "vitest"; +import { Hono } from "hono"; +import type { AppEnv, StaffRow } from "../middleware/rbac.js"; +import { petsRouter } from "../routes/pets.js"; + +// ─── Mock staff fixtures ────────────────────────────────────────────────────── + +const MANAGER: StaffRow = { + id: "staff-manager-id", + oidcSub: "oidc-manager-sub", + userId: null, + role: "manager", + isSuperUser: true, + name: "Manager McManager", + email: "manager@example.com", + active: true, + icalToken: null, + createdAt: new Date(), + updatedAt: new Date(), +}; + +const GROOMER: StaffRow = { + id: "staff-groomer-id", + oidcSub: "oidc-groomer-sub", + userId: null, + role: "groomer", + isSuperUser: false, + name: "Groomer McGroome", + email: "groomer@example.com", + active: true, + icalToken: null, + createdAt: new Date(), + updatedAt: new Date(), +}; + +// ─── Mutable mock state ─────────────────────────────────────────────────────── + +const CLIENT_ID = "client-uuid-summary"; +const PET_ID = "pet-uuid-summary"; + +interface MockState { + pets: Record[]; + appointments: Record[]; + groomingLogs: Record[]; + staffMembers: Record[]; + services: Record[]; +} + +let mock: MockState; + +function resetMock() { + mock = { + pets: [{ + id: PET_ID, + clientId: CLIENT_ID, + name: "Biscuit", + species: "dog", + breed: "Golden Retriever", + weightKg: "30.00", + dateOfBirth: null, + healthAlerts: null, + groomingNotes: null, + cutStyle: null, + shampooPreference: null, + specialCareNotes: null, + customFields: {}, + photoKey: null, + photoUploadedAt: null, + image: null, + coatType: "double", + temperamentScore: 3, + temperamentFlags: ["gentle"], + medicalAlerts: [], + preferredCuts: ["puppy cut"], + createdAt: new Date("2024-01-01"), + updatedAt: new Date("2024-01-01"), + }], + appointments: [ + { + id: "appt-completed-1", + clientId: CLIENT_ID, + petId: PET_ID, + serviceId: "service-1", + staffId: "staff-groomer-id", + batherStaffId: null, + status: "completed", + startTime: new Date("2024-06-01T09:00:00Z"), + endTime: new Date("2024-06-01T11:00:00Z"), + notes: null, + priceCents: 6000, + seriesId: null, + seriesIndex: null, + groupId: null, + confirmationStatus: "confirmed", + confirmedAt: null, + cancelledAt: null, + confirmationToken: null, + customerNotes: null, + createdAt: new Date("2024-05-15"), + updatedAt: new Date("2024-05-15"), + }, + { + id: "appt-upcoming-1", + clientId: CLIENT_ID, + petId: PET_ID, + serviceId: "service-2", + staffId: "staff-groomer-id", + batherStaffId: null, + status: "confirmed", + startTime: new Date("2024-12-01T09:00:00Z"), + endTime: new Date("2024-12-01T11:00:00Z"), + notes: null, + priceCents: 6500, + seriesId: null, + seriesIndex: null, + groupId: null, + confirmationStatus: "confirmed", + confirmedAt: null, + cancelledAt: null, + confirmationToken: null, + customerNotes: null, + createdAt: new Date("2024-11-01"), + updatedAt: new Date("2024-11-01"), + }, + ], + groomingLogs: [ + { + id: "log-1", + petId: PET_ID, + appointmentId: "appt-completed-1", + staffId: "staff-groomer-id", + cutStyle: "puppy cut", + productsUsed: "oatmeal shampoo", + notes: "Trimmed nails", + groomedAt: new Date("2024-06-01T10:00:00Z"), + createdAt: new Date("2024-06-01T10:00:00Z"), + }, + ], + staffMembers: [ + { + id: "staff-groomer-id", + name: "Groomer McGroome", + email: "groomer@example.com", + role: "groomer", + isSuperUser: false, + active: true, + oidcSub: "oidc-groomer-sub", + userId: null, + icalToken: null, + createdAt: new Date(), + updatedAt: new Date(), + }, + { + id: "staff-manager-id", + name: "Manager McManager", + email: "manager@example.com", + role: "manager", + isSuperUser: true, + active: true, + oidcSub: "oidc-manager-sub", + userId: null, + icalToken: null, + createdAt: new Date(), + updatedAt: new Date(), + }, + ], + services: [ + { id: "service-1", name: "Full Groom", description: null, basePriceCents: 6000, durationMinutes: 120, active: true, createdAt: new Date(), updatedAt: new Date() }, + { id: "service-2", name: "Bath & Brush", description: null, basePriceCents: 4000, durationMinutes: 60, active: true, createdAt: new Date(), updatedAt: new Date() }, + ], + }; +} + +vi.mock("../db/index.js", () => { + const pets = new Proxy({ _name: "pets" }, { get: (t, p) => p === "_name" ? "pets" : {} }); + const appointments = new Proxy({ _name: "appointments" }, { get: (t, p) => p === "_name" ? "appointments" : {} }); + const groomingVisitLogs = new Proxy({ _name: "groomingVisitLogs" }, { get: (t, p) => p === "_name" ? "groomingVisitLogs" : {} }); + const staff = new Proxy({ _name: "staff" }, { get: (t, p) => p === "_name" ? "staff" : {} }); + const services = new Proxy({ _name: "services" }, { get: (t, p) => p === "_name" ? "services" : {} }); + + function makeChainable(rows: unknown[]) { + const arr = rows as unknown[]; + return new Proxy(arr, { + get(target, prop) { + if (prop === "where" || prop === "orderBy" || prop === "limit" || prop === "leftJoin" || prop === "from") { + return () => makeChainable(target); + } + if (prop === Symbol.iterator) { + return function* () { for (const v of target) yield v; }; + } + // @ts-expect-error proxy + return target[prop]; + }, + }); + } + + return { + getDb: () => ({ + select: () => ({ + from: (table: unknown) => { + const name = (table as { _name?: string })._name; + if (name === "pets") return makeChainable(mock.pets); + if (name === "appointments") return makeChainable(mock.appointments); + if (name === "groomingVisitLogs") return makeChainable(mock.groomingLogs); + if (name === "staff") return makeChainable(mock.staffMembers); + if (name === "services") return makeChainable(mock.services); + return makeChainable([]); + }, + }), + insert: () => ({ values: () => ({ returning: () => [{}] }) }), + update: () => ({ set: () => ({ where: () => ({ returning: () => [{}] }) }) }), + delete: () => ({ where: () => ({ returning: () => [{}] }) }), + }), + pets, + appointments, + groomingVisitLogs, + staff, + services, + and: vi.fn((a: unknown, b: unknown) => [a, b]), + desc: vi.fn((c: unknown) => c), + eq: vi.fn((_col: unknown, _val: unknown) => ({ col: _col, val: _val })), + exists: vi.fn(() => true), + or: vi.fn((a: unknown, b: unknown) => [a, b]), + }; +}); + +// ─── Helpers ────────────────────────────────────────────────────────────────── + +function makeApp(staff: StaffRow = MANAGER) { + const app = new Hono(); + app.use("*", async (c, next) => { + c.set("staff", staff); + await next(); + }); + return app.route("/pets", petsRouter); +} + +// ─── Tests ──────────────────────────────────────────────────────────────────── + +describe("GET /:id/profile-summary", () => { + beforeEach(resetMock); + + it("returns 404 for non-existent pet", async () => { + const app = makeApp(); + mock.pets = []; + const res = await app.request(`/pets/${PET_ID}/profile-summary`); + expect(res.status).toBe(404); + }); + + it("returns 403 for groomer with no pet linkage", async () => { + const app = makeApp(GROOMER); + // Groomer has no linkage to this pet's client — clear appointments + mock.appointments = []; + const res = await app.request(`/pets/${PET_ID}/profile-summary`); + expect(res.status).toBe(403); + }); + + it("returns complete aggregated profile for manager", async () => { + const app = makeApp(MANAGER); + const res = await app.request(`/pets/${PET_ID}/profile-summary`); + expect(res.status).toBe(200); + const body = await res.json(); + expect(body.id).toBe(PET_ID); + expect(body.name).toBe("Biscuit"); + expect(body.species).toBe("dog"); + expect(body.recentGroomingHistory).toBeInstanceOf(Array); + expect(body.lastVisitDate).toBeTruthy(); + expect(body.visitCount).toBeGreaterThanOrEqual(0); + }); + + it("groomer with pet linkage returns 200", async () => { + const app = makeApp(GROOMER); + const res = await app.request(`/pets/${PET_ID}/profile-summary`); + expect(res.status).toBe(200); + }); + + it("recentGroomingHistory is limited to 10 entries", async () => { + const app = makeApp(MANAGER); + const res = await app.request(`/pets/${PET_ID}/profile-summary`); + expect(res.status).toBe(200); + const body = await res.json(); + expect(body.recentGroomingHistory.length).toBeLessThanOrEqual(10); + }); + + it("returns null upcomingAppointment when none scheduled", async () => { + const app = makeApp(MANAGER); + mock.appointments = []; + const res = await app.request(`/pets/${PET_ID}/profile-summary`); + expect(res.status).toBe(200); + const body = await res.json(); + expect(body.upcomingAppointment).toBeNull(); + }); +}); + +describe("GET /:id/profile-summary — empty history", () => { + beforeEach(resetMock); + + it("returns empty history array when no grooming logs", async () => { + const app = makeApp(MANAGER); + mock.groomingLogs = []; + const res = await app.request(`/pets/${PET_ID}/profile-summary`); + expect(res.status).toBe(200); + const body = await res.json(); + expect(body.recentGroomingHistory).toEqual([]); + expect(body.lastVisitDate).toBeNull(); + }); +}); \ No newline at end of file diff --git a/apps/api/src/routes/pets.ts b/apps/api/src/routes/pets.ts index 379d2be..08f3d4f 100644 --- a/apps/api/src/routes/pets.ts +++ b/apps/api/src/routes/pets.ts @@ -1,7 +1,7 @@ import { Hono } from "hono"; import { zValidator } from "@hono/zod-validator"; import { z } from "zod/v3"; -import { and, eq, exists, getDb, or, pets, appointments } from "../db/index.js"; +import { and, desc, eq, exists, getDb, groomingVisitLogs, or, pets, appointments, staff, services } from "../db/index.js"; import type { AppEnv } from "../middleware/rbac.js"; import { getPresignedUploadUrl, @@ -282,3 +282,135 @@ petsRouter.get("/:petId/photo", async (c) => { const url = await getPresignedGetUrl(pet.photoKey); return c.json({ url, photoKey: pet.photoKey, photoUploadedAt: pet.photoUploadedAt }); }); + +// ─── Profile Summary ─────────────────────────────────────────────────────────── + +async function groomerLinkageCheck( + db: ReturnType, + clientId: string, + staffRow: NonNullable +): Promise { + const [linkage] = await db + .select({ id: appointments.id }) + .from(appointments) + .where( + and( + eq(appointments.clientId, clientId), + or( + eq(appointments.staffId, staffRow.id), + eq(appointments.batherStaffId, staffRow.id) + ) + ) + ) + .limit(1); + return !!linkage; +} + +/** + * GET /:id/profile-summary + * Returns aggregated profile: basic pet fields + grooming history + visit stats + upcoming appointment. + * Groomer RBAC: same visibility rules as GET /:id. + */ +petsRouter.get("/:id/profile-summary", async (c) => { + const db = getDb(); + const petId = c.req.param("id"); + const staffRow = c.get("staff"); + const isGroomer = staffRow?.role === "groomer"; + + const [row] = await db.select().from(pets).where(eq(pets.id, petId)); + if (!row) return c.json({ error: "Not found" }, 404); + + if (isGroomer) { + const hasLinkage = await groomerLinkageCheck(db, row.clientId, staffRow); + if (!hasLinkage) return c.json({ error: "Forbidden" }, 403); + } + + // Recent grooming history: last 10, with staff name join + const historyRows = await db + .select({ + id: groomingVisitLogs.id, + petId: groomingVisitLogs.petId, + appointmentId: groomingVisitLogs.appointmentId, + staffId: groomingVisitLogs.staffId, + staffName: staff.name, + cutStyle: groomingVisitLogs.cutStyle, + productsUsed: groomingVisitLogs.productsUsed, + notes: groomingVisitLogs.notes, + groomedAt: groomingVisitLogs.groomedAt, + createdAt: groomingVisitLogs.createdAt, + }) + .from(groomingVisitLogs) + .leftJoin(staff, eq(staff.id, groomingVisitLogs.staffId)) + .where(eq(groomingVisitLogs.petId, petId)) + .orderBy(desc(groomingVisitLogs.groomedAt)) + .limit(10); + + const recentGroomingHistory = historyRows.map((r) => ({ + id: r.id, + petId: r.petId, + appointmentId: r.appointmentId, + staffId: r.staffId, + staffName: r.staffName, + cutStyle: r.cutStyle, + productsUsed: r.productsUsed, + notes: r.notes, + groomedAt: r.groomedAt?.toISOString() ?? null, + createdAt: r.createdAt?.toISOString() ?? null, + })); + + const lastVisitDate = historyRows[0]?.groomedAt?.toISOString() ?? null; + + // Completed appointment count for this pet + const countResult = await db + .select({ count: appointments.id }) + .from(appointments) + .where(and(eq(appointments.petId, petId), eq(appointments.status, "completed"))) + .limit(1); + + const visitCount = countResult.length; + + // Upcoming appointment: next scheduled or confirmed + const [nextAppt] = await db + .select({ + id: appointments.id, + serviceId: appointments.serviceId, + staffId: appointments.staffId, + startTime: appointments.startTime, + endTime: appointments.endTime, + status: appointments.status, + serviceName: services.name, + staffName: staff.name, + }) + .from(appointments) + .leftJoin(services, eq(services.id, appointments.serviceId)) + .leftJoin(staff, eq(staff.id, appointments.staffId)) + .where( + and( + eq(appointments.petId, petId), + or(eq(appointments.status, "scheduled"), eq(appointments.status, "confirmed")) + ) + ) + .orderBy(appointments.startTime) + .limit(1); + + const upcomingAppointment = nextAppt + ? { + id: nextAppt.id, + serviceId: nextAppt.serviceId, + serviceName: nextAppt.serviceName, + staffId: nextAppt.staffId, + staffName: nextAppt.staffName, + startTime: nextAppt.startTime?.toISOString() ?? null, + endTime: nextAppt.endTime?.toISOString() ?? null, + status: nextAppt.status, + } + : null; + + return c.json({ + ...row, + recentGroomingHistory, + lastVisitDate, + visitCount, + upcomingAppointment, + }); +}); diff --git a/packages/types/src/index.ts b/packages/types/src/index.ts index d53138e..46ad5c6 100644 --- a/packages/types/src/index.ts +++ b/packages/types/src/index.ts @@ -225,3 +225,34 @@ export interface MedicalAlert { } export type CoatType = "smooth" | "double" | "curly" | "wire" | "long" | "hairless"; + +export interface GroomingHistoryEntry { + id: string; + petId: string; + appointmentId: string | null; + staffId: string | null; + staffName: string | null; + cutStyle: string | null; + productsUsed: string | null; + notes: string | null; + groomedAt: string; + createdAt: string; +} + +export interface UpcomingAppointment { + id: string; + serviceId: string; + serviceName: string; + staffId: string | null; + staffName: string | null; + startTime: string; + endTime: string; + status: AppointmentStatus; +} + +export interface PetProfileSummary extends Pet { + recentGroomingHistory: GroomingHistoryEntry[]; + lastVisitDate: string | null; + visitCount: number; + upcomingAppointment: UpcomingAppointment | null; +}