mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-19 00:12:18 +00:00
feat(export): make the full backup actually full
The full-backup export (plaintext + passphrase-encrypted, and the weekly auto-backup) only ever covered measurements, medications, mood, and cycle data. Labs, illness episodes, allergies, family history, and workouts were silently missing, and the export UI implied "everything" was included. Adds a shared records-backup section (src/lib/export/records-backup.ts) that both the on-demand export routes and the weekly data-backup worker read from: lab results + the biomarker catalog, illness episodes (including flares via parentConditionId) with their day-logs, allergies, family history, and workout summaries. Every read is owner-scoped and soft-delete-filtered; encrypted free-text columns decrypt fail-soft so one bad row never aborts the whole backup. Document original files and workout GPS/per-sample time series stay deliberately out of the JSON payload (size). The payload now carries an explicit manifest disclosing exactly that, and the export UI states it in plain text instead of implying completeness. These new domains are export-only for now — admin restore still only recreates the original measurements/medications/mood/cycle domains; the payload validates and reports counts for the rest, but restoring a snapshot does not yet recreate them. Also fixes the admin backup-upload total to count every domain, not just the original four.
This commit is contained in:
1 parent
837c44e4c2
commit
3dd5dbeb42
22 files changed
+1491
-68
No files matched your search
@@ -0,0 +1,316 @@
|
||||
/**
|
||||
* v1.28 backup-completeness — `buildRecordsBackupSection` is the shared
|
||||
* builder both the on-demand full-backup export AND the weekly `data-backup`
|
||||
* worker read from (see the module doc comment in `../records-backup.ts`).
|
||||
* These tests pin:
|
||||
* - every read is scoped to the caller's `userId` (+ `deletedAt: null`
|
||||
* where the model soft-deletes) — no cross-user leakage,
|
||||
* - encrypted free-text columns decrypt through the real crypto path
|
||||
* (round-tripped with the same key-stub pattern `note-cipher.test.ts`
|
||||
* uses) rather than an opaque mock,
|
||||
* - a document manifest entry never carries the raw file bytes,
|
||||
* - the manifest discloses the two deliberate exclusions,
|
||||
* - `countRecordsBackupSection` totals every domain, including nested
|
||||
* illness day-logs.
|
||||
*/
|
||||
import { describe, it, expect, beforeEach, afterEach, vi } from "vitest";
|
||||
|
||||
import { encryptToBytes } from "@/lib/ai/coach/bytes-codec";
|
||||
import { encryptNoteToBytes } from "@/lib/labs/store";
|
||||
import { encryptContextToBytes } from "@/lib/labs/biomarker-store";
|
||||
import { encryptDocumentSummary } from "@/lib/documents/store";
|
||||
import {
|
||||
buildRecordsBackupSection,
|
||||
countRecordsBackupSection,
|
||||
DOCUMENTS_MANIFEST_NOTE,
|
||||
WORKOUTS_MANIFEST_NOTE,
|
||||
} from "../records-backup";
|
||||
|
||||
const KEY_A = "a".repeat(64);
|
||||
|
||||
beforeEach(() => {
|
||||
vi.stubEnv("ENCRYPTION_KEYS", "");
|
||||
vi.stubEnv("ENCRYPTION_ACTIVE_KEY_ID", "");
|
||||
vi.stubEnv("ENCRYPTION_KEY", KEY_A);
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
vi.unstubAllEnvs();
|
||||
});
|
||||
|
||||
const USER_ID = "user-1";
|
||||
|
||||
function buildMockPrisma() {
|
||||
return {
|
||||
labResult: {
|
||||
findMany: vi.fn().mockResolvedValue([
|
||||
{
|
||||
panel: "Lipid panel",
|
||||
analyte: "LDL",
|
||||
value: 118,
|
||||
valueText: null,
|
||||
unit: "mg/dL",
|
||||
referenceLow: null,
|
||||
referenceHigh: 130,
|
||||
takenAt: new Date("2026-04-01T09:00:00.000Z"),
|
||||
source: "MANUAL",
|
||||
biomarker: { name: "LDL Cholesterol" },
|
||||
noteEncrypted: encryptNoteToBytes("Fasted draw."),
|
||||
},
|
||||
]),
|
||||
},
|
||||
biomarker: {
|
||||
findMany: vi.fn().mockResolvedValue([
|
||||
{
|
||||
name: "LDL Cholesterol",
|
||||
unit: "mg/dL",
|
||||
lowerBound: null,
|
||||
upperBound: 130,
|
||||
panel: "Lipid panel",
|
||||
hidden: false,
|
||||
contextEncrypted: encryptContextToBytes("Low-density lipoprotein."),
|
||||
},
|
||||
]),
|
||||
},
|
||||
illnessEpisode: {
|
||||
findMany: vi.fn().mockResolvedValue([
|
||||
{
|
||||
id: "ep-parent",
|
||||
label: "Migraine",
|
||||
type: "CHRONIC",
|
||||
lifecycle: "CHRONIC_ONGOING",
|
||||
onsetAt: new Date("2026-01-01T00:00:00.000Z"),
|
||||
resolvedAt: null,
|
||||
parentConditionId: null,
|
||||
noteEncrypted: null,
|
||||
createdAt: new Date("2026-01-01T00:00:00.000Z"),
|
||||
updatedAt: new Date("2026-01-01T00:00:00.000Z"),
|
||||
dayLogs: [],
|
||||
},
|
||||
{
|
||||
id: "ep-flare",
|
||||
label: "Migraine flare",
|
||||
type: "CHRONIC",
|
||||
lifecycle: "FLARE",
|
||||
onsetAt: new Date("2026-04-10T00:00:00.000Z"),
|
||||
resolvedAt: new Date("2026-04-12T00:00:00.000Z"),
|
||||
parentConditionId: "ep-parent",
|
||||
noteEncrypted: encryptToBytes("Triggered by travel."),
|
||||
createdAt: new Date("2026-04-10T00:00:00.000Z"),
|
||||
updatedAt: new Date("2026-04-12T00:00:00.000Z"),
|
||||
dayLogs: [
|
||||
{
|
||||
id: "dl-1",
|
||||
episodeId: "ep-flare",
|
||||
date: "2026-04-10",
|
||||
functionalImpact: 2,
|
||||
feverC: null,
|
||||
noteEncrypted: encryptToBytes("Bad day."),
|
||||
updatedAt: new Date("2026-04-10T00:00:00.000Z"),
|
||||
symptomLinks: [{ severity: 3, symptom: { key: "headache" } }],
|
||||
},
|
||||
],
|
||||
},
|
||||
]),
|
||||
},
|
||||
allergy: {
|
||||
findMany: vi.fn().mockResolvedValue([
|
||||
{
|
||||
id: "al-1",
|
||||
substance: "Penicillin",
|
||||
category: "MEDICATION",
|
||||
type: "ALLERGY",
|
||||
severity: "SEVERE",
|
||||
status: "ACTIVE",
|
||||
onsetAt: null,
|
||||
reactionEncrypted: encryptToBytes("Hives"),
|
||||
notesEncrypted: null,
|
||||
createdAt: new Date("2026-01-01T00:00:00.000Z"),
|
||||
updatedAt: new Date("2026-01-01T00:00:00.000Z"),
|
||||
},
|
||||
]),
|
||||
},
|
||||
familyHistoryEntry: {
|
||||
findMany: vi.fn().mockResolvedValue([
|
||||
{
|
||||
id: "fh-1",
|
||||
relationship: "MOTHER",
|
||||
condition: "Type 2 diabetes",
|
||||
ageAtOnset: 52,
|
||||
notesEncrypted: null,
|
||||
createdAt: new Date("2026-01-01T00:00:00.000Z"),
|
||||
updatedAt: new Date("2026-01-01T00:00:00.000Z"),
|
||||
},
|
||||
]),
|
||||
},
|
||||
workout: {
|
||||
findMany: vi.fn().mockResolvedValue([
|
||||
{
|
||||
sportType: "running",
|
||||
startedAt: new Date("2026-04-01T07:00:00.000Z"),
|
||||
endedAt: new Date("2026-04-01T08:00:00.000Z"),
|
||||
durationSec: 3600,
|
||||
totalEnergyKcal: 600,
|
||||
totalDistanceM: 10000,
|
||||
avgHeartRate: 150,
|
||||
maxHeartRate: 175,
|
||||
minHeartRate: 110,
|
||||
stepCount: 9000,
|
||||
elevationM: 80,
|
||||
pauseDurationSec: 0,
|
||||
source: "APPLE_HEALTH",
|
||||
externalId: "hk-1",
|
||||
},
|
||||
]),
|
||||
},
|
||||
inboundDocument: {
|
||||
findMany: vi.fn().mockResolvedValue([
|
||||
{
|
||||
id: "doc-1",
|
||||
kind: "LAB_RESULT",
|
||||
title: "Blood panel",
|
||||
filename: "panel.pdf",
|
||||
mimeType: "application/pdf",
|
||||
byteSize: 12345,
|
||||
status: "STORED",
|
||||
reportDate: new Date("2026-03-30T00:00:00.000Z"),
|
||||
documentDate: new Date("2026-03-30T00:00:00.000Z"),
|
||||
summaryEncrypted: encryptDocumentSummary("Routine panel."),
|
||||
createdAt: new Date("2026-03-30T00:00:00.000Z"),
|
||||
},
|
||||
]),
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
describe("buildRecordsBackupSection", () => {
|
||||
it("scopes every soft-deletable read to userId + deletedAt: null", async () => {
|
||||
const prisma = buildMockPrisma();
|
||||
await buildRecordsBackupSection(prisma as never, USER_ID);
|
||||
|
||||
expect(prisma.labResult.findMany).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
where: { userId: USER_ID, deletedAt: null },
|
||||
}),
|
||||
);
|
||||
expect(prisma.illnessEpisode.findMany).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
where: { userId: USER_ID, deletedAt: null },
|
||||
}),
|
||||
);
|
||||
expect(prisma.allergy.findMany).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
where: { userId: USER_ID, deletedAt: null },
|
||||
}),
|
||||
);
|
||||
expect(prisma.familyHistoryEntry.findMany).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
where: { userId: USER_ID, deletedAt: null },
|
||||
}),
|
||||
);
|
||||
expect(prisma.inboundDocument.findMany).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
where: { userId: USER_ID, deletedAt: null },
|
||||
}),
|
||||
);
|
||||
// Biomarker + Workout are not soft-deleted models — userId-only scope.
|
||||
expect(prisma.biomarker.findMany).toHaveBeenCalledWith(
|
||||
expect.objectContaining({ where: { userId: USER_ID } }),
|
||||
);
|
||||
expect(prisma.workout.findMany).toHaveBeenCalledWith(
|
||||
expect.objectContaining({ where: { userId: USER_ID } }),
|
||||
);
|
||||
});
|
||||
|
||||
it("decrypts every free-text column through the real crypto path", async () => {
|
||||
const prisma = buildMockPrisma();
|
||||
const section = await buildRecordsBackupSection(prisma as never, USER_ID);
|
||||
|
||||
expect(section.labResults[0].note).toBe("Fasted draw.");
|
||||
expect(section.labResults[0].biomarkerName).toBe("LDL Cholesterol");
|
||||
expect(section.biomarkers[0].context).toBe("Low-density lipoprotein.");
|
||||
expect(section.allergies[0].reaction).toBe("Hives");
|
||||
expect(section.familyHistory[0].condition).toBe("Type 2 diabetes");
|
||||
expect(section.documents[0].summary).toBe("Routine panel.");
|
||||
});
|
||||
|
||||
it("threads an illness flare's parentConditionId and nests its day-log + symptom", async () => {
|
||||
const prisma = buildMockPrisma();
|
||||
const section = await buildRecordsBackupSection(prisma as never, USER_ID);
|
||||
|
||||
const flare = section.illnessEpisodes.find((e) => e.id === "ep-flare");
|
||||
expect(flare?.parentConditionId).toBe("ep-parent");
|
||||
expect(flare?.note).toBe("Triggered by travel.");
|
||||
expect(flare?.dayLogs).toHaveLength(1);
|
||||
expect(flare?.dayLogs[0].note).toBe("Bad day.");
|
||||
expect(flare?.dayLogs[0].symptoms).toEqual([
|
||||
{ key: "headache", severity: 3 },
|
||||
]);
|
||||
});
|
||||
|
||||
it("never carries the document's raw content bytes in the manifest entry", async () => {
|
||||
const prisma = buildMockPrisma();
|
||||
const section = await buildRecordsBackupSection(prisma as never, USER_ID);
|
||||
|
||||
expect(section.documents[0]).not.toHaveProperty("contentEncrypted");
|
||||
expect(section.documents[0]).not.toHaveProperty("content");
|
||||
// The read itself never selects the blob column — pin the `select`.
|
||||
const call = prisma.inboundDocument.findMany.mock.calls[0][0];
|
||||
expect(call.select).not.toHaveProperty("contentEncrypted");
|
||||
});
|
||||
|
||||
it("discloses the document + workout exclusions in the manifest", async () => {
|
||||
const prisma = buildMockPrisma();
|
||||
const section = await buildRecordsBackupSection(prisma as never, USER_ID);
|
||||
|
||||
expect(section.manifest.documents).toEqual({
|
||||
included: "metadata-only",
|
||||
note: DOCUMENTS_MANIFEST_NOTE,
|
||||
});
|
||||
expect(section.manifest.workouts).toEqual({
|
||||
included: "summary-only",
|
||||
note: WORKOUTS_MANIFEST_NOTE,
|
||||
});
|
||||
});
|
||||
|
||||
it("counts every domain, including nested illness day-logs", async () => {
|
||||
const prisma = buildMockPrisma();
|
||||
const section = await buildRecordsBackupSection(prisma as never, USER_ID);
|
||||
const counts = countRecordsBackupSection(section);
|
||||
|
||||
expect(counts).toEqual({
|
||||
labResults: 1,
|
||||
biomarkers: 1,
|
||||
illnessEpisodes: 2,
|
||||
illnessDayLogs: 1,
|
||||
allergies: 1,
|
||||
familyHistory: 1,
|
||||
workouts: 1,
|
||||
documents: 1,
|
||||
});
|
||||
});
|
||||
|
||||
it("fails soft (null, not a throw) on an undecryptable note", async () => {
|
||||
const prisma = buildMockPrisma();
|
||||
// Corrupt the ciphertext so decrypt throws internally — the row must
|
||||
// still come back with `note: null`, never abort the whole backup.
|
||||
prisma.labResult.findMany.mockResolvedValueOnce([
|
||||
{
|
||||
panel: null,
|
||||
analyte: "HbA1c",
|
||||
value: 5.4,
|
||||
valueText: null,
|
||||
unit: "%",
|
||||
referenceLow: null,
|
||||
referenceHigh: null,
|
||||
takenAt: new Date("2026-04-01T09:00:00.000Z"),
|
||||
source: "MANUAL",
|
||||
biomarker: null,
|
||||
noteEncrypted: new Uint8Array([1, 2, 3, 4]),
|
||||
},
|
||||
]);
|
||||
|
||||
const section = await buildRecordsBackupSection(prisma as never, USER_ID);
|
||||
expect(section.labResults[0].note).toBeNull();
|
||||
});
|
||||
});
|
||||
@@ -15,8 +15,13 @@ import { readNote } from "@/lib/crypto/note-cipher";
|
||||
import { findMeasurementsPaged } from "@/lib/export/paged-measurements";
|
||||
import { BACKUP_SCHEMA_VERSION } from "@/lib/validations/backup";
|
||||
import { buildCycleBackupSection } from "@/lib/cycle/backup";
|
||||
import {
|
||||
buildRecordsBackupSection,
|
||||
countRecordsBackupSection,
|
||||
type RecordsBackupCounts,
|
||||
} from "@/lib/export/records-backup";
|
||||
|
||||
export interface FullBackupCounts {
|
||||
export interface FullBackupCounts extends RecordsBackupCounts {
|
||||
measurements: number;
|
||||
medications: number;
|
||||
intakeEvents: number;
|
||||
@@ -38,7 +43,7 @@ export async function buildFullBackupPayload(
|
||||
prisma: PrismaClient,
|
||||
userId: string,
|
||||
): Promise<FullBackupResult> {
|
||||
const [measurements, medications, intakeEvents, moodEntries, cycle] =
|
||||
const [measurements, medications, intakeEvents, moodEntries, cycle, records] =
|
||||
await Promise.all([
|
||||
// v1.28.25 — keyset-paginated read with a narrow select. The backup
|
||||
// is inherently whole-history, so on a CGM / per-sample-HR account the
|
||||
@@ -88,6 +93,7 @@ export async function buildFullBackupPayload(
|
||||
orderBy: { moodLoggedAt: "desc" },
|
||||
}),
|
||||
buildCycleBackupSection(prisma, userId),
|
||||
buildRecordsBackupSection(prisma, userId),
|
||||
]);
|
||||
|
||||
const payload = {
|
||||
@@ -131,6 +137,18 @@ export async function buildFullBackupPayload(
|
||||
cycleProfile: cycle.cycleProfile,
|
||||
cycles: cycle.cycles,
|
||||
cycleDayLogs: cycle.cycleDayLogs,
|
||||
// v1.28 backup-completeness — the domains the pre-existing shape never
|
||||
// covered. `manifest` discloses the two deliberate exclusions (document
|
||||
// binaries, workout GPS/sample time series) inline in the file itself,
|
||||
// not just in the export UI copy.
|
||||
labResults: records.labResults,
|
||||
biomarkers: records.biomarkers,
|
||||
illnessEpisodes: records.illnessEpisodes,
|
||||
allergies: records.allergies,
|
||||
familyHistory: records.familyHistory,
|
||||
workouts: records.workouts,
|
||||
documents: records.documents,
|
||||
manifest: records.manifest,
|
||||
};
|
||||
|
||||
return {
|
||||
@@ -142,6 +160,7 @@ export async function buildFullBackupPayload(
|
||||
moodEntries: moodEntries.length,
|
||||
cycles: cycle.cycles.length,
|
||||
cycleDayLogs: cycle.cycleDayLogs.length,
|
||||
...countRecordsBackupSection(records),
|
||||
},
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,334 @@
|
||||
/**
|
||||
* Structured-records / workouts / documents-manifest backup section.
|
||||
*
|
||||
* Shared by the on-demand full-backup builder (`full-backup-payload.ts`,
|
||||
* consumed by `GET /api/export/full-backup` + `POST /api/export/encrypted`)
|
||||
* AND the weekly `data-backup` pg-boss worker
|
||||
* (`src/lib/jobs/reminder/backup-handlers.ts`) so both writers emit the same
|
||||
* shape — the doc-comment convention `buildCycleBackupSection` already
|
||||
* established for the cycle tables.
|
||||
*
|
||||
* Closes the backup-completeness gap: the pre-existing payload covered
|
||||
* measurements / medications / intake events / mood / cycle only. This adds
|
||||
* lab results + the biomarker catalog, illness episodes (including
|
||||
* flares/exacerbations via `parentConditionId`) with their day-logs,
|
||||
* allergies, family history, and workout summaries.
|
||||
*
|
||||
* Every read is scoped to `userId` + `deletedAt: null` (soft-deleted rows
|
||||
* never resurrect into a backup). Encrypted free-text columns are decrypted
|
||||
* FAIL-SOFT (null on a bad key / corrupt row, with a wide-event warning) so
|
||||
* one damaged row never aborts an entire backup — the same posture the
|
||||
* illness/allergy/family-history DTO layer already uses for list-style
|
||||
* reads. Never the ciphertext itself leaves this module.
|
||||
*
|
||||
* Document BINARIES are deliberately excluded — see `DOCUMENTS_MANIFEST`.
|
||||
* Workout GPS routes + per-sample time series (`WorkoutRoute` /
|
||||
* `WorkoutSamples`) are deliberately excluded — see `WORKOUTS_MANIFEST`.
|
||||
* Both exclusions are disclosed in the payload's `manifest` section (and in
|
||||
* the export UI copy) rather than silently implied as "everything".
|
||||
*/
|
||||
import type { PrismaClient } from "@/generated/prisma/client";
|
||||
import { decryptNoteSoft } from "@/lib/labs/store";
|
||||
import { decryptContextSoft } from "@/lib/labs/biomarker-store";
|
||||
import { decryptDocumentSummary } from "@/lib/documents/store";
|
||||
import { getEvent } from "@/lib/logging/context";
|
||||
import {
|
||||
toIllnessEpisodeDTO,
|
||||
toIllnessDayLogDTO,
|
||||
dayLogSymptomInclude,
|
||||
type IllnessEpisodeDTO,
|
||||
type IllnessDayLogDTO,
|
||||
} from "@/lib/illness/dto";
|
||||
import {
|
||||
toAllergyDTO,
|
||||
toFamilyHistoryEntryDTO,
|
||||
type AllergyDTO,
|
||||
type FamilyHistoryEntryDTO,
|
||||
} from "@/lib/records/dto";
|
||||
|
||||
/** Disclosed in the payload manifest AND mirrored in the export UI copy. */
|
||||
export const DOCUMENTS_MANIFEST_NOTE =
|
||||
"Document metadata (title, filename, type, dates, and the AI-generated " +
|
||||
"summary if one was generated) is included. The original uploaded files " +
|
||||
"are NOT included in this export — download them individually from " +
|
||||
"Settings → Documents, or via the document API.";
|
||||
|
||||
/** Disclosed in the payload manifest AND mirrored in the export UI copy. */
|
||||
export const WORKOUTS_MANIFEST_NOTE =
|
||||
"Workout summary records (duration, distance, heart rate, calories, " +
|
||||
"steps) are included. GPS routes and per-sample heart-rate/pace time " +
|
||||
"series are not included in this export.";
|
||||
|
||||
export interface LabResultBackupEntry {
|
||||
panel: string | null;
|
||||
analyte: string;
|
||||
value: number | null;
|
||||
valueText: string | null;
|
||||
unit: string;
|
||||
referenceLow: number | null;
|
||||
referenceHigh: number | null;
|
||||
takenAt: string;
|
||||
source: string;
|
||||
/** Human-readable cross-reference into `biomarkers` below, not an id. */
|
||||
biomarkerName: string | null;
|
||||
note: string | null;
|
||||
}
|
||||
|
||||
export interface BiomarkerBackupEntry {
|
||||
name: string;
|
||||
unit: string;
|
||||
lowerBound: number | null;
|
||||
upperBound: number | null;
|
||||
panel: string | null;
|
||||
hidden: boolean;
|
||||
context: string | null;
|
||||
}
|
||||
|
||||
export interface WorkoutBackupEntry {
|
||||
sportType: string;
|
||||
startedAt: string;
|
||||
endedAt: string;
|
||||
durationSec: number;
|
||||
totalEnergyKcal: number | null;
|
||||
totalDistanceM: number | null;
|
||||
avgHeartRate: number | null;
|
||||
maxHeartRate: number | null;
|
||||
minHeartRate: number | null;
|
||||
stepCount: number | null;
|
||||
elevationM: number | null;
|
||||
pauseDurationSec: number | null;
|
||||
source: string;
|
||||
externalId: string | null;
|
||||
}
|
||||
|
||||
export interface DocumentBackupEntry {
|
||||
id: string;
|
||||
kind: string;
|
||||
title: string | null;
|
||||
filename: string | null;
|
||||
mimeType: string;
|
||||
byteSize: number;
|
||||
status: string;
|
||||
reportDate: string | null;
|
||||
documentDate: string | null;
|
||||
summary: string | null;
|
||||
createdAt: string;
|
||||
}
|
||||
|
||||
export interface RecordsBackupSection {
|
||||
labResults: LabResultBackupEntry[];
|
||||
biomarkers: BiomarkerBackupEntry[];
|
||||
illnessEpisodes: Array<IllnessEpisodeDTO & { dayLogs: IllnessDayLogDTO[] }>;
|
||||
allergies: AllergyDTO[];
|
||||
familyHistory: FamilyHistoryEntryDTO[];
|
||||
workouts: WorkoutBackupEntry[];
|
||||
documents: DocumentBackupEntry[];
|
||||
manifest: {
|
||||
documents: { included: "metadata-only"; note: string };
|
||||
workouts: { included: "summary-only"; note: string };
|
||||
};
|
||||
}
|
||||
|
||||
export interface RecordsBackupCounts {
|
||||
labResults: number;
|
||||
biomarkers: number;
|
||||
illnessEpisodes: number;
|
||||
illnessDayLogs: number;
|
||||
allergies: number;
|
||||
familyHistory: number;
|
||||
workouts: number;
|
||||
documents: number;
|
||||
}
|
||||
|
||||
/** Decrypt a document's optional AI summary fail-soft (null on any error). */
|
||||
function decryptSummarySoft(buf: Uint8Array | null): string | null {
|
||||
if (!buf || buf.byteLength === 0) return null;
|
||||
try {
|
||||
return decryptDocumentSummary(buf);
|
||||
} catch (err) {
|
||||
getEvent()?.addWarning(
|
||||
`document summary decrypt failed: ${
|
||||
err instanceof Error ? err.message : String(err)
|
||||
}`,
|
||||
);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Build the structured-records / workouts / documents-manifest slice of a
|
||||
* user's full backup. Accepts any client exposing the needed delegates (the
|
||||
* app's global `prisma` OR the pg-boss worker's `getWorkerPrisma()` client)
|
||||
* so both writers share one read.
|
||||
*/
|
||||
export async function buildRecordsBackupSection(
|
||||
prisma: Pick<
|
||||
PrismaClient,
|
||||
| "labResult"
|
||||
| "biomarker"
|
||||
| "illnessEpisode"
|
||||
| "allergy"
|
||||
| "familyHistoryEntry"
|
||||
| "workout"
|
||||
| "inboundDocument"
|
||||
>,
|
||||
userId: string,
|
||||
): Promise<RecordsBackupSection> {
|
||||
const [
|
||||
labResultRows,
|
||||
biomarkerRows,
|
||||
episodeRows,
|
||||
allergyRows,
|
||||
familyRows,
|
||||
workoutRows,
|
||||
documentRows,
|
||||
] = await Promise.all([
|
||||
prisma.labResult.findMany({
|
||||
where: { userId, deletedAt: null },
|
||||
orderBy: { takenAt: "desc" },
|
||||
include: { biomarker: { select: { name: true } } },
|
||||
}),
|
||||
prisma.biomarker.findMany({
|
||||
where: { userId },
|
||||
orderBy: { name: "asc" },
|
||||
}),
|
||||
prisma.illnessEpisode.findMany({
|
||||
where: { userId, deletedAt: null },
|
||||
orderBy: { onsetAt: "desc" },
|
||||
include: {
|
||||
dayLogs: {
|
||||
where: { deletedAt: null },
|
||||
orderBy: { date: "asc" },
|
||||
include: dayLogSymptomInclude,
|
||||
},
|
||||
},
|
||||
}),
|
||||
prisma.allergy.findMany({
|
||||
where: { userId, deletedAt: null },
|
||||
orderBy: { createdAt: "desc" },
|
||||
}),
|
||||
prisma.familyHistoryEntry.findMany({
|
||||
where: { userId, deletedAt: null },
|
||||
orderBy: { createdAt: "desc" },
|
||||
}),
|
||||
prisma.workout.findMany({
|
||||
where: { userId },
|
||||
orderBy: { startedAt: "desc" },
|
||||
}),
|
||||
prisma.inboundDocument.findMany({
|
||||
where: { userId, deletedAt: null },
|
||||
orderBy: { createdAt: "desc" },
|
||||
select: {
|
||||
id: true,
|
||||
kind: true,
|
||||
title: true,
|
||||
filename: true,
|
||||
mimeType: true,
|
||||
byteSize: true,
|
||||
status: true,
|
||||
reportDate: true,
|
||||
documentDate: true,
|
||||
summaryEncrypted: true,
|
||||
createdAt: true,
|
||||
},
|
||||
}),
|
||||
]);
|
||||
|
||||
const labResults: LabResultBackupEntry[] = labResultRows.map((r) => ({
|
||||
panel: r.panel,
|
||||
analyte: r.analyte,
|
||||
value: r.value,
|
||||
valueText: r.valueText,
|
||||
unit: r.unit,
|
||||
referenceLow: r.referenceLow,
|
||||
referenceHigh: r.referenceHigh,
|
||||
takenAt: r.takenAt.toISOString(),
|
||||
source: r.source,
|
||||
biomarkerName: r.biomarker?.name ?? null,
|
||||
note: decryptNoteSoft(r.noteEncrypted),
|
||||
}));
|
||||
|
||||
const biomarkers: BiomarkerBackupEntry[] = biomarkerRows.map((b) => ({
|
||||
name: b.name,
|
||||
unit: b.unit,
|
||||
lowerBound: b.lowerBound,
|
||||
upperBound: b.upperBound,
|
||||
panel: b.panel,
|
||||
hidden: b.hidden,
|
||||
context: decryptContextSoft(b.contextEncrypted),
|
||||
}));
|
||||
|
||||
const illnessEpisodes = episodeRows.map((row) => ({
|
||||
...toIllnessEpisodeDTO(row),
|
||||
dayLogs: row.dayLogs.map(toIllnessDayLogDTO),
|
||||
}));
|
||||
|
||||
const allergies = allergyRows.map(toAllergyDTO);
|
||||
const familyHistory = familyRows.map(toFamilyHistoryEntryDTO);
|
||||
|
||||
const workouts: WorkoutBackupEntry[] = workoutRows.map((w) => ({
|
||||
sportType: w.sportType,
|
||||
startedAt: w.startedAt.toISOString(),
|
||||
endedAt: w.endedAt.toISOString(),
|
||||
durationSec: w.durationSec,
|
||||
totalEnergyKcal: w.totalEnergyKcal,
|
||||
totalDistanceM: w.totalDistanceM,
|
||||
avgHeartRate: w.avgHeartRate,
|
||||
maxHeartRate: w.maxHeartRate,
|
||||
minHeartRate: w.minHeartRate,
|
||||
stepCount: w.stepCount,
|
||||
elevationM: w.elevationM,
|
||||
pauseDurationSec: w.pauseDurationSec,
|
||||
source: w.source,
|
||||
externalId: w.externalId,
|
||||
}));
|
||||
|
||||
const documents: DocumentBackupEntry[] = documentRows.map((d) => ({
|
||||
id: d.id,
|
||||
kind: d.kind,
|
||||
title: d.title,
|
||||
filename: d.filename,
|
||||
mimeType: d.mimeType,
|
||||
byteSize: d.byteSize,
|
||||
status: d.status,
|
||||
reportDate: d.reportDate ? d.reportDate.toISOString().slice(0, 10) : null,
|
||||
documentDate: d.documentDate
|
||||
? d.documentDate.toISOString().slice(0, 10)
|
||||
: null,
|
||||
summary: decryptSummarySoft(d.summaryEncrypted),
|
||||
createdAt: d.createdAt.toISOString(),
|
||||
}));
|
||||
|
||||
return {
|
||||
labResults,
|
||||
biomarkers,
|
||||
illnessEpisodes,
|
||||
allergies,
|
||||
familyHistory,
|
||||
workouts,
|
||||
documents,
|
||||
manifest: {
|
||||
documents: { included: "metadata-only", note: DOCUMENTS_MANIFEST_NOTE },
|
||||
workouts: { included: "summary-only", note: WORKOUTS_MANIFEST_NOTE },
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
/** Row counts for the audit trail / annotate() meta, mirroring `FullBackupCounts`. */
|
||||
export function countRecordsBackupSection(
|
||||
section: RecordsBackupSection,
|
||||
): RecordsBackupCounts {
|
||||
return {
|
||||
labResults: section.labResults.length,
|
||||
biomarkers: section.biomarkers.length,
|
||||
illnessEpisodes: section.illnessEpisodes.length,
|
||||
illnessDayLogs: section.illnessEpisodes.reduce(
|
||||
(sum, e) => sum + e.dayLogs.length,
|
||||
0,
|
||||
),
|
||||
allergies: section.allergies.length,
|
||||
familyHistory: section.familyHistory.length,
|
||||
workouts: section.workouts.length,
|
||||
documents: section.documents.length,
|
||||
};
|
||||
}
|
||||
Reference in new issue
Block a user