mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-17 15:42:24 +00:00
feat(coach,admin): finish the self-context and invite surfaces end to end
Invites move into their own admin section with a real ledger: status badges, expiry, uses, a redemption history of who joined when (new redemption table, revocation is now soft so history survives), and an honest one-time link/QR moment. The assistant self-context grows structured fields for conditions, allergies and focus — encrypted like the free text — merges age and gender from the profile into one fenced prompt block, and asks up to three targeted follow-up questions that surface as tappable chips above the coach composer. A fourth nudge trigger gently suggests a check-up when the self-context is stale and the coach is actually in use.
This commit is contained in:
1 parent
9fe5673251
commit
5b75184649
32 files changed
+2648
-423
No files matched your search
@@ -0,0 +1,44 @@
|
||||
-- v1.16.0 — invite redemption ledger + soft revocation.
|
||||
--
|
||||
-- `invite_tokens.used_by` keeps only the LAST consumer (multi-use
|
||||
-- invites overwrite it); `invite_redemptions` carries one row per
|
||||
-- admitted signup so the admin table can show the full history with
|
||||
-- timestamps. `user_id` survives account deletion as NULL — the row
|
||||
-- still documents that a use was consumed.
|
||||
--
|
||||
-- `revoked_at` turns revocation into a soft state: a revoked invite
|
||||
-- keeps its history visible instead of vanishing, and the consume path
|
||||
-- refuses it like an expired one. Additive + non-destructive.
|
||||
ALTER TABLE "invite_tokens" ADD COLUMN "revoked_at" TIMESTAMP(3);
|
||||
|
||||
CREATE TABLE "invite_redemptions" (
|
||||
"id" TEXT NOT NULL,
|
||||
"invite_id" TEXT NOT NULL,
|
||||
"user_id" TEXT,
|
||||
"redeemed_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
|
||||
CONSTRAINT "invite_redemptions_pkey" PRIMARY KEY ("id")
|
||||
);
|
||||
|
||||
CREATE INDEX "invite_redemptions_invite_id_idx" ON "invite_redemptions"("invite_id");
|
||||
|
||||
ALTER TABLE "invite_redemptions"
|
||||
ADD CONSTRAINT "invite_redemptions_invite_id_fkey"
|
||||
FOREIGN KEY ("invite_id") REFERENCES "invite_tokens"("id") ON DELETE CASCADE ON UPDATE CASCADE;
|
||||
|
||||
ALTER TABLE "invite_redemptions"
|
||||
ADD CONSTRAINT "invite_redemptions_user_id_fkey"
|
||||
FOREIGN KEY ("user_id") REFERENCES "users"("id") ON DELETE SET NULL ON UPDATE CASCADE;
|
||||
|
||||
-- Backfill: every invite that already recorded a consumer becomes one
|
||||
-- redemption row, stamped with the invite's `used_at`. Invites whose
|
||||
-- consumer account was deleted (`used_by` NULL but `uses` > 0) cannot
|
||||
-- be reconstructed and stay ledger-less — the use counter still counts.
|
||||
INSERT INTO "invite_redemptions" ("id", "invite_id", "user_id", "redeemed_at")
|
||||
SELECT
|
||||
'invred_' || md5(random()::text || clock_timestamp()::text),
|
||||
"id",
|
||||
"used_by",
|
||||
COALESCE("used_at", "created_at")
|
||||
FROM "invite_tokens"
|
||||
WHERE "used_by" IS NOT NULL;
|
||||
@@ -0,0 +1,17 @@
|
||||
-- v1.16.0 — structured self-context fields on the health profile.
|
||||
--
|
||||
-- Three short structured companions to the free-text "about me"
|
||||
-- (chronic conditions, allergies/intolerances, what the Coach should
|
||||
-- watch), each encrypted at rest through the shared Bytes codec and
|
||||
-- capped at 500 chars BEFORE encryption. Age/gender deliberately do
|
||||
-- NOT live here — they come from the users table and are merged into
|
||||
-- the prompt block at assembly time.
|
||||
--
|
||||
-- `pending_questions_encrypted` carries up to 3 clarifying questions
|
||||
-- the server derives after a save (encrypted JSON string[]); the Coach
|
||||
-- composer renders them as tappable suggestion chips. Additive +
|
||||
-- non-destructive.
|
||||
ALTER TABLE "user_health_profiles" ADD COLUMN "conditions_encrypted" BYTEA;
|
||||
ALTER TABLE "user_health_profiles" ADD COLUMN "allergies_encrypted" BYTEA;
|
||||
ALTER TABLE "user_health_profiles" ADD COLUMN "coach_focus_encrypted" BYTEA;
|
||||
ALTER TABLE "user_health_profiles" ADD COLUMN "pending_questions_encrypted" BYTEA;
|
||||
+40
-2
@@ -520,6 +520,8 @@ model User {
|
||||
// multi-use invite).
|
||||
invitesCreated InviteToken[] @relation("InviteCreator")
|
||||
invitesUsed InviteToken[] @relation("InviteConsumer")
|
||||
// v1.16.0 — per-signup invite redemption ledger (multi-use history).
|
||||
inviteRedemptions InviteRedemption[] @relation("InviteRedemptions")
|
||||
|
||||
@@map("users")
|
||||
}
|
||||
@@ -1851,14 +1853,37 @@ model InviteToken {
|
||||
usedBy String? @map("used_by")
|
||||
maxUses Int @default(1) @map("max_uses")
|
||||
uses Int @default(0)
|
||||
// v1.16.0 — soft revocation. A revoked invite keeps its redemption
|
||||
// history visible in the admin table instead of vanishing; the
|
||||
// consume path refuses it like an expired one.
|
||||
revokedAt DateTime? @map("revoked_at")
|
||||
|
||||
creator User @relation("InviteCreator", fields: [createdBy], references: [id], onDelete: Cascade)
|
||||
consumer User? @relation("InviteConsumer", fields: [usedBy], references: [id], onDelete: SetNull)
|
||||
creator User @relation("InviteCreator", fields: [createdBy], references: [id], onDelete: Cascade)
|
||||
consumer User? @relation("InviteConsumer", fields: [usedBy], references: [id], onDelete: SetNull)
|
||||
redemptions InviteRedemption[]
|
||||
|
||||
@@index([expiresAt])
|
||||
@@map("invite_tokens")
|
||||
}
|
||||
|
||||
// v1.16.0 — one row per signup an invite admitted. `InviteToken.usedBy`
|
||||
// keeps only the LAST consumer (it overwrites on multi-use invites);
|
||||
// this ledger carries the full history so the admin table can show
|
||||
// every redemption with its timestamp. `userId` survives account
|
||||
// deletion as NULL — the row still documents that a use was consumed.
|
||||
model InviteRedemption {
|
||||
id String @id @default(cuid())
|
||||
inviteId String @map("invite_id")
|
||||
userId String? @map("user_id")
|
||||
redeemedAt DateTime @default(now()) @map("redeemed_at")
|
||||
|
||||
invite InviteToken @relation(fields: [inviteId], references: [id], onDelete: Cascade)
|
||||
user User? @relation("InviteRedemptions", fields: [userId], references: [id], onDelete: SetNull)
|
||||
|
||||
@@index([inviteId])
|
||||
@@map("invite_redemptions")
|
||||
}
|
||||
|
||||
// ─── Refresh Tokens (native client short-lived access + rotating refresh) ─
|
||||
|
||||
model RefreshToken {
|
||||
@@ -3080,6 +3105,19 @@ model UserHealthProfile {
|
||||
// Free text, hard-capped at 4 000 chars by the route's Zod schema
|
||||
// BEFORE encryption. NULL = cleared / never written.
|
||||
aboutMeEncrypted Bytes? @map("about_me_encrypted")
|
||||
// v1.16.0 — structured self-context companions to the free text.
|
||||
// Short answers (each capped at 500 chars BEFORE encryption).
|
||||
// Age/gender deliberately do NOT live here — they come from the User
|
||||
// profile and are merged into the prompt block at assembly time so
|
||||
// the data is never duplicated.
|
||||
conditionsEncrypted Bytes? @map("conditions_encrypted")
|
||||
allergiesEncrypted Bytes? @map("allergies_encrypted")
|
||||
coachFocusEncrypted Bytes? @map("coach_focus_encrypted")
|
||||
// v1.16.0 — up to 3 clarifying questions the server derives after a
|
||||
// save (AI-generated when a provider + budget allow, deterministic
|
||||
// hints otherwise). Encrypted JSON string[]; the Coach composer
|
||||
// renders them as tappable suggestion chips. NULL = none pending.
|
||||
pendingQuestionsEncrypted Bytes? @map("pending_questions_encrypted")
|
||||
createdAt DateTime @default(now()) @map("created_at")
|
||||
updatedAt DateTime @updatedAt @map("updated_at")
|
||||
|
||||
|
||||
Reference in new issue
Block a user