mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-18 08:02:12 +00:00
feat(medications): read a dose-history export directly
The intake importer took a hand-written list of dates and times for one medication. A self-hoster with a ten-column export covering sixteen medications and years of history was left translating it by hand, which is where the scheduled time and the time a dose was actually taken get flattened into one — the distinction the record wants most. The export keeps them as two columns, so read the file's own shape. `MedicationIntakeEvent` already models that pair as `scheduledFor` + `takenAt`; an imported dose now populates both instead of anchoring itself on the take. `Skipped` becomes a deliberate skip with no take time, which is how every other skip path writes it. A dose whose actual amount differs from the amount the schedule expected carries the difference as a per-dose override; an unremarkable amount stays absent, which is what absent means there. Timestamps carry their own UTC offset (`+1030` and `+0930` on adjacent rows of one file) and it is read literally: the epoch is computed from the stated offset rather than handed to the platform's lenient date parser, and a value without an offset is refused instead of assumed to be the server's zone. An export covers a whole regimen, so the job is account-wide and each payload entry names its own medication — migration 0276 widens `medication_intake_import_jobs.medication_id` to nullable for it. The per-medication route is unchanged and keeps naming its medication on the job row. A medication is never created from an import file; unmatched names come back verbatim so they can be added properly and the file re-run. Every column gets a verdict, and `?dryRun=1` returns the whole one before anything is written.
This commit is contained in:
1 parent
f0c5b5ae94
commit
11e6402b5a
13 files changed
+1748
-192
No files matched your search
@@ -24,8 +24,28 @@ export const MEDICATION_INTAKE_IMPORT_SEND_OPTIONS = {
|
||||
} as const;
|
||||
|
||||
export interface MedicationImportEntry {
|
||||
takenAt: string;
|
||||
/**
|
||||
* The slot instant this dose belongs to, and the fact it is identified by.
|
||||
* Equals `takenAt` for an ad-hoc dose that belongs to no slot.
|
||||
*/
|
||||
scheduledFor: string;
|
||||
/** When the dose was actually taken. `null` for a deliberate skip. */
|
||||
takenAt: string | null;
|
||||
idempotencyKey: string;
|
||||
/**
|
||||
* The medication this dose attaches to. Carried per entry because a dose
|
||||
* history exported from another app covers a whole regimen in one file, so the
|
||||
* job cannot be scoped to a single medication the way the per-medication
|
||||
* import route's job is. Absent on that route's jobs, where the job row names
|
||||
* the medication instead.
|
||||
*/
|
||||
medicationId?: string;
|
||||
/**
|
||||
* Per-dose override, free text, mirroring `Medication.dose`. Absent when the
|
||||
* configured schedule dose applies — which is the overwhelming majority, and
|
||||
* absence is how `MedicationIntakeEvent.doseTaken` already says so.
|
||||
*/
|
||||
doseTaken?: string;
|
||||
}
|
||||
|
||||
export interface MedicationImportPayload {
|
||||
@@ -33,22 +53,40 @@ export interface MedicationImportPayload {
|
||||
}
|
||||
|
||||
/**
|
||||
* Why a submitted entry did not become a row. Machine-readable; the client
|
||||
* turns each into a sentence.
|
||||
* Why a submitted row did not become a row on the ledger. Machine-readable; the
|
||||
* client turns each into a sentence.
|
||||
*
|
||||
* `duplicate_in_file` — two entries in the same submission resolve to the same
|
||||
* instant, so only one can be written.
|
||||
* `already_recorded` — the dose is already on the ledger for that instant, from
|
||||
* an earlier import or from any other intake surface.
|
||||
* `duplicate_in_file` — two rows of the same submission resolve to the same
|
||||
* medication and slot, so only one can be written.
|
||||
* `already_recorded` — the dose is already on the ledger for that slot, from an
|
||||
* earlier import or from any other intake surface.
|
||||
*
|
||||
* They used to be one `skippedDuplicates` number, which is how a file whose
|
||||
* every row collapsed onto one key could report "27 duplicates skipped" about
|
||||
* 27 doses that had no duplicate anywhere. Naming the reason is what makes the
|
||||
* count checkable against what the person actually submitted.
|
||||
* Those two used to be one `skippedDuplicates` number, which is how a file whose
|
||||
* every row collapsed onto one key could report "27 duplicates skipped" about 27
|
||||
* doses that had no duplicate anywhere. Naming the reason is what makes the count
|
||||
* checkable against what the person actually submitted.
|
||||
*
|
||||
* The rest are decided before the queue, when a whole export file is parsed and
|
||||
* matched against the record. They are seeded into the job's progress so the
|
||||
* final count covers the entire file rather than only the part that reached the
|
||||
* database — a row refused at the door is still a row the person needs told
|
||||
* about, by name.
|
||||
*/
|
||||
export const MEDICATION_IMPORT_SKIP_REASONS = [
|
||||
"duplicate_in_file",
|
||||
"already_recorded",
|
||||
"medication_not_found",
|
||||
"medication_ambiguous",
|
||||
"medication_is_mirrored",
|
||||
"status_not_recorded",
|
||||
"status_unknown",
|
||||
"missing_timestamp",
|
||||
"missing_timezone_offset",
|
||||
"unreadable_timestamp",
|
||||
"implausible_timestamp",
|
||||
"missing_medication",
|
||||
"unreadable_dosage",
|
||||
"unreadable_row",
|
||||
] as const;
|
||||
|
||||
export type MedicationImportSkipReason =
|
||||
@@ -197,16 +235,49 @@ function parsePayload(value: Prisma.JsonValue): MedicationImportPayload {
|
||||
typeof entry !== "object" ||
|
||||
entry === null ||
|
||||
Array.isArray(entry) ||
|
||||
typeof entry.takenAt !== "string" ||
|
||||
!Number.isFinite(Date.parse(entry.takenAt)) ||
|
||||
typeof entry.idempotencyKey !== "string" ||
|
||||
entry.idempotencyKey.length === 0
|
||||
) {
|
||||
throw new Error("Medication intake import entry is malformed");
|
||||
}
|
||||
const takenAt = entry.takenAt;
|
||||
if (
|
||||
takenAt !== null &&
|
||||
(typeof takenAt !== "string" || !Number.isFinite(Date.parse(takenAt)))
|
||||
) {
|
||||
throw new Error("Medication intake import entry is malformed");
|
||||
}
|
||||
// `scheduledFor` was added when the importer learnt to read an export that
|
||||
// states the scheduled time and the time taken as two separate facts. A job
|
||||
// row enqueued before that carries only `takenAt`, and such a row is an
|
||||
// ad-hoc dose anchored on itself — which is what the absence means. This is
|
||||
// for rows already in the database, not for a caller.
|
||||
const rawScheduledFor =
|
||||
entry.scheduledFor === undefined ? takenAt : entry.scheduledFor;
|
||||
if (
|
||||
typeof rawScheduledFor !== "string" ||
|
||||
!Number.isFinite(Date.parse(rawScheduledFor))
|
||||
) {
|
||||
throw new Error("Medication intake import entry is malformed");
|
||||
}
|
||||
if (
|
||||
entry.medicationId !== undefined &&
|
||||
(typeof entry.medicationId !== "string" ||
|
||||
entry.medicationId.length === 0)
|
||||
) {
|
||||
throw new Error("Medication intake import entry is malformed");
|
||||
}
|
||||
if (entry.doseTaken !== undefined && typeof entry.doseTaken !== "string") {
|
||||
throw new Error("Medication intake import entry is malformed");
|
||||
}
|
||||
return {
|
||||
takenAt: entry.takenAt,
|
||||
scheduledFor: rawScheduledFor,
|
||||
takenAt: takenAt as string | null,
|
||||
idempotencyKey: entry.idempotencyKey,
|
||||
...(entry.medicationId === undefined
|
||||
? {}
|
||||
: { medicationId: entry.medicationId }),
|
||||
...(entry.doseTaken === undefined ? {} : { doseTaken: entry.doseTaken }),
|
||||
};
|
||||
});
|
||||
return { entries };
|
||||
@@ -287,23 +358,64 @@ interface ProcessChunkOutcome {
|
||||
userId: string | null;
|
||||
}
|
||||
|
||||
/**
|
||||
* A day whose compliance rollup an import invalidated, as one string.
|
||||
*
|
||||
* A job can now span a whole regimen, so a bare day key no longer identifies
|
||||
* what to recompute — two medications touched on the same day are two rollup
|
||||
* rows. The medication is folded in ahead of a single space, which occurs in
|
||||
* neither a cuid nor a `YYYY-MM-DD` key. Not a NUL byte: this value is persisted
|
||||
* in the progress blob and Postgres rejects ` | ||||