Refactor document versioning to extract fields from uploaded .docx files on the server side

- Removed the `fields` parameter from the version upload form, as it is now extracted from the uploaded .docx file using the `extractContentControls` function.
- Updated related types and interfaces to reflect the new structure of version fields.
- Modified the OpenAPI specification to document the changes in the versioning process.
- Adjusted tests to ensure compliance with the new behavior, including validation for the absence of the `fields` parameter.
- Implemented a new function `extractDocumentSnapshot` to handle the extraction of content controls and document properties from .docx files.
- Updated metadata and versioning services to align with the new extraction logic.
This commit is contained in:
Suphonchai Phoonsawat 2026-10-31 10:21:17 +07:00
parent bc61f0fd2e
commit cdae1ceedd
17 changed files with 380 additions and 179 deletions

View file

@ -7,7 +7,9 @@
* - `startVersioning` (FR-1 "Start Versioning This Document") — เอกสารใหม่ที่ยังไม่ถูก track
* - `saveAndVersion` (FR-2) — ไล่ตามลำดับใน spec แบบเคร่งครัด:
* stamp properties → XML part (delete-then-add, fail ได้เงียบๆ) → document.save()
* → ดึงไฟล์ + extract fields (FR-11) → POST version → คืนผลให้ UI แสดง/refresh history
* → ดึงไฟล์ → POST version → คืนผลให้ UI แสดง/refresh history
* (FR-11 fields เดิมอ่านฝั่ง client ตอน capture — 2026-10-30 ยกให้ server แกะจาก
* ไฟล์ .docx ด้วยตัวเดียวกับ /api/template/extract แทน จึงไม่มีขั้นอ่าน fields แล้ว)
*
* หมายเหตุค่า id ใน metadata: `LastVersionId` และ `<Version id>` ใน XML part เก็บ
* **clientVersionId** (GUID ที่ client generate) เพราะต้อง stamp ลงไฟล์ *ก่อน* upload —
@ -21,7 +23,6 @@ import { DocumentCloseMode, closeDocument, saveDocumentVerified } from "./docume
import { getCurrentDocumentFile, getCurrentDocumentPdf } from "./fileService";
import {
generateGuid,
readContentControlFields,
readVersionHistoryFromXml,
stampTrackedProperties,
writeVersionHistoryToXml,
@ -82,7 +83,6 @@ function guessDocumentFileName(): string | undefined {
async function uploadCurrentFile(
documentId: string,
clientVersionId: string,
fields: Record<string, string>,
message?: string,
onProgress?: (progress: string) => void,
templateId?: string
@ -102,7 +102,7 @@ async function uploadCurrentFile(
documentId,
file,
clientVersionId,
fields,
undefined,
guessDocumentFileName(),
message,
pdf,
@ -139,15 +139,14 @@ export async function startVersioning(
{ id: clientVersionId, number: 1, timestamp, user: authService.getUsername() ?? "unknown" },
]);
// 3) native save แล้ว capture ไฟล์ + fields
// 3) native save แล้ว capture ไฟล์ (fields ของ version แกะจากไฟล์ฝั่ง server
// ด้วยตัวเดียวกับ /api/template/extract — contract 2026-10-30 ไม่ต้องอ่านเองแล้ว)
onProgress?.("กำลังบันทึกไฟล์…");
await saveDocumentVerified();
onProgress?.("กำลังอ่านค่า fields จากเอกสาร…");
const fields = await readContentControlFields();
// 4) upload version แรก (แนบ templateId เมื่อเป็นไฟล์ร่าง — FR-16)
onProgress?.("กำลังอัปโหลด version 1…");
const result = await uploadCurrentFile(documentId, clientVersionId, fields, message, onProgress, templateId);
const result = await uploadCurrentFile(documentId, clientVersionId, message, onProgress, templateId);
// FR-3 — สร้าง version สำเร็จ → เคลียร์ flag "มีแก้ไขที่ยังไม่เป็น version"
autoSaveService.clearUncommittedSinceVersion();
return { documentId, ...result };
@ -188,15 +187,13 @@ export async function saveAndVersion(
entries.unshift({ id: clientVersionId, number: predictedNumber, timestamp, user });
await writeVersionHistoryToXml(entries);
// 3) native save → capture ไฟล์ + fields ปัจจุบัน (FR-11)
// 3) native save → capture ไฟล์ปัจจุบัน (fields แกะฝั่ง server จากไฟล์ที่อัปโหลด — 2026-10-30)
onProgress?.("กำลังบันทึกไฟล์…");
await saveDocumentVerified();
onProgress?.("กำลังอ่านค่า fields จากเอกสาร…");
const fields = await readContentControlFields();
// 4) upload
onProgress?.("กำลังอัปโหลด version ใหม่…");
const result = await uploadCurrentFile(documentId, clientVersionId, fields, message, onProgress);
const result = await uploadCurrentFile(documentId, clientVersionId, message, onProgress);
// 5) เลขจริงจาก backend ต่างจากที่เดา (มี client อื่นแทรก) → แก้ XML part ให้ตรง (best effort)
if (result.versionNumber !== predictedNumber) {