mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-17 15:42:24 +00:00
fix(backups): preserve canonical restore identity
This commit is contained in:
1 parent
8b7cbddd72
commit
cd6b5f0cc6
10 files changed
+1722
-215
No files matched your search
@@ -0,0 +1,155 @@
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
|
||||
import { buildCycleBackupSection } from "../backup";
|
||||
|
||||
const deletedAt = new Date("2026-07-19T12:00:00.000Z");
|
||||
const createdAt = new Date("2026-07-01T08:00:00.000Z");
|
||||
const updatedAt = new Date("2026-07-19T11:00:00.000Z");
|
||||
|
||||
describe("buildCycleBackupSection disaster-recovery mode", () => {
|
||||
it("preserves stable ids, reconciliation fields, and tombstones", async () => {
|
||||
const prisma = {
|
||||
cycleProfile: {
|
||||
findUnique: vi.fn().mockResolvedValue({
|
||||
id: "profile-dr",
|
||||
goal: "GENERAL_HEALTH",
|
||||
cycleTrackingEnabled: true,
|
||||
typicalCycleLength: 28,
|
||||
typicalPeriodLength: 5,
|
||||
lutealPhaseLength: 14,
|
||||
secondarySymptom: "MUCUS",
|
||||
predictionEnabled: true,
|
||||
rawChartMode: false,
|
||||
discreetNotifications: true,
|
||||
sensitiveCategoryEncryption: true,
|
||||
createdAt,
|
||||
updatedAt,
|
||||
}),
|
||||
},
|
||||
menstrualCycle: {
|
||||
findMany: vi.fn().mockResolvedValue([
|
||||
{
|
||||
id: "cycle-dr",
|
||||
startDate: "2026-07-01",
|
||||
endDate: "2026-07-28",
|
||||
periodEndDate: "2026-07-05",
|
||||
lengthDays: 28,
|
||||
ovulationDate: "2026-07-14",
|
||||
ovulationConfirmed: true,
|
||||
isPredicted: false,
|
||||
tz: "Europe/London",
|
||||
syncVersion: 5,
|
||||
deletedAt,
|
||||
createdAt,
|
||||
updatedAt,
|
||||
},
|
||||
]),
|
||||
},
|
||||
cycleDayLog: {
|
||||
findMany: vi.fn().mockResolvedValue([
|
||||
{
|
||||
id: "cycle-day-dr",
|
||||
date: "2026-07-02",
|
||||
cycleId: "cycle-dr",
|
||||
flow: "HEAVY",
|
||||
intermenstrualBleeding: false,
|
||||
basalBodyTempC: 36.7,
|
||||
temperatureExcluded: true,
|
||||
ovulationTest: "NEGATIVE",
|
||||
cervicalMucus: "CREAMY",
|
||||
cervixPosition: "LOW",
|
||||
cervixFirmness: "FIRM",
|
||||
cervixOpening: "CLOSED",
|
||||
sexualActivity: false,
|
||||
protectedSex: null,
|
||||
pregnancyTest: null,
|
||||
progesteroneTest: null,
|
||||
contraceptive: null,
|
||||
sensitiveEncrypted: "sensitive-ciphertext",
|
||||
notesEncrypted: "notes-ciphertext",
|
||||
source: "APPLE_HEALTH",
|
||||
externalId: "cycle-day-external",
|
||||
tz: "Europe/London",
|
||||
syncVersion: 9,
|
||||
deletedAt,
|
||||
createdAt,
|
||||
updatedAt,
|
||||
symptomLinks: [{ symptom: { key: "cramps" } }],
|
||||
},
|
||||
]),
|
||||
},
|
||||
};
|
||||
|
||||
const section = await buildCycleBackupSection(
|
||||
prisma as never,
|
||||
"user-1",
|
||||
{ purpose: "disaster-recovery" },
|
||||
);
|
||||
|
||||
expect(prisma.menstrualCycle.findMany).toHaveBeenCalledWith(
|
||||
expect.objectContaining({ where: { userId: "user-1" } }),
|
||||
);
|
||||
expect(prisma.cycleDayLog.findMany).toHaveBeenCalledWith(
|
||||
expect.objectContaining({ where: { userId: "user-1" } }),
|
||||
);
|
||||
expect(section.cycleProfile).toEqual({
|
||||
id: "profile-dr",
|
||||
goal: "GENERAL_HEALTH",
|
||||
cycleTrackingEnabled: true,
|
||||
typicalCycleLength: 28,
|
||||
typicalPeriodLength: 5,
|
||||
lutealPhaseLength: 14,
|
||||
secondarySymptom: "MUCUS",
|
||||
predictionEnabled: true,
|
||||
rawChartMode: false,
|
||||
discreetNotifications: true,
|
||||
sensitiveCategoryEncryption: true,
|
||||
createdAt: createdAt.toISOString(),
|
||||
updatedAt: updatedAt.toISOString(),
|
||||
});
|
||||
expect(section.cycles[0]).toEqual({
|
||||
id: "cycle-dr",
|
||||
startDate: "2026-07-01",
|
||||
endDate: "2026-07-28",
|
||||
periodEndDate: "2026-07-05",
|
||||
lengthDays: 28,
|
||||
ovulationDate: "2026-07-14",
|
||||
ovulationConfirmed: true,
|
||||
isPredicted: false,
|
||||
tz: "Europe/London",
|
||||
syncVersion: 5,
|
||||
deletedAt: deletedAt.toISOString(),
|
||||
createdAt: createdAt.toISOString(),
|
||||
updatedAt: updatedAt.toISOString(),
|
||||
});
|
||||
expect(section.cycleDayLogs[0]).toEqual({
|
||||
id: "cycle-day-dr",
|
||||
date: "2026-07-02",
|
||||
cycleId: "cycle-dr",
|
||||
flow: "HEAVY",
|
||||
intermenstrualBleeding: false,
|
||||
basalBodyTempC: 36.7,
|
||||
temperatureExcluded: true,
|
||||
ovulationTest: "NEGATIVE",
|
||||
cervicalMucus: "CREAMY",
|
||||
cervixPosition: "LOW",
|
||||
cervixFirmness: "FIRM",
|
||||
cervixOpening: "CLOSED",
|
||||
sexualActivity: false,
|
||||
protectedSex: null,
|
||||
pregnancyTest: null,
|
||||
progesteroneTest: null,
|
||||
contraceptive: null,
|
||||
sensitiveEncrypted: "sensitive-ciphertext",
|
||||
notesEncrypted: "notes-ciphertext",
|
||||
source: "APPLE_HEALTH",
|
||||
externalId: "cycle-day-external",
|
||||
tz: "Europe/London",
|
||||
syncVersion: 9,
|
||||
deletedAt: deletedAt.toISOString(),
|
||||
createdAt: createdAt.toISOString(),
|
||||
updatedAt: updatedAt.toISOString(),
|
||||
symptomKeys: ["cramps"],
|
||||
});
|
||||
});
|
||||
});
|
||||
+79
-5
@@ -28,8 +28,13 @@ import type {
|
||||
import type { BackupPayload } from "@/lib/validations/backup";
|
||||
|
||||
/** The cycle slice of a backup payload. */
|
||||
export interface CycleBackupOptions {
|
||||
purpose?: "portable-export" | "disaster-recovery";
|
||||
}
|
||||
|
||||
export interface CycleBackupSection {
|
||||
cycleProfile: {
|
||||
id?: string;
|
||||
goal: string;
|
||||
cycleTrackingEnabled: boolean | null;
|
||||
typicalCycleLength: number | null;
|
||||
@@ -40,8 +45,11 @@ export interface CycleBackupSection {
|
||||
rawChartMode: boolean;
|
||||
discreetNotifications: boolean;
|
||||
sensitiveCategoryEncryption: boolean;
|
||||
createdAt?: string;
|
||||
updatedAt?: string;
|
||||
} | null;
|
||||
cycles: Array<{
|
||||
id?: string;
|
||||
startDate: string;
|
||||
endDate: string | null;
|
||||
periodEndDate: string | null;
|
||||
@@ -49,8 +57,15 @@ export interface CycleBackupSection {
|
||||
ovulationDate: string | null;
|
||||
ovulationConfirmed: boolean;
|
||||
tz: string | null;
|
||||
isPredicted?: boolean;
|
||||
syncVersion?: number;
|
||||
deletedAt?: string | null;
|
||||
createdAt?: string;
|
||||
updatedAt?: string;
|
||||
}>;
|
||||
cycleDayLogs: Array<{
|
||||
id?: string;
|
||||
cycleId?: string | null;
|
||||
date: string;
|
||||
flow: string | null;
|
||||
intermenstrualBleeding: boolean;
|
||||
@@ -71,6 +86,10 @@ export interface CycleBackupSection {
|
||||
source: string;
|
||||
externalId: string | null;
|
||||
tz: string | null;
|
||||
syncVersion?: number;
|
||||
deletedAt?: string | null;
|
||||
createdAt?: string;
|
||||
updatedAt?: string;
|
||||
symptomKeys: string[];
|
||||
}>;
|
||||
}
|
||||
@@ -83,15 +102,19 @@ export interface CycleBackupSection {
|
||||
export async function buildCycleBackupSection(
|
||||
prisma: Pick<PrismaClient, "cycleProfile" | "menstrualCycle" | "cycleDayLog">,
|
||||
userId: string,
|
||||
options: CycleBackupOptions = {},
|
||||
): Promise<CycleBackupSection> {
|
||||
const disasterRecovery = options.purpose === "disaster-recovery";
|
||||
const [profile, cycles, dayLogs] = await Promise.all([
|
||||
prisma.cycleProfile.findUnique({ where: { userId } }),
|
||||
prisma.menstrualCycle.findMany({
|
||||
where: { userId, deletedAt: null, isPredicted: false },
|
||||
where: disasterRecovery
|
||||
? { userId }
|
||||
: { userId, deletedAt: null, isPredicted: false },
|
||||
orderBy: { startDate: "asc" },
|
||||
}),
|
||||
prisma.cycleDayLog.findMany({
|
||||
where: { userId, deletedAt: null },
|
||||
where: disasterRecovery ? { userId } : { userId, deletedAt: null },
|
||||
orderBy: { date: "asc" },
|
||||
include: {
|
||||
symptomLinks: { include: { symptom: { select: { key: true } } } },
|
||||
@@ -102,6 +125,13 @@ export async function buildCycleBackupSection(
|
||||
return {
|
||||
cycleProfile: profile
|
||||
? {
|
||||
...(disasterRecovery
|
||||
? {
|
||||
id: profile.id,
|
||||
createdAt: profile.createdAt.toISOString(),
|
||||
updatedAt: profile.updatedAt.toISOString(),
|
||||
}
|
||||
: {}),
|
||||
goal: profile.goal,
|
||||
cycleTrackingEnabled: profile.cycleTrackingEnabled,
|
||||
typicalCycleLength: profile.typicalCycleLength,
|
||||
@@ -115,6 +145,16 @@ export async function buildCycleBackupSection(
|
||||
}
|
||||
: null,
|
||||
cycles: cycles.map((c) => ({
|
||||
...(disasterRecovery
|
||||
? {
|
||||
id: c.id,
|
||||
isPredicted: c.isPredicted,
|
||||
syncVersion: c.syncVersion,
|
||||
deletedAt: c.deletedAt?.toISOString() ?? null,
|
||||
createdAt: c.createdAt.toISOString(),
|
||||
updatedAt: c.updatedAt.toISOString(),
|
||||
}
|
||||
: {}),
|
||||
startDate: c.startDate,
|
||||
endDate: c.endDate,
|
||||
periodEndDate: c.periodEndDate,
|
||||
@@ -124,6 +164,16 @@ export async function buildCycleBackupSection(
|
||||
tz: c.tz,
|
||||
})),
|
||||
cycleDayLogs: dayLogs.map((d) => ({
|
||||
...(disasterRecovery
|
||||
? {
|
||||
id: d.id,
|
||||
cycleId: d.cycleId,
|
||||
syncVersion: d.syncVersion,
|
||||
deletedAt: d.deletedAt?.toISOString() ?? null,
|
||||
createdAt: d.createdAt.toISOString(),
|
||||
updatedAt: d.updatedAt.toISOString(),
|
||||
}
|
||||
: {}),
|
||||
date: d.date,
|
||||
flow: d.flow,
|
||||
intermenstrualBleeding: d.intermenstrualBleeding,
|
||||
@@ -139,7 +189,6 @@ export async function buildCycleBackupSection(
|
||||
pregnancyTest: d.pregnancyTest,
|
||||
progesteroneTest: d.progesteroneTest,
|
||||
contraceptive: d.contraceptive,
|
||||
// Both ciphertext envelopes carried verbatim — never decrypted here.
|
||||
sensitiveEncrypted: d.sensitiveEncrypted,
|
||||
notesEncrypted: d.notesEncrypted,
|
||||
source: d.source,
|
||||
@@ -258,6 +307,7 @@ export async function restoreCycleData(
|
||||
const p = payload.cycleProfile;
|
||||
await tx.cycleProfile.create({
|
||||
data: {
|
||||
...(p.id ? { id: p.id } : {}),
|
||||
userId: ownerId,
|
||||
goal: enumOrNull(p.goal, CYCLE_GOALS) ?? "GENERAL_HEALTH",
|
||||
cycleTrackingEnabled: p.cycleTrackingEnabled ?? null,
|
||||
@@ -270,6 +320,8 @@ export async function restoreCycleData(
|
||||
rawChartMode: p.rawChartMode ?? false,
|
||||
discreetNotifications: p.discreetNotifications ?? false,
|
||||
sensitiveCategoryEncryption: p.sensitiveCategoryEncryption ?? true,
|
||||
...(p.createdAt ? { createdAt: new Date(p.createdAt) } : {}),
|
||||
...(p.updatedAt ? { updatedAt: new Date(p.updatedAt) } : {}),
|
||||
},
|
||||
});
|
||||
}
|
||||
@@ -277,9 +329,11 @@ export async function restoreCycleData(
|
||||
// Recreate observed cycle spans; map startDate → id so day-logs can
|
||||
// re-attach to their owning span.
|
||||
const cycleIdByStart = new Map<string, string>();
|
||||
const restoredCycleIds = new Set<string>();
|
||||
for (const c of payload.cycles) {
|
||||
const created = await tx.menstrualCycle.create({
|
||||
data: {
|
||||
...(c.id ? { id: c.id } : {}),
|
||||
userId: ownerId,
|
||||
startDate: c.startDate,
|
||||
endDate: c.endDate ?? null,
|
||||
@@ -288,10 +342,15 @@ export async function restoreCycleData(
|
||||
ovulationDate: c.ovulationDate ?? null,
|
||||
ovulationConfirmed: c.ovulationConfirmed ?? false,
|
||||
tz: c.tz ?? null,
|
||||
isPredicted: false,
|
||||
isPredicted: c.isPredicted ?? false,
|
||||
syncVersion: c.syncVersion ?? 0,
|
||||
deletedAt: c.deletedAt ? new Date(c.deletedAt) : null,
|
||||
...(c.createdAt ? { createdAt: new Date(c.createdAt) } : {}),
|
||||
...(c.updatedAt ? { updatedAt: new Date(c.updatedAt) } : {}),
|
||||
},
|
||||
});
|
||||
cycleIdByStart.set(c.startDate, created.id);
|
||||
restoredCycleIds.add(created.id);
|
||||
}
|
||||
|
||||
// Resolve the seeded symptom catalogue once for the link re-creation.
|
||||
@@ -328,11 +387,22 @@ export async function restoreCycleData(
|
||||
const symptomIds = (d.symptomKeys ?? [])
|
||||
.map((k) => symptomIdByKey.get(k))
|
||||
.filter((v): v is string => v !== undefined);
|
||||
const cycleId =
|
||||
d.cycleId !== undefined
|
||||
? d.cycleId === null
|
||||
? null
|
||||
: restoredCycleIds.has(d.cycleId)
|
||||
? d.cycleId
|
||||
: (() => {
|
||||
throw new Error(`Unknown cycle reference: ${d.cycleId}`);
|
||||
})()
|
||||
: owningCycleId(d.date);
|
||||
await tx.cycleDayLog.create({
|
||||
data: {
|
||||
...(d.id ? { id: d.id } : {}),
|
||||
userId: ownerId,
|
||||
date: d.date,
|
||||
cycleId: owningCycleId(d.date),
|
||||
cycleId,
|
||||
flow: enumOrNull(d.flow, FLOW_LEVELS),
|
||||
intermenstrualBleeding: d.intermenstrualBleeding ?? false,
|
||||
basalBodyTempC: d.basalBodyTempC ?? null,
|
||||
@@ -353,6 +423,10 @@ export async function restoreCycleData(
|
||||
source: enumOrNull(d.source, CYCLE_SOURCES) ?? "MANUAL",
|
||||
externalId: d.externalId ?? null,
|
||||
tz: d.tz ?? null,
|
||||
syncVersion: d.syncVersion ?? 0,
|
||||
deletedAt: d.deletedAt ? new Date(d.deletedAt) : null,
|
||||
...(d.createdAt ? { createdAt: new Date(d.createdAt) } : {}),
|
||||
...(d.updatedAt ? { updatedAt: new Date(d.updatedAt) } : {}),
|
||||
...(symptomIds.length > 0
|
||||
? {
|
||||
symptomLinks: {
|
||||
|
||||
Reference in new issue
Block a user