Files
bookmanager/src/lib/word-import-db.ts
T
bobbanandClaude Sonnet 5.5 1fd1ce78cb Krockskydd för mallar, inställningar och markeringar, flera val i kopplingsfält, Word-import av scheman och kartregister, fler namn för äldre år
Mallar, bokinställningar och kartmarkeringar skickar med version och visar
en krockruta. Kopplingsfält kan ha flera val. Scheman och kartregister kan
importeras från Word, och nya ID hoppar över upptagna. Äldre födelseår får
fler namn genom två SCB-tabeller och grannår. Migrering 0008.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-08 23:23:31 +01:00

398 lines
18 KiB
TypeScript

import type { EntityKind, Prisma } from "@prisma/client";
import { prisma } from "./db";
import { ENTITY_PREFIX, nextEntityCode, nextMapCode, nextMarkerCode, nextRelationCode, nextScheduleCode, reserveNumber } from "./ids";
import { WORKPLACE_KINDS, parseKind } from "./kinds";
import { normalizeStoryDate } from "./story-date";
import { getTemplate, refIds, type FieldDef } from "./templates";
import { DocxError, readDocx } from "./word-docx";
import {
IMPORT_KINDS,
analyzeDocument,
describeUnmatched,
overflowField,
usableCode,
type DocKind,
type ImportKind,
type ItemLink,
type KnownEntity,
type Pair,
type ParsedDocument,
type ParsedItem,
} from "./word-import";
import type { ParsedMap, ParsedShift } from "./word-schedule";
// Förhandsgranskning och sparande av importen från Word. Tolkningen själv ligger i word-import.ts.
export const MAX_WORD_FILES = 20;
export const MAX_WORD_ITEMS = 500;
export type PreviewItem = ParsedItem & { duplicates: { id: string; name: string; code: string | null }[] };
export type PreviewMap = ParsedMap & { /** Det finns redan en karta med det Kart-ID:t i boken */ exists: boolean };
export type PreviewDocument = Omit<ParsedDocument, "items" | "maps"> & {
items: PreviewItem[];
maps: PreviewMap[];
/** Satt om filen inte gick att läsa */
error: "not-docx" | "too-large" | "invalid" | null;
};
async function loadContext(bookId: string) {
const entities = await prisma.entity.findMany({ where: { bookId }, select: { id: true, name: true, code: true, kind: true } });
const known: KnownEntity[] = entities;
const templates: Partial<Record<ImportKind, FieldDef[]>> = {};
for (const kind of IMPORT_KINDS) templates[kind] = (await getTemplate(bookId, kind, "sv")).fields;
return { known, templates };
}
/** Läser Word-filerna och tolkar dem, utan att spara något. `kinds` låter användaren byta dokumenttyp per fil. */
export async function previewWordFiles(
bookId: string,
files: { name: string; bytes: Uint8Array }[],
kinds: (DocKind | null)[] = [],
): Promise<PreviewDocument[]> {
const { known, templates } = await loadContext(bookId);
const byName = new Map<string, KnownEntity[]>();
for (const e of known) byName.set(`${e.kind}|${e.name.toLowerCase()}`, [...(byName.get(`${e.kind}|${e.name.toLowerCase()}`) ?? []), e]);
const mapCodes = new Set((await prisma.bookMap.findMany({ where: { bookId, code: { not: null } }, select: { code: true } })).map((m) => m.code));
const docs = files.slice(0, MAX_WORD_FILES).map((file, index): PreviewDocument => {
try {
const blocks = readDocx(file.bytes);
const parsed = analyzeDocument(blocks, file.name, known, { kind: kinds[index] ?? null, templates });
return {
...parsed,
error: null,
items: parsed.items.map((item) => ({
...item,
duplicates:
item.entityKind === "RELATION" ? [] : (byName.get(`${item.entityKind}|${item.name.toLowerCase()}`) ?? []).map((e) => ({ id: e.id, name: e.name, code: e.code })),
})),
maps: parsed.maps.map((m) => ({ ...m, exists: !!m.code && mapCodes.has(m.code) })),
};
} catch (error) {
if (error instanceof DocxError) return { file: file.name, kind: null, items: [], relation: null, schedule: null, scheduleRows: [], maps: [], warnings: [], error: error.code };
throw error;
}
});
await linkSchedules(bookId, docs);
return docs;
}
/**
* Ger importerade scheman en person och en arbetsplats: först från schemaregistret i samma uppladdning, annars från ett
* schema med samma Schema-ID som redan finns i boken. Annars får användaren välja i förhandsgranskningen.
*/
async function linkSchedules(bookId: string, docs: PreviewDocument[]) {
const rows = docs.flatMap((d) => d.scheduleRows);
for (const doc of docs) {
const schedule = doc.schedule;
if (!schedule) continue;
if (schedule.code) {
const row = rows.find((r) => r.code === schedule.code && r.personId);
if (row) {
Object.assign(schedule, { personId: row.personId, workplaceId: row.workplaceId, source: "register" });
} else {
const shift = await prisma.shift.findFirst({ where: { bookId, scheduleCode: schedule.code }, select: { personId: true, workplaceId: true } });
if (shift) Object.assign(schedule, { personId: shift.personId, workplaceId: shift.workplaceId, source: "existing" });
}
}
if (schedule.personId) schedule.existing = await prisma.shift.count({ where: { bookId, personId: schedule.personId, workplaceId: schedule.workplaceId } });
}
}
/** Personer och arbetsplatser att välja bland för scheman som inte fick någon från registret. */
export async function loadScheduleOptions(bookId: string) {
const entities = await prisma.entity.findMany({
where: { bookId, kind: { in: ["CHARACTER", ...WORKPLACE_KINDS] } },
select: { id: true, name: true, kind: true, code: true },
orderBy: { name: "asc" },
});
return { people: entities.filter((e) => e.kind === "CHARACTER"), workplaces: entities.filter((e) => e.kind !== "CHARACTER") };
}
// ─── Spara ──────────────────────────────────────────────────────────────────
/** Det som webbläsaren skickar tillbaka efter förhandsgranskningen. Allt kontrolleras igen innan något sparas. */
export type CommitItem = {
include: boolean;
entityKind: string;
name: string;
code: string | null;
data: Record<string, unknown>;
links: ItemLink[];
unmatched: Pair[];
/** Deltagare i en relation (post-id) */
participants?: string[];
};
/** Ett schema att spara: personen och arbetsplatsen väljs av användaren eller kommer från schemaregistret. */
export type CommitSchedule = {
include: boolean;
code: string | null;
personId: string | null;
workplaceId: string | null;
shifts: ParsedShift[];
/** Ta bort personens befintliga pass hos arbetsplatsen först */
replace: boolean;
};
export type CommitMap = {
include: boolean;
code: string | null;
name: string;
kind: string;
markers: { code: string | null; label: string; note: string | null }[];
};
export type CommitResult = { created: number; relations: number; skipped: number; shifts: number; maps: number; markers: number };
const TIME = /^([01]\d|2[0-3]):[0-5]\d$/;
// Markeringar från kartregistret saknar position och läggs i kartans mitt, lätt utspridda, så att de går att dra på plats
const DEFAULT_CENTER: [number, number] = [59.3293, 18.0686];
/**
* Ett nytt ID från räknaren som inte redan används. Räknaren kan ligga efter om ID har lagts in utan den (till exempel
* poster som skapats för hand eller av en äldre version), och då hoppar vi över de som är upptagna.
*/
async function freeCode(next: () => Promise<string | null>, taken: (code: string) => boolean | Promise<boolean>): Promise<string | null> {
for (let i = 0; i < 200; i++) {
const code = await next();
if (code === null || !(await taken(code))) return code;
}
throw new Error("Hittade inget ledigt ID");
}
const text = (value: unknown, max: number): string => (typeof value === "string" ? value.trim().slice(0, max) : "");
/** Behåller bara de fält som finns i mallen och har ett giltigt värde av rätt sort. */
export function cleanData(fields: FieldDef[], data: Record<string, unknown>, entities: Map<string, EntityKind>): Record<string, unknown> {
const clean: Record<string, unknown> = {};
for (const field of fields) {
const value = data[field.key];
if (value === undefined || value === null || value === "") continue;
switch (field.type) {
case "number":
if (typeof value === "number" && Number.isFinite(value)) clean[field.key] = value;
break;
case "checkbox":
if (typeof value === "boolean") clean[field.key] = value;
break;
case "date": {
const date = normalizeStoryDate(value);
if (date) clean[field.key] = date;
break;
}
case "select":
if (typeof value === "string" && (field.options ?? []).includes(value)) clean[field.key] = value;
break;
case "url":
if (typeof value === "string" && /^https?:\/\/\S+$/i.test(value) && value.length <= 2000) clean[field.key] = value;
break;
case "reference": {
const ids = refIds(value).filter((id) => {
const kind = entities.get(id);
return kind && (!field.refKind || field.refKind === kind);
});
if (ids.length > 0) clean[field.key] = field.multiple ? ids : ids[0];
break;
}
default:
if (typeof value === "string") clean[field.key] = value.trim().slice(0, 20000);
}
}
return clean;
}
const RELATION_BY_CATEGORY: Record<string, { type: string; bond: "PARTNER" | null }> = {
Vännergrupp: { type: "Vänner", bond: null },
Partner: { type: "Partner till", bond: "PARTNER" },
Familj: { type: "Familj", bond: null },
Kollegor: { type: "Kollega till", bond: null },
};
export async function commitWordImport(
bookId: string,
userId: string,
items: CommitItem[],
options: { appendUnmatched: boolean; schedules?: CommitSchedule[]; maps?: CommitMap[] },
): Promise<CommitResult> {
const chosen = items.slice(0, MAX_WORD_ITEMS).filter((i) => i && i.include);
const schedules = (options.schedules ?? []).slice(0, 50).filter((s) => s && s.include);
const maps = (options.maps ?? []).slice(0, 100).filter((m) => m && m.include);
const result: CommitResult = { created: 0, relations: 0, skipped: items.length - chosen.length, shifts: 0, maps: 0, markers: 0 };
if (chosen.length === 0 && schedules.length === 0 && maps.length === 0) return result;
const existing = await prisma.entity.findMany({ where: { bookId }, select: { id: true, kind: true, code: true } });
const kindOf = new Map(existing.map((e) => [e.id, e.kind]));
const takenCodes = new Set(existing.map((e) => e.code).filter((c): c is string => !!c));
const templates = new Map<ImportKind, FieldDef[]>();
for (const kind of IMPORT_KINDS) templates.set(kind, (await getTemplate(bookId, kind, "sv")).fields);
await prisma.$transaction(
async (tx) => {
for (const item of chosen) {
const kind = item.entityKind === "RELATION" ? "RELATION" : parseKind(item.entityKind);
if (!kind) {
result.skipped++;
continue;
}
const fields = templates.get(kind)!;
const data = cleanData(fields, item.data ?? {}, kindOf);
const unmatched = (Array.isArray(item.unmatched) ? item.unmatched : []).filter((p) => text(p?.label, 200) && text(p?.value, 20000)).slice(0, 100);
const overflow = overflowField(fields);
if (options.appendUnmatched && unmatched.length > 0 && overflow) {
const extra = describeUnmatched(unmatched.map((p) => ({ label: text(p.label, 200), value: text(p.value, 5000) })));
const current = typeof data[overflow] === "string" ? `${data[overflow]}\n\n` : "";
data[overflow] = `${current}${extra}`.slice(0, 20000);
}
if (kind === "RELATION") {
const ids = [...new Set((item.participants ?? []).filter((id) => typeof id === "string" && kindOf.has(id)))];
if (ids.length < 2) {
result.skipped++;
continue;
}
const mapped = RELATION_BY_CATEGORY[String(data.category ?? "")];
await tx.relation.create({
data: {
bookId,
code: await freeCode(() => nextRelationCode(bookId, tx), async (c) => !!(await tx.relation.findFirst({ where: { bookId, code: c }, select: { id: true } }))),
fromId: ids[0],
toId: ids[1],
type: mapped?.type ?? (text(item.name, 100) || "Relation"),
bond: mapped?.bond ?? null,
data: data as Prisma.InputJsonObject,
updatedById: userId,
members: { create: ids.slice(2).map((entityId) => ({ entityId })) },
},
});
result.relations++;
continue;
}
const name = text(item.name, 200);
if (!name) {
result.skipped++;
continue;
}
const wanted = usableCode(kind, text(item.code, 50) || null);
let code: string | null;
if (wanted && !takenCodes.has(wanted)) {
code = wanted;
await reserveNumber(bookId, ENTITY_PREFIX[kind]!, Number(wanted.split("-")[1]), tx);
} else {
code = await freeCode(() => nextEntityCode(bookId, kind, tx), (c) => takenCodes.has(c));
}
if (code) takenCodes.add(code);
const entity = await tx.entity.create({
data: { bookId, kind, name, code, tags: [], data: data as Prisma.InputJsonObject, updatedById: userId },
});
kindOf.set(entity.id, kind);
result.created++;
for (const link of Array.isArray(item.links) ? item.links.slice(0, 200) : []) {
const other = typeof link?.entityId === "string" && kindOf.has(link.entityId) ? link.entityId : null;
const type = text(link?.type, 100);
if (!other || !type || other === entity.id) continue;
await tx.relation.create({
data: {
bookId,
code: await freeCode(() => nextRelationCode(bookId, tx), async (c) => !!(await tx.relation.findFirst({ where: { bookId, code: c }, select: { id: true } }))),
fromId: link.toDocument ? other : entity.id,
toId: link.toDocument ? entity.id : other,
type,
description: text(link.note, 2000) || null,
updatedById: userId,
},
});
result.relations++;
}
}
// Scheman: ett schema-ID per person och arbetsplats, och redan befintliga pass läggs inte till en gång till
for (const schedule of schedules) {
const personId = typeof schedule.personId === "string" && kindOf.get(schedule.personId) === "CHARACTER" ? schedule.personId : null;
const workKind = typeof schedule.workplaceId === "string" ? kindOf.get(schedule.workplaceId) : undefined;
const workplaceId = schedule.workplaceId && workKind && WORKPLACE_KINDS.includes(workKind) ? schedule.workplaceId : null;
const shifts = (Array.isArray(schedule.shifts) ? schedule.shifts : [])
.slice(0, 200)
.filter((s) => s && Number.isInteger(s.weekday) && s.weekday >= 0 && s.weekday <= 6 && TIME.test(String(s.startTime)) && TIME.test(String(s.endTime)) && s.startTime !== s.endTime)
.map((s) => ({ weekday: s.weekday, startTime: s.startTime, endTime: s.endTime, label: text(s.label, 100) || null }));
if (!personId || shifts.length === 0) {
result.skipped++;
continue;
}
if (schedule.replace) await tx.shift.deleteMany({ where: { bookId, personId, workplaceId } });
const present = await tx.shift.findMany({ where: { bookId, personId, workplaceId }, select: { weekday: true, startTime: true, endTime: true, scheduleCode: true } });
const have = new Set(present.map((s) => `${s.weekday}|${s.startTime}|${s.endTime}`));
const fresh = shifts.filter((s) => !have.has(`${s.weekday}|${s.startTime}|${s.endTime}`));
let scheduleCode = present.find((s) => s.scheduleCode)?.scheduleCode ?? null;
if (!scheduleCode) {
const wanted = /^SCI-\d+$/.test(text(schedule.code, 20)) ? text(schedule.code, 20) : null;
if (wanted && !(await tx.shift.findFirst({ where: { bookId, scheduleCode: wanted }, select: { id: true } }))) {
scheduleCode = wanted;
await reserveNumber(bookId, "SCI", Number(wanted.split("-")[1]), tx);
} else {
scheduleCode = (await freeCode(() => nextScheduleCode(bookId, tx), async (c) => !!(await tx.shift.findFirst({ where: { bookId, scheduleCode: c }, select: { id: true } })))) as string;
}
}
await tx.shift.createMany({ data: fresh.map((s) => ({ bookId, personId, workplaceId, ...s, weeks: "ALL" as const, scheduleCode })) });
result.shifts += fresh.length;
}
// Kartor och placeringar från kartregistret. Bildkartor kräver en bild och skapas inte här.
for (const map of maps) {
const name = text(map.name, 200);
if (!name || map.kind !== "GEO") {
result.skipped++;
continue;
}
const wantedMap = /^KD-R_\d+$/.test(text(map.code, 20)) ? text(map.code, 20) : null;
let mapCode: string;
if (wantedMap && !(await tx.bookMap.findFirst({ where: { bookId, code: wantedMap }, select: { id: true } }))) {
mapCode = wantedMap;
await reserveNumber(bookId, "KD-R", Number(wantedMap.split("_")[1]), tx);
} else {
mapCode = (await freeCode(() => nextMapCode(bookId, "GEO", tx), async (c) => !!(await tx.bookMap.findFirst({ where: { bookId, code: c }, select: { id: true } })))) as string;
}
const created = await tx.bookMap.create({
data: { bookId, name, kind: "GEO", code: mapCode, centerLat: DEFAULT_CENTER[0], centerLng: DEFAULT_CENTER[1], zoom: 11 },
});
result.maps++;
for (const [i, marker] of (Array.isArray(map.markers) ? map.markers.slice(0, 500) : []).entries()) {
const label = text(marker?.label, 200);
if (!label) continue;
const wanted = /^PI-\d+$/.test(text(marker.code, 20)) ? text(marker.code, 20) : null;
let code: string;
if (wanted && !(await tx.marker.findFirst({ where: { map: { bookId }, code: wanted }, select: { id: true } }))) {
code = wanted;
await reserveNumber(bookId, "PI", Number(wanted.split("-")[1]), tx);
} else {
code = (await freeCode(() => nextMarkerCode(bookId, tx), async (c) => !!(await tx.marker.findFirst({ where: { map: { bookId }, code: c }, select: { id: true } })))) as string;
}
await tx.marker.create({
data: {
mapId: created.id,
code,
label,
note: text(marker.note, 5000) || null,
lat: DEFAULT_CENTER[0] + Math.floor(i / 10) * 0.004,
lng: DEFAULT_CENTER[1] + (i % 10) * 0.004,
updatedById: userId,
},
});
result.markers++;
}
}
},
{ timeout: 60_000, maxWait: 10_000 },
);
return result;
}