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>
This commit is contained in:
1 parent
86fd435438
commit
1fd1ce78cb
37 files changed
+2274
-149
No files matched your search
+178
-16
@@ -1,9 +1,9 @@
|
||||
import type { EntityKind, Prisma } from "@prisma/client";
|
||||
import { prisma } from "./db";
|
||||
import { ENTITY_PREFIX, nextEntityCode, nextRelationCode, reserveNumber } from "./ids";
|
||||
import { parseKind } from "./kinds";
|
||||
import { ENTITY_PREFIX, nextEntityCode, nextMapCode, nextMarkerCode, nextRelationCode, nextScheduleCode, reserveNumber } from "./ids";
|
||||
import { WORKPLACE_KINDS, parseKind } from "./kinds";
|
||||
import { normalizeStoryDate } from "./story-date";
|
||||
import { getTemplate, type FieldDef } from "./templates";
|
||||
import { getTemplate, refIds, type FieldDef } from "./templates";
|
||||
import { DocxError, readDocx } from "./word-docx";
|
||||
import {
|
||||
IMPORT_KINDS,
|
||||
@@ -11,6 +11,7 @@ import {
|
||||
describeUnmatched,
|
||||
overflowField,
|
||||
usableCode,
|
||||
type DocKind,
|
||||
type ImportKind,
|
||||
type ItemLink,
|
||||
type KnownEntity,
|
||||
@@ -18,6 +19,7 @@ import {
|
||||
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.
|
||||
|
||||
@@ -26,8 +28,11 @@ export const MAX_WORD_ITEMS = 500;
|
||||
|
||||
export type PreviewItem = ParsedItem & { duplicates: { id: string; name: string; code: string | null }[] };
|
||||
|
||||
export type PreviewDocument = Omit<ParsedDocument, "items"> & {
|
||||
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;
|
||||
};
|
||||
@@ -44,13 +49,15 @@ async function loadContext(bookId: string) {
|
||||
export async function previewWordFiles(
|
||||
bookId: string,
|
||||
files: { name: string; bytes: Uint8Array }[],
|
||||
kinds: (ImportKind | null)[] = [],
|
||||
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]);
|
||||
|
||||
return files.slice(0, MAX_WORD_FILES).map((file, index): PreviewDocument => {
|
||||
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 });
|
||||
@@ -62,12 +69,48 @@ export async function previewWordFiles(
|
||||
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, warnings: [], error: error.code };
|
||||
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 ──────────────────────────────────────────────────────────────────
|
||||
@@ -85,7 +128,42 @@ export type CommitItem = {
|
||||
participants?: string[];
|
||||
};
|
||||
|
||||
export type CommitResult = { created: number; relations: number; skipped: number };
|
||||
/** 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) : "");
|
||||
|
||||
@@ -114,8 +192,11 @@ export function cleanData(fields: FieldDef[], data: Record<string, unknown>, ent
|
||||
if (typeof value === "string" && /^https?:\/\/\S+$/i.test(value) && value.length <= 2000) clean[field.key] = value;
|
||||
break;
|
||||
case "reference": {
|
||||
const kind = typeof value === "string" ? entities.get(value) : undefined;
|
||||
if (typeof value === "string" && kind && (!field.refKind || field.refKind === kind)) clean[field.key] = value;
|
||||
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:
|
||||
@@ -136,11 +217,13 @@ export async function commitWordImport(
|
||||
bookId: string,
|
||||
userId: string,
|
||||
items: CommitItem[],
|
||||
options: { appendUnmatched: boolean },
|
||||
options: { appendUnmatched: boolean; schedules?: CommitSchedule[]; maps?: CommitMap[] },
|
||||
): Promise<CommitResult> {
|
||||
const chosen = items.slice(0, MAX_WORD_ITEMS).filter((i) => i && i.include);
|
||||
const result: CommitResult = { created: 0, relations: 0, skipped: items.length - chosen.length, };
|
||||
if (chosen.length === 0) return result;
|
||||
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]));
|
||||
@@ -176,7 +259,7 @@ export async function commitWordImport(
|
||||
await tx.relation.create({
|
||||
data: {
|
||||
bookId,
|
||||
code: await nextRelationCode(bookId, tx),
|
||||
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"),
|
||||
@@ -201,7 +284,7 @@ export async function commitWordImport(
|
||||
code = wanted;
|
||||
await reserveNumber(bookId, ENTITY_PREFIX[kind]!, Number(wanted.split("-")[1]), tx);
|
||||
} else {
|
||||
code = await nextEntityCode(bookId, kind, tx);
|
||||
code = await freeCode(() => nextEntityCode(bookId, kind, tx), (c) => takenCodes.has(c));
|
||||
}
|
||||
if (code) takenCodes.add(code);
|
||||
const entity = await tx.entity.create({
|
||||
@@ -217,7 +300,7 @@ export async function commitWordImport(
|
||||
await tx.relation.create({
|
||||
data: {
|
||||
bookId,
|
||||
code: await nextRelationCode(bookId, tx),
|
||||
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,
|
||||
@@ -228,6 +311,85 @@ export async function commitWordImport(
|
||||
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 },
|
||||
);
|
||||
|
||||
Reference in new issue
Block a user