Status
Accepted (2026-12-02)
Context
Fofo's core promise is that every family member knows whether a pet has already eaten or received medication, with zero accidental double doses — even when two caregivers tap "Gave it now" within milliseconds or while one device is offline.
Decision
- Deterministic dose window key. Every scheduled medication administration computes a canonical
windowKey(YYYY-MM-DD#<slotIndex>in the family's timezone, or a configurable minimum interval bucket for interval-based schedules). - Database-enforced uniqueness with explicit exception path. The
dose_logtable enforces a partial unique index on(organization_id, medication_id, window_key) WHERE is_exception = false.- The first caregiver transaction inserts cleanly and records
givenByUserId,givenByName, andgivenAt. - Any concurrent or subsequent attempt for the same
(medication_id, window_key)withis_exception = falsehits either the pre-check or the unique constraint, rolls back safely, and returns a structuredDoubleDoseBlockederror carrying who gave the dose and when. - A caregiver can override the block only by submitting a non-empty written
exceptionReason, which inserts withis_exception = trueand records an audit event.
- The first caregiver transaction inserts cleanly and records
- Duplicate meal confirmation.
meal_logchecks whether a meal for the same(pet_id, meal_slot, meal_date)already exists. UnlessconfirmDuplicate: trueis passed, the API returns a structuredMealAlreadyLoggedresponse naming who fed the pet and when. - Offline queue and sync. Mobile stores pets, plans, and pending logs locally via the encrypted SQLite/storage queue and flushes idempotently when online. Server arbitration wins on dose-window conflicts; rejected offline duplicate doses surface a visible conflict notice rather than silently merging.
- Near-realtime family updates. Caregivers receive updates within 2 seconds via lightweight cursor polling (
sync_cursor/realtime) combined with TanStack Query invalidation on mutations, keeping Cloudflare Worker costs predictable while meeting the 2-second target.
Consequences
- 100 concurrent dose requests against the same medication window deterministically yield 1 committed dose and 99 blocked responses.
- Works identically on PostgreSQL 16–19 with RLS enabled and without external coordination services.