ไฟล์ร่าง (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>
38 lines
2 KiB
C#
38 lines
2 KiB
C#
using System.Globalization;
|
|
using WordVersioning.Api.Models;
|
|
|
|
namespace WordVersioning.Api.Services;
|
|
|
|
/// <summary>FR-10/FR-16 — validate required + type ของค่าที่กรอก (ใช้ร่วมทั้ง generate และ generate-draft)
|
|
/// field ที่ส่งเกินนิยาม ignore ตาม policy เดียวกับ FR-11</summary>
|
|
public static class TemplateFieldValidation
|
|
{
|
|
/// <summary>null = ผ่าน; ไม่ null = ข้อความ error สำหรับ BadRequest (รูปแบบเดียวกับที่เคยอยู่ใน Generate)</summary>
|
|
public static string? Validate(IEnumerable<TemplateField> fieldDefs, IReadOnlyDictionary<string, string?> values)
|
|
{
|
|
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 $"กรอก 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)");
|
|
}
|
|
return typeErrors.Count > 0 ? string.Join("; ", typeErrors) : null;
|
|
}
|
|
}
|