mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-19 08:12:52 +00:00
feat(documents): add a document upload token for imports from other systems
A narrow `documents:write` token, minted cookie-only at
`POST /api/tokens/documents` and accepted by the vault upload route alone.
A scoped caller draws on its own hourly bucket per token
(`DOCUMENT_UPLOAD_LIMIT_PER_HOUR`, default 120) and gets a receipt
`{ id, duplicate }` instead of the stored row.
Uploads take optional `sourceSystem` / `sourceId` fields. The source key is
unique per account across live and deleted documents, and the tombstone
purge carries it to a small ledger, so re-sending a document the owner
deleted answers 200 `deleted: true` and stores nothing. `aiRead=defer`
indexes locally and skips the summary and lab staging.
The detail sheet shows where an imported document came from; backup and
restore carry both fields; Delete All Data clears the ledger.
Refs #1038, #938
This commit is contained in:
1 parent
af65f01fec
commit
55a32ff7d1
45 files changed
+2261
-96
No files matched your search
@@ -81,6 +81,10 @@ import {
|
||||
emergencyBloodTypeSchema,
|
||||
organDonorStatusSchema,
|
||||
} from "@/lib/validations/emergency-profile";
|
||||
import {
|
||||
DOCUMENT_SOURCE_ID_MAX,
|
||||
DOCUMENT_SOURCE_SYSTEMS,
|
||||
} from "@/lib/validations/inbound-documents";
|
||||
import { REMINDER_EVENT_SOURCES } from "@/lib/measurement-reminders/satisfy";
|
||||
import {
|
||||
UNITS_PER_DOSE_MESSAGE,
|
||||
@@ -1621,6 +1625,10 @@ const documentBackupSchema = z
|
||||
// Free string on purpose: a future outcome value must not fail a restore.
|
||||
lastIndexAttemptAt: isoDateTime.nullable().optional(),
|
||||
lastIndexOutcome: z.string().nullable().optional(),
|
||||
// v1.39.2 (#1038) — import provenance. Closed like the upload field, so a
|
||||
// restore cannot write a system the detail sheet has no name for.
|
||||
sourceSystem: z.enum(DOCUMENT_SOURCE_SYSTEMS).nullable().optional(),
|
||||
sourceId: z.string().max(DOCUMENT_SOURCE_ID_MAX).nullable().optional(),
|
||||
summary: z.string().nullable().optional(),
|
||||
createdAt: isoDateTime.optional(),
|
||||
updatedAt: isoDateTime.optional(),
|
||||
|
||||
@@ -441,6 +441,12 @@ export interface InboundDocumentDto {
|
||||
* shows its kind icon instead of fetching a thumbnail that 404s.
|
||||
*/
|
||||
hasThumbnail: boolean;
|
||||
/**
|
||||
* v1.39.2 (#1038) — the system an imported document came from, or null for
|
||||
* one added in HealthLog. Paired with `sourceId`, its id over there.
|
||||
*/
|
||||
sourceSystem: DocumentSourceSystemValue | null;
|
||||
sourceId: string | null;
|
||||
createdAt: string;
|
||||
updatedAt: string;
|
||||
}
|
||||
@@ -669,13 +675,59 @@ const vaccinationIdList = z
|
||||
* becoming a second trip. No `userId` field; it is always narrowed from the
|
||||
* session.
|
||||
*/
|
||||
export const documentCreateSchema = z.object({
|
||||
title: z.string().trim().min(1).max(DOCUMENT_TITLE_MAX).optional(),
|
||||
kind: z.enum(INBOUND_DOCUMENT_KINDS).optional(),
|
||||
documentDate: isoDateString.optional(),
|
||||
episodeIds: episodeIdList.optional(),
|
||||
encounterIds: encounterIdList.optional(),
|
||||
});
|
||||
/**
|
||||
* v1.39.2 (#1038) — the systems an imported document can say it came from.
|
||||
* Closed on purpose: the detail sheet names each one, and a free string here
|
||||
* would be a label the person never chose rendered as if HealthLog vouched for
|
||||
* it. OTHER covers any importer that is neither.
|
||||
*/
|
||||
export const DOCUMENT_SOURCE_SYSTEMS = ["PAPERLESS", "PAPRA", "OTHER"] as const;
|
||||
export type DocumentSourceSystemValue =
|
||||
(typeof DOCUMENT_SOURCE_SYSTEMS)[number];
|
||||
|
||||
/** Narrow a stored `sourceSystem` string to the closed set (null otherwise). */
|
||||
export function toDocumentSourceSystem(
|
||||
value: string | null | undefined,
|
||||
): DocumentSourceSystemValue | null {
|
||||
return (DOCUMENT_SOURCE_SYSTEMS as readonly string[]).includes(value ?? "")
|
||||
? (value as DocumentSourceSystemValue)
|
||||
: null;
|
||||
}
|
||||
|
||||
/** Max length of an imported document's id in its source system. */
|
||||
export const DOCUMENT_SOURCE_ID_MAX = 128;
|
||||
|
||||
export const documentCreateSchema = z
|
||||
.object({
|
||||
title: z.string().trim().min(1).max(DOCUMENT_TITLE_MAX).optional(),
|
||||
kind: z.enum(INBOUND_DOCUMENT_KINDS).optional(),
|
||||
documentDate: isoDateString.optional(),
|
||||
episodeIds: episodeIdList.optional(),
|
||||
encounterIds: encounterIdList.optional(),
|
||||
/** Where an imported document came from (provenance, shown on detail). */
|
||||
sourceSystem: z.enum(DOCUMENT_SOURCE_SYSTEMS).optional(),
|
||||
/**
|
||||
* The document's id in `sourceSystem`. Printable ASCII only: it is a key,
|
||||
* not prose, and it is echoed on the detail sheet.
|
||||
*/
|
||||
sourceId: z
|
||||
.string()
|
||||
.trim()
|
||||
.min(1)
|
||||
.max(DOCUMENT_SOURCE_ID_MAX)
|
||||
.regex(/^[\x21-\x7e]+$/u, "Expected printable characters without spaces")
|
||||
.optional(),
|
||||
/**
|
||||
* `defer` holds back automatic AI reading for this upload: the thumbnail
|
||||
* and a local text index still run, the summary and the lab staging do
|
||||
* not. Absent means today's behaviour.
|
||||
*/
|
||||
aiRead: z.enum(["defer"]).optional(),
|
||||
})
|
||||
.refine((v) => v.sourceId === undefined || v.sourceSystem !== undefined, {
|
||||
path: ["sourceSystem"],
|
||||
message: "sourceId needs a sourceSystem",
|
||||
});
|
||||
|
||||
export type DocumentCreateInput = z.infer<typeof documentCreateSchema>;
|
||||
|
||||
|
||||
@@ -24,3 +24,13 @@ export const createMeasurementTokenSchema = z.object({
|
||||
export type CreateMeasurementTokenInput = z.infer<
|
||||
typeof createMeasurementTokenSchema
|
||||
>;
|
||||
|
||||
/**
|
||||
* Body of `POST /api/tokens/documents` — mint a Bearer that can upload
|
||||
* documents and nothing else. The same shape as the measurement mint and for
|
||||
* the same reasons: no scope field, a bounded lifetime.
|
||||
*/
|
||||
export const createDocumentTokenSchema = z.object({
|
||||
name: z.string().min(1, "Name required").max(100),
|
||||
expiresInDays: z.number().int().min(1).max(365).optional(),
|
||||
});
|
||||
Reference in new issue
Block a user