Phase 12: draft generation (FR-16)
ไฟล์ร่าง (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>
This commit is contained in:
parent
0edb70fd38
commit
b3c1c667f7
30 changed files with 879 additions and 93 deletions
|
|
@ -1,4 +1,3 @@
|
|||
using System.Globalization;
|
||||
using System.Text.Json;
|
||||
using Amazon.Runtime;
|
||||
using DocumentFormat.OpenXml.Packaging;
|
||||
|
|
@ -52,6 +51,18 @@ public class DocumentsController(
|
|||
return BadRequest("ต้องแนบไฟล์ `file` (docx)");
|
||||
if (!Guid.TryParse(request.ClientVersionId, out _))
|
||||
return BadRequest("`clientVersionId` ต้องเป็น GUID");
|
||||
|
||||
// FR-16 — templateId ทางเลือก (ส่งจาก Start Versioning ของไฟล์ร่าง): validate ทุกครั้งที่ส่งมา
|
||||
// (ใช้จริงเฉพาะตอน auto-create Document ด้านล่าง — document มีอยู่แล้วจะถูกเฉย)
|
||||
Guid? templateIdToLink = null;
|
||||
if (!string.IsNullOrWhiteSpace(request.TemplateId))
|
||||
{
|
||||
if (!Guid.TryParse(request.TemplateId, out var parsedTemplateId))
|
||||
return BadRequest("`templateId` ต้องเป็น GUID");
|
||||
if (!await db.Templates.AsNoTracking().AnyAsync(t => t.Id == parsedTemplateId, ct))
|
||||
return BadRequest($"ไม่พบ template {parsedTemplateId}");
|
||||
templateIdToLink = parsedTemplateId; // template ที่ deactivate ผูกได้ — ไฟล์ร่างถูกสร้างตอน template ยัง active (นโยบายเดียวกับ FR-9/FR-12)
|
||||
}
|
||||
if (request.File.Length > MaxUploadBytes)
|
||||
return StatusCode(StatusCodes.Status413PayloadTooLarge, $"ไฟล์ใหญ่เกิน {MaxUploadBytes / (1024 * 1024)}MB");
|
||||
if (!DocxUploadValidation.IsValidDocx(request.File))
|
||||
|
|
@ -97,7 +108,7 @@ public class DocumentsController(
|
|||
var contentHash = await hasher.ComputeSha256HexAsync(uploadStream, ct);
|
||||
uploadStream.Seek(0, SeekOrigin.Begin);
|
||||
|
||||
// สร้าง Document ถ้ายังไม่มี (FR-5)
|
||||
// สร้าง Document ถ้ายังไม่มี (FR-5) — ไฟล์ร่าง (FR-16) ผูก template ต้นทางจาก templateId ที่ส่งมา
|
||||
var doc = await db.Documents.FirstOrDefaultAsync(d => d.Id == documentId, ct);
|
||||
var createdDoc = false;
|
||||
if (doc is null)
|
||||
|
|
@ -107,6 +118,7 @@ public class DocumentsController(
|
|||
Id = documentId,
|
||||
FileName = fileName,
|
||||
AddInSignature = string.Empty, // ฝั่ง add-in stamp ในไฟล์เอง
|
||||
TemplateId = templateIdToLink, // null = เอกสาร track เอง · มีค่า = ไฟล์ร่างจาก template (field sync ทำงานเต็มระบบที่ v1)
|
||||
CreatedAt = now,
|
||||
};
|
||||
db.Documents.Add(doc);
|
||||
|
|
@ -257,32 +269,10 @@ public class DocumentsController(
|
|||
if (message is { Length: > MaxVersionMessageLength })
|
||||
return BadRequest($"`message` ยาวเกิน {MaxVersionMessageLength} อักขระ");
|
||||
|
||||
// validate required + type (FR-10) — field ที่ส่งเกินนิยาม ignore ตาม policy เดียวกับ FR-11
|
||||
var missingRequired = fieldDefs
|
||||
.Where(f => f.IsRequired && (!values.TryGetValue(f.Key, out var v) || string.IsNullOrWhiteSpace(v ?? string.Empty)))
|
||||
.Select(f => f.Key)
|
||||
.ToList();
|
||||
if (missingRequired.Count > 0)
|
||||
return BadRequest($"กรอก required fields ให้ครบ: {string.Join(", ", missingRequired)}");
|
||||
|
||||
var typeErrors = new List<string>();
|
||||
foreach (var def in fieldDefs)
|
||||
{
|
||||
if (!values.TryGetValue(def.Key, out var v) || string.IsNullOrWhiteSpace(v))
|
||||
continue;
|
||||
var valid = def.Type switch
|
||||
{
|
||||
"number" => decimal.TryParse(v, NumberStyles.Number, CultureInfo.InvariantCulture, out _),
|
||||
"date" => DateTimeOffset.TryParse(v, CultureInfo.InvariantCulture, DateTimeStyles.None, out _),
|
||||
_ => true,
|
||||
};
|
||||
if (!valid)
|
||||
typeErrors.Add(def.Type == "number"
|
||||
? $"`{def.Key}` ต้องเป็นตัวเลข"
|
||||
: $"`{def.Key}` ต้องเป็นวันที่ที่ parse ได้ (ISO-8601 เช่น 2026-08-19)");
|
||||
}
|
||||
if (typeErrors.Count > 0)
|
||||
return BadRequest(string.Join("; ", typeErrors));
|
||||
// validate required + type (FR-10) — ชุดเดียวกับ generate-draft (FR-16)
|
||||
var validationError = TemplateFieldValidation.Validate(fieldDefs, values);
|
||||
if (validationError is not null)
|
||||
return BadRequest(validationError);
|
||||
|
||||
// โหลด template จาก MinIO → generate ไฟล์ + stamp custom properties (FR-10)
|
||||
Stream templateStream;
|
||||
|
|
@ -326,7 +316,7 @@ public class DocumentsController(
|
|||
var doc = new Document
|
||||
{
|
||||
Id = documentId,
|
||||
FileName = BuildGeneratedFileName(template.Name, now),
|
||||
FileName = DocxGenerationService.BuildGeneratedFileName(template.Name, now),
|
||||
AddInSignature = AddInMetadata.AddInSignatureGuid,
|
||||
TemplateId = template.Id,
|
||||
CreatedAt = now,
|
||||
|
|
@ -578,16 +568,6 @@ public class DocumentsController(
|
|||
}
|
||||
}
|
||||
|
||||
/// <summary>ชื่อไฟล์ของเอกสารที่ generate: ชื่อ template + timestamp — ตัดอักขระที่ filename ใช้ไม่ได้ออก</summary>
|
||||
private static string BuildGeneratedFileName(string templateName, DateTimeOffset now)
|
||||
{
|
||||
var invalid = Path.GetInvalidFileNameChars();
|
||||
var clean = new string(templateName.Select(c => invalid.Contains(c) ? '_' : c).ToArray()).Trim();
|
||||
if (clean.Length == 0)
|
||||
clean = "document";
|
||||
return $"{clean}_{now:yyyyMMdd-HHmmss}.docx";
|
||||
}
|
||||
|
||||
private static object? ParseSnapshot(string? json)
|
||||
{
|
||||
if (json is null)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue