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>
240 lines
14 KiB
TypeScript
240 lines
14 KiB
TypeScript
import { beforeEach, describe, expect, it } from "vitest";
|
|
import { nextMapCode, nextMarkerCode, nextScheduleCode } from "@/lib/ids";
|
|
import { commitWordImport, previewWordFiles, type CommitMap } from "@/lib/word-import-db";
|
|
import { makeDocx, para, table } from "../helpers/docx";
|
|
import { describeDb, makeBook, makeEntity, makeUser, prisma, resetAll } from "../helpers/db";
|
|
|
|
const bytes = (doc: Uint8Array) => ({ name: "doc.docx", bytes: doc });
|
|
|
|
describeDb("import från Word: scheman och kartregister", () => {
|
|
let ownerId: string;
|
|
let bookId: string;
|
|
|
|
beforeEach(async () => {
|
|
await resetAll();
|
|
ownerId = (await makeUser()).id;
|
|
bookId = (await makeBook(ownerId)).id;
|
|
});
|
|
|
|
describe("scheman", () => {
|
|
const grid = (cells: [number, number, string][], code = "Schema: Schema ID: SCI-0009") => {
|
|
const days = ["Tid:", "Måndag:", "Tisdag:", "Onsdag:", "Torsdag:", "Fredag:", "Lördag:", "Söndag:"];
|
|
const rows = ["06:00", "07:00", "08:00", "09:00", "10:00", "11:00", "12:00"].map((t) => [t, "", "", "", "", "", "", ""]);
|
|
for (const [r, c, v] of cells) rows[r][c] = v;
|
|
return makeDocx([para(code), table(days, ...rows)]);
|
|
};
|
|
const register = (rows: string[][]) => makeDocx([table(["Schema ID:", "Karaktärs ID:", "Företags/utbildnings ID:", "Dag/natt:"], ...rows, ["", "", "", ""])]);
|
|
const shiftsOf = () => prisma.shift.findMany({ where: { bookId }, orderBy: [{ weekday: "asc" }, { startTime: "asc" }] });
|
|
const commit = (schedules: Parameters<typeof commitWordImport>[3]["schedules"]) => commitWordImport(bookId, ownerId, [], { appendUnmatched: false, schedules });
|
|
|
|
it("schemaregistret i samma uppladdning ger scheman person och arbetsplats", async () => {
|
|
const anna = await makeEntity(bookId, "CHARACTER", "Anna");
|
|
const firma = await makeEntity(bookId, "COMPANY", "Firma");
|
|
const docs = await previewWordFiles(bookId, [
|
|
bytes(grid([[2, 1, "Arbete"], [3, 1, "Arbete"]])),
|
|
bytes(register([["SCI-0009", anna.code!, firma.code!, "Dag: X Natt:"]])),
|
|
]);
|
|
expect(docs[0].kind).toBe("SCHEDULE");
|
|
expect(docs[0].schedule).toMatchObject({ code: "SCI-0009", personId: anna.id, workplaceId: firma.id, source: "register", existing: 0 });
|
|
expect(docs[0].schedule?.shifts).toEqual([{ weekday: 0, startTime: "08:00", endTime: "10:00", label: "Arbete" }]);
|
|
expect(docs[1].scheduleRows[0]).toMatchObject({ code: "SCI-0009", personId: anna.id, workplaceId: firma.id });
|
|
});
|
|
|
|
it("ett schema med ett Schema-ID som redan finns i boken får samma person och arbetsplats, och antal befintliga pass", async () => {
|
|
const anna = await makeEntity(bookId, "CHARACTER", "Anna");
|
|
const firma = await makeEntity(bookId, "COMPANY", "Firma");
|
|
await prisma.shift.create({ data: { bookId, personId: anna.id, workplaceId: firma.id, weekday: 2, startTime: "08:00", endTime: "16:00", scheduleCode: "SCI-0009" } });
|
|
const [doc] = await previewWordFiles(bookId, [bytes(grid([[2, 1, "X"]]))]);
|
|
expect(doc.schedule).toMatchObject({ personId: anna.id, workplaceId: firma.id, source: "existing", existing: 1 });
|
|
});
|
|
|
|
it("utan känt Schema-ID får personen väljas, och inget är förvalt", async () => {
|
|
const [doc] = await previewWordFiles(bookId, [bytes(grid([[2, 1, "X"]], "Schema: Schema ID:____"))]);
|
|
expect(doc.schedule).toMatchObject({ code: null, personId: null, workplaceId: null, source: null });
|
|
});
|
|
|
|
it("sparar pass för vald person och arbetsplats med dokumentets Schema-ID", async () => {
|
|
const anna = await makeEntity(bookId, "CHARACTER", "Anna");
|
|
const firma = await makeEntity(bookId, "COMPANY", "Firma");
|
|
const result = await commit([
|
|
{
|
|
include: true,
|
|
code: "SCI-0009",
|
|
personId: anna.id,
|
|
workplaceId: firma.id,
|
|
replace: false,
|
|
shifts: [
|
|
{ weekday: 0, startTime: "08:00", endTime: "16:00", label: "Arbete" },
|
|
{ weekday: 1, startTime: "18:00", endTime: "06:00", label: null },
|
|
],
|
|
},
|
|
]);
|
|
expect(result).toMatchObject({ shifts: 2 });
|
|
expect((await shiftsOf()).map((s) => [s.weekday, s.startTime, s.endTime, s.label, s.scheduleCode, s.weeks])).toEqual([
|
|
[0, "08:00", "16:00", "Arbete", "SCI-0009", "ALL"],
|
|
[1, "18:00", "06:00", null, "SCI-0009", "ALL"],
|
|
]);
|
|
// Räknaren har flyttats förbi det importerade ID:t
|
|
expect(await nextScheduleCode(bookId)).toBe("SCI-0010");
|
|
});
|
|
|
|
it("samma person och arbetsplats behåller sitt Schema-ID, och pass som redan finns läggs inte till igen", async () => {
|
|
const anna = await makeEntity(bookId, "CHARACTER", "Anna");
|
|
await prisma.shift.create({ data: { bookId, personId: anna.id, workplaceId: null, weekday: 0, startTime: "08:00", endTime: "16:00", scheduleCode: "SCI-0003" } });
|
|
const result = await commit([
|
|
{
|
|
include: true,
|
|
code: "SCI-0099",
|
|
personId: anna.id,
|
|
workplaceId: null,
|
|
replace: false,
|
|
shifts: [
|
|
{ weekday: 0, startTime: "08:00", endTime: "16:00", label: null },
|
|
{ weekday: 3, startTime: "09:00", endTime: "12:00", label: null },
|
|
],
|
|
},
|
|
]);
|
|
expect(result.shifts).toBe(1);
|
|
expect((await shiftsOf()).map((s) => [s.weekday, s.scheduleCode])).toEqual([[0, "SCI-0003"], [3, "SCI-0003"]]);
|
|
});
|
|
|
|
it("kan ersätta personens befintliga pass hos arbetsplatsen, men bara hos den", async () => {
|
|
const anna = await makeEntity(bookId, "CHARACTER", "Anna");
|
|
const firma = await makeEntity(bookId, "COMPANY", "Firma");
|
|
const annat = await makeEntity(bookId, "COMPANY", "Annat");
|
|
await prisma.shift.createMany({
|
|
data: [
|
|
{ bookId, personId: anna.id, workplaceId: firma.id, weekday: 0, startTime: "08:00", endTime: "16:00", scheduleCode: "SCI-0001" },
|
|
{ bookId, personId: anna.id, workplaceId: annat.id, weekday: 1, startTime: "08:00", endTime: "16:00", scheduleCode: "SCI-0002" },
|
|
],
|
|
});
|
|
await commit([{ include: true, code: null, personId: anna.id, workplaceId: firma.id, replace: true, shifts: [{ weekday: 4, startTime: "10:00", endTime: "14:00", label: null }] }]);
|
|
expect((await shiftsOf()).map((s) => [s.weekday, s.workplaceId === firma.id ? "firma" : "annat"])).toEqual([
|
|
[1, "annat"],
|
|
[4, "firma"],
|
|
]);
|
|
});
|
|
|
|
it("avvisar ogiltiga personer, arbetsplatser och pass", async () => {
|
|
const anna = await makeEntity(bookId, "CHARACTER", "Anna");
|
|
const sketch = await makeEntity(bookId, "SKETCH", "Skiss");
|
|
const other = await makeBook(ownerId, "Annan");
|
|
const foreign = await makeEntity(other.id, "CHARACTER", "Främling");
|
|
const ok = { weekday: 0, startTime: "08:00", endTime: "16:00", label: null };
|
|
const result = await commit([
|
|
{ include: true, code: null, personId: foreign.id, workplaceId: null, replace: false, shifts: [ok] }, // person i en annan bok
|
|
{ include: true, code: null, personId: null, workplaceId: null, replace: false, shifts: [ok] }, // ingen person
|
|
{ include: true, code: null, personId: anna.id, workplaceId: null, replace: false, shifts: [{ ...ok, weekday: 7 }, { ...ok, startTime: "25:00" }, { ...ok, endTime: "08:00" }] }, // bara ogiltiga pass
|
|
{ include: false, code: null, personId: anna.id, workplaceId: null, replace: false, shifts: [ok] }, // bortvald
|
|
{ include: true, code: null, personId: anna.id, workplaceId: sketch.id, replace: false, shifts: [{ ...ok, weekday: 2 }] }, // arbetsplats av fel typ: pass utan arbetsplats
|
|
]);
|
|
expect(result.shifts).toBe(1);
|
|
expect(result.skipped).toBe(3);
|
|
const [only] = await shiftsOf();
|
|
expect(only).toMatchObject({ weekday: 2, workplaceId: null });
|
|
});
|
|
|
|
it("ett Schema-ID som används av en annan får personen ett nytt", async () => {
|
|
const anna = await makeEntity(bookId, "CHARACTER", "Anna");
|
|
const bo = await makeEntity(bookId, "CHARACTER", "Bo");
|
|
await prisma.shift.create({ data: { bookId, personId: bo.id, workplaceId: null, weekday: 0, startTime: "08:00", endTime: "09:00", scheduleCode: "SCI-0005" } });
|
|
await commit([{ include: true, code: "SCI-0005", personId: anna.id, workplaceId: null, replace: false, shifts: [{ weekday: 1, startTime: "08:00", endTime: "09:00", label: null }] }]);
|
|
const codes = (await prisma.shift.findMany({ where: { personId: anna.id } })).map((s) => s.scheduleCode);
|
|
expect(codes).toHaveLength(1);
|
|
expect(codes[0]).not.toBe("SCI-0005");
|
|
});
|
|
});
|
|
|
|
describe("kartregister", () => {
|
|
const map = (overrides: Partial<CommitMap> = {}): CommitMap => ({
|
|
include: true,
|
|
code: "KD-R_0005",
|
|
name: "Lund",
|
|
kind: "GEO",
|
|
markers: [
|
|
{ code: "PI-0007", label: "Kaféet", note: "Vid torget" },
|
|
{ code: null, label: "Biblioteket", note: null },
|
|
],
|
|
...overrides,
|
|
});
|
|
const commitMaps = (maps: CommitMap[]) => commitWordImport(bookId, ownerId, [], { appendUnmatched: false, maps });
|
|
|
|
it("skapar kartor med markeringar i kartans mitt, och behåller lediga ID", async () => {
|
|
expect(await commitMaps([map()])).toMatchObject({ maps: 1, markers: 2 });
|
|
const created = await prisma.bookMap.findFirstOrThrow({ where: { bookId }, include: { markers: { orderBy: { code: "asc" } } } });
|
|
expect(created).toMatchObject({ name: "Lund", kind: "GEO", code: "KD-R_0005", zoom: 11 });
|
|
expect(created.centerLat).toBeCloseTo(59.3293);
|
|
expect(created.markers.map((m) => [m.code, m.label, m.note])).toEqual([
|
|
["PI-0007", "Kaféet", "Vid torget"],
|
|
["PI-0008", "Biblioteket", null],
|
|
]);
|
|
// Markeringarna ligger på olika ställen så att de går att dra isär
|
|
expect(new Set(created.markers.map((m) => `${m.lat},${m.lng}`)).size).toBe(2);
|
|
expect(created.markers.every((m) => m.updatedById === ownerId)).toBe(true);
|
|
});
|
|
|
|
it("räknarna fortsätter efter de importerade ID:na", async () => {
|
|
await commitMaps([map()]);
|
|
expect(await nextMapCode(bookId, "GEO")).toBe("KD-R_0006");
|
|
expect(await nextMarkerCode(bookId)).toBe("PI-0009");
|
|
});
|
|
|
|
it("ett ID som redan används ger ett nytt, och bildkartor och namnlösa kartor hoppas över", async () => {
|
|
await commitMaps([map()]);
|
|
const result = await commitMaps([map(), map({ kind: "IMAGE", code: "KD-E_0001" }), map({ name: " " }), map({ include: false })]);
|
|
expect(result).toMatchObject({ maps: 1, skipped: 2 });
|
|
const codes = (await prisma.bookMap.findMany({ where: { bookId } })).map((m) => m.code);
|
|
expect(new Set(codes).size).toBe(2);
|
|
const markerCodes = (await prisma.marker.findMany({ where: { map: { bookId } } })).map((m) => m.code);
|
|
expect(new Set(markerCodes).size).toBe(markerCodes.length);
|
|
});
|
|
|
|
it("hoppar över ID som redan används även när räknaren ligger efter", async () => {
|
|
// Poster som lagts in utan räknaren (för hand eller av en äldre version)
|
|
const existing = await prisma.bookMap.create({ data: { bookId, name: "Befintlig", kind: "GEO", code: "KD-R_0001" } });
|
|
await prisma.marker.create({ data: { mapId: existing.id, label: "Gammal", code: "PI-0001", lat: 1, lng: 1 } });
|
|
await prisma.entity.create({ data: { bookId, kind: "CHARACTER", name: "Gammal", code: "KI-0001" } });
|
|
await prisma.shift.create({ data: { bookId, personId: (await prisma.entity.findFirstOrThrow({ where: { bookId } })).id, weekday: 0, startTime: "08:00", endTime: "09:00", scheduleCode: "SCI-0001" } });
|
|
|
|
const result = await commitWordImport(
|
|
bookId,
|
|
ownerId,
|
|
[{ include: true, entityKind: "CHARACTER", name: "Ny", code: null, data: {}, links: [], unmatched: [] }],
|
|
{ appendUnmatched: false, maps: [map({ code: null, markers: [{ code: null, label: "Ny markering", note: null }] })] },
|
|
);
|
|
expect(result).toMatchObject({ created: 1, maps: 1, markers: 1 });
|
|
expect((await prisma.bookMap.findMany({ where: { bookId }, orderBy: { code: "asc" } })).map((m) => m.code)).toEqual(["KD-R_0001", "KD-R_0002"]);
|
|
expect((await prisma.marker.findMany({ where: { map: { bookId } }, orderBy: { code: "asc" } })).map((m) => m.code)).toEqual(["PI-0001", "PI-0002"]);
|
|
expect((await prisma.entity.findFirstOrThrow({ where: { bookId, name: "Ny" } })).code).toBe("KI-0002");
|
|
|
|
// Och samma för ett nytt schema
|
|
const bo = await prisma.entity.create({ data: { bookId, kind: "CHARACTER", name: "Bo", code: "KI-0003" } });
|
|
await commitWordImport(bookId, ownerId, [], {
|
|
appendUnmatched: false,
|
|
schedules: [{ include: true, code: null, personId: bo.id, workplaceId: null, replace: false, shifts: [{ weekday: 1, startTime: "08:00", endTime: "09:00", label: null }] }],
|
|
});
|
|
expect((await prisma.shift.findFirstOrThrow({ where: { personId: bo.id } })).scheduleCode).toBe("SCI-0002");
|
|
});
|
|
|
|
it("hoppar över markeringar utan etikett och kortar för långa texter", async () => {
|
|
await commitMaps([map({ markers: [{ code: null, label: "", note: null }, { code: null, label: "x".repeat(500), note: "y".repeat(9000) }] })]);
|
|
const [marker] = await prisma.marker.findMany({ where: { map: { bookId } } });
|
|
expect(marker.label).toHaveLength(200);
|
|
expect(marker.note).toHaveLength(5000);
|
|
});
|
|
|
|
it("förhandsgranskningen visar vilka kartor som redan finns", async () => {
|
|
await prisma.bookMap.create({ data: { bookId, name: "Befintlig", kind: "GEO", code: "KD-R_0001" } });
|
|
const doc = makeDocx([
|
|
table(["Kart ID:", "Placerings ID:", "Stad/Område:", "Beskrivning:", "Övrigt:"], ["KD-R_0001", "PI-0001", "Lund", "Kaféet", ""], ["KD-R_0002", "", "Malmö", "", ""], ["", "", "", "", ""]),
|
|
]);
|
|
const [preview] = await previewWordFiles(bookId, [bytes(doc)]);
|
|
expect(preview.kind).toBe("MAP_REGISTER");
|
|
expect(preview.maps.map((m) => [m.code, m.exists])).toEqual([
|
|
["KD-R_0001", true],
|
|
["KD-R_0002", false],
|
|
]);
|
|
});
|
|
});
|
|
});
|