Fix build time fail: export applyTemplate not compatible with Next.js(move to lib-tmpl), add var in .env.example
All checks were successful
Deploy eCourt Note Web to Server / Validate Database Schema (Drizzle) (push) Successful in 3s
Deploy eCourt Note Web to Server / Build Web Docker Image (push) Successful in 1m23s
Deploy eCourt Note Web to Server / Deploy eCourt Note Web to Server (push) Successful in 5s
Deploy eCourt Note Web to Server / Notify Deployment Status (push) Successful in 1s

This commit is contained in:
oom 2026-10-27 10:15:35 +07:00
parent a3d14fd58d
commit 69d83ea493
4 changed files with 73 additions and 49 deletions

View file

@ -1,3 +1,4 @@
import { generateHmacSha256, signEd25519, sortObjectKeys } from '@/lib/lib-utils';
import createReport from 'docx-templates';
import PizzaZip from "pizzip";
import {
@ -1261,3 +1262,53 @@ export const templateDynamicContents = async (template: Buffer, data: Record<str
}
});
}
// สร้างเอกสารจาก เทมเพลต+data render และ generate ใช้งานเหมือนกัน
// ยังไม่ค่อยแน่ใจว่าควรอยู่ใน library นี้หรือเปล่าเพราะมีการทำ hash/sign ticket_data
// - ticket_data ต้องมีข้อมูลสมบูรณ์ {fid,tid,uid,uname}
// - สร้าง signature/hash จาก ticket_data
// - สร้าง Dynamic Content (docx-templates)
// - เอาข้อมูลใส่ Content Control
export async function applyTemplate(templateBuffer:Buffer,templateData:DocumentTemplate,
secret:string,private_key:string,ttl:number):Promise<Buffer<ArrayBufferLike>>
{
//Calculate hash
const originalTicket_data = templateData?.info?.ticket_data //expect valid json string
// console.log("templateData",templateData?.info?.ticket_data)
if (originalTicket_data && typeof originalTicket_data === 'string') {
try {
const originalTicketObj = JSON.parse(originalTicket_data)//sort and add expire to ticket data
if (originalTicketObj && typeof originalTicketObj === 'object') {
console.log("Found valid ticket data: hash it")
// คำนวณค่า expire (ปัจจุบัน + จำนวนวินาทีที่ต้องการให้หมดอายุ)
// หาร 1000 เพราะต้องการหน่วยวินาที (sec) ไม่ใช่มิลลิวินาที (ms)
const expireTimestamp = Math.floor(Date.now() / 1000) + ttl;
const fullObj = { ...originalTicketObj, exp: expireTimestamp };
const sortedObj = sortObjectKeys(fullObj);
// แปลงกลับเป็น JSON string เพื่อนำไปใช้ส่งและทำ Hash
const ticket_data = JSON.stringify(sortedObj);
// console.log("check secret:",TEST_SECRET,TEST_PRIVATE_KEY_HEX,ticket_data)
const HmacSha256 = generateHmacSha256(ticket_data, secret)
const Ed25519 = signEd25519(ticket_data, private_key)
templateBuffer = setCustomDocProps(templateBuffer, { ticket_data, HmacSha256, Ed25519 })
}
} catch (error) { //just skip
console.error("Set custom property fail",error);
}
}
// Apply Dynamic Content
if (templateData?.dc && typeof templateData?.dc === "object") {
console.log("Apply Dynamic Content")
templateBuffer = Buffer.from(await templateDynamicContents(templateBuffer, templateData.dc))
}
// Apply Content Control
if (templateData?.cc && typeof templateData?.cc === "object") {
console.log("Apply Content Control")
templateBuffer = templateContentControls(templateBuffer, templateData.cc);
}
return templateBuffer
}