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

@ -1,6 +1,5 @@
import { NextResponse } from "next/server";
import { extractContentControls, extractBookmark, templateContentControls, setCustomDocProps, getDocProps,templateDynamicContents } from "@/lib/lib-tmpl";
import { verifyEd25519, verifyHmacSha256 } from "@/lib/lib-utils";
import { extractDocumentSnapshot } from "@/lib/lib-tmpl";
// export const runtime = "nodejs";
const DOCX_CONTENT_TYPE =
@ -40,32 +39,13 @@ export async function POST(request: Request) {
// File → Buffer
const buffer = Buffer.from(await file.arrayBuffer());
// ใช้ lib-tmpl.ts เดิม
// const data = extractContentControls(buffer);
const cc = extractContentControls(buffer);
const bookmark = extractBookmark(buffer)
const properties = getDocProps(buffer)
let verify: Record<string, unknown> ={}
const ticket_data = properties?.custom?.ticket_data
const HmacSha256 = String(properties?.custom?.HmacSha256)
const Ed25519 = String(properties?.custom?.Ed25519)
if(ticket_data && typeof ticket_data === 'string'){
const ticketDataObj= JSON.parse(ticket_data)
const unix_timestamp = Number(ticketDataObj.exp)*1000
if(unix_timestamp){
verify['exp'] = new Date(unix_timestamp).toLocaleString("th-TH", { timeZone: "Asia/Bangkok" })
}
if(TEST_SECRET && HmacSha256){
verify['HmacSha256'] = verifyHmacSha256(ticket_data,TEST_SECRET,HmacSha256)
}
if(TEST_PUBLIC_KEY_HEX&& Ed25519){
verify["Ed25519"]=verifyEd25519(ticket_data,Ed25519,TEST_PUBLIC_KEY_HEX)
}
}
let returnData : Record<string, unknown> = {cc,bookmark,properties,verify}
// ชุดแกะข้อมูล `{cc, bookmark, properties, verify}` จาก lib-tmpl.ts — ตัวเดียวกับที่
// ใช้เก็บเป็น fields ของ version ตอน POST /api/documents/versions (2026-10-30) ·
// secret/public_key เป็นของผู้เรียกเพื่อ "ทดสอบ verify" เฉพาะฝั่งนี้
const returnData = extractDocumentSnapshot(buffer, {
secret: TEST_SECRET || undefined,
publicKeyHex: TEST_PUBLIC_KEY_HEX || undefined,
});
return NextResponse.json(returnData);
} catch (error) {
console.error(error);