ไฟล์ร่าง (draft) — generate docx จาก template แบบ stateless ไม่สร้าง
Document/version; stamp เฉพาะ custom property TemplateId เพื่อให้ add-in
เห็นเป็น untracked; Start Versioning ส่ง form field templateId ให้ backend
ผูก template ตอน auto-create — เอกสารเริ่มที่ v1 (ไม่มี v0) + field sync เต็มระบบ
- Backend: POST /api/templates/{id}/generate-draft (200 คืนไฟล์) +
DocxGenerationService.GenerateDraft + แยก TemplateFieldValidation ใช้ร่วมกับ
generate + UploadVersion รับ templateId (non-GUID/unknown → 400,
deactivated → ผูกได้, document เดิม → เฉย) + CORS expose Content-Disposition
- Add-in: readTrackedProperties อ่าน TemplateId + ส่ง templateId เฉพาะ
startVersioning + hint ไฟล์ร่างใน task pane
- Web: ปุ่ม "สร้างไฟล์ร่าง" บนฟอร์มเดิม + downloadFile รับ init (POST → blob)
- Docs: spec v8 (FR-16, AC-19) + Phase 12 + CONTEXT.md + ADR 0002 +
acceptance-review AC-19
- Tests: backend 82 / add-in 66 / web 59 + build ครบ; QA reviewer PASS
(2 rounds); ค้าง manual บน Word จริง
Co-Authored-By: Claude Code <noreply@anthropic.com>
102 lines
4.1 KiB
TypeScript
102 lines
4.1 KiB
TypeScript
import { config } from "../config";
|
|
import { UploadVersionResult, VersionRecord } from "../models/versionRecord";
|
|
import { apiFetch, apiGetJson, apiSendExpectJson, ApiError, readErrorMessage } from "./apiClient";
|
|
import { generateGuid } from "./metadataService";
|
|
|
|
/**
|
|
* เรียก version API ของ backend (FR-5/6/7 ฝั่ง client)
|
|
* `fields` = map จาก `readContentControlFields()` ตาม FR-11
|
|
*/
|
|
|
|
/** FR-5 — อัปโหลดไฟล์ปัจจุบันเป็น version ใหม่ (แนบ fields map ถ้ามี)
|
|
* + FR-13: แนบ commit message ทางเลือกของผู้ใช้
|
|
* + FR-16: แนบ templateId เฉพาะตอน Start Versioning ของไฟล์ร่าง (backend ใช้ตอน auto-create Document) */
|
|
export async function uploadVersion(
|
|
documentId: string,
|
|
file: Blob,
|
|
clientVersionId: string,
|
|
fields?: Record<string, string>,
|
|
fileName?: string,
|
|
message?: string,
|
|
pdf?: Blob,
|
|
templateId?: string
|
|
): Promise<UploadVersionResult> {
|
|
const form = new FormData();
|
|
form.append("file", file, fileName || `${documentId}.docx`);
|
|
form.append("clientVersionId", clientVersionId);
|
|
if (pdf) form.append("pdf", pdf, `${documentId}.pdf`);
|
|
if (fields && Object.keys(fields).length > 0) {
|
|
form.append("fields", JSON.stringify(fields));
|
|
}
|
|
const trimmedMessage = message?.trim();
|
|
if (trimmedMessage) {
|
|
form.append("message", trimmedMessage);
|
|
}
|
|
if (templateId) {
|
|
form.append("templateId", templateId);
|
|
}
|
|
return apiSendExpectJson<UploadVersionResult>(
|
|
`/api/documents/${documentId}/versions`,
|
|
{ method: "POST", body: form },
|
|
"อัปโหลด version ไม่สำเร็จ"
|
|
);
|
|
}
|
|
|
|
/** FR-6 — version history เรียงใหม่ → เก่า */
|
|
export async function getVersions(documentId: string): Promise<VersionRecord[]> {
|
|
return apiGetJson<VersionRecord[]>(
|
|
`/api/documents/${documentId}/versions`,
|
|
"โหลด version history ไม่สำเร็จ"
|
|
);
|
|
}
|
|
|
|
/** FR-7 — URL ของ download endpoint (ผ่าน backend proxy เท่านั้น — MinIO ไม่ถูก expose) */
|
|
export function buildDownloadUrl(
|
|
documentId: string,
|
|
versionId: string,
|
|
format?: "docx" | "pdf"
|
|
): string {
|
|
return `${config.apiBaseUrl}/api/documents/${documentId}/versions/${versionId}/download${format === "pdf" ? "?format=pdf" : ""}`;
|
|
}
|
|
|
|
export interface VersionDownloadLink {
|
|
url: string;
|
|
/** ISO-8601 เวลาที่ URL หมดอายุ */
|
|
expiresAt: string;
|
|
}
|
|
|
|
/**
|
|
* FR-7 — ขอ presigned URL อายุสั้นของ version หนึ่ง เพื่อเปิดในเบราว์เซอร์ของผู้ใช้
|
|
* (webview ของ Word ทิ้ง blob download เงียบๆ — ให้เบราว์เซอร์จริงจัดการแทน)
|
|
*/
|
|
export async function getDownloadLink(
|
|
documentId: string,
|
|
versionId: string,
|
|
format?: "docx" | "pdf"
|
|
): Promise<VersionDownloadLink> {
|
|
return apiGetJson<VersionDownloadLink>(
|
|
`/api/documents/${documentId}/versions/${versionId}/download-link${format === "pdf" ? "?format=pdf" : ""}`,
|
|
"ขอลิงก์ดาวน์โหลดไม่สำเร็จ"
|
|
);
|
|
}
|
|
|
|
/** FR-7 — ดาวน์โหลดไฟล์ของ version หนึ่งเป็น Blob (ต้องแนบ Bearer จึงต้อง fetch แทน link ตรง) */
|
|
export async function downloadVersion(
|
|
documentId: string,
|
|
versionId: string,
|
|
format?: "docx" | "pdf"
|
|
): Promise<Blob> {
|
|
const response = await apiFetch(buildDownloadUrl(documentId, versionId, format));
|
|
if (!response.ok) {
|
|
throw new ApiError(
|
|
response.status,
|
|
await readErrorMessage(response, `ดาวน์โหลด version ไม่สำเร็จ (${response.status})`)
|
|
);
|
|
}
|
|
return response.blob();
|
|
}
|
|
|
|
/** สร้าง clientVersionId ใหม่สำหรับ upload ครั้งถัดไป */
|
|
export function newClientVersionId(): string {
|
|
return generateGuid();
|
|
}
|