mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-17 07:42:23 +00:00
Merge branch 'fix/v1438-db' into release/v1.43.8
This commit is contained in:
commit
1d9758e3e8
91 files changed
+2107
-2058
No files matched your search
@@ -75,7 +75,6 @@ export const WIPE_MODELS = [
|
||||
"MedicationSideEffect",
|
||||
"MedicationInventoryItem",
|
||||
"MedicationIntakeImportJob",
|
||||
"MedicationComplianceRollup",
|
||||
// The account's own category names (v1.40, #1041). No child rows; the
|
||||
// medications that name one go with their own entry.
|
||||
"MedicationCategoryLabel",
|
||||
|
||||
@@ -44,7 +44,7 @@ vi.mock("@/lib/logging/context", () => ({ annotate: vi.fn() }));
|
||||
vi.mock("@/lib/rollups/mood-rollups", () => ({
|
||||
recomputeUserMoodRollups: vi.fn(),
|
||||
}));
|
||||
vi.mock("@/lib/rollups/medication-compliance-rollups", () => ({
|
||||
vi.mock("@/lib/medications/dose-day-refresh", () => ({
|
||||
recomputeUserMedicationCompliance: vi.fn(),
|
||||
MEDICATION_COMPLIANCE_BACKFILL_DAYS: 30,
|
||||
}));
|
||||
|
||||
@@ -66,7 +66,7 @@ vi.mock("@/lib/cache/invalidate", () => ({ invalidateUserData: vi.fn() }));
|
||||
vi.mock("@/lib/rollups/mood-rollups", () => ({
|
||||
recomputeUserMoodRollups: vi.fn(),
|
||||
}));
|
||||
vi.mock("@/lib/rollups/medication-compliance-rollups", () => ({
|
||||
vi.mock("@/lib/medications/dose-day-refresh", () => ({
|
||||
recomputeUserMedicationCompliance: vi.fn(),
|
||||
MEDICATION_COMPLIANCE_BACKFILL_DAYS: 30,
|
||||
}));
|
||||
|
||||
@@ -655,8 +655,6 @@ export const COVERAGE_PENDING: Readonly<Record<string, string>> = {
|
||||
export const DERIVED_MODELS: Readonly<Record<string, string>> = {
|
||||
MeasurementRollup:
|
||||
"A per-day, per-metric reconstruction of `Measurement`. The boot-time rollup backfill rebuilds it for an account that has none.",
|
||||
MedicationComplianceRollup:
|
||||
"Recomputed from `MedicationIntakeEvent` against the schedule by the compliance analytics tier.",
|
||||
MoodEntryRollup:
|
||||
"A per-day reconstruction of `MoodEntry`, rebuilt by the same rollup tier that rebuilds the measurement one.",
|
||||
MoodPrediction:
|
||||
|
||||
@@ -80,10 +80,6 @@ import {
|
||||
type BackupSummary,
|
||||
} from "@/lib/validations/backup-summary";
|
||||
import { recomputeUserMoodRollups } from "@/lib/rollups/mood-rollups";
|
||||
import {
|
||||
recomputeUserMedicationCompliance,
|
||||
MEDICATION_COMPLIANCE_BACKFILL_DAYS,
|
||||
} from "@/lib/rollups/medication-compliance-rollups";
|
||||
import { recomputeUserRollups } from "@/lib/rollups/measurement-rollups";
|
||||
import { restoreCycleData } from "@/lib/cycle/backup";
|
||||
import {
|
||||
@@ -3058,33 +3054,6 @@ export async function restoreBackup(
|
||||
}
|
||||
}
|
||||
|
||||
// v1.4.39 — re-fold the medication-compliance rollup tier
|
||||
// from the restored intake events. The medication delete inside the
|
||||
// transaction already cascaded the existing rollup partition for
|
||||
// this owner via the FK, so the fold mints fresh rows. Boot-time
|
||||
// backfill is the safety net if this best-effort call fails.
|
||||
if (payload.intakeEvents.length > 0) {
|
||||
try {
|
||||
const restoreUser = await prisma.user.findUnique({
|
||||
where: { id: ownerId },
|
||||
select: { timezone: true },
|
||||
});
|
||||
await recomputeUserMedicationCompliance(
|
||||
ownerId,
|
||||
MEDICATION_COMPLIANCE_BACKFILL_DAYS,
|
||||
restoreUser?.timezone ?? null,
|
||||
);
|
||||
} catch (err) {
|
||||
annotate({
|
||||
meta: {
|
||||
medication_compliance_rollup_restore_failed: true,
|
||||
medication_compliance_rollup_restore_error:
|
||||
err instanceof Error ? err.message : String(err),
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
const summary = {
|
||||
...summarizeBackup(payload),
|
||||
measurements: measurementsRestored,
|
||||
|
||||
@@ -20,8 +20,8 @@ import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||
vi.mock("@/lib/medications/inventory/consumption", () => ({
|
||||
consumeForIntake: vi.fn(async () => undefined),
|
||||
}));
|
||||
vi.mock("@/lib/rollups/medication-compliance-rollups", () => ({
|
||||
recomputeMedicationComplianceForEvent: vi.fn(async () => undefined),
|
||||
vi.mock("@/lib/medications/dose-day-refresh", () => ({
|
||||
refreshDoseSlot: vi.fn(async () => undefined),
|
||||
dayKeyForScheduledFor: (at: Date, tz: string) =>
|
||||
at.toLocaleDateString("sv-SE", { timeZone: tz }),
|
||||
}));
|
||||
@@ -36,7 +36,7 @@ vi.mock("@/lib/medications/lifecycle", () => ({
|
||||
}));
|
||||
|
||||
import { consumeForIntake } from "@/lib/medications/inventory/consumption";
|
||||
import { recomputeMedicationComplianceForEvent } from "@/lib/rollups/medication-compliance-rollups";
|
||||
import { refreshDoseSlot } from "@/lib/medications/dose-day-refresh";
|
||||
import { queueMedicationIntakeSync } from "@/lib/notifications/medication-intake-sync";
|
||||
import {
|
||||
INTAKE_AUTO_TAKE_CRON,
|
||||
@@ -590,7 +590,7 @@ function row(partial: Partial<FakeRow> & { id: string; scheduledFor: Date }) {
|
||||
describe("runIntakeAutoTakePass — over a fake client", () => {
|
||||
beforeEach(() => {
|
||||
vi.mocked(consumeForIntake).mockClear();
|
||||
vi.mocked(recomputeMedicationComplianceForEvent).mockClear();
|
||||
vi.mocked(refreshDoseSlot).mockClear();
|
||||
vi.mocked(queueMedicationIntakeSync).mockClear();
|
||||
});
|
||||
|
||||
|
||||
@@ -3,7 +3,6 @@ import { describe, expect, it } from "vitest";
|
||||
import {
|
||||
MEDICATION_IMPORT_SKIP_DETAIL_LIMIT,
|
||||
MEDICATION_INTAKE_IMPORT_CHUNK_SIZE,
|
||||
MEDICATION_INTAKE_IMPORT_ROLLUP_CHUNK_SIZE,
|
||||
_setMedicationIntakeImportPrismaForTests,
|
||||
advanceMedicationImportProgress,
|
||||
appendMedicationImportSkipDetails,
|
||||
@@ -48,21 +47,18 @@ describe("medication intake import worker bounds", () => {
|
||||
processed: 100,
|
||||
imported: 98,
|
||||
skippedByReason: { duplicate_in_file: 2 },
|
||||
touchedDays: ["2026-07-20", "2026-07-21"],
|
||||
});
|
||||
const second = advanceMedicationImportProgress(first, {
|
||||
from: 100,
|
||||
processed: 100,
|
||||
imported: 97,
|
||||
skippedByReason: { duplicate_in_file: 1, already_recorded: 2 },
|
||||
touchedDays: ["2026-07-21", "2026-07-22"],
|
||||
});
|
||||
const replay = advanceMedicationImportProgress(second, {
|
||||
from: 0,
|
||||
processed: 100,
|
||||
imported: 98,
|
||||
skippedByReason: { duplicate_in_file: 2 },
|
||||
touchedDays: ["2026-07-20"],
|
||||
});
|
||||
|
||||
expect(first.processed).toBe(100);
|
||||
@@ -71,7 +67,7 @@ describe("medication intake import worker bounds", () => {
|
||||
total: 1_000,
|
||||
imported: 195,
|
||||
skippedByReason: { duplicate_in_file: 3, already_recorded: 2 },
|
||||
touchedDays: ["2026-07-20", "2026-07-21", "2026-07-22"],
|
||||
touchedDays: [],
|
||||
rollupProcessed: 0,
|
||||
});
|
||||
expect(replay).toBe(second);
|
||||
@@ -89,7 +85,11 @@ describe("medication intake import worker bounds", () => {
|
||||
expect(failure.length).toBeLessThanOrEqual(1_000);
|
||||
});
|
||||
|
||||
it("checkpoints bounded rollup transactions across many touched days", async () => {
|
||||
it("finishes a job admitted with pending rollup days without recomputing them", async () => {
|
||||
// A job admitted before the compliance rollup was dropped carries the
|
||||
// days it touched and how many of them were recomputed. It now finishes
|
||||
// on its next pass, writes nothing per day, and keeps its stored
|
||||
// progress shape.
|
||||
const touchedDays = Array.from({ length: 1_000 }, (_, index) =>
|
||||
new Date(Date.UTC(2020, 0, index + 1)).toISOString().slice(0, 10),
|
||||
);
|
||||
@@ -99,32 +99,27 @@ describe("medication intake import worker bounds", () => {
|
||||
imported: 0,
|
||||
skippedByReason: {},
|
||||
touchedDays,
|
||||
rollupProcessed: 40,
|
||||
};
|
||||
let status = "running";
|
||||
const executeRawCounts: number[] = [];
|
||||
let executeRawCount = 0;
|
||||
let transactions = 0;
|
||||
let auditCount = 0;
|
||||
let transactionNumber = 0;
|
||||
let rejectRollup = true;
|
||||
|
||||
const client = {
|
||||
$transaction: async (
|
||||
operation: (tx: Record<string, unknown>) => Promise<unknown>,
|
||||
) => {
|
||||
transactionNumber += 1;
|
||||
let executeRawCount = 0;
|
||||
transactions += 1;
|
||||
const tx = {
|
||||
$queryRaw: async () => [{ id: "job-many-days" }],
|
||||
$queryRaw: async () => [{ id: "job-legacy-days" }],
|
||||
$executeRaw: async () => {
|
||||
executeRawCount += 1;
|
||||
if (rejectRollup && transactionNumber === 3) {
|
||||
rejectRollup = false;
|
||||
throw new Error("rollup transaction failed");
|
||||
}
|
||||
return 1;
|
||||
},
|
||||
medicationIntakeImportJob: {
|
||||
findUnique: async () => ({
|
||||
id: "job-many-days",
|
||||
id: "job-legacy-days",
|
||||
recordUserId: "user-1",
|
||||
actorUserId: "user-1",
|
||||
medicationId: "medication-1",
|
||||
@@ -132,7 +127,6 @@ describe("medication intake import worker bounds", () => {
|
||||
payload: { entries: [] },
|
||||
progress: persistedProgress,
|
||||
startedAt: new Date(),
|
||||
recordUser: { timezone: "UTC" },
|
||||
}),
|
||||
update: async (args: {
|
||||
data: { progress?: unknown; status?: string };
|
||||
@@ -151,43 +145,25 @@ describe("medication intake import worker bounds", () => {
|
||||
},
|
||||
},
|
||||
};
|
||||
try {
|
||||
return await operation(tx);
|
||||
} finally {
|
||||
executeRawCounts.push(executeRawCount);
|
||||
}
|
||||
return operation(tx);
|
||||
},
|
||||
};
|
||||
|
||||
_setMedicationIntakeImportPrismaForTests(client as never);
|
||||
try {
|
||||
await expect(
|
||||
processMedicationIntakeImportJob("job-many-days"),
|
||||
).rejects.toThrow("rollup transaction failed");
|
||||
expect(persistedProgress).toMatchObject({
|
||||
rollupProcessed: MEDICATION_INTAKE_IMPORT_ROLLUP_CHUNK_SIZE * 2,
|
||||
});
|
||||
expect(status).toBe("running");
|
||||
expect(auditCount).toBe(0);
|
||||
|
||||
await processMedicationIntakeImportJob("job-many-days");
|
||||
await processMedicationIntakeImportJob("job-legacy-days");
|
||||
} finally {
|
||||
_setMedicationIntakeImportPrismaForTests(null);
|
||||
}
|
||||
|
||||
expect(executeRawCounts).toHaveLength(
|
||||
Math.ceil(
|
||||
touchedDays.length / MEDICATION_INTAKE_IMPORT_ROLLUP_CHUNK_SIZE,
|
||||
) + 1,
|
||||
);
|
||||
expect(Math.max(...executeRawCounts)).toBeLessThanOrEqual(
|
||||
MEDICATION_INTAKE_IMPORT_ROLLUP_CHUNK_SIZE * 2,
|
||||
);
|
||||
expect(status).toBe("done");
|
||||
expect(persistedProgress).toMatchObject({
|
||||
rollupProcessed: touchedDays.length,
|
||||
});
|
||||
expect(transactions).toBe(1);
|
||||
expect(executeRawCount).toBe(0);
|
||||
expect(auditCount).toBe(1);
|
||||
expect(persistedProgress).toMatchObject({
|
||||
touchedDays,
|
||||
rollupProcessed: 40,
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -229,14 +205,12 @@ describe("medication import skip reporting (#650)", () => {
|
||||
processed: 100,
|
||||
imported: 90,
|
||||
skippedByReason: { duplicate_in_file: 10 },
|
||||
touchedDays: [],
|
||||
});
|
||||
const second = advanceMedicationImportProgress(first, {
|
||||
from: 100,
|
||||
processed: 100,
|
||||
imported: 95,
|
||||
skippedByReason: { already_recorded: 5 },
|
||||
touchedDays: [],
|
||||
});
|
||||
|
||||
expect(second.skippedByReason).toEqual({
|
||||
|
||||
@@ -99,9 +99,6 @@ vi.mock("@/lib/notifications/dispatcher", () => ({
|
||||
}));
|
||||
vi.mock("@/lib/crypto", () => ({ decrypt: vi.fn(() => "token") }));
|
||||
vi.mock("@/lib/telegram", () => ({ deleteMessage: vi.fn() }));
|
||||
vi.mock("@/lib/rollups/medication-compliance-rollups", () => ({
|
||||
recomputeMedicationComplianceForEvent: vi.fn().mockResolvedValue(undefined),
|
||||
}));
|
||||
vi.mock("@/lib/notifications/reminder-dedup", () => ({
|
||||
claimNotificationEvent: vi.fn().mockResolvedValue(true),
|
||||
medicationReminderDedupKey: vi.fn(() => "dedup-key"),
|
||||
|
||||
@@ -124,9 +124,6 @@ vi.mock("@/lib/notifications/dispatcher", () => ({
|
||||
}));
|
||||
vi.mock("@/lib/crypto", () => ({ decrypt: vi.fn(() => "token") }));
|
||||
vi.mock("@/lib/telegram", () => ({ deleteMessage: vi.fn() }));
|
||||
vi.mock("@/lib/rollups/medication-compliance-rollups", () => ({
|
||||
recomputeMedicationComplianceForEvent: vi.fn().mockResolvedValue(undefined),
|
||||
}));
|
||||
|
||||
import { handleReminderCheck } from "@/lib/jobs/reminder/medication-reminder-check";
|
||||
import { dispatchNotification } from "@/lib/notifications/dispatcher";
|
||||
|
||||
@@ -60,7 +60,6 @@ describe("rollup registrar queue policies", () => {
|
||||
it.each([
|
||||
["ROLLUP_FULL_BACKFILL_QUEUE", "exclusive"],
|
||||
["MOOD_ROLLUP_FULL_BACKFILL_QUEUE", "exclusive"],
|
||||
["MEDICATION_COMPLIANCE_BACKFILL_QUEUE", "exclusive"],
|
||||
["STEP_CONSOLIDATION_QUEUE", "exclusive"],
|
||||
["STEP_CONSOLIDATION_REPAIR_QUEUE", "exclusive"],
|
||||
["CUMULATIVE_PR_REDERIVE_QUEUE", "exclusive"],
|
||||
|
||||
@@ -0,0 +1,128 @@
|
||||
/**
|
||||
* Structural guard: every model that soft-deletes has exactly one retention
|
||||
* entry, and every entry names a model that soft-deletes.
|
||||
*
|
||||
* Twenty of the twenty-three soft-delete models had no purge at all: their
|
||||
* tombstones stayed for as long as the instance ran. The set is derived from
|
||||
* `prisma/schema.prisma` (a `deletedAt` field), so a model that starts
|
||||
* soft-deleting cannot ship without saying how long its tombstones live.
|
||||
*/
|
||||
import { mkdtempSync, rmSync, writeFileSync } from "node:fs";
|
||||
import { tmpdir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
import {
|
||||
readDeclaredSchema,
|
||||
type DeclaredSchema,
|
||||
} from "@/__tests__/helpers/prisma-schema-names";
|
||||
import { TOMBSTONE_RETENTION_DAYS } from "@/lib/auth/native-client";
|
||||
import {
|
||||
SOFT_DELETE_RETENTION,
|
||||
purgeModels,
|
||||
type SoftDeleteRetention,
|
||||
} from "@/lib/jobs/soft-delete-retention";
|
||||
|
||||
function problems(
|
||||
schema: DeclaredSchema,
|
||||
register: Readonly<Record<string, SoftDeleteRetention>>,
|
||||
): string[] {
|
||||
const found: string[] = [];
|
||||
const softDeleting = schema.models.filter((model) =>
|
||||
model.fields.some((f) => f.name === "deletedAt"),
|
||||
);
|
||||
for (const model of softDeleting) {
|
||||
const entry = register[model.name];
|
||||
if (!entry) {
|
||||
found.push(`${model.name}: soft-deletes but has no retention entry`);
|
||||
continue;
|
||||
}
|
||||
if (entry.kind !== "purge") continue;
|
||||
if (entry.table !== model.physicalName) {
|
||||
found.push(
|
||||
`${model.name}: entry purges ${entry.table}, the model is ${model.physicalName}`,
|
||||
);
|
||||
}
|
||||
for (const column of ["user_id", "deleted_at"]) {
|
||||
if (!model.fields.some((f) => f.physicalName === column)) {
|
||||
found.push(`${model.name}: the purge needs a ${column} column`);
|
||||
}
|
||||
}
|
||||
}
|
||||
const names = new Set(softDeleting.map((m) => m.name));
|
||||
for (const name of Object.keys(register)) {
|
||||
if (!names.has(name)) {
|
||||
found.push(`${name}: has a retention entry but does not soft-delete`);
|
||||
}
|
||||
}
|
||||
return found;
|
||||
}
|
||||
|
||||
describe("soft-delete retention register", () => {
|
||||
const schema = readDeclaredSchema();
|
||||
|
||||
it("covers every soft-delete model exactly once", () => {
|
||||
const softDeleting = schema.models.filter((model) =>
|
||||
model.fields.some((f) => f.name === "deletedAt"),
|
||||
);
|
||||
// A reader that stopped matching would find nothing and pass.
|
||||
expect(softDeleting.length).toBeGreaterThanOrEqual(23);
|
||||
const found = problems(schema, SOFT_DELETE_RETENTION);
|
||||
expect(found, found.join("\n")).toEqual([]);
|
||||
});
|
||||
|
||||
it("keeps every tombstone at least as long as the sync feed's horizon", () => {
|
||||
for (const [model, entry] of Object.entries(SOFT_DELETE_RETENTION)) {
|
||||
if (entry.kind !== "purge") continue;
|
||||
expect(entry.days(), model).toBeGreaterThanOrEqual(
|
||||
TOMBSTONE_RETENTION_DAYS,
|
||||
);
|
||||
}
|
||||
expect(purgeModels()).toContain("MenstrualCycle");
|
||||
expect(purgeModels()).toContain("CycleDayLog");
|
||||
});
|
||||
|
||||
it("explains every entry that departs from the default", () => {
|
||||
for (const [model, entry] of Object.entries(SOFT_DELETE_RETENTION)) {
|
||||
if (entry.kind !== "purge") continue;
|
||||
const departs =
|
||||
entry.keep !== undefined || entry.days() !== TOMBSTONE_RETENTION_DAYS;
|
||||
if (departs) expect(entry.why?.length ?? 0, model).toBeGreaterThan(40);
|
||||
}
|
||||
});
|
||||
|
||||
it("counter-check: names a new soft-delete model, a stale entry and a wrong table", () => {
|
||||
const dir = mkdtempSync(join(tmpdir(), "hl-retention-"));
|
||||
try {
|
||||
const path = join(dir, "schema.prisma");
|
||||
writeFileSync(
|
||||
path,
|
||||
`model Widget {
|
||||
id String @id
|
||||
userId String @map("user_id")
|
||||
deletedAt DateTime? @map("deleted_at")
|
||||
@@map("widgets")
|
||||
}
|
||||
model Gadget {
|
||||
id String @id
|
||||
userId String @map("user_id")
|
||||
deletedAt DateTime? @map("deleted_at")
|
||||
@@map("gadgets")
|
||||
}
|
||||
`,
|
||||
);
|
||||
const synthetic = readDeclaredSchema(path);
|
||||
const found = problems(synthetic, {
|
||||
Gadget: { kind: "purge", table: "gadget", days: () => 75 },
|
||||
Gone: { kind: "purge", table: "gone", days: () => 75 },
|
||||
});
|
||||
expect(found).toEqual([
|
||||
"Widget: soft-deletes but has no retention entry",
|
||||
"Gadget: entry purges gadget, the model is gadgets",
|
||||
"Gone: has a retention entry but does not soft-delete",
|
||||
]);
|
||||
} finally {
|
||||
rmSync(dir, { recursive: true });
|
||||
}
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,163 @@
|
||||
/**
|
||||
* Nightly cleanup of the dead rows of the sign-in ledgers.
|
||||
*
|
||||
* Every sign-in writes a session, every native sign-in and refresh writes a
|
||||
* refresh token and an access token, every second-factor prompt and passkey
|
||||
* ceremony writes a challenge, and "remember this device" writes a trusted
|
||||
* device. Nothing removed them once they expired, so the tables were mostly
|
||||
* dead rows: on a long-running instance nine in ten sessions and access
|
||||
* tokens had expired.
|
||||
*
|
||||
* What still reads a dead row decides how long it stays:
|
||||
*
|
||||
* - Sessions and refresh tokens: the login throttle recognises a source by a
|
||||
* session or refresh token created from the same address in the last 30
|
||||
* days (`KNOWN_ADDRESS_WINDOW_MS`), and by any refresh token of the device.
|
||||
* Reuse detection reads a consumed refresh token until it expires (an
|
||||
* expired one is refused before reuse is considered). So a row goes 30 days
|
||||
* after it expired: it was created before that, so the address window
|
||||
* cannot reach it, and a device that has not signed in for that long is a
|
||||
* new source again. A revoked token waits for its expiry like any other.
|
||||
* - Access tokens (`api_tokens`): the token list shows revoked and expired
|
||||
* tokens so their owner can see what ended. One goes 30 days after it
|
||||
* expired; a revoked token without an expiry stays, since nothing records
|
||||
* when it was revoked. Connector tokens have their own sweep
|
||||
* (`mcp-token-cleanup`).
|
||||
* - Second-factor and passkey challenges: nothing reads one past its expiry.
|
||||
* A day of slack keeps a ceremony in flight at midnight out of the way.
|
||||
* - Trusted devices: an expired one no longer skips the second factor and is
|
||||
* not listed; it goes 30 days after it expired.
|
||||
*
|
||||
* Security activity is read from the audit log, never from these tables, so
|
||||
* it loses nothing. Each table is deleted in bounded batches.
|
||||
*/
|
||||
import type { Job } from "pg-boss";
|
||||
|
||||
import type { PrismaClient } from "@/generated/prisma/client";
|
||||
import { jobDone, type JobOutcome } from "@/lib/jobs/job-outcome";
|
||||
import { PURGE_BATCH_SIZE, PURGE_MAX_BATCHES } from "@/lib/jobs/purge-batch";
|
||||
import { getWorkerPrisma } from "@/lib/jobs/reminder/shared";
|
||||
import { withBackgroundEvent } from "@/lib/logging/background";
|
||||
|
||||
export const AUTH_LEDGER_CLEANUP_QUEUE = "auth-ledger-cleanup";
|
||||
/** Daily at 03:35 Europe/Berlin, inside the 03:xx maintenance window. */
|
||||
export const AUTH_LEDGER_CLEANUP_CRON = "35 3 * * *";
|
||||
|
||||
const DAY_MS = 86_400_000;
|
||||
|
||||
/** Days an expired session, token or trusted device is kept. */
|
||||
export const AUTH_LEDGER_EXPIRED_GRACE_DAYS = 30;
|
||||
/** Days an expired challenge is kept. */
|
||||
export const AUTH_CHALLENGE_GRACE_DAYS = 1;
|
||||
|
||||
export interface AuthLedgerCleanupPayload {
|
||||
triggeredAt?: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* The tables and the column each one expires by. Literals, spliced into the
|
||||
* delete; `api_tokens.expires_at` is nullable and a null never matches.
|
||||
*/
|
||||
const LEDGERS = [
|
||||
{
|
||||
key: "sessions",
|
||||
table: "sessions",
|
||||
graceDays: AUTH_LEDGER_EXPIRED_GRACE_DAYS,
|
||||
},
|
||||
{
|
||||
key: "refresh_tokens",
|
||||
table: "refresh_tokens",
|
||||
graceDays: AUTH_LEDGER_EXPIRED_GRACE_DAYS,
|
||||
},
|
||||
{
|
||||
key: "api_tokens",
|
||||
table: "api_tokens",
|
||||
graceDays: AUTH_LEDGER_EXPIRED_GRACE_DAYS,
|
||||
},
|
||||
{
|
||||
key: "trusted_devices",
|
||||
table: "trusted_devices",
|
||||
graceDays: AUTH_LEDGER_EXPIRED_GRACE_DAYS,
|
||||
},
|
||||
{
|
||||
key: "mfa_challenges",
|
||||
table: "mfa_challenges",
|
||||
graceDays: AUTH_CHALLENGE_GRACE_DAYS,
|
||||
},
|
||||
{
|
||||
key: "auth_challenges",
|
||||
table: "auth_challenges",
|
||||
graceDays: AUTH_CHALLENGE_GRACE_DAYS,
|
||||
},
|
||||
] as const;
|
||||
|
||||
export type AuthLedger = (typeof LEDGERS)[number]["key"];
|
||||
|
||||
export interface AuthLedgerCleanupResult {
|
||||
deleted: Record<AuthLedger, number>;
|
||||
/** False when a table stopped at the batch cap with rows left. */
|
||||
drained: boolean;
|
||||
}
|
||||
|
||||
export async function cleanupAuthLedgers(
|
||||
prisma: PrismaClient,
|
||||
now: Date = new Date(),
|
||||
{ batchSize = PURGE_BATCH_SIZE, maxBatches = PURGE_MAX_BATCHES } = {},
|
||||
): Promise<AuthLedgerCleanupResult> {
|
||||
const deleted = {} as Record<AuthLedger, number>;
|
||||
let drained = true;
|
||||
for (const ledger of LEDGERS) {
|
||||
const cutoff = new Date(now.getTime() - ledger.graceDays * DAY_MS);
|
||||
let total = 0;
|
||||
let finished = false;
|
||||
for (let batch = 0; batch < maxBatches; batch++) {
|
||||
// `ledger.table` is one of the literals above, never input.
|
||||
const removed = await prisma.$executeRawUnsafe(
|
||||
`DELETE FROM "${ledger.table}" WHERE "id" IN (
|
||||
SELECT "id" FROM "${ledger.table}"
|
||||
WHERE "expires_at" < $1 LIMIT $2)`,
|
||||
cutoff,
|
||||
batchSize,
|
||||
);
|
||||
total += removed;
|
||||
if (removed < batchSize) {
|
||||
finished = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
deleted[ledger.key] = total;
|
||||
if (!finished) drained = false;
|
||||
}
|
||||
return { deleted, drained };
|
||||
}
|
||||
|
||||
export async function handleAuthLedgerCleanup(
|
||||
jobs: Job<AuthLedgerCleanupPayload>[],
|
||||
): Promise<JobOutcome> {
|
||||
void jobs;
|
||||
return withBackgroundEvent("job.auth_ledger_cleanup", async (evt) => {
|
||||
try {
|
||||
const { deleted, drained } = await cleanupAuthLedgers(getWorkerPrisma());
|
||||
for (const [key, count] of Object.entries(deleted)) {
|
||||
evt.addMeta(`auth_ledger_cleanup_${key}`, count);
|
||||
}
|
||||
evt.addMeta("auth_ledger_cleanup_drained", drained);
|
||||
if (!drained) {
|
||||
evt.addWarning(
|
||||
"auth-ledger-cleanup stopped at the batch cap; a backlog remains for the next run",
|
||||
);
|
||||
}
|
||||
return jobDone({
|
||||
sessions_deleted: deleted.sessions,
|
||||
refresh_tokens_deleted: deleted.refresh_tokens,
|
||||
access_tokens_deleted: deleted.api_tokens,
|
||||
trusted_devices_deleted: deleted.trusted_devices,
|
||||
challenges_deleted: deleted.mfa_challenges + deleted.auth_challenges,
|
||||
drained,
|
||||
});
|
||||
} catch (err) {
|
||||
evt.addWarning(`auth-ledger-cleanup failed: ${err}`);
|
||||
throw err;
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -85,8 +85,8 @@ import { queueMedicationIntakeSync } from "@/lib/notifications/medication-intake
|
||||
import { isP2002 } from "@/lib/prisma-errors";
|
||||
import {
|
||||
dayKeyForScheduledFor,
|
||||
recomputeMedicationComplianceForEvent,
|
||||
} from "@/lib/rollups/medication-compliance-rollups";
|
||||
refreshDoseSlot,
|
||||
} from "@/lib/medications/dose-day-refresh";
|
||||
import { DEFAULT_TIMEZONE, validTimezoneOr } from "@/lib/tz/format";
|
||||
import { getEvent } from "@/lib/logging/context";
|
||||
|
||||
@@ -375,7 +375,7 @@ async function autoTakeOne(
|
||||
const dayKey = dayKeyForScheduledFor(slot.at, userTz);
|
||||
if (days.has(dayKey)) continue;
|
||||
days.add(dayKey);
|
||||
await recomputeMedicationComplianceForEvent({
|
||||
await refreshDoseSlot({
|
||||
userId: medication.userId,
|
||||
medicationId: medication.id,
|
||||
scheduledFor: slot.at,
|
||||
|
||||
@@ -64,6 +64,8 @@ export const JOB_FACT_ALLOWLIST: ReadonlySet<string> = new Set([
|
||||
"budget_blocked",
|
||||
"cached",
|
||||
"candidates_scanned",
|
||||
// The nightly sign-in ledger cleanup (`auth-ledger-cleanup`).
|
||||
"challenges_deleted",
|
||||
"connect_tickets_deleted",
|
||||
"connections_deleted",
|
||||
"considered",
|
||||
@@ -208,6 +210,7 @@ export const JOB_FACT_ALLOWLIST: ReadonlySet<string> = new Set([
|
||||
"records_oversized",
|
||||
"records_read",
|
||||
"refreshed",
|
||||
"refresh_tokens_deleted",
|
||||
"refused",
|
||||
"reminders_due",
|
||||
"removed",
|
||||
@@ -251,6 +254,7 @@ export const JOB_FACT_ALLOWLIST: ReadonlySet<string> = new Set([
|
||||
"scheduled",
|
||||
"sent",
|
||||
"served",
|
||||
"sessions_deleted",
|
||||
"side_effects_migrated",
|
||||
// v1.42.4 — integration watchdog: sources newly flagged as silent.
|
||||
"silences_flagged",
|
||||
@@ -277,6 +281,7 @@ export const JOB_FACT_ALLOWLIST: ReadonlySet<string> = new Set([
|
||||
"tls_pin_monitor_known_count",
|
||||
"tls_pin_monitor_outcome",
|
||||
"total",
|
||||
"trusted_devices_deleted",
|
||||
"unchanged",
|
||||
"updated",
|
||||
"users",
|
||||
|
||||
@@ -9,15 +9,10 @@ import { jobBudget } from "@/lib/jobs/job-budget";
|
||||
import { enqueueSupplementNutrientRecompute } from "@/lib/medications/ingredients/recompute-job";
|
||||
import { consumeImportedIntakesBatch } from "@/lib/medications/inventory/consumption";
|
||||
import { queueMedicationIntakeSync } from "@/lib/notifications/medication-intake-sync";
|
||||
import {
|
||||
dayKeyForScheduledFor,
|
||||
recomputeMedicationComplianceForDay,
|
||||
} from "@/lib/rollups/medication-compliance-rollups";
|
||||
|
||||
export const MEDICATION_INTAKE_IMPORT_QUEUE = "medication-intake-import";
|
||||
export const MEDICATION_INTAKE_IMPORT_CONCURRENCY = 2;
|
||||
export const MEDICATION_INTAKE_IMPORT_CHUNK_SIZE = 100;
|
||||
export const MEDICATION_INTAKE_IMPORT_ROLLUP_CHUNK_SIZE = 20;
|
||||
export const MEDICATION_INTAKE_IMPORT_STALE_AFTER_MS = 15 * 60_000;
|
||||
|
||||
export const MEDICATION_INTAKE_IMPORT_SEND_OPTIONS = {
|
||||
@@ -136,6 +131,11 @@ export interface MedicationImportProgress {
|
||||
skipDetails?: MedicationImportSkipDetail[];
|
||||
/** Details beyond the hard cap, counted without retaining their contents. */
|
||||
skippedDetailsOmitted?: number;
|
||||
/**
|
||||
* Wire fields of the dropped compliance rollup's recompute phase. A job
|
||||
* admitted now carries `[]` and `0` and the worker never advances them;
|
||||
* they stay on the published progress shape for clients that decode it.
|
||||
*/
|
||||
touchedDays: string[];
|
||||
rollupProcessed: number;
|
||||
}
|
||||
@@ -218,7 +218,6 @@ export interface MedicationImportChunkResult {
|
||||
imported: number;
|
||||
skippedByReason: MedicationImportSkipCounts;
|
||||
skipDetails?: MedicationImportSkipDetail[];
|
||||
touchedDays: string[];
|
||||
}
|
||||
|
||||
let workerPrismaSingleton: PrismaClient | null = null;
|
||||
@@ -251,15 +250,6 @@ export function advanceMedicationImportProgress(
|
||||
previous.total,
|
||||
previous.processed + Math.max(0, Math.trunc(chunk.processed)),
|
||||
);
|
||||
const touchedDays = [...previous.touchedDays];
|
||||
const seenDays = new Set(touchedDays);
|
||||
for (const day of chunk.touchedDays) {
|
||||
if (!seenDays.has(day)) {
|
||||
seenDays.add(day);
|
||||
touchedDays.push(day);
|
||||
}
|
||||
}
|
||||
|
||||
const skippedByReason: MedicationImportSkipCounts = {
|
||||
...previous.skippedByReason,
|
||||
};
|
||||
@@ -293,7 +283,7 @@ export function advanceMedicationImportProgress(
|
||||
}
|
||||
: {}),
|
||||
rollupProcessed: previous.rollupProcessed,
|
||||
touchedDays,
|
||||
touchedDays: previous.touchedDays,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -503,67 +493,6 @@ interface ProcessChunkOutcome {
|
||||
userId: string | null;
|
||||
}
|
||||
|
||||
/**
|
||||
* A day whose compliance rollup an import invalidated, as one string.
|
||||
*
|
||||
* A job can now span a whole regimen, so a bare day key no longer identifies
|
||||
* what to recompute — two medications touched on the same day are two rollup
|
||||
* rows. The medication is folded in ahead of a single space, which occurs in
|
||||
* neither a cuid nor a `YYYY-MM-DD` key. Not a NUL byte: this value is persisted
|
||||
* in the progress blob and Postgres rejects a NUL byte inside a jsonb string.
|
||||
*/
|
||||
const TOUCHED_DAY_SEPARATOR = " ";
|
||||
|
||||
function encodeTouchedDay(medicationId: string, dayKey: string): string {
|
||||
return `${medicationId}${TOUCHED_DAY_SEPARATOR}${dayKey}`;
|
||||
}
|
||||
|
||||
/**
|
||||
* Split a stored touched-day back into its parts.
|
||||
*
|
||||
* A value with no separator was written before a job could span more than one
|
||||
* medication, so it is a bare day key belonging to the job's own medication.
|
||||
* Returns null when neither reading yields a medication — a per-medication job
|
||||
* that lost its medication cannot be resolved and the day is left alone rather
|
||||
* than recomputed against a guess.
|
||||
*/
|
||||
function decodeTouchedDay(
|
||||
value: string,
|
||||
fallbackMedicationId: string | null,
|
||||
): { medicationId: string; dayKey: string } | null {
|
||||
const separator = value.indexOf(TOUCHED_DAY_SEPARATOR);
|
||||
if (separator === -1) {
|
||||
if (!fallbackMedicationId) return null;
|
||||
return { medicationId: fallbackMedicationId, dayKey: value };
|
||||
}
|
||||
const medicationId = value.slice(0, separator);
|
||||
const dayKey = value.slice(separator + 1);
|
||||
if (medicationId.length === 0 || dayKey.length === 0) return null;
|
||||
return { medicationId, dayKey };
|
||||
}
|
||||
|
||||
async function recomputeTouchedDays(
|
||||
client: Pick<PrismaClient, "$executeRaw">,
|
||||
userId: string,
|
||||
jobMedicationId: string | null,
|
||||
timezone: string,
|
||||
days: readonly string[],
|
||||
): Promise<void> {
|
||||
await Promise.all(
|
||||
days.map((day) => {
|
||||
const decoded = decodeTouchedDay(day, jobMedicationId);
|
||||
if (!decoded) return Promise.resolve();
|
||||
return recomputeMedicationComplianceForDay(
|
||||
userId,
|
||||
decoded.medicationId,
|
||||
decoded.dayKey,
|
||||
timezone,
|
||||
client,
|
||||
);
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
async function processNextChunk(
|
||||
client: PrismaClient,
|
||||
jobId: string,
|
||||
@@ -587,7 +516,6 @@ async function processNextChunk(
|
||||
|
||||
const row = await tx.medicationIntakeImportJob.findUnique({
|
||||
where: { id: jobId },
|
||||
include: { recordUser: { select: { timezone: true } } },
|
||||
});
|
||||
if (!row || row.status === "done" || row.status === "failed") {
|
||||
return {
|
||||
@@ -601,40 +529,6 @@ async function processNextChunk(
|
||||
const payload = parsePayload(row.payload);
|
||||
const progress = parseProgress(row.progress, payload.entries.length);
|
||||
if (progress.processed >= progress.total) {
|
||||
const rollupDays = progress.touchedDays.slice(
|
||||
progress.rollupProcessed,
|
||||
progress.rollupProcessed + MEDICATION_INTAKE_IMPORT_ROLLUP_CHUNK_SIZE,
|
||||
);
|
||||
await recomputeTouchedDays(
|
||||
tx,
|
||||
row.recordUserId,
|
||||
row.medicationId,
|
||||
row.recordUser.timezone,
|
||||
rollupDays,
|
||||
);
|
||||
const finalizedProgress: MedicationImportProgress = {
|
||||
...progress,
|
||||
rollupProcessed: progress.rollupProcessed + rollupDays.length,
|
||||
};
|
||||
if (finalizedProgress.rollupProcessed < progress.touchedDays.length) {
|
||||
const heartbeatAt = new Date();
|
||||
await tx.medicationIntakeImportJob.update({
|
||||
where: { id: row.id },
|
||||
data: {
|
||||
status: "running",
|
||||
progress: toJson(finalizedProgress),
|
||||
heartbeatAt,
|
||||
startedAt: row.startedAt ?? heartbeatAt,
|
||||
failureReason: null,
|
||||
},
|
||||
});
|
||||
return {
|
||||
terminal: false,
|
||||
finalized: false,
|
||||
result: null,
|
||||
userId: row.recordUserId,
|
||||
};
|
||||
}
|
||||
const result: MedicationImportResult = {
|
||||
imported: progress.imported,
|
||||
skipped: totalMedicationImportSkips(progress.skippedByReason),
|
||||
@@ -665,7 +559,7 @@ async function processNextChunk(
|
||||
data: {
|
||||
status: "done",
|
||||
result: toJson(result),
|
||||
progress: toJson(finalizedProgress),
|
||||
progress: toJson(progress),
|
||||
failureReason: null,
|
||||
heartbeatAt: completedAt,
|
||||
completedAt,
|
||||
@@ -782,12 +676,6 @@ async function processNextChunk(
|
||||
});
|
||||
}
|
||||
|
||||
const touchedDays = created.map((event) =>
|
||||
encodeTouchedDay(
|
||||
event.medicationId,
|
||||
dayKeyForScheduledFor(event.scheduledFor, row.recordUser.timezone),
|
||||
),
|
||||
);
|
||||
// Two different facts, counted apart. The Map collapse above is entries
|
||||
// of this submission that resolve to the same instant; `skipDuplicates`
|
||||
// then drops whatever the ledger already holds for that instant, from an
|
||||
@@ -803,7 +691,6 @@ async function processNextChunk(
|
||||
already_recorded: uniqueEntries.length - created.length,
|
||||
},
|
||||
skipDetails: [...duplicateDetails, ...alreadyRecordedDetails],
|
||||
touchedDays,
|
||||
});
|
||||
const heartbeatAt = new Date();
|
||||
await tx.medicationIntakeImportJob.update({
|
||||
|
||||
@@ -203,9 +203,6 @@ export const QUEUE_RUNTIME: Readonly<Record<string, QueueRuntime>> = {
|
||||
"mood-rollup-full-backfill": short(
|
||||
"One account's mood entries, a few a day.",
|
||||
),
|
||||
"medication-compliance-full-backfill": short(
|
||||
"One account's 90-day compliance window.",
|
||||
),
|
||||
"step-consolidation": short(
|
||||
"One account's legacy step rows, one transaction per day.",
|
||||
),
|
||||
@@ -316,6 +313,10 @@ export const QUEUE_RUNTIME: Readonly<Record<string, QueueRuntime>> = {
|
||||
"At most 40 batches of 5 000 rows per leg.",
|
||||
),
|
||||
"document-tombstone-purge": short("One deleteMany."),
|
||||
"soft-delete-purge": short(
|
||||
"At most 40 batches of 5 000 rows per table, over a few small tables.",
|
||||
),
|
||||
"auth-ledger-cleanup": short("At most 40 batches of 5 000 rows per table."),
|
||||
"document-summary-reaper": short("One updateMany."),
|
||||
"cycle-prediction-refresh": short(
|
||||
"Arithmetic per eligible account, no provider call.",
|
||||
|
||||
@@ -8,7 +8,6 @@ import { type Job } from "pg-boss";
|
||||
import type { Locale } from "@/lib/i18n/config";
|
||||
import { resolveJobLocale } from "@/lib/i18n/job-locale";
|
||||
import { recordError, recordReminderCheck } from "@/lib/jobs/worker-status";
|
||||
import { recomputeMedicationComplianceForEvent } from "@/lib/rollups/medication-compliance-rollups";
|
||||
import { decrypt } from "@/lib/crypto";
|
||||
import { jobDone, type JobOutcome } from "@/lib/jobs/job-outcome";
|
||||
import { withBackgroundEvent } from "@/lib/logging/background";
|
||||
@@ -708,15 +707,6 @@ export async function handleReminderCheck(
|
||||
|
||||
missedDosesMinted++;
|
||||
evt.addMeta("missed_dose", `${med.name}:${slotTime}`);
|
||||
|
||||
// v1.4.39 — refresh the compliance rollup so the
|
||||
// per-day `scheduled` count increments before any read.
|
||||
await recomputeMedicationComplianceForEvent({
|
||||
userId: med.user.id,
|
||||
medicationId: med.id,
|
||||
scheduledFor: slotScheduledFor,
|
||||
tz: med.user.timezone,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -290,6 +290,18 @@ import {
|
||||
handleDocumentPurge,
|
||||
type DocumentPurgePayload,
|
||||
} from "@/lib/jobs/document-purge";
|
||||
import {
|
||||
SOFT_DELETE_PURGE_QUEUE,
|
||||
SOFT_DELETE_PURGE_CRON,
|
||||
handleSoftDeletePurge,
|
||||
type SoftDeletePurgePayload,
|
||||
} from "@/lib/jobs/soft-delete-purge";
|
||||
import {
|
||||
AUTH_LEDGER_CLEANUP_QUEUE,
|
||||
AUTH_LEDGER_CLEANUP_CRON,
|
||||
handleAuthLedgerCleanup,
|
||||
type AuthLedgerCleanupPayload,
|
||||
} from "@/lib/jobs/auth-ledger-cleanup";
|
||||
import {
|
||||
DOCUMENT_SUMMARY_REAPER_QUEUE,
|
||||
DOCUMENT_SUMMARY_REAPER_CRON,
|
||||
@@ -580,6 +592,12 @@ const allQueues = [
|
||||
// the daily schedule silently no-ops and "deleted" documents hold backup
|
||||
// weight forever.
|
||||
DOCUMENT_PURGE_QUEUE,
|
||||
// Nightly purge of expired tombstones for every soft-delete model the
|
||||
// retention register hands to it (`soft-delete-retention.ts`).
|
||||
SOFT_DELETE_PURGE_QUEUE,
|
||||
// Nightly removal of expired sessions, tokens, challenges and trusted
|
||||
// devices.
|
||||
AUTH_LEDGER_CLEANUP_QUEUE,
|
||||
// Document vault — hourly reaper for summary rows stuck on PENDING (a
|
||||
// job dead past its retry budget). Without this entry the schedule
|
||||
// silently no-ops and a dead "wird generiert" claim never converges to
|
||||
@@ -779,6 +797,11 @@ const schedules: ScheduleEntry[] = [
|
||||
// Document vault — daily 04:10 Europe/Berlin purge for tombstoned
|
||||
// documents past the 30-day undo grace.
|
||||
[DOCUMENT_PURGE_QUEUE, DOCUMENT_PURGE_CRON, cronIsTheRetry],
|
||||
// Daily 03:42 Europe/Berlin, after the sync-domain tombstone cleanup. The
|
||||
// next night is the retry: the purge picks up whatever a failed run left.
|
||||
[SOFT_DELETE_PURGE_QUEUE, SOFT_DELETE_PURGE_CRON, cronIsTheRetry],
|
||||
// Daily 03:35 Europe/Berlin. Same: the next night deletes what is left.
|
||||
[AUTH_LEDGER_CLEANUP_QUEUE, AUTH_LEDGER_CLEANUP_CRON, cronIsTheRetry],
|
||||
// Document vault — hourly heal of summary rows stuck on PENDING. The next
|
||||
// tick is the retry: the updateMany is idempotent and the TTL predicate
|
||||
// re-selects anything a failed tick left behind.
|
||||
@@ -1140,6 +1163,20 @@ export async function registerMaintenanceQueues(
|
||||
{ localConcurrency: 1 },
|
||||
handleDocumentPurge,
|
||||
);
|
||||
// Single-flight like every other cleanup queue; both deletes are bounded
|
||||
// and idempotent, so a duplicate tick finds nothing.
|
||||
await createAndWork<SoftDeletePurgePayload>(
|
||||
boss,
|
||||
SOFT_DELETE_PURGE_QUEUE,
|
||||
{ localConcurrency: 1 },
|
||||
handleSoftDeletePurge,
|
||||
);
|
||||
await createAndWork<AuthLedgerCleanupPayload>(
|
||||
boss,
|
||||
AUTH_LEDGER_CLEANUP_QUEUE,
|
||||
{ localConcurrency: 1 },
|
||||
handleAuthLedgerCleanup,
|
||||
);
|
||||
// v1.40 — background document AI runs. Each job is one run, under the lock
|
||||
// `lockedPass` takes per run id, so a redelivery never reads beside the
|
||||
// first delivery; the claim inside is conditional as well.
|
||||
|
||||
@@ -38,13 +38,6 @@ import {
|
||||
type MoodRollupFullBackfillPayload,
|
||||
type MoodRollupRecomputePayload,
|
||||
} from "@/lib/rollups/mood-rollups";
|
||||
import {
|
||||
MEDICATION_COMPLIANCE_BACKFILL_QUEUE,
|
||||
MEDICATION_COMPLIANCE_BACKFILL_CONCURRENCY,
|
||||
recomputeUserMedicationCompliance,
|
||||
enqueueBootTimeMedicationComplianceBackfill,
|
||||
type MedicationComplianceBackfillPayload,
|
||||
} from "@/lib/rollups/medication-compliance-rollups";
|
||||
import {
|
||||
SUPPLEMENT_NUTRIENT_RECOMPUTE_QUEUE,
|
||||
SUPPLEMENT_NUTRIENT_RECOMPUTE_CONCURRENCY,
|
||||
@@ -114,6 +107,7 @@ import {
|
||||
createAndSchedule,
|
||||
createAndWork,
|
||||
cronIsTheRetry,
|
||||
retireQueues,
|
||||
type QueuePolicyTable,
|
||||
type ScheduleEntry,
|
||||
} from "./registrar-shared";
|
||||
@@ -154,10 +148,6 @@ const allQueues = [
|
||||
MOOD_ROLLUP_RECOMPUTE_QUEUE,
|
||||
// v1.4.39 — boot-time fold queue for the mood rollup tier.
|
||||
MOOD_ROLLUP_FULL_BACKFILL_QUEUE,
|
||||
// v1.4.39 — boot-time fold for the medication-compliance
|
||||
// rollup tier. Discovery enqueues one job per user with intake
|
||||
// events but no rollup coverage; idempotent across reboots.
|
||||
MEDICATION_COMPLIANCE_BACKFILL_QUEUE,
|
||||
// v1.5.6 — boot-time legacy step consolidation. Discovery enqueues
|
||||
// one job per user still holding live pre-v1.5.0 granular step rows.
|
||||
STEP_CONSOLIDATION_QUEUE,
|
||||
@@ -248,11 +238,6 @@ const queuePolicies: QueuePolicyTable = {
|
||||
reason:
|
||||
"Per-user mood backfill; boot-discovery driven and self-converging on rollup coverage.",
|
||||
},
|
||||
[MEDICATION_COMPLIANCE_BACKFILL_QUEUE]: {
|
||||
policy: "exclusive",
|
||||
reason:
|
||||
"Per-user compliance backfill; boot-discovery driven and self-converging on rollup coverage.",
|
||||
},
|
||||
[STEP_CONSOLIDATION_QUEUE]: {
|
||||
policy: "exclusive",
|
||||
reason:
|
||||
@@ -289,6 +274,13 @@ const queuePolicies: QueuePolicyTable = {
|
||||
// empty key and buy nothing.
|
||||
};
|
||||
|
||||
/**
|
||||
* The boot backfill of the per-day medication-compliance rollup, whose table
|
||||
* had no reader left and was dropped. Its queue, and any job still waiting in
|
||||
* it, is removed on an instance that still has it.
|
||||
*/
|
||||
const RETIRED_ROLLUP_QUEUES = ["medication-compliance-full-backfill"] as const;
|
||||
|
||||
/**
|
||||
* Register every rollup / consolidation queue. Returns the queue names created
|
||||
* (for the boot-level aggregate assertion).
|
||||
@@ -297,6 +289,7 @@ export async function registerRollupQueues(
|
||||
boss: PgBoss,
|
||||
): Promise<readonly string[]> {
|
||||
await createAndSchedule(boss, allQueues, schedules, queuePolicies);
|
||||
await retireQueues(boss, RETIRED_ROLLUP_QUEUES);
|
||||
|
||||
// v1.5.0 — persistent measurement rollup worker. Folds the
|
||||
// WEEK / MONTH / YEAR buckets that the write-path hooks enqueue;
|
||||
@@ -722,44 +715,6 @@ export async function registerRollupQueues(
|
||||
},
|
||||
);
|
||||
|
||||
// v1.4.39 — medication-compliance boot-backfill worker. The
|
||||
// discovery helper below sends one job per user with intake events
|
||||
// but zero rollup coverage; this handler folds the trailing 90-day
|
||||
// window per account. Concurrency-1 so the populator never crowds
|
||||
// the request pool.
|
||||
await createAndWork<MedicationComplianceBackfillPayload>(
|
||||
boss,
|
||||
MEDICATION_COMPLIANCE_BACKFILL_QUEUE,
|
||||
{ localConcurrency: MEDICATION_COMPLIANCE_BACKFILL_CONCURRENCY },
|
||||
async (jobs) => {
|
||||
let rowsUpsertedTotal = 0;
|
||||
for (const job of jobs) {
|
||||
const { userId } = job.data;
|
||||
try {
|
||||
const { rowsUpserted, durationMs } =
|
||||
await recomputeUserMedicationCompliance(userId);
|
||||
rowsUpsertedTotal += rowsUpserted;
|
||||
workerLog(
|
||||
"info",
|
||||
`[medication-compliance-backfill] user=${userId} rows=${rowsUpserted} duration=${durationMs}ms`,
|
||||
);
|
||||
} catch (err) {
|
||||
recordError();
|
||||
workerLog(
|
||||
"error",
|
||||
`[medication-compliance-backfill] user=${userId} failed`,
|
||||
err,
|
||||
);
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
return jobDone({
|
||||
users: jobs.length,
|
||||
rows_upserted: rowsUpsertedTotal,
|
||||
});
|
||||
},
|
||||
);
|
||||
|
||||
// v1.4.37 — nightly drain worker. Walks every user × cumulative
|
||||
// type and folds per-sample APPLE_HEALTH rows older than the cutoff
|
||||
// into one `stats:…` row per calendar day. Idempotent — a second run
|
||||
@@ -1138,34 +1093,6 @@ export async function enqueueRollupBootDiscovery(): Promise<void> {
|
||||
);
|
||||
}
|
||||
|
||||
// v1.4.39 — medication compliance rollup tier. Mirrors the v1.4.35.1
|
||||
// pattern: one job per user with intake events but no rollup coverage.
|
||||
// Idempotent across reboots and singleton-keyed inside pg-boss.
|
||||
try {
|
||||
// Stage 4.
|
||||
const { enqueued, skipped, error } =
|
||||
await enqueueBootTimeMedicationComplianceBackfill(
|
||||
BOOT_BACKFILL_STAGGER_SECONDS * 4,
|
||||
);
|
||||
if (error) {
|
||||
workerLog(
|
||||
"error",
|
||||
`[medication-compliance-backfill] boot discovery failed: ${error}`,
|
||||
);
|
||||
} else {
|
||||
workerLog(
|
||||
"info",
|
||||
`[medication-compliance-backfill] boot discovery: enqueued=${enqueued} skipped=${skipped}`,
|
||||
);
|
||||
}
|
||||
} catch (err) {
|
||||
workerLog(
|
||||
"error",
|
||||
"[medication-compliance-backfill] boot discovery threw an unexpected error",
|
||||
err,
|
||||
);
|
||||
}
|
||||
|
||||
// v1.28.37 — step-consolidation repair. Finds every account still holding
|
||||
// a resurrectable tombstoned GOOGLE_HEALTH / FITBIT daily-total step row
|
||||
// (swept by the pre-fix consolidation) and enqueues one repair job per
|
||||
|
||||
@@ -0,0 +1,63 @@
|
||||
/**
|
||||
* Nightly purge of expired tombstones for every soft-delete model the
|
||||
* register hands to it (`SOFT_DELETE_RETENTION`, kind `purge`).
|
||||
*
|
||||
* One leg per table, one table after the other: the tables are small next to
|
||||
* the sync domains, and the job has the night. A leg that stops at its batch
|
||||
* cap, or leaves an account under restore for the next night, says so in the
|
||||
* run report rather than looking like a night with nothing to do.
|
||||
*/
|
||||
import type { Job } from "pg-boss";
|
||||
|
||||
import { jobDone, type JobOutcome } from "@/lib/jobs/job-outcome";
|
||||
import { getWorkerPrisma } from "@/lib/jobs/reminder/shared";
|
||||
import { purgeSoftDeletedRows } from "@/lib/jobs/soft-delete-retention";
|
||||
import { withBackgroundEvent } from "@/lib/logging/background";
|
||||
|
||||
export const SOFT_DELETE_PURGE_QUEUE = "soft-delete-purge";
|
||||
/** Daily at 03:42 Europe/Berlin, after the sync-domain tombstone cleanup. */
|
||||
export const SOFT_DELETE_PURGE_CRON = "42 3 * * *";
|
||||
|
||||
export interface SoftDeletePurgePayload {
|
||||
triggeredAt?: string;
|
||||
}
|
||||
|
||||
export async function handleSoftDeletePurge(
|
||||
jobs: Job<SoftDeletePurgePayload>[],
|
||||
): Promise<JobOutcome> {
|
||||
void jobs;
|
||||
return withBackgroundEvent("job.soft_delete_purge", async (evt) => {
|
||||
try {
|
||||
const legs = await purgeSoftDeletedRows(getWorkerPrisma());
|
||||
const deleted = legs.reduce((sum, leg) => sum + leg.deleted, 0);
|
||||
const drained = legs.every((leg) => leg.drained);
|
||||
const deferredAccounts = Math.max(
|
||||
0,
|
||||
...legs.map((leg) => leg.deferredAccounts),
|
||||
);
|
||||
evt.addMeta("soft_delete_purge_deleted", deleted);
|
||||
evt.addMeta("soft_delete_purge_drained", drained);
|
||||
evt.addMeta("soft_delete_purge_deferred_accounts", deferredAccounts);
|
||||
for (const leg of legs) {
|
||||
if (leg.deleted > 0) {
|
||||
evt.addMeta(`soft_delete_purge_${leg.table}`, leg.deleted);
|
||||
}
|
||||
}
|
||||
if (!drained) {
|
||||
evt.addWarning(
|
||||
"soft-delete-purge stopped at the batch cap; a backlog remains for the next run",
|
||||
);
|
||||
}
|
||||
return jobDone({
|
||||
deleted,
|
||||
drained,
|
||||
deferred_accounts: deferredAccounts,
|
||||
});
|
||||
} catch (err) {
|
||||
// Rethrow, as the sync-domain purge does: a retention purge that fails
|
||||
// quietly every night looks exactly like one with nothing to do.
|
||||
evt.addWarning(`soft-delete-purge failed: ${err}`);
|
||||
throw err;
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,178 @@
|
||||
/**
|
||||
* How long a soft-deleted row is kept, for every model that soft-deletes.
|
||||
*
|
||||
* A delete in HealthLog tombstones the row (`deletedAt`) so the undo and the
|
||||
* restore endpoints can bring it back and the `/api/sync/changes` feed can
|
||||
* tell an offline phone about it. Without a horizon the tombstone is forever:
|
||||
* a deleted lab result, visit or allergy stayed in the database, in every
|
||||
* backup and in every key rotation, for as long as the instance ran.
|
||||
*
|
||||
* Every model with a `deletedAt` column has exactly one entry here
|
||||
* (`src/lib/jobs/__tests__/soft-delete-retention-register.test.ts` derives the
|
||||
* set from the schema). An entry says which job removes the tombstones and
|
||||
* after how long:
|
||||
*
|
||||
* - `sync-feed`: the three dense sync domains, purged by the nightly
|
||||
* `measurement-tombstone-cleanup` job after `TOMBSTONE_RETENTION_DAYS`.
|
||||
* - `document-grace`: the document vault, purged by `document-tombstone-purge`
|
||||
* after its 30-day undo grace (it also carries the import keys over).
|
||||
* - `purge`: everything else, purged here by `soft-delete-purge`. The horizon
|
||||
* is `TOMBSTONE_RETENTION_DAYS` unless the entry says otherwise: the same as
|
||||
* the sync feed's (two of these models ride it, and a cursor older than the
|
||||
* horizon is already answered with `cursorExpired`), and long past any undo.
|
||||
* A restore endpoint asked for a purged row finds nothing, as it does for a
|
||||
* purged measurement.
|
||||
*
|
||||
* The delete runs per account under the restore lock, in bounded batches
|
||||
* (`purgeTombstonesByAccount`). Foreign keys into these tables cascade or set
|
||||
* null (none restricts), so a purged visit takes its link rows with it and a
|
||||
* live vaccination that named it simply loses the pointer.
|
||||
*/
|
||||
import type { PrismaClient } from "@/generated/prisma/client";
|
||||
import { TOMBSTONE_RETENTION_DAYS } from "@/lib/auth/native-client";
|
||||
import { getCoachMessageRetentionDays } from "@/lib/jobs/coach-message-cleanup";
|
||||
import { DOCUMENT_PURGE_GRACE_DAYS } from "@/lib/jobs/document-purge";
|
||||
import {
|
||||
purgeTombstonesByAccount,
|
||||
type TombstonePurgeOutcome,
|
||||
} from "@/lib/jobs/measurement-tombstone-cleanup";
|
||||
|
||||
export type SoftDeleteRetention =
|
||||
| { kind: "sync-feed"; days: number }
|
||||
| { kind: "document-grace"; days: number }
|
||||
| {
|
||||
kind: "purge";
|
||||
/** The physical table; a literal, spliced into the purge SQL. */
|
||||
table: string;
|
||||
/** Days a tombstone is kept. */
|
||||
days: () => number;
|
||||
/**
|
||||
* Extra condition a purgeable row must meet, a literal SQL fragment over
|
||||
* the alias `t`. For a tombstone something live still depends on.
|
||||
*/
|
||||
keep?: string;
|
||||
/** Why the horizon or the condition differs from the default. */
|
||||
why?: string;
|
||||
};
|
||||
|
||||
const standard = (table: string): SoftDeleteRetention => ({
|
||||
kind: "purge",
|
||||
table,
|
||||
days: () => TOMBSTONE_RETENTION_DAYS,
|
||||
});
|
||||
|
||||
export const SOFT_DELETE_RETENTION: Readonly<
|
||||
Record<string, SoftDeleteRetention>
|
||||
> = {
|
||||
Measurement: { kind: "sync-feed", days: TOMBSTONE_RETENTION_DAYS },
|
||||
MoodEntry: { kind: "sync-feed", days: TOMBSTONE_RETENTION_DAYS },
|
||||
MedicationIntakeEvent: { kind: "sync-feed", days: TOMBSTONE_RETENTION_DAYS },
|
||||
InboundDocument: { kind: "document-grace", days: DOCUMENT_PURGE_GRACE_DAYS },
|
||||
|
||||
// Both ride the sync feed; the default horizon is the feed's.
|
||||
CycleDayLog: standard("cycle_day_logs"),
|
||||
MenstrualCycle: {
|
||||
kind: "purge",
|
||||
table: "menstrual_cycles",
|
||||
days: () => TOMBSTONE_RETENTION_DAYS,
|
||||
keep: `NOT EXISTS (SELECT 1 FROM "menstrual_cycles" a
|
||||
WHERE a."id" = t."absorbed_into_id"
|
||||
AND a."deleted_at" IS NULL)`,
|
||||
why: "A start folded into a later cycle is tombstoned with `absorbedIntoId` and given back when that cycle is deleted (`cycle-boundaries.ts`), so it stays while the cycle that absorbed it is live.",
|
||||
},
|
||||
CoachFact: {
|
||||
kind: "purge",
|
||||
table: "coach_facts",
|
||||
days: getCoachMessageRetentionDays,
|
||||
why: "A stored answer's trail can name a removed medication fact, and the trail is withheld against the fact rows (`trail-read.ts`); the fact therefore outlives every message that may name it, which is the coach message retention.",
|
||||
},
|
||||
|
||||
MentalHealthAssessment: standard("mental_health_assessments"),
|
||||
MeasurementReminder: standard("measurement_reminders"),
|
||||
LabResult: standard("lab_results"),
|
||||
CustomMetric: standard("custom_metrics"),
|
||||
CustomMetricEntry: standard("custom_metric_entries"),
|
||||
CoachPlan: standard("coach_plans"),
|
||||
CoachReminder: standard("coach_reminders"),
|
||||
IllnessEpisode: standard("illness_episodes"),
|
||||
IllnessDayLog: standard("illness_day_logs"),
|
||||
Allergy: standard("allergies"),
|
||||
FamilyHistoryEntry: standard("family_history_entries"),
|
||||
Encounter: standard("encounters"),
|
||||
Practitioner: standard("practitioners"),
|
||||
VaccinationRecord: standard("vaccination_records"),
|
||||
CustomVaccine: standard("custom_vaccines"),
|
||||
LifeEvent: standard("life_events"),
|
||||
};
|
||||
|
||||
const TABLE = /^[a-z_]+$/;
|
||||
const DAY_MS = 86_400_000;
|
||||
|
||||
/** One table's purge, for the run report. */
|
||||
export interface SoftDeletePurgeLeg extends TombstonePurgeOutcome {
|
||||
model: string;
|
||||
table: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Purge one register entry's expired tombstones. The table name and the extra
|
||||
* condition come from the register's literals, never from input; the name is
|
||||
* asserted against a closed pattern before it is spliced.
|
||||
*/
|
||||
export async function purgeSoftDeletedTable(
|
||||
prisma: PrismaClient,
|
||||
model: string,
|
||||
now: Date = new Date(),
|
||||
): Promise<SoftDeletePurgeLeg> {
|
||||
const entry = SOFT_DELETE_RETENTION[model];
|
||||
if (!entry || entry.kind !== "purge" || !TABLE.test(entry.table)) {
|
||||
throw new Error(`No purge entry for ${model}`);
|
||||
}
|
||||
const table = `"${entry.table}"`;
|
||||
const cutoff = new Date(now.getTime() - entry.days() * DAY_MS);
|
||||
const keep = entry.keep ? `AND ${entry.keep}` : "";
|
||||
const outcome = await purgeTombstonesByAccount({
|
||||
prisma,
|
||||
findRows: (take, skipUserIds) =>
|
||||
prisma.$queryRawUnsafe<Array<{ id: string; userId: string }>>(
|
||||
`SELECT t."id", t."user_id" AS "userId" FROM ${table} t
|
||||
WHERE t."deleted_at" IS NOT NULL AND t."deleted_at" < $1
|
||||
AND NOT (t."user_id" = ANY($2::text[])) ${keep}
|
||||
LIMIT $3`,
|
||||
cutoff,
|
||||
skipUserIds,
|
||||
take,
|
||||
),
|
||||
// The row is re-checked inside the delete: a restore between the read and
|
||||
// the delete un-tombstones it, and then it stays.
|
||||
deleteIds: (tx, ids, userId) =>
|
||||
tx.$executeRawUnsafe(
|
||||
`DELETE FROM ${table} t
|
||||
WHERE t."id" = ANY($1::text[]) AND t."user_id" = $2
|
||||
AND t."deleted_at" IS NOT NULL AND t."deleted_at" < $3 ${keep}`,
|
||||
ids,
|
||||
userId,
|
||||
cutoff,
|
||||
),
|
||||
});
|
||||
return { ...outcome, model, table: entry.table };
|
||||
}
|
||||
|
||||
/** Every `purge` entry of the register, in declaration order. */
|
||||
export function purgeModels(): string[] {
|
||||
return Object.entries(SOFT_DELETE_RETENTION)
|
||||
.filter(([, entry]) => entry.kind === "purge")
|
||||
.map(([model]) => model);
|
||||
}
|
||||
|
||||
/** Run every `purge` entry, one table after the other. */
|
||||
export async function purgeSoftDeletedRows(
|
||||
prisma: PrismaClient,
|
||||
now: Date = new Date(),
|
||||
): Promise<SoftDeletePurgeLeg[]> {
|
||||
const legs: SoftDeletePurgeLeg[] = [];
|
||||
for (const model of purgeModels()) {
|
||||
legs.push(await purgeSoftDeletedTable(prisma, model, now));
|
||||
}
|
||||
return legs;
|
||||
}
|
||||
@@ -72,7 +72,6 @@ const subsystemSurface = {
|
||||
"rollup-full-backfill": { audience: "system" },
|
||||
"mood-rollup-recompute": { audience: "system" },
|
||||
"mood-rollup-full-backfill": { audience: "system" },
|
||||
"medication-compliance-full-backfill": { audience: "system" },
|
||||
"step-consolidation": { audience: "system" },
|
||||
"step-consolidation-repair": { audience: "system" },
|
||||
"cumulative-pr-rederive": { audience: "system" },
|
||||
@@ -128,6 +127,8 @@ const subsystemSurface = {
|
||||
"mood-prognosis-refresh": { audience: "system" },
|
||||
"achievement-unlock-sweep": { audience: "system" },
|
||||
"measurement-tombstone-cleanup": { audience: "system" },
|
||||
"soft-delete-purge": { audience: "system" },
|
||||
"auth-ledger-cleanup": { audience: "system" },
|
||||
"coach-message-cleanup": { audience: "system" },
|
||||
"workout-insight-claim-cleanup": { audience: "system" },
|
||||
"note-encryption-backfill": { audience: "system" },
|
||||
|
||||
@@ -35,8 +35,8 @@ vi.mock("@/lib/db", () => ({
|
||||
|
||||
vi.mock("@/lib/jobs/boss-instance", () => ({ getGlobalBoss: vi.fn() }));
|
||||
vi.mock("@/lib/logging/context", () => ({ annotate: vi.fn() }));
|
||||
vi.mock("@/lib/rollups/medication-compliance-rollups", () => ({
|
||||
recomputeMedicationComplianceForEvent: vi.fn().mockResolvedValue(undefined),
|
||||
vi.mock("@/lib/medications/dose-day-refresh", () => ({
|
||||
refreshDoseSlot: vi.fn().mockResolvedValue(undefined),
|
||||
}));
|
||||
|
||||
import { dedupeUserIntakeSlots } from "../intake-slot-dedup";
|
||||
|
||||
@@ -27,8 +27,8 @@ vi.mock("@/lib/db", () => ({
|
||||
|
||||
vi.mock("@/lib/jobs/boss-instance", () => ({ getGlobalBoss: vi.fn() }));
|
||||
vi.mock("@/lib/logging/context", () => ({ annotate: vi.fn() }));
|
||||
vi.mock("@/lib/rollups/medication-compliance-rollups", () => ({
|
||||
recomputeMedicationComplianceForEvent: vi.fn().mockResolvedValue(undefined),
|
||||
vi.mock("@/lib/medications/dose-day-refresh", () => ({
|
||||
refreshDoseSlot: vi.fn().mockResolvedValue(undefined),
|
||||
}));
|
||||
vi.mock("@/lib/cache/invalidate", () => ({
|
||||
invalidateUserMedications: vi.fn(),
|
||||
|
||||
@@ -25,13 +25,13 @@ vi.mock("@/lib/db", () => ({
|
||||
|
||||
vi.mock("@/lib/jobs/boss-instance", () => ({ getGlobalBoss: vi.fn() }));
|
||||
vi.mock("@/lib/logging/context", () => ({ annotate: vi.fn() }));
|
||||
vi.mock("@/lib/rollups/medication-compliance-rollups", () => ({
|
||||
recomputeMedicationComplianceForEvent: vi.fn().mockResolvedValue(undefined),
|
||||
vi.mock("@/lib/medications/dose-day-refresh", () => ({
|
||||
refreshDoseSlot: vi.fn().mockResolvedValue(undefined),
|
||||
}));
|
||||
|
||||
import { dedupeUserIntakeSlots } from "../intake-slot-dedup";
|
||||
import { prisma } from "@/lib/db";
|
||||
import { recomputeMedicationComplianceForEvent } from "@/lib/rollups/medication-compliance-rollups";
|
||||
import { refreshDoseSlot } from "@/lib/medications/dose-day-refresh";
|
||||
|
||||
const TZ = "Europe/Berlin";
|
||||
|
||||
@@ -156,7 +156,7 @@ describe("dedupeUserIntakeSlots — per-slot error isolation", () => {
|
||||
// succeeded → exactly one normalised row counted.
|
||||
expect(summary.rowsNormalised).toBe(1);
|
||||
// Both canonical days recomputed despite the collision.
|
||||
expect(recomputeMedicationComplianceForEvent).toHaveBeenCalledTimes(2);
|
||||
expect(refreshDoseSlot).toHaveBeenCalledTimes(2);
|
||||
});
|
||||
|
||||
it("isolates a non-P2002 error per slot without dead-lettering the job", async () => {
|
||||
|
||||
@@ -45,8 +45,8 @@ vi.mock("@/lib/db", () => ({
|
||||
|
||||
vi.mock("@/lib/jobs/boss-instance", () => ({ getGlobalBoss: vi.fn() }));
|
||||
vi.mock("@/lib/logging/context", () => ({ annotate: vi.fn() }));
|
||||
vi.mock("@/lib/rollups/medication-compliance-rollups", () => ({
|
||||
recomputeMedicationComplianceForEvent: vi.fn().mockResolvedValue(undefined),
|
||||
vi.mock("@/lib/medications/dose-day-refresh", () => ({
|
||||
refreshDoseSlot: vi.fn().mockResolvedValue(undefined),
|
||||
}));
|
||||
|
||||
import { dedupeUserIntakeSlots } from "../intake-slot-dedup";
|
||||
|
||||
@@ -0,0 +1,62 @@
|
||||
/**
|
||||
* The write-path hook every dose mutation funnels through: after a dose of a
|
||||
* medication was taken, skipped, corrected, deleted, undone or recorded
|
||||
* automatically, the rows derived from that dose's local day are recomputed.
|
||||
*
|
||||
* Today that is one derived row family, the micronutrients a taken
|
||||
* supplement dose adds (`refreshSupplementNutrientsForDose`). The hook used to
|
||||
* upsert a per-day compliance rollup as well; that table had no reader left
|
||||
* and was dropped, so this is what remains of it, under a name that says what
|
||||
* it does.
|
||||
*
|
||||
* Best-effort, like every derived row: a failure is reported by the refresh
|
||||
* itself and never fails the dose write.
|
||||
*/
|
||||
import { refreshSupplementNutrientsForDose } from "@/lib/medications/ingredients/recompute";
|
||||
import { DEFAULT_TIMEZONE, isValidTimezone, userDayKey } from "@/lib/tz/format";
|
||||
|
||||
function safeTimezone(tz: string | null | undefined): string {
|
||||
return tz && isValidTimezone(tz) ? tz : DEFAULT_TIMEZONE;
|
||||
}
|
||||
|
||||
/**
|
||||
* The account-local day (`YYYY-MM-DD`) a dose slot belongs to: the local day
|
||||
* of its `scheduledFor` in the account's timezone, falling back to the server
|
||||
* default for a missing or unknown zone.
|
||||
*/
|
||||
export function dayKeyForScheduledFor(
|
||||
scheduledFor: Date,
|
||||
tz: string | null | undefined,
|
||||
): string {
|
||||
return userDayKey(scheduledFor, safeTimezone(tz));
|
||||
}
|
||||
|
||||
/** Refresh the derived rows of one medication's local day. */
|
||||
export async function refreshDoseDay(input: {
|
||||
userId: string;
|
||||
medicationId: string;
|
||||
dayKey: string;
|
||||
tz: string | null | undefined;
|
||||
}): Promise<void> {
|
||||
await refreshSupplementNutrientsForDose({
|
||||
userId: input.userId,
|
||||
medicationId: input.medicationId,
|
||||
dayKey: input.dayKey,
|
||||
timezone: safeTimezone(input.tz),
|
||||
});
|
||||
}
|
||||
|
||||
/** Refresh the derived rows of the local day a dose slot sits on. */
|
||||
export async function refreshDoseSlot(input: {
|
||||
userId: string;
|
||||
medicationId: string;
|
||||
scheduledFor: Date;
|
||||
tz: string | null | undefined;
|
||||
}): Promise<void> {
|
||||
await refreshDoseDay({
|
||||
userId: input.userId,
|
||||
medicationId: input.medicationId,
|
||||
dayKey: dayKeyForScheduledFor(input.scheduledFor, input.tz),
|
||||
tz: input.tz,
|
||||
});
|
||||
}
|
||||
@@ -59,7 +59,7 @@ import type {
|
||||
WorkerMedicationRow,
|
||||
WorkerScheduleRow,
|
||||
} from "@/lib/medications/scheduling/worker-helpers";
|
||||
import { recomputeMedicationComplianceForEvent } from "@/lib/rollups/medication-compliance-rollups";
|
||||
import { refreshDoseSlot } from "@/lib/medications/dose-day-refresh";
|
||||
|
||||
export const INTAKE_SLOT_DEDUP_QUEUE = "intake-slot-dedup";
|
||||
|
||||
@@ -462,7 +462,7 @@ export async function dedupeUserIntakeSlots(
|
||||
const sep = key.indexOf("|");
|
||||
const medicationId = key.slice(0, sep);
|
||||
const instantIso = key.slice(sep + 1);
|
||||
await recomputeMedicationComplianceForEvent({
|
||||
await refreshDoseSlot({
|
||||
userId,
|
||||
medicationId,
|
||||
scheduledFor: new Date(instantIso),
|
||||
|
||||
@@ -18,9 +18,6 @@
|
||||
* 2. Reads existing rows in the today-window.
|
||||
* 3. Inserts the missing rows (`skipDuplicates: true` for the
|
||||
* `(userId, medicationId, scheduledFor, source)` unique index).
|
||||
* 4. Fires one compliance-rollup recompute per distinct
|
||||
* `(medicationId, dayKey)` so the rollup matches the new
|
||||
* `scheduled` count before the next read.
|
||||
*
|
||||
* The caller owns its own typed re-read of `MedicationIntakeEvent`
|
||||
* afterwards — the two routes return different projections of those
|
||||
@@ -35,14 +32,12 @@ import {
|
||||
scheduleEmitsInWindow,
|
||||
} from "@/lib/medications/scheduling/worker-helpers";
|
||||
import { localHmAsUtc } from "@/lib/tz/local-day";
|
||||
import { recomputeMedicationComplianceForEvent } from "@/lib/rollups/medication-compliance-rollups";
|
||||
import { TRACKED_INTAKE_WHERE } from "@/lib/medications/intake-tracking";
|
||||
import {
|
||||
isBeforeLiveEra,
|
||||
LIVE_ERA_REVISION_ARGS,
|
||||
liveEraStart,
|
||||
} from "@/lib/medications/scheduling/live-era";
|
||||
import { dayKeyForScheduledFor } from "@/lib/rollups/medication-compliance-rollups";
|
||||
|
||||
export interface ProjectTodayIntakesResult {
|
||||
projected: number;
|
||||
@@ -224,36 +219,5 @@ export async function projectTodayIntakesAndRecompute(input: {
|
||||
skipDuplicates: true,
|
||||
});
|
||||
|
||||
// Close the v1.4.39.4 compliance-rollup hook gap: a bulk projection
|
||||
// mints fresh `(medicationId, scheduledFor)` rows in PENDING state;
|
||||
// without this hook the rollup row stays at its previous
|
||||
// `scheduled` count, which inflates compliance % until the user
|
||||
// actually logs against the new row. Coalesce by `(medicationId,
|
||||
// dayKey)` so we fire one recompute per distinct day-tuple instead
|
||||
// of one per row.
|
||||
//
|
||||
// `Promise.allSettled` keeps the best-effort contract explicit at
|
||||
// the call site: the helper swallows internally today, but a
|
||||
// future refactor that lets a throw escape would otherwise turn
|
||||
// the parent POST into a 5xx.
|
||||
const seenDayKeys = new Set<string>();
|
||||
const recomputeJobs: Array<Promise<void>> = [];
|
||||
for (const m of missing) {
|
||||
// The compliance rollup's own day for the slot: two slots on different
|
||||
// local days that share a UTC date must each get their recompute.
|
||||
const key = `${m.medicationId}|${dayKeyForScheduledFor(m.scheduledFor, userTz)}`;
|
||||
if (seenDayKeys.has(key)) continue;
|
||||
seenDayKeys.add(key);
|
||||
recomputeJobs.push(
|
||||
recomputeMedicationComplianceForEvent({
|
||||
userId,
|
||||
medicationId: m.medicationId,
|
||||
scheduledFor: m.scheduledFor,
|
||||
tz: userTz,
|
||||
}),
|
||||
);
|
||||
}
|
||||
await Promise.allSettled(recomputeJobs);
|
||||
|
||||
return { projected: projected.length, backfilled: missing.length };
|
||||
}
|
||||
@@ -1355,13 +1355,15 @@ export const medicationImportProgress = z
|
||||
touchedDays: z
|
||||
.array(z.string())
|
||||
.describe(
|
||||
"Local calendar days (YYYY-MM-DD) touched so far, driving the chunked rollup recompute behind the write.",
|
||||
"Deprecated and inert: always `[]` on a job admitted from v1.43.8, when the per-day compliance rollup it drove was removed. A job admitted earlier keeps the days it recorded. Kept for clients that decode it; do not read it.",
|
||||
),
|
||||
rollupProcessed: z
|
||||
.number()
|
||||
.int()
|
||||
.min(0)
|
||||
.describe("Of `touchedDays`, how many have had their rollup recomputed."),
|
||||
.describe(
|
||||
"Deprecated and inert: always `0` on a job admitted from v1.43.8. Kept for clients that decode it; do not read it.",
|
||||
),
|
||||
})
|
||||
.meta({
|
||||
id: "MedicationImportProgress",
|
||||
|
||||
@@ -2326,7 +2326,7 @@ export const profilePaths: NonNullable<ZodOpenApiObject["paths"]> = {
|
||||
summary: "Set the account timezone",
|
||||
description:
|
||||
"Lifted out of the profile patch so the picker can send one field without round-tripping the rest. Accepts any zone the running engine's `Intl` knows; anything else is 422 rather than stored, so the resolver never has to defend against a poisoned column. Audit-logged with the old and new zone.\n\n" +
|
||||
"A change is not only a display preference. The medication-compliance rollup keys its rows by the LOCAL day a dose fell on, minted under whatever zone was in force at write time, so moving the zone re-buckets that history and the stored keys go stale — and the coverage probe compares counts rather than keys, so it cannot notice. The route therefore enqueues a re-fold of the trailing window whenever the zone actually changes. Best-effort and de-duplicated, so rapid re-picks collapse into one.",
|
||||
"A change is not only a display preference. The supplement nutrient rows key on the LOCAL day a dose fell on, minted under whatever zone was in force at write time, so moving the zone re-buckets them. The route therefore enqueues a recompute of the account's supplement nutrients whenever the zone actually changes. Best-effort and de-duplicated, so rapid re-picks collapse into one.",
|
||||
requestBody: {
|
||||
required: true,
|
||||
content: { "application/json": { schema: timezonePutRequest } },
|
||||
|
||||
@@ -19,8 +19,8 @@
|
||||
* - All-time scan rather than a rolling window. The
|
||||
* `personal_records_user_metric_idx` index makes
|
||||
* `ORDER BY value LIMIT 1` constant-time; reading the worst-case
|
||||
* ~30k measurement rows per metric is ~50 ms with the typed
|
||||
* `measurements_user_id_type_measured_at_idx` index.
|
||||
* ~30k measurement rows per metric is ~50 ms over the
|
||||
* `(user_id, type, measured_at, …)` unique index.
|
||||
* - Tie handling: an equal-value reading writes a row but the caller
|
||||
* suppresses the push notification (the user matched, not beat
|
||||
* their best). The notification suppression lives on the job
|
||||
|
||||
@@ -1,403 +0,0 @@
|
||||
/**
|
||||
* v1.4.39 — unit tests for the medication-compliance rollup tier.
|
||||
*
|
||||
* `prisma` is mocked at the module boundary so the test can pin the
|
||||
* intake-event fan-out + the rollup upsert / read shape without
|
||||
* standing up a real Postgres. Integration coverage (real Postgres +
|
||||
* the FK cascade behaviour) lives next to the route test suite when
|
||||
* the W-MED phase grows them; this file proves the helpers'
|
||||
* contract.
|
||||
*/
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
||||
|
||||
const mocks = vi.hoisted(() => ({
|
||||
intakeFindMany: vi.fn(),
|
||||
rollupUpsert: vi.fn(),
|
||||
rollupDeleteMany: vi.fn(),
|
||||
rollupFindMany: vi.fn(),
|
||||
rollupFindFirst: vi.fn(),
|
||||
queryRaw: vi.fn(),
|
||||
executeRaw: vi.fn(),
|
||||
bossSend: vi.fn(),
|
||||
getGlobalBossMock: vi.fn(),
|
||||
}));
|
||||
|
||||
vi.mock("@/lib/db", () => ({
|
||||
prisma: {
|
||||
medicationIntakeEvent: {
|
||||
findMany: mocks.intakeFindMany,
|
||||
},
|
||||
medicationComplianceRollup: {
|
||||
upsert: mocks.rollupUpsert,
|
||||
deleteMany: mocks.rollupDeleteMany,
|
||||
findMany: mocks.rollupFindMany,
|
||||
findFirst: mocks.rollupFindFirst,
|
||||
},
|
||||
$queryRaw: mocks.queryRaw,
|
||||
$executeRaw: mocks.executeRaw,
|
||||
},
|
||||
}));
|
||||
|
||||
vi.mock("@/lib/jobs/boss-instance", () => ({
|
||||
getGlobalBoss: () => mocks.getGlobalBossMock(),
|
||||
}));
|
||||
|
||||
vi.mock("@/lib/logging/context", () => ({
|
||||
annotate: vi.fn(),
|
||||
}));
|
||||
|
||||
import {
|
||||
dayKeyForScheduledFor,
|
||||
recomputeMedicationComplianceForDay,
|
||||
recomputeMedicationComplianceForEvent,
|
||||
recomputeUserMedicationCompliance,
|
||||
enqueueBootTimeMedicationComplianceBackfill,
|
||||
enqueueUserMedicationComplianceBackfill,
|
||||
MEDICATION_COMPLIANCE_BACKFILL_QUEUE,
|
||||
} from "../medication-compliance-rollups";
|
||||
|
||||
const {
|
||||
intakeFindMany,
|
||||
rollupUpsert,
|
||||
rollupDeleteMany,
|
||||
rollupFindMany,
|
||||
rollupFindFirst,
|
||||
queryRaw,
|
||||
executeRaw,
|
||||
bossSend,
|
||||
getGlobalBossMock,
|
||||
} = mocks;
|
||||
|
||||
beforeEach(() => {
|
||||
vi.resetAllMocks();
|
||||
intakeFindMany.mockResolvedValue([]);
|
||||
rollupUpsert.mockResolvedValue({});
|
||||
rollupDeleteMany.mockResolvedValue({ count: 0 });
|
||||
rollupFindMany.mockResolvedValue([]);
|
||||
rollupFindFirst.mockResolvedValue(null);
|
||||
queryRaw.mockResolvedValue([]);
|
||||
executeRaw.mockResolvedValue(0);
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
vi.useRealTimers();
|
||||
});
|
||||
|
||||
describe("dayKeyForScheduledFor", () => {
|
||||
it("anchors the dayKey on the user's tz wall clock", () => {
|
||||
// 2026-05-17T23:30:00Z — Berlin local is 2026-05-18 01:30 (CEST).
|
||||
const ts = new Date("2026-05-17T23:30:00.000Z");
|
||||
expect(dayKeyForScheduledFor(ts, "Europe/Berlin")).toBe("2026-05-18");
|
||||
// Los Angeles is UTC-7 (PDT) — still 2026-05-17 16:30 local.
|
||||
expect(dayKeyForScheduledFor(ts, "America/Los_Angeles")).toBe("2026-05-17");
|
||||
});
|
||||
|
||||
it("falls back to the server default when tz is null", () => {
|
||||
const ts = new Date("2026-05-17T22:00:00.000Z");
|
||||
expect(dayKeyForScheduledFor(ts, null)).toBe(
|
||||
dayKeyForScheduledFor(ts, "Europe/Berlin"),
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe("recomputeMedicationComplianceForDay", () => {
|
||||
/**
|
||||
* Helper: pull the bind-variable values out of a Prisma tagged-template
|
||||
* call so the test can inspect the user-id / day / window bounds the
|
||||
* SQL was rendered with. Prisma's `$executeRaw` tag passes the
|
||||
* template strings as the first arg and the substituted values as the
|
||||
* remaining positional args.
|
||||
*/
|
||||
function bindValues(call: unknown[]): unknown[] {
|
||||
return call.slice(1);
|
||||
}
|
||||
|
||||
it("runs an atomic INSERT … ON CONFLICT upsert with the (user, medication, day) tuple", async () => {
|
||||
// QA F-H-02 (v1.4.39): the recompute is now a single SQL statement
|
||||
// that re-aggregates inside the upsert; the JS pre-fold from the
|
||||
// previous tier is gone so the test inspects the bind values
|
||||
// instead of the intake fold.
|
||||
await recomputeMedicationComplianceForDay(
|
||||
"user-1",
|
||||
"med-1",
|
||||
"2026-05-18",
|
||||
"Europe/Berlin",
|
||||
);
|
||||
|
||||
// Two statements fire: the INSERT … ON CONFLICT upsert and the
|
||||
// companion DELETE for the "all events removed" branch.
|
||||
expect(executeRaw).toHaveBeenCalledTimes(2);
|
||||
const insertBinds = bindValues(executeRaw.mock.calls[0]);
|
||||
expect(insertBinds).toContain("user-1");
|
||||
expect(insertBinds).toContain("med-1");
|
||||
expect(insertBinds).toContain("2026-05-18");
|
||||
const deleteBinds = bindValues(executeRaw.mock.calls[1]);
|
||||
expect(deleteBinds).toContain("user-1");
|
||||
expect(deleteBinds).toContain("med-1");
|
||||
expect(deleteBinds).toContain("2026-05-18");
|
||||
});
|
||||
|
||||
it("aggregates at SLOT level so cross-source duplicate rows cannot inflate scheduled", async () => {
|
||||
// v1.15.19 — the partial unique index carries `source`, so a pending
|
||||
// REMINDER row and a taken API row can share one `scheduled_for`
|
||||
// instant. A row-level COUNT(*) counted that slot twice. Pin that the
|
||||
// statement groups by `scheduled_for` and folds each slot with
|
||||
// taken-beats-skipped priority (BOOL_OR) before counting. Semantics
|
||||
// (two live rows, one slot, different source → scheduled=1, taken=1)
|
||||
// are proven on real Postgres in
|
||||
// `tests/integration/medication-compliance-rollup-slot.test.ts`.
|
||||
await recomputeMedicationComplianceForDay(
|
||||
"user-1",
|
||||
"med-1",
|
||||
"2026-05-18",
|
||||
"Europe/Berlin",
|
||||
);
|
||||
|
||||
const insertSql = (executeRaw.mock.calls[0][0] as readonly string[]).join(
|
||||
"?",
|
||||
);
|
||||
expect(insertSql).toMatch(/GROUP BY\s+"scheduled_for"/);
|
||||
expect(insertSql).toMatch(
|
||||
/BOOL_OR\("taken_at" IS NOT NULL AND NOT "skipped"\)/,
|
||||
);
|
||||
expect(insertSql).toMatch(/BOOL_OR\("skipped"\)/);
|
||||
// Slot-level taken beats skipped: a slot is `skipped` only when no row
|
||||
// in it is taken.
|
||||
expect(insertSql).toMatch(/NOT "slot_taken" AND "slot_skipped"/);
|
||||
// Scheduled counts slots (the grouped CTE), not raw event rows.
|
||||
expect(insertSql).toMatch(/FROM slot/);
|
||||
});
|
||||
|
||||
it("emits a paired DELETE so a fully-cleared day removes its row", async () => {
|
||||
// The DELETE fires unconditionally and gates on NOT EXISTS — when
|
||||
// the day still has events the predicate matches zero rows. The
|
||||
// test pins that the DELETE statement was issued so a future
|
||||
// refactor that drops the second statement breaks here.
|
||||
await recomputeMedicationComplianceForDay(
|
||||
"user-1",
|
||||
"med-1",
|
||||
"2026-05-18",
|
||||
"Europe/Berlin",
|
||||
);
|
||||
|
||||
expect(executeRaw).toHaveBeenCalledTimes(2);
|
||||
});
|
||||
|
||||
it("is idempotent across repeated invocations", async () => {
|
||||
await recomputeMedicationComplianceForDay(
|
||||
"user-1",
|
||||
"med-1",
|
||||
"2026-05-18",
|
||||
"Europe/Berlin",
|
||||
);
|
||||
await recomputeMedicationComplianceForDay(
|
||||
"user-1",
|
||||
"med-1",
|
||||
"2026-05-18",
|
||||
"Europe/Berlin",
|
||||
);
|
||||
|
||||
expect(executeRaw).toHaveBeenCalledTimes(4);
|
||||
expect(bindValues(executeRaw.mock.calls[0])).toEqual(
|
||||
bindValues(executeRaw.mock.calls[2]),
|
||||
);
|
||||
expect(bindValues(executeRaw.mock.calls[1])).toEqual(
|
||||
bindValues(executeRaw.mock.calls[3]),
|
||||
);
|
||||
});
|
||||
|
||||
it("binds a tz-anchored UTC window into the upsert", async () => {
|
||||
await recomputeMedicationComplianceForDay(
|
||||
"user-1",
|
||||
"med-1",
|
||||
"2026-05-18",
|
||||
"Europe/Berlin",
|
||||
);
|
||||
|
||||
const insertBinds = bindValues(executeRaw.mock.calls[0]);
|
||||
const insertDates = insertBinds.filter((b): b is Date => b instanceof Date);
|
||||
// Berlin 2026-05-18 00:00 → UTC 2026-05-17 22:00 (CEST is UTC+2).
|
||||
expect(insertDates[0].toISOString()).toBe("2026-05-17T22:00:00.000Z");
|
||||
// Window closes 24h later — Berlin 2026-05-19 00:00 → UTC 2026-05-18 22:00.
|
||||
expect(insertDates[1].toISOString()).toBe("2026-05-18T22:00:00.000Z");
|
||||
});
|
||||
|
||||
it("buckets the same UTC instant on different days for Berlin vs LA", async () => {
|
||||
await recomputeMedicationComplianceForDay(
|
||||
"user-1",
|
||||
"med-1",
|
||||
"2026-05-18",
|
||||
"Europe/Berlin",
|
||||
);
|
||||
const berlinInsert = bindValues(executeRaw.mock.calls[0]).filter(
|
||||
(b): b is Date => b instanceof Date,
|
||||
);
|
||||
|
||||
executeRaw.mockClear();
|
||||
|
||||
await recomputeMedicationComplianceForDay(
|
||||
"user-2",
|
||||
"med-2",
|
||||
"2026-05-17",
|
||||
"America/Los_Angeles",
|
||||
);
|
||||
const laInsert = bindValues(executeRaw.mock.calls[0]).filter(
|
||||
(b): b is Date => b instanceof Date,
|
||||
);
|
||||
|
||||
// Berlin 2026-05-18 starts at UTC 2026-05-17 22:00.
|
||||
expect(berlinInsert[0].toISOString()).toBe("2026-05-17T22:00:00.000Z");
|
||||
// LA 2026-05-17 starts at UTC 2026-05-17 07:00 (PDT is UTC-7).
|
||||
expect(laInsert[0].toISOString()).toBe("2026-05-17T07:00:00.000Z");
|
||||
});
|
||||
|
||||
it("commits the strictest aggregate under concurrent recompute calls", async () => {
|
||||
// QA F-H-02 race pin: two concurrent recomputes for the same
|
||||
// (user, medication, day) must both go through the atomic SQL
|
||||
// path. The earlier `findMany`-then-`upsert` pattern could
|
||||
// interleave A-SELECT → B-SELECT → B-UPSERT (correct) → A-UPSERT
|
||||
// (stale N-1); the atomic upsert serialises on the row lock so
|
||||
// each statement re-aggregates a snapshot taken after the prior
|
||||
// commit. The mock can't reproduce real Postgres concurrency, but
|
||||
// the test pins that BOTH callers issue the atomic statement (not
|
||||
// the legacy read-aggregate-then-upsert) so a regression to the
|
||||
// racy pattern would fail this assertion.
|
||||
await Promise.all([
|
||||
recomputeMedicationComplianceForDay(
|
||||
"user-1",
|
||||
"med-1",
|
||||
"2026-05-18",
|
||||
"Europe/Berlin",
|
||||
),
|
||||
recomputeMedicationComplianceForDay(
|
||||
"user-1",
|
||||
"med-1",
|
||||
"2026-05-18",
|
||||
"Europe/Berlin",
|
||||
),
|
||||
]);
|
||||
// 2 callers × 2 statements (INSERT + DELETE) each.
|
||||
expect(executeRaw).toHaveBeenCalledTimes(4);
|
||||
// No legacy intake-findMany was issued — the atomic path replaces it.
|
||||
expect(intakeFindMany).not.toHaveBeenCalled();
|
||||
// No legacy rollupUpsert / rollupDeleteMany either.
|
||||
expect(rollupUpsert).not.toHaveBeenCalled();
|
||||
expect(rollupDeleteMany).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
describe("recomputeMedicationComplianceForEvent", () => {
|
||||
it("derives the dayKey from scheduledFor + tz then dispatches", async () => {
|
||||
// 2026-05-17T23:30:00Z → Berlin day 2026-05-18.
|
||||
await recomputeMedicationComplianceForEvent({
|
||||
userId: "user-1",
|
||||
medicationId: "med-1",
|
||||
scheduledFor: new Date("2026-05-17T23:30:00.000Z"),
|
||||
tz: "Europe/Berlin",
|
||||
});
|
||||
// The atomic upsert binds the dayKey as one of the positional
|
||||
// arguments; assert "2026-05-18" reached the SQL layer.
|
||||
const insertBinds = executeRaw.mock.calls[0].slice(1);
|
||||
expect(insertBinds).toContain("2026-05-18");
|
||||
});
|
||||
|
||||
it("swallows recompute errors so the parent write never blocks", async () => {
|
||||
executeRaw.mockRejectedValueOnce(new Error("DB melted"));
|
||||
|
||||
await expect(
|
||||
recomputeMedicationComplianceForEvent({
|
||||
userId: "user-1",
|
||||
medicationId: "med-1",
|
||||
scheduledFor: new Date("2026-05-17T12:00:00.000Z"),
|
||||
tz: "Europe/Berlin",
|
||||
}),
|
||||
).resolves.toBeUndefined();
|
||||
});
|
||||
});
|
||||
|
||||
describe("recomputeUserMedicationCompliance", () => {
|
||||
it("upserts one rollup per (medication, day) pair returned by discovery", async () => {
|
||||
queryRaw.mockResolvedValue([
|
||||
{ medication_id: "med-1", day: "2026-05-17" },
|
||||
{ medication_id: "med-1", day: "2026-05-18" },
|
||||
{ medication_id: "med-2", day: "2026-05-18" },
|
||||
]);
|
||||
|
||||
const result = await recomputeUserMedicationCompliance(
|
||||
"user-1",
|
||||
30,
|
||||
"Europe/Berlin",
|
||||
);
|
||||
|
||||
expect(result.rowsUpserted).toBe(3);
|
||||
// Each (medication, day) pair issues two atomic SQL statements:
|
||||
// the INSERT … ON CONFLICT upsert and the companion DELETE that
|
||||
// gates on `NOT EXISTS`.
|
||||
expect(executeRaw).toHaveBeenCalledTimes(6);
|
||||
});
|
||||
});
|
||||
|
||||
describe("enqueueBootTimeMedicationComplianceBackfill", () => {
|
||||
it("is a no-op when no boss is attached", async () => {
|
||||
getGlobalBossMock.mockReturnValue(null);
|
||||
const result = await enqueueBootTimeMedicationComplianceBackfill();
|
||||
expect(result).toEqual({ enqueued: 0, skipped: 0, error: null });
|
||||
expect(queryRaw).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("enqueues one job per uncovered user", async () => {
|
||||
getGlobalBossMock.mockReturnValue({ send: bossSend });
|
||||
queryRaw.mockResolvedValue([{ id: "user-1" }, { id: "user-2" }]);
|
||||
bossSend.mockResolvedValueOnce("job-1");
|
||||
bossSend.mockResolvedValueOnce(null); // coalesced
|
||||
const result = await enqueueBootTimeMedicationComplianceBackfill();
|
||||
expect(result.enqueued).toBe(1);
|
||||
expect(result.skipped).toBe(1);
|
||||
expect(result.error).toBeNull();
|
||||
expect(bossSend).toHaveBeenCalledWith(
|
||||
MEDICATION_COMPLIANCE_BACKFILL_QUEUE,
|
||||
expect.objectContaining({ userId: "user-1" }),
|
||||
expect.objectContaining({
|
||||
singletonKey: "medication-compliance-boot-backfill|user-1",
|
||||
}),
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe("enqueueUserMedicationComplianceBackfill", () => {
|
||||
it("is a no-op when no boss is attached", async () => {
|
||||
getGlobalBossMock.mockReturnValue(null);
|
||||
const result = await enqueueUserMedicationComplianceBackfill("user-1");
|
||||
expect(result).toEqual({ enqueued: false, error: null });
|
||||
// Critically: no `$queryRaw` cluster-wide LEFT JOIN scan.
|
||||
expect(queryRaw).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("enqueues exactly one job for the caller's user and never touches the cluster-wide LEFT JOIN", async () => {
|
||||
// QA F-SEC-M-01 (v1.4.39): pre-fix the request-path coverage-miss
|
||||
// path called `enqueueBootTimeMedicationComplianceBackfill` which
|
||||
// issued a cluster-wide `LEFT JOIN medication_intake_events ×
|
||||
// medication_compliance_rollups` on every hit. The user-scoped
|
||||
// helper sends one targeted job and never runs the discovery scan.
|
||||
getGlobalBossMock.mockReturnValue({ send: bossSend });
|
||||
bossSend.mockResolvedValueOnce("job-1");
|
||||
const result = await enqueueUserMedicationComplianceBackfill("user-1");
|
||||
expect(result).toEqual({ enqueued: true, error: null });
|
||||
expect(queryRaw).not.toHaveBeenCalled();
|
||||
expect(bossSend).toHaveBeenCalledTimes(1);
|
||||
expect(bossSend).toHaveBeenCalledWith(
|
||||
MEDICATION_COMPLIANCE_BACKFILL_QUEUE,
|
||||
expect.objectContaining({ userId: "user-1" }),
|
||||
expect.objectContaining({
|
||||
singletonKey: "medication-compliance-boot-backfill|user-1",
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
it("reports a coalesced enqueue as `enqueued:false`", async () => {
|
||||
getGlobalBossMock.mockReturnValue({ send: bossSend });
|
||||
bossSend.mockResolvedValueOnce(null);
|
||||
const result = await enqueueUserMedicationComplianceBackfill("user-2");
|
||||
expect(result).toEqual({ enqueued: false, error: null });
|
||||
});
|
||||
});
|
||||
@@ -1,438 +0,0 @@
|
||||
/**
|
||||
* v1.4.39 — persistent medication-compliance rollup tier.
|
||||
*
|
||||
* Replaces the unbounded `MedicationIntakeEvent.findMany` walk on
|
||||
* `/api/medications/intake?scope=compliance` with a ledger keyed
|
||||
* `(userId, medicationId, day)` where `day` is `YYYY-MM-DD` anchored
|
||||
* to the user's IANA timezone.
|
||||
*
|
||||
* Write hooks fire from every `MedicationIntakeEvent` mutation site
|
||||
* (create / update / delete / bulk import) plus the reminder-worker's
|
||||
* mint path that creates `takenAt:null` rows on RED-phase reminders.
|
||||
* The dashboard tile it was built for reads the schedule-anchored
|
||||
* compliance (`src/lib/analytics/schedule-anchored-compliance.ts`) since
|
||||
* v1.26, and the per-day recompute below is now what keeps a day's
|
||||
* supplement nutrients in step with its doses; the table itself has no
|
||||
* reader left.
|
||||
*
|
||||
* v1.15.18 DESIGN DECISION — this tier stays a RAW per-day COVERAGE count,
|
||||
* NOT the band-attributed compliance %. It aggregates, across ALL of a
|
||||
* user's medications, "of the intake rows whose `scheduled_for` lands on
|
||||
* this day, how many carry a non-null `taken_at`" — a coarse daily
|
||||
* cross-medication coverage strip for the dashboard mini-tiles. The
|
||||
* unified band-membership attribution (the `tallyComplianceFromLedger`
|
||||
* keystone) is per-medication and per-cadence: it depends on the slot
|
||||
* bands of each schedule and the user timezone, which cannot be expressed
|
||||
* in a single cross-medication SQL `SUM(CASE …)`. Re-attributing every
|
||||
* intake against its cadence's bands before the tally would mean running
|
||||
* the band minter per `(medication, day)` inside the rollup, which defeats
|
||||
* the rollup's whole purpose (bounded SQL, no per-row Node walk). The
|
||||
* detail page + the medication card already read the band-attributed % via
|
||||
* `calculateCompliance` / `complianceChips`; this tier deliberately remains
|
||||
* the cheaper coverage signal and is documented as such so the two are not
|
||||
* confused. The `scheduled` / `taken` column names reflect this: a day's
|
||||
* `taken` is "slots with an intake logged taken", not "doses inside their
|
||||
* on-time band".
|
||||
*
|
||||
* v1.15.19 — the count is SLOT-level, not row-level. The partial unique
|
||||
* index on `(user, medication, scheduled_for, source)` lets two live rows
|
||||
* share one slot instant when their `source` differs (e.g. a pending
|
||||
* REMINDER row plus a taken API row for the same dose); a row-level
|
||||
* `COUNT(*)` double-counted that slot as two scheduled doses. The
|
||||
* aggregate now groups by `scheduled_for` first: `scheduled` counts
|
||||
* distinct slot instants, and each slot folds its rows with
|
||||
* taken-beats-skipped priority (`BOOL_OR` of taken, then skipped only when
|
||||
* no row in the slot is taken). This neutralises cross-source duplicate
|
||||
* rows retroactively — any recompute over pre-existing duplicates yields
|
||||
* the deduplicated counts without a data migration.
|
||||
*
|
||||
* Audit anchor: `.planning/round-v1438-perf-analysis.md` §2.5 + §5 P4.
|
||||
*/
|
||||
import type { PrismaClient } from "@/generated/prisma/client";
|
||||
import { prisma } from "@/lib/db";
|
||||
import { getGlobalBoss } from "@/lib/jobs/boss-instance";
|
||||
import { refreshSupplementNutrientsForDose } from "@/lib/medications/ingredients/recompute";
|
||||
import { annotate } from "@/lib/logging/context";
|
||||
import { DEFAULT_TIMEZONE, isValidTimezone, userDayKey } from "@/lib/tz/format";
|
||||
import { localDayWindow, startOfLocalDayKey } from "@/lib/tz/local-day";
|
||||
|
||||
/**
|
||||
* pg-boss queue name for the boot-time medication compliance backfill.
|
||||
* Mirrors the `rollup-full-backfill` v1.4.35.1 pattern: discovery is
|
||||
* idempotent across reboots and the worker upserts every
|
||||
* `(user, medication, day)` row for the trailing window.
|
||||
*/
|
||||
export const MEDICATION_COMPLIANCE_BACKFILL_QUEUE =
|
||||
"medication-compliance-full-backfill";
|
||||
|
||||
/** Worker concurrency cap — backfill stays serial to spare the pool. */
|
||||
export const MEDICATION_COMPLIANCE_BACKFILL_CONCURRENCY = 1;
|
||||
|
||||
/** Trailing window the boot backfill folds per uncovered user. */
|
||||
export const MEDICATION_COMPLIANCE_BACKFILL_DAYS = 90;
|
||||
|
||||
/** Payload `boss.send` carries onto the backfill queue. */
|
||||
export interface MedicationComplianceBackfillPayload {
|
||||
userId: string;
|
||||
/** Wall-clock kick-off for debugging only. */
|
||||
enqueuedAt: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolve a safe tz string — fall back to the server default when the
|
||||
* supplied value is missing or not an IANA zone the host recognises.
|
||||
*/
|
||||
function safeTimezone(tz: string | null | undefined): string {
|
||||
if (tz && isValidTimezone(tz)) return tz;
|
||||
return DEFAULT_TIMEZONE;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert a `scheduledFor` UTC instant to the user's `YYYY-MM-DD`
|
||||
* day-key. Re-exposed so the write-hook caller can derive the same
|
||||
* `dayKey` the rollup row is anchored on.
|
||||
*/
|
||||
export function dayKeyForScheduledFor(
|
||||
scheduledFor: Date,
|
||||
tz: string | null | undefined,
|
||||
): string {
|
||||
return userDayKey(scheduledFor, safeTimezone(tz));
|
||||
}
|
||||
|
||||
type MedicationComplianceRollupClient = Pick<PrismaClient, "$executeRaw">;
|
||||
|
||||
/**
|
||||
* Recompute the rollup row for one `(userId, medicationId, dayKey)`
|
||||
* tuple. Reads every `MedicationIntakeEvent` whose `scheduledFor` lands
|
||||
* in `[startOfDayInTz, startOfNextDayInTz)` then upserts the
|
||||
* `medication_compliance_rollups` row.
|
||||
*
|
||||
* The hook is idempotent: re-running for the same key produces the
|
||||
* same row. When the day window holds zero events the row is deleted
|
||||
* so the read path returns the trailing-window zero-default rather
|
||||
* than a stale stub.
|
||||
*
|
||||
*/
|
||||
export async function recomputeMedicationComplianceForDay(
|
||||
userId: string,
|
||||
medicationId: string,
|
||||
dayKey: string,
|
||||
tz: string | null | undefined,
|
||||
client: MedicationComplianceRollupClient = prisma,
|
||||
): Promise<void> {
|
||||
const safeTz = safeTimezone(tz);
|
||||
// The local day's own bounds: 23, 24 or 25 hours on DST days.
|
||||
const { dayStart: start, dayEnd: windowEnd } = localDayWindow(dayKey, safeTz);
|
||||
|
||||
// v1.4.39: atomic upsert closes the race window
|
||||
// between the prior `SELECT … aggregate then UPSERT` pattern. Two
|
||||
// concurrent writes for the same (user, medication, day) used to
|
||||
// interleave A-SELECT → B-SELECT → B-UPSERT (correct) → A-UPSERT
|
||||
// (stale N-1). The INSERT … SELECT re-aggregates inside the upsert
|
||||
// statement so each write commits with a snapshot taken after the
|
||||
// prior commit released its row lock; the trailing DELETE handles
|
||||
// the "all events for the day were removed" case in the same shot.
|
||||
//
|
||||
// Both statements key on (user_id, medication_id, day) so they
|
||||
// serialise behind the row lock the rollup table's primary key
|
||||
// provides. The DELETE runs second so it cannot clobber a row the
|
||||
// INSERT just wrote — when the day still has events the
|
||||
// `WHERE NOT EXISTS` predicate matches zero rows.
|
||||
// v1.15.19 — slot-level aggregation. The inner CTE folds the rows that
|
||||
// share one `scheduled_for` instant (cross-source duplicates: pending
|
||||
// REMINDER + taken API) into one slot with taken-beats-skipped priority;
|
||||
// the outer aggregate then counts slots, not rows, so a duplicated slot
|
||||
// can never inflate `scheduled`. Pure SQL — re-running the recompute
|
||||
// over historic duplicates self-corrects the counts.
|
||||
await client.$executeRaw`
|
||||
WITH slot AS (
|
||||
SELECT
|
||||
BOOL_OR("taken_at" IS NOT NULL AND NOT "skipped") AS slot_taken,
|
||||
BOOL_OR("skipped") AS slot_skipped
|
||||
FROM "medication_intake_events"
|
||||
WHERE "user_id" = ${userId}
|
||||
AND "medication_id" = ${medicationId}
|
||||
AND "deleted_at" IS NULL
|
||||
AND "scheduled_for" >= ${start}
|
||||
AND "scheduled_for" < ${windowEnd}
|
||||
GROUP BY "scheduled_for"
|
||||
),
|
||||
aggregate AS (
|
||||
SELECT
|
||||
COUNT(*)::int AS scheduled,
|
||||
COALESCE(SUM(CASE WHEN "slot_taken" THEN 1 ELSE 0 END), 0)::int AS taken,
|
||||
COALESCE(SUM(CASE WHEN NOT "slot_taken" AND "slot_skipped" THEN 1 ELSE 0 END), 0)::int AS skipped
|
||||
FROM slot
|
||||
)
|
||||
INSERT INTO "medication_compliance_rollups"
|
||||
("user_id", "medication_id", "day", "scheduled", "taken", "skipped", "computed_at")
|
||||
SELECT
|
||||
${userId},
|
||||
${medicationId},
|
||||
${dayKey},
|
||||
aggregate.scheduled,
|
||||
aggregate.taken,
|
||||
aggregate.skipped,
|
||||
NOW()
|
||||
FROM aggregate
|
||||
WHERE aggregate.scheduled > 0
|
||||
ON CONFLICT ("user_id", "medication_id", "day") DO UPDATE
|
||||
SET "scheduled" = EXCLUDED."scheduled",
|
||||
"taken" = EXCLUDED."taken",
|
||||
"skipped" = EXCLUDED."skipped",
|
||||
"computed_at" = EXCLUDED."computed_at"
|
||||
`;
|
||||
await client.$executeRaw`
|
||||
DELETE FROM "medication_compliance_rollups"
|
||||
WHERE "user_id" = ${userId}
|
||||
AND "medication_id" = ${medicationId}
|
||||
AND "day" = ${dayKey}
|
||||
AND NOT EXISTS (
|
||||
SELECT 1
|
||||
FROM "medication_intake_events"
|
||||
WHERE "user_id" = ${userId}
|
||||
AND "medication_id" = ${medicationId}
|
||||
AND "deleted_at" IS NULL
|
||||
AND "scheduled_for" >= ${start}
|
||||
AND "scheduled_for" < ${windowEnd}
|
||||
)
|
||||
`;
|
||||
|
||||
// v1.43 — the day's other derived row: the micronutrients its taken
|
||||
// supplement doses add (`source = SUPPLEMENT`). Every dose write path
|
||||
// already funnels through this recompute, so this is the one place the
|
||||
// hook needs to live. Only on the base client: a caller inside its own
|
||||
// transaction (the dose import) has not committed the doses yet and
|
||||
// queues the account's full recompute once it has.
|
||||
if (client === prisma) {
|
||||
await refreshSupplementNutrientsForDose({
|
||||
userId,
|
||||
medicationId,
|
||||
dayKey,
|
||||
timezone: safeTz,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Convenience hook for write-paths that have the `scheduledFor`
|
||||
* instant in hand. Derives `dayKey` in the user's tz then dispatches
|
||||
* to `recomputeMedicationComplianceForDay`. Best-effort: surfaces
|
||||
* failures through `annotate` + `console.error` so a silent populator
|
||||
* regression shows up in ops without blocking the parent write.
|
||||
*/
|
||||
export async function recomputeMedicationComplianceForEvent(input: {
|
||||
userId: string;
|
||||
medicationId: string;
|
||||
scheduledFor: Date;
|
||||
tz: string | null | undefined;
|
||||
}): Promise<void> {
|
||||
const dayKey = dayKeyForScheduledFor(input.scheduledFor, input.tz);
|
||||
try {
|
||||
await recomputeMedicationComplianceForDay(
|
||||
input.userId,
|
||||
input.medicationId,
|
||||
dayKey,
|
||||
input.tz,
|
||||
);
|
||||
} catch (err) {
|
||||
const message = err instanceof Error ? err.message : String(err);
|
||||
annotate({
|
||||
meta: {
|
||||
medication_compliance_rollup_failed: true,
|
||||
medication_compliance_rollup_error: message,
|
||||
},
|
||||
});
|
||||
console.error("[medication-compliance-rollups] recompute failed:", message);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Full-fold helper. Walks every `(medicationId, day)` pair the user
|
||||
* has logged across the trailing `days` window and upserts each
|
||||
* rollup row. The boot-time backfill worker calls this once per
|
||||
* uncovered user; the route's lazy fallback enqueues it when the
|
||||
* coverage probe returns false.
|
||||
*
|
||||
* Bounded SQL: one `DISTINCT (medicationId, dayKey)` $queryRaw scan
|
||||
* over the trailing window, then one upsert per row. The dayKey is
|
||||
* computed in Postgres in the user's zone so we
|
||||
* don't have to ship millions of rows back to Node.
|
||||
*/
|
||||
export async function recomputeUserMedicationCompliance(
|
||||
userId: string,
|
||||
days: number = MEDICATION_COMPLIANCE_BACKFILL_DAYS,
|
||||
tz: string | null | undefined = null,
|
||||
): Promise<{ rowsUpserted: number; durationMs: number }> {
|
||||
const startedAt = Date.now();
|
||||
const safeTz = safeTimezone(tz);
|
||||
|
||||
const now = new Date();
|
||||
const oldestKey = userDayKey(
|
||||
new Date(now.getTime() - (days - 1) * 86_400_000),
|
||||
safeTz,
|
||||
);
|
||||
const oldestStart = startOfLocalDayKey(oldestKey, safeTz);
|
||||
|
||||
// Distinct (medication_id, dayKey) pairs the user has any intake
|
||||
// event for inside the trailing window. `scheduled_for` is a naive UTC
|
||||
// timestamp: it is read as UTC first and only then shifted into the
|
||||
// user's zone. A single `AT TIME ZONE $tz` reads it as local time and
|
||||
// shifts it the wrong way, which keyed every slot within the zone's
|
||||
// offset of midnight onto the neighbouring day.
|
||||
const pairs = await prisma.$queryRaw<
|
||||
Array<{ medication_id: string; day: string }>
|
||||
>`
|
||||
SELECT DISTINCT
|
||||
"medication_id",
|
||||
to_char(
|
||||
("scheduled_for" AT TIME ZONE 'UTC') AT TIME ZONE ${safeTz},
|
||||
'YYYY-MM-DD'
|
||||
) AS "day"
|
||||
FROM "medication_intake_events"
|
||||
WHERE "user_id" = ${userId}
|
||||
AND "deleted_at" IS NULL
|
||||
AND "scheduled_for" >= ${oldestStart}
|
||||
`;
|
||||
|
||||
let rowsUpserted = 0;
|
||||
for (const pair of pairs) {
|
||||
await recomputeMedicationComplianceForDay(
|
||||
userId,
|
||||
pair.medication_id,
|
||||
pair.day,
|
||||
safeTz,
|
||||
);
|
||||
rowsUpserted += 1;
|
||||
}
|
||||
|
||||
return { rowsUpserted, durationMs: Date.now() - startedAt };
|
||||
}
|
||||
|
||||
/**
|
||||
* v1.4.39 — user-scoped enqueue helper for the
|
||||
* request-path coverage-miss fallback.
|
||||
*
|
||||
* Mirrors `ensureUserMoodRollupsFresh`: the route fires this when its
|
||||
* coverage probe returned false so the caller's account picks up a
|
||||
* targeted boot-backfill job, instead of running a cluster-wide
|
||||
* `LEFT JOIN` over `medication_intake_events × medication_compliance_rollups`
|
||||
* on every authenticated coverage-miss request. The cluster-wide scan
|
||||
* was a soft DoS amplifier — an authenticated user iterating
|
||||
* coverage-miss requests could drive a multi-tenant table scan on
|
||||
* every hit.
|
||||
*
|
||||
* The boot-time discovery helper below stays cluster-wide (it runs
|
||||
* once per worker boot, not per request).
|
||||
*/
|
||||
export async function enqueueUserMedicationComplianceBackfill(
|
||||
userId: string,
|
||||
): Promise<{ enqueued: boolean; error: string | null }> {
|
||||
const boss = getGlobalBoss();
|
||||
if (!boss) {
|
||||
return { enqueued: false, error: null };
|
||||
}
|
||||
try {
|
||||
const payload: MedicationComplianceBackfillPayload = {
|
||||
userId,
|
||||
enqueuedAt: new Date().toISOString(),
|
||||
};
|
||||
const jobId = await boss.send(
|
||||
MEDICATION_COMPLIANCE_BACKFILL_QUEUE,
|
||||
payload,
|
||||
{
|
||||
retryLimit: 3,
|
||||
retryDelay: 60,
|
||||
retryBackoff: true,
|
||||
singletonKey: `medication-compliance-boot-backfill|${userId}`,
|
||||
},
|
||||
);
|
||||
return { enqueued: jobId !== null && jobId !== undefined, error: null };
|
||||
} catch (err) {
|
||||
return {
|
||||
enqueued: false,
|
||||
error: err instanceof Error ? err.message : String(err),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Boot-time enqueue helper — mirrors v1.4.35.1
|
||||
* `enqueueBootTimeRollupBackfill`. Finds users with intake events but
|
||||
* zero compliance-rollup coverage for the trailing window and enqueues
|
||||
* one full-fold per account onto `MEDICATION_COMPLIANCE_BACKFILL_QUEUE`.
|
||||
*
|
||||
* Idempotent across reboots: once an account has at least one rollup
|
||||
* row inside the trailing window the discovery query drops them.
|
||||
* pg-boss `singletonKey` coalesces duplicate sends.
|
||||
*/
|
||||
export async function enqueueBootTimeMedicationComplianceBackfill(
|
||||
// Optional boot-storm stagger. When > 0 the per-user sends carry a
|
||||
// `startAfter` delay (seconds) so this full-history fold does not drain in
|
||||
// parallel with the other boot backfills onto one heavy tenant. Default 0
|
||||
// keeps immediate semantics for any non-boot caller.
|
||||
startAfterSeconds: number = 0,
|
||||
): Promise<{
|
||||
enqueued: number;
|
||||
skipped: number;
|
||||
error: string | null;
|
||||
}> {
|
||||
const boss = getGlobalBoss();
|
||||
if (!boss) {
|
||||
return { enqueued: 0, skipped: 0, error: null };
|
||||
}
|
||||
|
||||
try {
|
||||
// Trailing window cutoff for the existence probe — `scheduled_for`
|
||||
// older than this is not what the read path consults.
|
||||
const cutoff = new Date(
|
||||
Date.now() - MEDICATION_COMPLIANCE_BACKFILL_DAYS * 86_400_000,
|
||||
);
|
||||
const users = await prisma.$queryRaw<Array<{ id: string }>>`
|
||||
SELECT DISTINCT mie."user_id" AS id
|
||||
FROM "medication_intake_events" mie
|
||||
LEFT JOIN "medication_compliance_rollups" r
|
||||
ON r."user_id" = mie."user_id"
|
||||
AND r."medication_id" = mie."medication_id"
|
||||
WHERE mie."scheduled_for" >= ${cutoff}
|
||||
AND mie."deleted_at" IS NULL
|
||||
AND r."day" IS NULL
|
||||
`;
|
||||
|
||||
if (users.length === 0) {
|
||||
return { enqueued: 0, skipped: 0, error: null };
|
||||
}
|
||||
|
||||
let enqueued = 0;
|
||||
let skipped = 0;
|
||||
for (const { id } of users) {
|
||||
const payload: MedicationComplianceBackfillPayload = {
|
||||
userId: id,
|
||||
enqueuedAt: new Date().toISOString(),
|
||||
};
|
||||
const jobId = await boss.send(
|
||||
MEDICATION_COMPLIANCE_BACKFILL_QUEUE,
|
||||
payload,
|
||||
{
|
||||
retryLimit: 3,
|
||||
retryDelay: 60,
|
||||
retryBackoff: true,
|
||||
singletonKey: `medication-compliance-boot-backfill|${id}`,
|
||||
...(startAfterSeconds > 0 ? { startAfter: startAfterSeconds } : {}),
|
||||
},
|
||||
);
|
||||
if (jobId) {
|
||||
enqueued += 1;
|
||||
} else {
|
||||
skipped += 1;
|
||||
}
|
||||
}
|
||||
return { enqueued, skipped, error: null };
|
||||
} catch (err) {
|
||||
return {
|
||||
enqueued: 0,
|
||||
skipped: 0,
|
||||
error: err instanceof Error ? err.message : String(err),
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -9,7 +9,7 @@ import {
|
||||
import { getServerTranslator } from "@/lib/i18n/server-translator";
|
||||
import { locales, type Locale } from "@/lib/i18n/config";
|
||||
import { foldForMatch } from "@/lib/i18n/fold-for-match";
|
||||
import { recomputeMedicationComplianceForEvent } from "@/lib/rollups/medication-compliance-rollups";
|
||||
import { refreshDoseSlot } from "@/lib/medications/dose-day-refresh";
|
||||
import {
|
||||
applyCanonicalSlotWrite,
|
||||
resolveForcedSlotForWrite,
|
||||
@@ -212,8 +212,8 @@ async function findTelegramUser(chatId: string) {
|
||||
id: true,
|
||||
telegramBotToken: true,
|
||||
locale: true,
|
||||
// v1.4.39 — pulled into the select so the post-write
|
||||
// compliance-rollup hook can anchor the user-day bucket.
|
||||
// Pulled into the select so the post-write dose-day refresh can
|
||||
// anchor the user's local day.
|
||||
timezone: true,
|
||||
},
|
||||
});
|
||||
@@ -428,7 +428,7 @@ async function markMedicationTaken(
|
||||
// affected day so the next read after the cache miss reflects the
|
||||
// new dose. Only fires when the write landed a row.
|
||||
if (scheduledFor) {
|
||||
await recomputeMedicationComplianceForEvent({
|
||||
await refreshDoseSlot({
|
||||
userId,
|
||||
medicationId: medication.id,
|
||||
scheduledFor,
|
||||
@@ -690,7 +690,7 @@ export async function handleCallback(update: TelegramUpdate) {
|
||||
// event's user-day so the next read sees the (scheduled, skipped)
|
||||
// counts incremented.
|
||||
if (skippedScheduledFor) {
|
||||
await recomputeMedicationComplianceForEvent({
|
||||
await refreshDoseSlot({
|
||||
userId: user.id,
|
||||
medicationId: medication.id,
|
||||
scheduledFor: skippedScheduledFor,
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
/**
|
||||
* Wall-clock-in-timezone helper shared by the medication scheduling
|
||||
* + compliance-rollup paths.
|
||||
* paths.
|
||||
*
|
||||
* Decomposes a `Date` instant into the year/month/day/hour/minute/
|
||||
* second/weekday tuple an observer in `tz` would read off a wall
|
||||
@@ -12,7 +12,7 @@
|
||||
* per-user timezone work keep their original shape.
|
||||
*
|
||||
* v1.4.40 W-GHOSTS consolidated two file-local copies
|
||||
* (`compliance-rollups.ts` returning `{year,month,day}` only +
|
||||
* (a compliance-rollup copy returning `{year,month,day}` only +
|
||||
* `scheduling/cadence.ts` returning the full tuple) into this single
|
||||
* canonical export. The cross-tz ±3 h guard hinges on these helpers
|
||||
* staying in lock-step; keeping them as separate definitions made
|
||||
|
||||
Reference in new issue
Block a user