mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-18 16:12:11 +00:00
fix(import): take the app's own full backup and JSON export back
The JSON import refused the app's own files: the full backup writes an empty note, tag list or glucose context as null and the JSON export as an empty string, and the import schema read either as a value of the wrong type. The file is now read in the exporter's terms before it is validated: the export's envelope is opened, every null or empty field is treated as absent, a backup row carrying a deletion is left out and counted as skipped, and a unit the app converts on every other way in is converted to the stored one. The round trip also showed the portable backup dropping what a reading itself says: glucose context, sleep stage, range bounds, rhythm and device. They ride the portable file now, and a portable restore puts them back. Sync bookkeeping stays out. An integration test exports an account three ways (full backup, JSON export, measurement CSV), imports each into a fresh account and compares what the import carries.
This commit is contained in:
1 parent
8a11b7bfc6
commit
3d26cee566
7 files changed
+510
-12
No files matched your search
@@ -1,7 +1,8 @@
|
||||
/**
|
||||
* `POST /api/import` — import user data (JSON format matching the export
|
||||
* structure). The route authenticates, meters and parses the file; the
|
||||
* writers live in `src/lib/import/json/`.
|
||||
* writers live in `src/lib/import/json/`. The app's own full backup and JSON
|
||||
* export are read in their own terms first (`normalizeJsonImportFile`).
|
||||
*/
|
||||
import { apiHandler, requireAuth } from "@/lib/api-handler";
|
||||
import { annotate } from "@/lib/logging/context";
|
||||
@@ -28,6 +29,7 @@ import {
|
||||
} from "@/lib/validations/entry-instant";
|
||||
import { isoDateTime } from "@/lib/validations/datetime";
|
||||
import { fallbackMoodDateStart } from "@/lib/import/json/mood";
|
||||
import { normalizeJsonImportFile } from "@/lib/import/json/normalize";
|
||||
import { runJsonImport } from "@/lib/import/json/run";
|
||||
|
||||
// Derived from canonical enum so round-trip export → import covers every
|
||||
@@ -120,7 +122,8 @@ export const POST = apiHandler(async (request: NextRequest) => {
|
||||
});
|
||||
|
||||
if (jsonError) return jsonError;
|
||||
const parsed = importSchema.safeParse(body);
|
||||
const normalized = normalizeJsonImportFile(body);
|
||||
const parsed = importSchema.safeParse(normalized.file);
|
||||
if (!parsed.success) {
|
||||
return apiValidationError(
|
||||
parsed.error.issues[0]?.message ?? "Invalid import payload",
|
||||
@@ -129,12 +132,13 @@ export const POST = apiHandler(async (request: NextRequest) => {
|
||||
);
|
||||
}
|
||||
|
||||
const { stats, writeFailed } = await runJsonImport({
|
||||
const { stats: written, writeFailed } = await runJsonImport({
|
||||
userId: user.id,
|
||||
measurements: parsed.data.measurements,
|
||||
moodEntries: parsed.data.moodEntries,
|
||||
ipAddress: getClientIp(request),
|
||||
});
|
||||
const stats = { ...written, skipped: written.skipped + normalized.skipped };
|
||||
|
||||
if (writeFailed) {
|
||||
return apiError("Import write failed", 503, {
|
||||
|
||||
Reference in new issue
Block a user