Files
HealthLog/src/lib/validations/document-sources.ts
T
Marc-André Bombeck 1eac05b454 fix(documents): key imports by source instance, keep a token on its origin, and harden the import path
- Source keys carry the normalised origin of the Paperless-ngx or Papra
  they came from (source_instance on the document, the alias and the
  purge ledger; unique NULLS NOT DISTINCT). A key stored without one
  (v1.39.2 imports) matches any instance of its system, and a lookup
  without one matches any stored instance. The upload route, the lookup
  route and the import script accept or send it; backups carry it.
- Saving a connection reuses the stored token only for the same origin;
  a new origin needs the token again (422 tokenRequired).
- Document ids are checked against what each system issues (Paperless
  digits, Papra [A-Za-z0-9_-], never . or ..) before any URL is built;
  the Papra organization id likewise.
- Paperless API 9 filters dates on created__gte / created__lte, in the
  picker and the script. A search page the local re-check empties reads
  on, up to five source pages. Tags are read past the first 250.
- The import's key pre-check uses the lookup allowance, a failed fetch
  no longer charges the upload allowance, and one import per key runs at
  a time under an advisory lock, so a double click downloads once.
- Reading and deleting one's own connection works with the documents
  module off and the operator list unset.
2026-09-26 14:20:17 +02:00

91 lines
2.7 KiB
TypeScript

/**
* Request schemas of the document picker (#1038): saving a connection,
* searching a source, importing one picked document. No `userId` field in any
* of them; the owner is always the authenticated session.
*/
import { z } from "zod/v4";
import { DOCUMENT_SOURCE_BASE_URL_MAX } from "@/lib/documents/sources/origins";
import {
DOCUMENT_PICKER_LINK_KINDS,
DOCUMENT_PICKER_MAX_PAGE,
DOCUMENT_PICKER_QUERY_MAX,
PAPRA_ID,
} from "@/lib/documents/sources/types";
import {
documentSourceIdSchema,
INBOUND_DOCUMENT_KINDS,
} from "@/lib/validations/inbound-documents";
/** Printable ASCII without spaces: an id or a key, never prose. */
const printable = /^[\x21-\x7e]+$/u;
const isoDate = z
.string()
.regex(/^\d{4}-\d{2}-\d{2}$/u, "Expected a YYYY-MM-DD date");
/** Longest API token or key a connection stores. */
export const DOCUMENT_SOURCE_TOKEN_MAX = 512;
/**
* Save a connection. `token` may be left out when a connection for the system
* already exists: the stored one is kept, so the address or the organization
* can change without typing the key again. The first save must carry it (the
* route says so). `organizationId` is Papra's and required there.
*/
export const documentSourceSaveSchema = z.object({
baseUrl: z.string().trim().min(1).max(DOCUMENT_SOURCE_BASE_URL_MAX),
organizationId: z
.string()
.trim()
.regex(PAPRA_ID, "Expected letters, digits, _ or -")
.optional(),
token: z
.string()
.trim()
.min(1)
.max(DOCUMENT_SOURCE_TOKEN_MAX)
.regex(printable, "Expected printable characters without spaces")
.optional(),
});
export type DocumentSourceSaveInput = z.infer<typeof documentSourceSaveSchema>;
export const documentSourceSearchSchema = z
.object({
q: z.string().trim().max(DOCUMENT_PICKER_QUERY_MAX).default(""),
tag: z
.string()
.trim()
.min(1)
.max(64)
.regex(printable, "Expected printable characters without spaces")
.optional(),
from: isoDate.optional(),
to: isoDate.optional(),
page: z.coerce
.number()
.int()
.min(1)
.max(DOCUMENT_PICKER_MAX_PAGE)
.default(1),
})
.refine((v) => !v.from || !v.to || v.from <= v.to, {
path: ["to"],
message: "The end date is before the start date",
});
export const documentSourceImportSchema = z.object({
sourceId: documentSourceIdSchema,
kind: z.enum(INBOUND_DOCUMENT_KINDS).optional(),
/** The record the picker was opened from, to link the document to. */
link: z
.object({
kind: z.enum(DOCUMENT_PICKER_LINK_KINDS),
id: z.string().trim().min(1).max(64),
})
.optional(),
});
export type DocumentSourceImportInput = z.infer<
typeof documentSourceImportSchema
>;