mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-19 00:12:18 +00:00
feat(mental-health): accept client source + externalId with durable dedup
The screener POST hardcoded source=WEB and ignored any client-supplied externalId, so the native client could not tag provenance and an outbox replay beyond the 24h idempotency-key window minted duplicate administrations plus duplicate trend points. Accept an optional closed source enum (WEB|IOS, default WEB) and an optional bounded externalId, and persist both. Deduplicate on (userId, externalId) when present: the route returns the existing administration on replay, and a partial unique index over (user_id, external_id) WHERE external_id IS NOT NULL (migration 0221) backstops a concurrent insert race (P2002 resolved to the winner). The derived *_SCORE measurement keeps its stable per-assessment externalId, so dedup at the assessment level prevents duplicate trend points. Item answers stay encrypted and out of AI/MCP/export. Regenerate OpenAPI.
This commit is contained in:
1 parent
e93f74381e
commit
930cb227eb
6 files changed
+250
-32
No files matched your search
@@ -0,0 +1,22 @@
|
||||
-- v1.25 — iOS parity: durable dedup for mental-health screener administrations.
|
||||
--
|
||||
-- The native client queues completed check-ins in an outbox and replays them on
|
||||
-- reconnect. The 24h idempotency-key window covers a fast retry, but a queued
|
||||
-- entry can replay long after that window closes. Without a stable client key
|
||||
-- the replay would mint a duplicate administration AND a duplicate *_SCORE trend
|
||||
-- point.
|
||||
--
|
||||
-- The `source` + `external_id` columns already exist (migration 0213). This adds
|
||||
-- the DB-level backstop: a partial UNIQUE index over `(user_id, external_id)`
|
||||
-- restricted to non-NULL externalIds. It deduplicates across sources (a repeat
|
||||
-- externalId collides regardless of WEB vs IOS), while NULL externalIds (the
|
||||
-- web path that supplies none) stay unconstrained by the partial predicate.
|
||||
--
|
||||
-- The route pre-checks `(user_id, external_id)` and returns the existing row;
|
||||
-- this index is the race backstop (a concurrent insert surfaces P2002, which
|
||||
-- the route catches and resolves to the winning row). Idempotent + forward-only;
|
||||
-- reversible with `DROP INDEX IF EXISTS "mental_health_assessments_user_external_id_unique"`.
|
||||
|
||||
CREATE UNIQUE INDEX IF NOT EXISTS "mental_health_assessments_user_external_id_unique"
|
||||
ON "mental_health_assessments" ("user_id", "external_id")
|
||||
WHERE "external_id" IS NOT NULL;
|
||||
@@ -1328,6 +1328,12 @@ model MentalHealthAssessment {
|
||||
@@unique([userId, source, externalId])
|
||||
@@index([userId, instrument, takenAt])
|
||||
@@index([userId, updatedAt, id])
|
||||
// `mental_health_assessments_user_external_id_unique` is a partial UNIQUE
|
||||
// index over `(user_id, external_id) WHERE external_id IS NOT NULL` — see
|
||||
// migration 0221. It deduplicates an outbox replay across sources (a repeat
|
||||
// client externalId returns the existing administration). Prisma can't express
|
||||
// a partial index in the schema language, so it lives in raw SQL and is
|
||||
// intentionally omitted here.
|
||||
@@map("mental_health_assessments")
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user