mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-19 08:12:52 +00:00
fix(labs): keep a deleted Health Records result deleted once its row is gone
The import recognised a deleted result by the identity on its own tombstone, so anything that removed the row brought the result back with the next Apple Health export or resend: deleting the whole marker, which hard-deletes its readings, the nightly tombstone purge, and a portable backup, which leaves deleted readings out. The identities now move into an import ledger (LabResultImportKey, migration 0501) in the same transaction as the marker delete and the purge, and the import checks it beside the stored rows. The backup carries the ledger, a portable file also the identities of deleted readings still waiting as tombstones; the restore writes it back without the identities a restored reading holds. The data wipe clears it, account deletion cascades.
This commit is contained in:
1 parent
0fedabde13
commit
f3662a8dc8
38 files changed
+2356
-1635
No files matched your search
@@ -1,7 +1,7 @@
|
||||
{
|
||||
"$comment": "Production files over 800 lines and their length. Maintained by src/__tests__/source-size-ratchet.test.ts.",
|
||||
"count": 84,
|
||||
"total": 93813,
|
||||
"total": 93815,
|
||||
"entries": {
|
||||
"app/medications/page-client.tsx": 842,
|
||||
"components/charts/mood-chart.tsx": 1166,
|
||||
@@ -42,7 +42,7 @@
|
||||
"lib/dashboard-layout.ts": 977,
|
||||
"lib/dashboard/snapshot.ts": 1548,
|
||||
"lib/day/records.ts": 1172,
|
||||
"lib/export/backup-plan.ts": 838,
|
||||
"lib/export/backup-plan.ts": 840,
|
||||
"lib/export/coach-backup.ts": 1070,
|
||||
"lib/export/profile-backup.ts": 1019,
|
||||
"lib/fitbit/client.ts": 1262,
|
||||
|
||||
@@ -22,13 +22,18 @@ vi.mock("@/lib/api-handler", async (importOriginal) => {
|
||||
};
|
||||
});
|
||||
|
||||
vi.mock("@/lib/db", () => ({
|
||||
prisma: {
|
||||
vi.mock("@/lib/db", () => {
|
||||
const prisma = {
|
||||
biomarker: { findFirst: vi.fn(), delete: vi.fn() },
|
||||
labResult: { deleteMany: vi.fn() },
|
||||
$transaction: vi.fn(async (ops: Promise<unknown>[]) => Promise.all(ops)),
|
||||
},
|
||||
}));
|
||||
labResult: { deleteMany: vi.fn(), findMany: vi.fn(async () => []) },
|
||||
labResultImportKey: { createMany: vi.fn() },
|
||||
$transaction: vi.fn(),
|
||||
};
|
||||
prisma.$transaction.mockImplementation(
|
||||
async (fn: (tx: typeof prisma) => unknown) => fn(prisma),
|
||||
);
|
||||
return { prisma };
|
||||
});
|
||||
vi.mock("@/lib/auth/audit", () => ({
|
||||
auditLog: vi.fn().mockResolvedValue(undefined),
|
||||
}));
|
||||
@@ -79,11 +84,20 @@ describe("DELETE /api/biomarkers/[id] record authority", () => {
|
||||
expect(requireAuth).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("narrows both delete legs to the resolved record", async () => {
|
||||
it("narrows every delete leg to the resolved record", async () => {
|
||||
await DELETE(delReq(), params);
|
||||
expect(prisma.labResult.deleteMany).toHaveBeenCalledWith({
|
||||
where: { userId: "record-1", biomarkerId: "bm-1" },
|
||||
});
|
||||
// The identities the import ledger keeps come from the same rows.
|
||||
expect(prisma.labResult.findMany).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
where: expect.objectContaining({
|
||||
userId: "record-1",
|
||||
biomarkerId: "bm-1",
|
||||
}),
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
it("refuses when the record authority refuses, deleting nothing", async () => {
|
||||
|
||||
@@ -17,10 +17,11 @@ vi.mock("@/lib/db", () => {
|
||||
},
|
||||
biomarkerMerge: { findFirst: vi.fn(async () => null) },
|
||||
labResult: { deleteMany: vi.fn(), findMany: vi.fn(async () => []) },
|
||||
labResultImportKey: { createMany: vi.fn() },
|
||||
auditLog: { create: vi.fn().mockResolvedValue({}) },
|
||||
};
|
||||
// `$transaction` is set in `beforeEach`: the batch form for the delete, the
|
||||
// callback form for the edit.
|
||||
// `$transaction` is set in `beforeEach`: the callback form for the delete
|
||||
// and the edit, the batch form where a caller still uses it.
|
||||
return { prisma: { ...prisma, $transaction: vi.fn() } };
|
||||
});
|
||||
|
||||
@@ -158,6 +159,12 @@ describe("DELETE /api/biomarkers/[id]", () => {
|
||||
vi.mocked(prisma.labResult.deleteMany).mockResolvedValue({
|
||||
count: 3,
|
||||
} as never);
|
||||
vi.mocked(prisma.labResult.findMany).mockResolvedValue([
|
||||
{ source: "APPLE_HEALTH", externalId: "fhir:a" },
|
||||
] as never);
|
||||
vi.mocked(prisma.labResultImportKey.createMany).mockResolvedValue({
|
||||
count: 1,
|
||||
} as never);
|
||||
vi.mocked(prisma.biomarker.delete).mockResolvedValue(EXISTING as never);
|
||||
|
||||
const res = await DELETE(delReq(), params("bm-1"));
|
||||
@@ -171,6 +178,13 @@ describe("DELETE /api/biomarkers/[id]", () => {
|
||||
expect(prisma.biomarker.delete).toHaveBeenCalledWith({
|
||||
where: { id: "bm-1" },
|
||||
});
|
||||
// A Health Records reading's identity outlives it in the import ledger.
|
||||
expect(prisma.labResultImportKey.createMany).toHaveBeenCalledWith({
|
||||
data: [
|
||||
{ userId: "user-1", source: "APPLE_HEALTH", externalId: "fhir:a" },
|
||||
],
|
||||
skipDuplicates: true,
|
||||
});
|
||||
expect(prisma.$transaction).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
|
||||
@@ -27,6 +27,7 @@ import { matchAnalyteKey } from "@/lib/labs/analyte-match";
|
||||
import { sameLabUnit } from "@/lib/labs/unit-normalise";
|
||||
import { annotate } from "@/lib/logging/context";
|
||||
import { computeLabTrend } from "@/lib/labs/lab-trend";
|
||||
import { retireLabImportKeys } from "@/lib/labs/fhir-import/import-keys";
|
||||
|
||||
/** Upper bound on readings the trend line reads. */
|
||||
const LAB_TREND_READ_CAP = 2000;
|
||||
@@ -319,14 +320,17 @@ export const DELETE = apiHandler(
|
||||
// Hard delete the marker AND its readings in one transaction. Deleting a
|
||||
// biomarker is a deliberate "remove this and its history" action, so the
|
||||
// readings go with it rather than lingering as orphaned, unlinked rows.
|
||||
// Both legs are userId-narrowed so a cross-user id can never reach another
|
||||
// account's data.
|
||||
const [removedReadings] = await prisma.$transaction([
|
||||
prisma.labResult.deleteMany({
|
||||
where: { userId: user.id, biomarkerId: id },
|
||||
}),
|
||||
prisma.biomarker.delete({ where: { id } }),
|
||||
]);
|
||||
// Every leg is userId-narrowed so a cross-user id can never reach another
|
||||
// account's data. A reading imported from Health Records leaves its
|
||||
// identity in the import ledger first, so the next export or resend does
|
||||
// not write it again.
|
||||
const removedReadings = await prisma.$transaction(async (tx) => {
|
||||
const where = { userId: user.id, biomarkerId: id };
|
||||
await retireLabImportKeys(tx, user.id, where);
|
||||
const removed = await tx.labResult.deleteMany({ where });
|
||||
await tx.biomarker.delete({ where: { id } });
|
||||
return removed;
|
||||
});
|
||||
invalidateUserHealthScore(user.id);
|
||||
|
||||
await auditLog("biomarker.delete", {
|
||||
|
||||
@@ -95,6 +95,7 @@ vi.mock("@/lib/db", () => ({
|
||||
biomarker: { findMany: vi.fn() },
|
||||
biomarkerAlias: { findMany: vi.fn() },
|
||||
biomarkerMerge: { findMany: vi.fn() },
|
||||
labResultImportKey: { findMany: vi.fn(async () => []) },
|
||||
illnessEpisode: { findMany: vi.fn() },
|
||||
allergy: { findMany: vi.fn() },
|
||||
familyHistoryEntry: { findMany: vi.fn() },
|
||||
|
||||
@@ -69,6 +69,7 @@ vi.mock("@/lib/db", () => ({
|
||||
biomarker: { findMany: vi.fn() },
|
||||
biomarkerAlias: { findMany: vi.fn() },
|
||||
biomarkerMerge: { findMany: vi.fn() },
|
||||
labResultImportKey: { findMany: vi.fn(async () => []) },
|
||||
allergy: { findMany: vi.fn() },
|
||||
familyHistoryEntry: { findMany: vi.fn() },
|
||||
workout: { findMany: vi.fn() },
|
||||
|
||||
@@ -86,6 +86,7 @@ vi.mock("@/lib/db", () => ({
|
||||
biomarker: { findMany: vi.fn().mockResolvedValue([]) },
|
||||
biomarkerAlias: { findMany: vi.fn().mockResolvedValue([]) },
|
||||
biomarkerMerge: { findMany: vi.fn().mockResolvedValue([]) },
|
||||
labResultImportKey: { findMany: vi.fn().mockResolvedValue([]) },
|
||||
illnessEpisode: { findMany: vi.fn().mockResolvedValue([]) },
|
||||
allergy: { findMany: vi.fn().mockResolvedValue([]) },
|
||||
familyHistoryEntry: { findMany: vi.fn().mockResolvedValue([]) },
|
||||
|
||||
@@ -81,7 +81,10 @@ export async function importAppleHealthClinicalLabs(input: {
|
||||
userId: string;
|
||||
tz: string;
|
||||
references: readonly ClinicalLabReference[];
|
||||
prisma?: Pick<PrismaClient, "labResult" | "biomarker" | "biomarkerAlias">;
|
||||
prisma?: Pick<
|
||||
PrismaClient,
|
||||
"labResult" | "biomarker" | "biomarkerAlias" | "labResultImportKey"
|
||||
>;
|
||||
limits?: AppleHealthClinicalArchiveLimits;
|
||||
}): Promise<FhirLabCounts> {
|
||||
const counts = emptyFhirLabCounts();
|
||||
|
||||
@@ -136,6 +136,10 @@ export const WIPE_MODELS = [
|
||||
// ── Clinical record ─────────────────────────────────────────────────────
|
||||
"MentalHealthAssessment",
|
||||
"LabResult",
|
||||
// The identities of Health Records readings the person deleted (#1400).
|
||||
// Kept, they would make the first import after the wipe skip results for
|
||||
// a decision about a record that no longer exists.
|
||||
"LabResultImportKey",
|
||||
// v1.43 — a marker's other spellings and the merges folded into it. Both
|
||||
// cascade from the marker; named before it so their counts read true.
|
||||
"BiomarkerAlias",
|
||||
|
||||
@@ -464,6 +464,7 @@ function makePrisma() {
|
||||
documentConditionLink: { findMany: vi.fn().mockResolvedValue([]) },
|
||||
extractedFact: { findMany: vi.fn().mockResolvedValue([]) },
|
||||
documentImportKey: { findMany: vi.fn().mockResolvedValue([]) },
|
||||
labResultImportKey: { findMany: vi.fn().mockResolvedValue([]) },
|
||||
inboundDocument: { findMany: vi.fn().mockResolvedValue([]) },
|
||||
// The ECG strips. Empty for the same reason as the sections above:
|
||||
// an account whose watch has never taken one is the ordinary case the
|
||||
|
||||
@@ -75,6 +75,7 @@ function buildMockPrisma() {
|
||||
},
|
||||
biomarkerAlias: { findMany: vi.fn().mockResolvedValue([]) },
|
||||
biomarkerMerge: { findMany: vi.fn().mockResolvedValue([]) },
|
||||
labResultImportKey: { findMany: vi.fn().mockResolvedValue([]) },
|
||||
illnessEpisode: {
|
||||
findMany: vi.fn().mockResolvedValue([
|
||||
{
|
||||
@@ -473,6 +474,7 @@ describe("buildRecordsBackupSection", () => {
|
||||
|
||||
expect(counts).toEqual({
|
||||
labResults: 1,
|
||||
labResultImportKeys: 0,
|
||||
biomarkers: 1,
|
||||
illnessEpisodes: 2,
|
||||
illnessDayLogs: 1,
|
||||
|
||||
@@ -167,6 +167,7 @@ function sourceClient() {
|
||||
biomarker: empty,
|
||||
biomarkerAlias: empty,
|
||||
biomarkerMerge: empty,
|
||||
labResultImportKey: empty,
|
||||
illnessEpisode: empty,
|
||||
allergy: empty,
|
||||
familyHistoryEntry: empty,
|
||||
|
||||
@@ -101,6 +101,7 @@ export const BACKED_UP_MODELS = [
|
||||
// ── Clinical record ───────────────────────────────────────────────────────
|
||||
"MentalHealthAssessment",
|
||||
"LabResult",
|
||||
"LabResultImportKey", // Deleted Health Records readings: `labs-backup.ts`.
|
||||
"Biomarker",
|
||||
"Allergy",
|
||||
"FamilyHistoryEntry",
|
||||
@@ -365,6 +366,7 @@ export const TWO_ENDED_MODELS = [
|
||||
"MoodEntryTagLink",
|
||||
"MoodTag",
|
||||
"LabResult",
|
||||
"LabResultImportKey",
|
||||
"Biomarker",
|
||||
"Allergy",
|
||||
"FamilyHistoryEntry",
|
||||
|
||||
@@ -157,6 +157,7 @@ const SUMMARY_COUNTS = [
|
||||
"cycles",
|
||||
"cycleDayLogs",
|
||||
"labResults",
|
||||
"labResultImportKeys",
|
||||
"nutrientDays",
|
||||
"biomarkers",
|
||||
"illnessEpisodes",
|
||||
|
||||
@@ -117,3 +117,15 @@ export const biomarkerMergeBackupSchema = z
|
||||
mergedAt: isoDateTime,
|
||||
})
|
||||
.passthrough();
|
||||
|
||||
/**
|
||||
* The identity of a Health Records reading the person deleted (the import
|
||||
* ledger). Bounded like the reading's own `externalId`.
|
||||
*/
|
||||
export const labResultImportKeyBackupSchema = z
|
||||
.object({
|
||||
source: z.string().min(1).max(32),
|
||||
externalId: z.string().min(1).max(128),
|
||||
createdAt: isoDateTime.optional(),
|
||||
})
|
||||
.passthrough();
|
||||
+137
-31
@@ -145,11 +145,28 @@ export interface BiomarkerMergeBackupEntry {
|
||||
mergedAt: string;
|
||||
}
|
||||
|
||||
/** The lab slice of the records section: results, markers, merge ledger. */
|
||||
/**
|
||||
* The identity of a Health Records reading the person deleted (the import
|
||||
* ledger, `LabResultImportKey`). A digest, no lab content.
|
||||
*/
|
||||
export interface LabResultImportKeyBackupEntry {
|
||||
source: string;
|
||||
externalId: string;
|
||||
createdAt: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* The lab slice of the records section: results, markers, merge ledger, and
|
||||
* the identities of the Health Records readings the person deleted.
|
||||
*/
|
||||
export async function buildLabsBackupSlice(
|
||||
prisma: Pick<
|
||||
PrismaClient,
|
||||
"labResult" | "biomarker" | "biomarkerAlias" | "biomarkerMerge"
|
||||
| "labResult"
|
||||
| "biomarker"
|
||||
| "biomarkerAlias"
|
||||
| "biomarkerMerge"
|
||||
| "labResultImportKey"
|
||||
>,
|
||||
userId: string,
|
||||
disasterRecovery: boolean,
|
||||
@@ -157,35 +174,42 @@ export async function buildLabsBackupSlice(
|
||||
labResults: LabResultBackupEntry[];
|
||||
biomarkers: BiomarkerBackupEntry[];
|
||||
biomarkerMerges: BiomarkerMergeBackupEntry[];
|
||||
labResultImportKeys: LabResultImportKeyBackupEntry[];
|
||||
}> {
|
||||
const [labResultRows, biomarkerRows, biomarkerAliasRows, biomarkerMergeRows] =
|
||||
await Promise.all([
|
||||
prisma.labResult.findMany({
|
||||
where: disasterRecovery ? { userId } : { userId, deletedAt: null },
|
||||
orderBy: [{ takenAt: "desc" }, { id: "asc" }],
|
||||
include: { biomarker: { select: { name: true } } },
|
||||
}),
|
||||
prisma.biomarker.findMany({
|
||||
where: { userId },
|
||||
orderBy: [{ name: "asc" }, { id: "asc" }],
|
||||
}),
|
||||
prisma.biomarkerAlias.findMany({
|
||||
where: { userId },
|
||||
orderBy: [{ biomarkerId: "asc" }, { nameFolded: "asc" }, { id: "asc" }],
|
||||
select: {
|
||||
id: true,
|
||||
biomarkerId: true,
|
||||
name: true,
|
||||
source: true,
|
||||
createdAt: true,
|
||||
},
|
||||
}),
|
||||
prisma.biomarkerMerge.findMany({
|
||||
where: { userId },
|
||||
orderBy: [{ mergedAt: "asc" }, { id: "asc" }],
|
||||
include: { target: { select: { name: true } } },
|
||||
}),
|
||||
]);
|
||||
const [
|
||||
labResultRows,
|
||||
biomarkerRows,
|
||||
biomarkerAliasRows,
|
||||
biomarkerMergeRows,
|
||||
labResultImportKeys,
|
||||
] = await Promise.all([
|
||||
prisma.labResult.findMany({
|
||||
where: disasterRecovery ? { userId } : { userId, deletedAt: null },
|
||||
orderBy: [{ takenAt: "desc" }, { id: "asc" }],
|
||||
include: { biomarker: { select: { name: true } } },
|
||||
}),
|
||||
prisma.biomarker.findMany({
|
||||
where: { userId },
|
||||
orderBy: [{ name: "asc" }, { id: "asc" }],
|
||||
}),
|
||||
prisma.biomarkerAlias.findMany({
|
||||
where: { userId },
|
||||
orderBy: [{ biomarkerId: "asc" }, { nameFolded: "asc" }, { id: "asc" }],
|
||||
select: {
|
||||
id: true,
|
||||
biomarkerId: true,
|
||||
name: true,
|
||||
source: true,
|
||||
createdAt: true,
|
||||
},
|
||||
}),
|
||||
prisma.biomarkerMerge.findMany({
|
||||
where: { userId },
|
||||
orderBy: [{ mergedAt: "asc" }, { id: "asc" }],
|
||||
include: { target: { select: { name: true } } },
|
||||
}),
|
||||
buildDeletedLabImportKeys(prisma, userId, disasterRecovery),
|
||||
]);
|
||||
|
||||
const labResults: LabResultBackupEntry[] = labResultRows.map((r) => ({
|
||||
...(disasterRecovery
|
||||
@@ -288,7 +312,56 @@ export async function buildLabsBackupSlice(
|
||||
aliases: aliasesByMarker.get(b.id) ?? [],
|
||||
}));
|
||||
|
||||
return { labResults, biomarkers, biomarkerMerges };
|
||||
return { labResults, biomarkers, biomarkerMerges, labResultImportKeys };
|
||||
}
|
||||
|
||||
/**
|
||||
* What the person deleted among their Health Records readings, as the import
|
||||
* ledger would hold it once every deletion is purged: the ledger's own rows,
|
||||
* then, in a portable file, the identities of the deleted readings still
|
||||
* waiting as tombstones. A portable file does not carry those readings, as it
|
||||
* carries no tombstone; without their identities a restore onto a fresh
|
||||
* instance would let the next import write each one again. A disaster-recovery
|
||||
* file carries the tombstones themselves. An identity is carried once.
|
||||
*/
|
||||
async function buildDeletedLabImportKeys(
|
||||
prisma: Pick<PrismaClient, "labResult" | "labResultImportKey">,
|
||||
userId: string,
|
||||
disasterRecovery: boolean,
|
||||
): Promise<LabResultImportKeyBackupEntry[]> {
|
||||
const [ledger, tombstones] = await Promise.all([
|
||||
prisma.labResultImportKey.findMany({
|
||||
where: { userId },
|
||||
orderBy: [{ createdAt: "asc" }, { id: "asc" }],
|
||||
select: { source: true, externalId: true, createdAt: true },
|
||||
}),
|
||||
disasterRecovery
|
||||
? Promise.resolve([])
|
||||
: prisma.labResult.findMany({
|
||||
where: {
|
||||
userId,
|
||||
deletedAt: { not: null },
|
||||
externalId: { not: null },
|
||||
},
|
||||
orderBy: [{ deletedAt: "asc" }, { id: "asc" }],
|
||||
select: { source: true, externalId: true, deletedAt: true },
|
||||
}),
|
||||
]);
|
||||
const out: LabResultImportKeyBackupEntry[] = [];
|
||||
const seen = new Set<string>();
|
||||
const add = (source: string, externalId: string, at: Date) => {
|
||||
const key = JSON.stringify([source, externalId]);
|
||||
if (seen.has(key)) return;
|
||||
seen.add(key);
|
||||
out.push({ source, externalId, createdAt: at.toISOString() });
|
||||
};
|
||||
for (const row of ledger) add(row.source, row.externalId, row.createdAt);
|
||||
for (const row of tombstones) {
|
||||
if (row.externalId && row.deletedAt) {
|
||||
add(row.source, row.externalId, row.deletedAt);
|
||||
}
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
// ── Restore ──────────────────────────────────────────────────────────────────
|
||||
@@ -298,6 +371,11 @@ export async function clearLabResults(ctx: RestoreContext): Promise<void> {
|
||||
where: { userId: ctx.ownerId },
|
||||
});
|
||||
ctx.cleared.labResults = labResults.count;
|
||||
// The ledger is what the file says was deleted: replaced, like the rest.
|
||||
const keys = await ctx.tx.labResultImportKey.deleteMany({
|
||||
where: { userId: ctx.ownerId },
|
||||
});
|
||||
ctx.cleared.labResultImportKeys = keys.count;
|
||||
}
|
||||
|
||||
export async function clearBiomarkers(ctx: RestoreContext): Promise<void> {
|
||||
@@ -487,4 +565,32 @@ export async function restoreLabResults(ctx: RestoreContext): Promise<void> {
|
||||
const labSourceId = lab.id ?? lab.ref;
|
||||
if (labSourceId) labResultIdBySourceId.set(labSourceId, createdLab.id);
|
||||
}
|
||||
await restoreLabImportKeys(ctx);
|
||||
}
|
||||
|
||||
/**
|
||||
* The import ledger, after the results: an identity a restored reading holds
|
||||
* (live, or a tombstone in a disaster-recovery file) is that reading's, and
|
||||
* the ledger only remembers identities whose row is gone.
|
||||
*/
|
||||
async function restoreLabImportKeys(ctx: RestoreContext): Promise<void> {
|
||||
const { tx, ownerId, payload } = ctx;
|
||||
const held = new Set(
|
||||
payload.labResults.flatMap((lab) =>
|
||||
lab.externalId ? [JSON.stringify([lab.source, lab.externalId])] : [],
|
||||
),
|
||||
);
|
||||
const keys = (payload.labResultImportKeys ?? []).filter(
|
||||
(key) => !held.has(JSON.stringify([key.source, key.externalId])),
|
||||
);
|
||||
if (keys.length === 0) return;
|
||||
await tx.labResultImportKey.createMany({
|
||||
data: keys.map((key) => ({
|
||||
userId: ownerId,
|
||||
source: key.source,
|
||||
externalId: key.externalId,
|
||||
...(key.createdAt ? { createdAt: new Date(key.createdAt) } : {}),
|
||||
})),
|
||||
skipDuplicates: true,
|
||||
});
|
||||
}
|
||||
@@ -39,6 +39,7 @@ import {
|
||||
type BiomarkerBackupEntry,
|
||||
type BiomarkerMergeBackupEntry,
|
||||
type LabResultBackupEntry,
|
||||
type LabResultImportKeyBackupEntry,
|
||||
} from "@/lib/export/labs-backup";
|
||||
import {
|
||||
buildWorkoutsBackupSlice,
|
||||
@@ -71,6 +72,7 @@ export interface RecordsBackupSection {
|
||||
labResults: LabResultBackupEntry[];
|
||||
biomarkers: BiomarkerBackupEntry[];
|
||||
biomarkerMerges: BiomarkerMergeBackupEntry[];
|
||||
labResultImportKeys: LabResultImportKeyBackupEntry[];
|
||||
illnessEpisodes: CanonicalIllnessEpisode[];
|
||||
allergies: CanonicalAllergy[];
|
||||
familyHistory: FamilyHistoryEntryDTO[];
|
||||
@@ -87,6 +89,7 @@ export interface RecordsBackupSection {
|
||||
|
||||
export interface RecordsBackupCounts {
|
||||
labResults: number;
|
||||
labResultImportKeys: number;
|
||||
biomarkers: number;
|
||||
illnessEpisodes: number;
|
||||
illnessDayLogs: number;
|
||||
@@ -107,6 +110,7 @@ export async function buildRecordsBackupSection(
|
||||
| "biomarker"
|
||||
| "biomarkerAlias"
|
||||
| "biomarkerMerge"
|
||||
| "labResultImportKey"
|
||||
| "illnessEpisode"
|
||||
| "allergy"
|
||||
| "familyHistoryEntry"
|
||||
@@ -157,6 +161,7 @@ export function countRecordsBackupSection(
|
||||
): RecordsBackupCounts {
|
||||
return {
|
||||
labResults: section.labResults.length,
|
||||
labResultImportKeys: section.labResultImportKeys.length,
|
||||
biomarkers: section.biomarkers.length,
|
||||
illnessEpisodes: section.illnessEpisodes.length,
|
||||
illnessDayLogs: section.illnessEpisodes.reduce(
|
||||
|
||||
@@ -63,6 +63,7 @@ export const RESTORE_CLEARED_KEYS = [
|
||||
"cycleDayLogs",
|
||||
"cycleProfile",
|
||||
"labResults",
|
||||
"labResultImportKeys",
|
||||
"biomarkers",
|
||||
"nutrientDays",
|
||||
"illnessEpisodes",
|
||||
|
||||
@@ -34,10 +34,11 @@
|
||||
* so a new nulling key into a purged model cannot ship without one.
|
||||
*/
|
||||
// account-deletion(file): a retention sweep over rows already deleted, with its own retention clock; it does no work for an account.
|
||||
import type { PrismaClient } from "@/generated/prisma/client";
|
||||
import type { Prisma, 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-queue";
|
||||
import { retireLabImportKeys } from "@/lib/labs/fhir-import/import-keys";
|
||||
import {
|
||||
purgeTombstonesByAccount,
|
||||
type TombstonePurgeOutcome,
|
||||
@@ -72,6 +73,15 @@ export type SoftDeleteRetention =
|
||||
keep?: string;
|
||||
/** The live references that hold a tombstone back. */
|
||||
referencedBy?: readonly LiveReferrer[];
|
||||
/**
|
||||
* Runs in the purge's transaction before the delete, over the rows the
|
||||
* delete is about to take: for what a tombstone remembers that must
|
||||
* outlive the row.
|
||||
*/
|
||||
beforePurge?: (
|
||||
tx: Prisma.TransactionClient,
|
||||
rows: { ids: string[]; userId: string; cutoff: Date },
|
||||
) => Promise<unknown>;
|
||||
/** Why the horizon or the condition differs from the default. */
|
||||
why?: string;
|
||||
};
|
||||
@@ -129,7 +139,17 @@ export const SOFT_DELETE_RETENTION: Readonly<
|
||||
live("encounters", "reminder_id"),
|
||||
live("vaccination_records", "reminder_id"),
|
||||
]),
|
||||
LabResult: standard("lab_results"),
|
||||
LabResult: {
|
||||
kind: "purge",
|
||||
table: "lab_results",
|
||||
days: () => TOMBSTONE_RETENTION_DAYS,
|
||||
beforePurge: (tx, { ids, userId, cutoff }) =>
|
||||
retireLabImportKeys(tx, userId, {
|
||||
id: { in: ids },
|
||||
deletedAt: { not: null, lt: cutoff },
|
||||
}),
|
||||
why: "A deleted Health Records reading's identity is what keeps the next import from writing it again; the purge moves it into the import ledger (`LabResultImportKey`) before the row goes.",
|
||||
},
|
||||
CustomMetric: standard("custom_metrics"),
|
||||
CustomMetricEntry: standard("custom_metric_entries"),
|
||||
CoachPlan: standard("coach_plans"),
|
||||
@@ -217,15 +237,17 @@ export async function purgeSoftDeletedTable(
|
||||
),
|
||||
// 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(
|
||||
deleteIds: async (tx, ids, userId) => {
|
||||
await entry.beforePurge?.(tx, { ids, userId, cutoff });
|
||||
return 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 };
|
||||
}
|
||||
|
||||
@@ -0,0 +1,63 @@
|
||||
/**
|
||||
* The import ledger of Health Records lab readings the person deleted
|
||||
* (`LabResultImportKey`, #1400).
|
||||
*
|
||||
* A deleted reading first waits as a tombstone, and its own `externalId` is
|
||||
* the memory that the person deleted it: the import finds the row and answers
|
||||
* "duplicate". Two things remove the row for good, the nightly tombstone purge
|
||||
* and deleting the reading's whole marker, and both used to take that memory
|
||||
* with them, so the next Apple Health export (which carries the whole history
|
||||
* every time) wrote the reading again. Each now calls
|
||||
* {@link retireLabImportKeys} in the same transaction as the delete, which
|
||||
* moves the identities of the rows about to go into the ledger.
|
||||
*
|
||||
* The ledger holds the digest only, never lab content. It is read by
|
||||
* {@link heldLabImportKeys} (the import's duplicate check) and carried by the
|
||||
* backup (`src/lib/export/labs-backup.ts`), together with the identities of
|
||||
* the deleted readings a portable file leaves out.
|
||||
*/
|
||||
import type { Prisma, PrismaClient } from "@/generated/prisma/client";
|
||||
|
||||
type LedgerClient = Pick<PrismaClient, "labResult" | "labResultImportKey">;
|
||||
|
||||
/**
|
||||
* Move the identities of the lab rows `where` selects into the ledger, before
|
||||
* the caller deletes those rows. Rows without an identity are not imported
|
||||
* from anywhere and have nothing to remember. Run it inside the delete's
|
||||
* transaction so the identity and the row never both go missing.
|
||||
*/
|
||||
export async function retireLabImportKeys(
|
||||
tx: LedgerClient,
|
||||
userId: string,
|
||||
where: Prisma.LabResultWhereInput,
|
||||
): Promise<number> {
|
||||
const rows = await tx.labResult.findMany({
|
||||
where: { ...where, userId, externalId: { not: null } },
|
||||
select: { source: true, externalId: true },
|
||||
});
|
||||
if (rows.length === 0) return 0;
|
||||
const { count } = await tx.labResultImportKey.createMany({
|
||||
data: rows.map((row) => ({
|
||||
userId,
|
||||
source: row.source,
|
||||
externalId: row.externalId!,
|
||||
})),
|
||||
skipDuplicates: true,
|
||||
});
|
||||
return count;
|
||||
}
|
||||
|
||||
/** The identities among `externalIds` the ledger holds as deleted. */
|
||||
export async function heldLabImportKeys(
|
||||
client: Pick<PrismaClient, "labResultImportKey">,
|
||||
userId: string,
|
||||
source: string,
|
||||
externalIds: readonly string[],
|
||||
): Promise<Set<string>> {
|
||||
if (externalIds.length === 0) return new Set();
|
||||
const rows = await client.labResultImportKey.findMany({
|
||||
where: { userId, source, externalId: { in: [...externalIds] } },
|
||||
select: { externalId: true },
|
||||
});
|
||||
return new Set(rows.map((row) => row.externalId));
|
||||
}
|
||||
@@ -11,8 +11,10 @@
|
||||
*
|
||||
* Two duplicate checks, in this order:
|
||||
* 1. the identity (`externalId`) under `source = APPLE_HEALTH`, against
|
||||
* every row including the deleted ones: a result the person deleted
|
||||
* stays deleted however often the export or the phone sends it again;
|
||||
* every row including the deleted ones and against the import ledger
|
||||
* that keeps a deleted reading's identity once its row is gone
|
||||
* (`./import-keys.ts`): a result the person deleted stays deleted
|
||||
* however often the export or the phone sends it again;
|
||||
* 2. the reading itself (marker or name, day, value) against the live
|
||||
* record, so a result already typed, scanned or imported from a CSV is
|
||||
* not written a second time.
|
||||
@@ -43,6 +45,7 @@ import {
|
||||
import { readingKeys } from "@/lib/labs/reading-keys";
|
||||
import { encryptNoteToBytes } from "@/lib/labs/store";
|
||||
|
||||
import { heldLabImportKeys } from "./import-keys";
|
||||
import {
|
||||
APPLE_HEALTH_LAB_SOURCE,
|
||||
type FhirLabWriteStatus,
|
||||
@@ -53,7 +56,7 @@ const CHUNK = 200;
|
||||
|
||||
type LabWriteClient = Pick<
|
||||
PrismaClient,
|
||||
"labResult" | "biomarker" | "biomarkerAlias"
|
||||
"labResult" | "biomarker" | "biomarkerAlias" | "labResultImportKey"
|
||||
>;
|
||||
|
||||
export interface FhirLabWriteResult {
|
||||
@@ -118,18 +121,27 @@ export async function writeFhirLabReadings(args: {
|
||||
seenIds.add(reading.externalId);
|
||||
fresh.push({ at, reading });
|
||||
}
|
||||
const held = new Set(
|
||||
const freshIds = fresh.map((f) => f.reading.externalId);
|
||||
const held = new Set<string | null>(
|
||||
(
|
||||
await client.labResult.findMany({
|
||||
where: {
|
||||
userId,
|
||||
source: APPLE_HEALTH_LAB_SOURCE,
|
||||
externalId: { in: fresh.map((f) => f.reading.externalId) },
|
||||
externalId: { in: freshIds },
|
||||
},
|
||||
select: { externalId: true },
|
||||
})
|
||||
).map((row) => row.externalId),
|
||||
);
|
||||
for (const id of await heldLabImportKeys(
|
||||
client,
|
||||
userId,
|
||||
APPLE_HEALTH_LAB_SOURCE,
|
||||
freshIds,
|
||||
)) {
|
||||
held.add(id);
|
||||
}
|
||||
|
||||
// 2. The marker, and whether the unit fits it.
|
||||
const planned: Pending[] = [];
|
||||
|
||||
@@ -40,6 +40,8 @@ export interface BackupSummary {
|
||||
cycleDayLogs: number;
|
||||
/** Lab results in the backup. */
|
||||
labResults: number;
|
||||
/** Identities of Health Records lab readings the person deleted. */
|
||||
labResultImportKeys: number;
|
||||
nutrientDays: number;
|
||||
/** User-scoped biomarker catalog entries in the backup. */
|
||||
biomarkers: number;
|
||||
@@ -146,6 +148,7 @@ export function summarizeBackup(payload: BackupPayload): BackupSummary {
|
||||
cycleDayLogs: payload.cycleDayLogs.length,
|
||||
nutrientDays: payload.nutrientDays.length,
|
||||
labResults: payload.labResults.length,
|
||||
labResultImportKeys: payload.labResultImportKeys.length,
|
||||
biomarkers: payload.biomarkers.length,
|
||||
illnessEpisodes: payload.illnessEpisodes.length,
|
||||
illnessDayLogs: payload.illnessEpisodes.reduce(
|
||||
|
||||
@@ -67,6 +67,7 @@ import { intradayProfileBackupSchema } from "@/lib/export/intraday-profile-backu
|
||||
import {
|
||||
biomarkerBackupSchema,
|
||||
biomarkerMergeBackupSchema,
|
||||
labResultImportKeyBackupSchema,
|
||||
labResultBackupSchema,
|
||||
} from "@/lib/export/labs-backup-schema";
|
||||
import { lifeEventBackupSchema } from "@/lib/export/life-events-backup-schema";
|
||||
@@ -167,6 +168,9 @@ export const backupPayloadSchema = z
|
||||
biomarkers: z.array(biomarkerBackupSchema).default([]),
|
||||
// Defaulted: a file written before v1.43 carries no key.
|
||||
biomarkerMerges: z.array(biomarkerMergeBackupSchema).default([]),
|
||||
// The identities of Health Records readings the person deleted.
|
||||
// Defaulted: a file written before the lab import ledger carries no key.
|
||||
labResultImportKeys: z.array(labResultImportKeyBackupSchema).default([]),
|
||||
illnessEpisodes: z.array(illnessEpisodeBackupSchema).default([]),
|
||||
// Defaulted: a file written before v1.40 carries no key.
|
||||
symptomDefinitions: z.array(symptomDefinitionBackupSchema).default([]),
|
||||
|
||||
Reference in new issue
Block a user