Files
petvity/lib/api/signal-cache.ts
T
GandClaude Fable 5 9ed916af14 feat(system): healthz, one-click /demo funnel, public demo profile, new-pet grace, score labeling (#17)
- /api/healthz: public DB-pinging healthcheck. /api/health is the auth-gated
  pet-data API, so deploys and uptime monitors had nothing honest to probe.
- /demo: signs a visitor into the shared demo account in one click; linked
  from the marketing hero ('Try the live demo') and nav across all locales.
  Also fixes two hardcoded English strings in HeroCTA.
- Demo seed claims the public handle /pets/luna (when free), so the
  public-influencer-profile feature has a live showcase.
- New-pet grace: a profile younger than the 7-day alert window with no data
  and nothing overdue reads healthy/'new profile' instead of an alarming
  Watch badge seconds after creation. Overdue vaccinations still escalate.
- Digital-twin score is labeled 'Emotional wellness' (9 locales) so it stops
  reading as a contradiction of the all-metric wellness % on the health page.

Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
2026-08-07 21:00:07 +00:00

65 lines
2.2 KiB
TypeScript

/**
* Refreshes the cached wellness signal on a pet row.
*
* Call after any mutation that can affect the signal:
* - health metrics upsert
* - vaccination create / update / delete
*/
import { and, desc, eq, gte } from "drizzle-orm";
import { getInstance } from "@/lib/db";
import { healthMetrics, pets, petSignalHistory, vaccinations } from "@/lib/db/schema";
import { computePetSignal } from "@/lib/domain/pet-signal";
import { SIGNAL_METRIC_WINDOW_DAYS } from "@/lib/config/pet-signal";
import { countOverdueVaccinations } from "@/lib/config/vaccinations";
import type { SpeciesId } from "@/lib/config/species";
export async function refreshSignalCache(petId: string, now = new Date()): Promise<void> {
const db = getInstance();
const sinceStr = new Date(now.getTime() - SIGNAL_METRIC_WINDOW_DAYS * 86400000)
.toISOString()
.slice(0, 10);
const todayStr = now.toISOString().slice(0, 10);
const [pet, recentMetrics, allVacc] = await Promise.all([
db.query.pets.findFirst({ where: eq(pets.id, petId) }),
db.query.healthMetrics.findMany({
where: and(eq(healthMetrics.petId, petId), gte(healthMetrics.date, sinceStr)),
orderBy: [desc(healthMetrics.date)],
}),
db.query.vaccinations.findMany({ where: eq(vaccinations.petId, petId) }),
]);
if (!pet) return; // pet deleted concurrently — nothing to update
const overdueCount = countOverdueVaccinations(allVacc, todayStr);
const signal = computePetSignal({
species: pet.species as SpeciesId,
recentMetrics,
overdueVaccinations: overdueCount,
petCreatedAt: pet.createdAt,
now,
});
// Only record a history row when the signal actually changes — not on every refresh.
// History insert is non-critical: a transient failure must not block the signal cache update.
if (pet.lastKnownSignal !== signal.signal) {
try {
await db.insert(petSignalHistory).values({
petId,
signal: signal.signal as "healthy" | "watch" | "concern",
reason: signal.reason,
reasonData: signal.reasonData,
source: "user_action",
recordedAt: now,
});
} catch {
// Non-fatal — cache update still proceeds
}
}
await db.update(pets).set({ lastKnownSignal: signal.signal }).where(eq(pets.id, petId));
}