mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-17 23:52:24 +00:00
fix(dashboard,medications): review findings before the cut
A fresh crisis-level blood-pressure reading outranks an overdue dose in the daily verdict, and once it ages past a day the dose leads again. The greeting returns to the page header whenever the daily band is hidden, and the settings toggle says it rides along. The nightly convergence pass honours the early grace, so a take recorded shortly before a window that starts at the dose time heals like the write path credits it. The early-take context line states a fact — last dose yesterday, last dose n days ago — instead of claiming an early intake after a late one, the quick-add default pick runs in the profile timezone, a dose line without a medication name reads cleanly, the early-grace constant has one home, and the Telegram confirm writes intake and snooze atomically again.
This commit is contained in:
1 parent
aaecd141ee
commit
b4d2d51d46
29 files changed
+601
-210
No files matched your search
@@ -14,10 +14,10 @@ import { join } from "node:path";
|
||||
* silhouettes use) a footprint-identical skeleton holds the band,
|
||||
* so the hero and the tiles swap to content in the SAME render
|
||||
* pass;
|
||||
* - the greeting paragraph left the header for the hero. The header
|
||||
* renders no name-bearing text at all any more, so the SSR pass
|
||||
* stays name-free by construction (the hero itself only mounts
|
||||
* once the snapshot resolved — post-hydration).
|
||||
* - the greeting paragraph lives in the hero when that renders; when
|
||||
* the hero does NOT render (snapshot flag off, or hidden via the
|
||||
* layout toggle) the header takes it back behind `showGreeting`,
|
||||
* fed from the page's hero gate — the greeting never disappears.
|
||||
*/
|
||||
const PAGE_PATH = join(process.cwd(), "src/app/page.tsx");
|
||||
const HEADER_PATH = join(
|
||||
@@ -69,9 +69,19 @@ describe("dashboard hero — page wiring", () => {
|
||||
describe("dashboard hero — header handoff", () => {
|
||||
const header = readFileSync(HEADER_PATH, "utf8");
|
||||
|
||||
it("the header dropped the greeting paragraph and its reservation", () => {
|
||||
expect(header).not.toContain("welcomeText");
|
||||
expect(header).not.toContain("min-h-5");
|
||||
it("the header keeps the greeting behind showGreeting (hero-hidden fallback)", () => {
|
||||
// The greeting renders here ONLY when the hero band does not — the
|
||||
// prop gates the paragraph, and the `min-h-5` line-box reservation
|
||||
// keeps the header stable through the post-hydration name swap.
|
||||
expect(header).toContain("showGreeting");
|
||||
expect(header).toMatch(/\{showGreeting\s*\?/);
|
||||
expect(header).toContain("min-h-5");
|
||||
expect(header).toContain('data-slot="dashboard-header-greeting"');
|
||||
});
|
||||
|
||||
it("the page feeds showGreeting from the inverted hero gate", () => {
|
||||
const src = readFileSync(PAGE_PATH, "utf8");
|
||||
expect(src).toMatch(/showGreeting=\{!heroVisible\}/);
|
||||
});
|
||||
|
||||
it("the header keeps the title + customize + add actions", () => {
|
||||
|
||||
@@ -137,7 +137,13 @@ beforeEach(() => {
|
||||
vi.mocked(prisma.telegramScheduledDeletion.createMany).mockResolvedValue({
|
||||
count: 0,
|
||||
} as never);
|
||||
vi.mocked(prisma.$transaction).mockResolvedValue([] as never);
|
||||
// Interactive transactions run their callback against the same mock
|
||||
// client (the route converges the intake write + snooze update inside
|
||||
// one transaction); batch arrays resolve like Promise.all.
|
||||
vi.mocked(prisma.$transaction).mockImplementation(((arg: unknown) =>
|
||||
typeof arg === "function"
|
||||
? (arg as (tx: unknown) => unknown)(prisma)
|
||||
: Promise.all(arg as Promise<unknown>[])) as never);
|
||||
vi.mocked(answerTelegramCallbackQuery).mockResolvedValue(undefined as never);
|
||||
vi.mocked(deleteMessage).mockResolvedValue(undefined as never);
|
||||
vi.mocked(sendTelegramMessage).mockResolvedValue({
|
||||
@@ -249,6 +255,8 @@ describe("Telegram webhook — callback dispatch", () => {
|
||||
where: { id: "med-1" },
|
||||
data: { snoozedUntil: null },
|
||||
});
|
||||
// Intake write + snooze reset commit atomically.
|
||||
expect(prisma.$transaction).toHaveBeenCalledTimes(1);
|
||||
|
||||
expect(answerTelegramCallbackQuery).toHaveBeenCalledTimes(1);
|
||||
expect(deleteMessage).toHaveBeenCalledWith(
|
||||
@@ -319,6 +327,8 @@ describe("Telegram webhook — callback dispatch", () => {
|
||||
.snoozedUntil;
|
||||
expect(snoozedUntil.getHours()).toBe(23);
|
||||
expect(snoozedUntil.getMinutes()).toBe(59);
|
||||
// Skip row + rest-of-day snooze commit atomically.
|
||||
expect(prisma.$transaction).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it("'ack:<medId>' answers the callback without creating an intake event", async () => {
|
||||
|
||||
@@ -259,40 +259,53 @@ async function markMedicationTaken(
|
||||
canonicalSlot = attribution.slotInstant;
|
||||
}
|
||||
|
||||
if (canonicalSlot) {
|
||||
const applied = await applyCanonicalSlotWrite({
|
||||
client: prisma,
|
||||
userId,
|
||||
medicationId: medication.id,
|
||||
canonicalSlot,
|
||||
takenAt,
|
||||
skipped: false,
|
||||
isExplicitTaken: true,
|
||||
isExplicitSkip: false,
|
||||
idempotencyKey,
|
||||
createSource: "REMINDER",
|
||||
attributionSource: "AUTO",
|
||||
});
|
||||
scheduledFor = applied.row.scheduledFor;
|
||||
} else {
|
||||
// Ad-hoc / PRN — standalone row under the documented contract
|
||||
// (`scheduledFor = takenAt`).
|
||||
const created = await prisma.medicationIntakeEvent.create({
|
||||
data: {
|
||||
// The intake write and the snooze reset commit atomically — a
|
||||
// recorded dose must never leave the medication snoozed-out of its
|
||||
// own reminders, and a snooze reset must never land without the dose
|
||||
// (the pre-band shape used a batch transaction here; the slot
|
||||
// convergence dropped it). The canonical slot write accepts the
|
||||
// transaction client; its P2002 race-recovery cannot continue inside
|
||||
// an aborted Postgres transaction, so the rare create race rolls the
|
||||
// whole write back instead of converging in place — Telegram
|
||||
// redelivers the non-200 update and the retry finds the raced row.
|
||||
scheduledFor = await prisma.$transaction(async (tx) => {
|
||||
let slotScheduledFor: Date;
|
||||
if (canonicalSlot) {
|
||||
const applied = await applyCanonicalSlotWrite({
|
||||
client: tx,
|
||||
userId,
|
||||
medicationId: medication.id,
|
||||
scheduledFor: takenAt,
|
||||
canonicalSlot,
|
||||
takenAt,
|
||||
skipped: false,
|
||||
source: "REMINDER",
|
||||
isExplicitTaken: true,
|
||||
isExplicitSkip: false,
|
||||
idempotencyKey,
|
||||
},
|
||||
createSource: "REMINDER",
|
||||
attributionSource: "AUTO",
|
||||
});
|
||||
slotScheduledFor = applied.row.scheduledFor;
|
||||
} else {
|
||||
// Ad-hoc / PRN — standalone row under the documented contract
|
||||
// (`scheduledFor = takenAt`).
|
||||
const created = await tx.medicationIntakeEvent.create({
|
||||
data: {
|
||||
userId,
|
||||
medicationId: medication.id,
|
||||
scheduledFor: takenAt,
|
||||
takenAt,
|
||||
skipped: false,
|
||||
source: "REMINDER",
|
||||
idempotencyKey,
|
||||
},
|
||||
});
|
||||
slotScheduledFor = created.scheduledFor;
|
||||
}
|
||||
await tx.medication.update({
|
||||
where: { id: medication.id },
|
||||
data: { snoozedUntil: null },
|
||||
});
|
||||
scheduledFor = created.scheduledFor;
|
||||
}
|
||||
await prisma.medication.update({
|
||||
where: { id: medication.id },
|
||||
data: { snoozedUntil: null },
|
||||
return slotScheduledFor;
|
||||
});
|
||||
// The confirmation must reflect on the next read (today feed, card
|
||||
// pill, compliance) rather than wait out the cache TTL.
|
||||
@@ -476,37 +489,46 @@ async function handleCallback(update: TelegramUpdate) {
|
||||
canonicalSlot = attribution.slotInstant;
|
||||
}
|
||||
|
||||
if (canonicalSlot) {
|
||||
const applied = await applyCanonicalSlotWrite({
|
||||
client: prisma,
|
||||
userId: user.id,
|
||||
medicationId: medication.id,
|
||||
canonicalSlot,
|
||||
takenAt: null,
|
||||
skipped: true,
|
||||
isExplicitTaken: false,
|
||||
isExplicitSkip: true,
|
||||
idempotencyKey,
|
||||
createSource: "REMINDER",
|
||||
});
|
||||
skippedScheduledFor = applied.row.scheduledFor;
|
||||
} else {
|
||||
const created = await prisma.medicationIntakeEvent.create({
|
||||
data: {
|
||||
// Atomic like the take path: the skip row and the rest-of-day
|
||||
// snooze commit together, so a failure can never leave the user
|
||||
// re-reminded for a recorded skip or silenced without one. Same
|
||||
// P2002 trade-off as the take path — the rare create race rolls
|
||||
// back and Telegram's redelivery converges on the raced row.
|
||||
skippedScheduledFor = await prisma.$transaction(async (tx) => {
|
||||
let slotScheduledFor: Date;
|
||||
if (canonicalSlot) {
|
||||
const applied = await applyCanonicalSlotWrite({
|
||||
client: tx,
|
||||
userId: user.id,
|
||||
medicationId: medication.id,
|
||||
scheduledFor: skipMoment,
|
||||
canonicalSlot,
|
||||
takenAt: null,
|
||||
skipped: true,
|
||||
source: "REMINDER",
|
||||
isExplicitTaken: false,
|
||||
isExplicitSkip: true,
|
||||
idempotencyKey,
|
||||
},
|
||||
createSource: "REMINDER",
|
||||
});
|
||||
slotScheduledFor = applied.row.scheduledFor;
|
||||
} else {
|
||||
const created = await tx.medicationIntakeEvent.create({
|
||||
data: {
|
||||
userId: user.id,
|
||||
medicationId: medication.id,
|
||||
scheduledFor: skipMoment,
|
||||
takenAt: null,
|
||||
skipped: true,
|
||||
source: "REMINDER",
|
||||
idempotencyKey,
|
||||
},
|
||||
});
|
||||
slotScheduledFor = created.scheduledFor;
|
||||
}
|
||||
await tx.medication.update({
|
||||
where: { id: medication.id },
|
||||
data: { snoozedUntil: endOfDay },
|
||||
});
|
||||
skippedScheduledFor = created.scheduledFor;
|
||||
}
|
||||
await prisma.medication.update({
|
||||
where: { id: medication.id },
|
||||
data: { snoozedUntil: endOfDay },
|
||||
return slotScheduledFor;
|
||||
});
|
||||
invalidateUserMedications(user.id, { evict: true });
|
||||
}
|
||||
|
||||
+8
-2
@@ -711,13 +711,19 @@ export default function DashboardPage() {
|
||||
// with it). The hero gates on the layout's `heroVisible` flag
|
||||
// (default on; anything but the literal `false` renders) and holds
|
||||
// a footprint-identical skeleton while the snapshot is in flight,
|
||||
// so the band swaps in place — no growth, no greeting flash.
|
||||
// so the band swaps in place — no growth, no greeting flash. When
|
||||
// the hero does NOT render (toggle off, or snapshot flag off) the
|
||||
// header takes the greeting back — it must never disappear with the
|
||||
// band.
|
||||
const heroVisible = snapshotEnabled && layout.heroVisible !== false;
|
||||
const heroSnapshot = snapshotQuery.data;
|
||||
|
||||
return (
|
||||
<div className="space-y-6">
|
||||
<DashboardHeader onQuickEntry={setQuickEntryDialog} />
|
||||
<DashboardHeader
|
||||
onQuickEntry={setQuickEntryDialog}
|
||||
showGreeting={!heroVisible}
|
||||
/>
|
||||
|
||||
{heroVisible &&
|
||||
(primaryLoading || !heroSnapshot ? (
|
||||
|
||||
Reference in new issue
Block a user