mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-18 16:12:11 +00:00
fix(backup): let an account fresh from setup take an erased account's copy
The empty-account check counted every backed-up model, and signing up and answering the setup questions always leaves a setup row, plus a cycle profile when the cycle area is chosen. The account of someone who has signed up again, the case the restore target is for, was therefore always refused. Those two models are one row per account and are deleted and re-created from the copy by every restore, so they no longer count. A reading or medication entered on setup's last screen still refuses the account, because the restore would delete it. A guard holds the exempt models to that shape, and the erased-account integration file now drives the real setup routes before the restore.
This commit is contained in:
1 parent
21f6f5796b
commit
648810745a
4 files changed
+203
-5
No files matched your search
@@ -7,17 +7,26 @@
|
||||
*
|
||||
* Mutation check: drop a model from `BACKED_UP_CHILD_MODELS` and the first
|
||||
* case names it (no `userId`); add an owned model to it and the second does.
|
||||
* Add a model with many rows per account (`Measurement`) to the setup list
|
||||
* and the third does.
|
||||
*/
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
import { parseWipeSchema } from "@/__tests__/helpers/wipe-schema-shape";
|
||||
import { readFileSync } from "node:fs";
|
||||
|
||||
import {
|
||||
parseWipeSchema,
|
||||
WIPE_SCHEMA_PATH,
|
||||
} from "@/__tests__/helpers/wipe-schema-shape";
|
||||
import { BACKED_UP_MODELS } from "@/lib/export/backup-plan";
|
||||
import {
|
||||
BACKED_UP_CHILD_MODELS,
|
||||
RESTORE_TARGET_OWNED_MODELS,
|
||||
RESTORE_TARGET_SETUP_MODELS,
|
||||
} from "@/lib/export/restore-target";
|
||||
|
||||
const models = parseWipeSchema();
|
||||
const SCHEMA = readFileSync(WIPE_SCHEMA_PATH, "utf8");
|
||||
|
||||
describe("restore target models", () => {
|
||||
it("reads every owned backed-up model by its userId", () => {
|
||||
@@ -42,4 +51,30 @@ describe("restore target models", () => {
|
||||
expect(RESTORE_TARGET_OWNED_MODELS, parent).toContain(parent);
|
||||
}
|
||||
});
|
||||
|
||||
it("leaves out only one-row-per-account setup state that no other backed-up model hangs off", () => {
|
||||
const setup = Object.keys(RESTORE_TARGET_SETUP_MODELS);
|
||||
expect(setup.sort()).toEqual(["CycleProfile", "OnboardingRecord"]);
|
||||
for (const model of setup) {
|
||||
expect(RESTORE_TARGET_OWNED_MODELS, model).toContain(model);
|
||||
// One row per account: a list of things the person recorded is not
|
||||
// setup state.
|
||||
const body = SCHEMA.match(
|
||||
new RegExp(`^model ${model} \\{([\\s\\S]*?)^\\}`, "m"),
|
||||
)?.[1];
|
||||
expect(
|
||||
/^\s*userId\s+String\s+@unique/m.test(body ?? "") ||
|
||||
/@@unique\(\[userId\]\)/.test(body ?? ""),
|
||||
model,
|
||||
).toBe(true);
|
||||
// Nothing backed up hangs off it, so leaving it out of the check
|
||||
// leaves no record unchecked behind it.
|
||||
for (const other of BACKED_UP_MODELS) {
|
||||
expect(
|
||||
models.get(other)?.relations.some((r) => r.target === model),
|
||||
`${other} -> ${model}`,
|
||||
).not.toBe(true);
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
@@ -8,7 +8,11 @@
|
||||
* that account holds none of those rows, and the check is exactly that: a row
|
||||
* of any model the backup carries ({@link BACKED_UP_MODELS}) under the
|
||||
* account refuses it. Everything a restore leaves alone (credentials,
|
||||
* sessions, devices, grants) does not count.
|
||||
* sessions, devices, grants) does not count, and neither does the setup state
|
||||
* signing up leaves behind ({@link RESTORE_TARGET_SETUP_MODELS}): the person
|
||||
* did not enter it as a record, and the restore replaces it with the copy's.
|
||||
* A reading or a medication entered during setup is a record like any other
|
||||
* and still refuses, because the restore would delete it.
|
||||
*
|
||||
* The other two refusals keep the account rules intact. A managed profile is
|
||||
* its Guardians' record, which an admin does not write into (the users list
|
||||
@@ -47,6 +51,29 @@ export const BACKED_UP_CHILD_MODELS: Readonly<Record<string, string>> = {
|
||||
export const RESTORE_TARGET_OWNED_MODELS: readonly string[] =
|
||||
BACKED_UP_MODELS.filter((model) => !(model in BACKED_UP_CHILD_MODELS));
|
||||
|
||||
/**
|
||||
* The backed-up models an account holds a row of after signing up and going
|
||||
* through setup, before anything has been recorded, each with the writer.
|
||||
* One row per account, settings-shaped, and deleted and re-created from the
|
||||
* copy by every restore, so the copy's state replaces it and nothing the
|
||||
* person entered is lost. Without this list the account of someone who has
|
||||
* signed up again was always refused, which is the case the target exists
|
||||
* for. `restore-target-models.test.ts` holds each to that shape.
|
||||
*/
|
||||
export const RESTORE_TARGET_SETUP_MODELS: Readonly<Record<string, string>> = {
|
||||
// `PATCH /api/onboarding/answers` and `POST /api/onboarding/complete`.
|
||||
OnboardingRecord: "the setup answers",
|
||||
// Created by the setup confirm when the cycle area is chosen, and by the
|
||||
// first read of the cycle settings.
|
||||
CycleProfile: "the cycle settings",
|
||||
};
|
||||
|
||||
/** The models whose rows make an account a refused restore target. */
|
||||
const RESTORE_TARGET_CHECKED_MODELS: readonly string[] =
|
||||
RESTORE_TARGET_OWNED_MODELS.filter(
|
||||
(model) => !(model in RESTORE_TARGET_SETUP_MODELS),
|
||||
);
|
||||
|
||||
interface OwnedDelegate {
|
||||
findFirst(args: {
|
||||
where: { userId: string };
|
||||
@@ -65,13 +92,16 @@ function ownedDelegate(db: object, model: string): OwnedDelegate {
|
||||
return delegate;
|
||||
}
|
||||
|
||||
/** The backed-up models under which `userId` holds at least one row. */
|
||||
/**
|
||||
* The backed-up models under which `userId` holds at least one row, setup
|
||||
* state left out.
|
||||
*/
|
||||
export async function modelsHoldingRecords(
|
||||
db: Prisma.TransactionClient,
|
||||
userId: string,
|
||||
): Promise<string[]> {
|
||||
const held: string[] = [];
|
||||
for (const model of RESTORE_TARGET_OWNED_MODELS) {
|
||||
for (const model of RESTORE_TARGET_CHECKED_MODELS) {
|
||||
const row = await ownedDelegate(db, model).findFirst({
|
||||
where: { userId },
|
||||
select: { userId: true },
|
||||
|
||||
Reference in new issue
Block a user