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 1m7s
Deploy eCourt Note Web to Server / Deploy eCourt Note Web to Server (push) Successful in 4s
Deploy eCourt Note Web to Server / Notify Deployment Status (push) Successful in 1s
manifest ของ add-in เดิมเป็น static ใน public/ ฝัง URL dev (localhost:3000) ทั้งไฟล์ — deploy จริงต้อง rewrite มือเองทุกครั้ง ก่อนนำไป sideload ใน Word ตอนนี้ serve ผ่าน route แทน: app/wordaddin/manifest.xml/route.ts อ่าน template (public/wordaddin/manifest.template.xml — ย้ายจาก manifest.xml ด้วย git mv) แล้วเขียนทับ origin ให้ตรงโฮสต์ที่ขอ ไฟล์ — dev/UAT/production ดาวน์โหลด manifest.xml ขอ environment ตัวเองแล้ว sideload ได้ทันที - ถอด requestOrigin จาก N10 route เป็น utils/request-origin.ts ใช้ร่วม + เพิ่ม heuristic: โฮสต์ไม่ใช่ localhost + proto http = บังคับ https (Office รับ https เท่านั้น และ hop ภายในหลัง IIS เป็น http เสมอ — Next server ก็ฉีด x-forwarded-proto เอง จึง ชี้ขาดจากโฮสต์ปลายทาง ไม่เดาว่า header มาจาก proxy หรือถูกฉีด) deep link ของ N10 ได้พฤติกรรมเดียวกันเป็นผลพวง - ตรวจ nft trace แล้ว template ถูกดึงเข้า standalone ด้วย (คู่ กับ COPY public จาก commit ก่อน = ชัวร์สองชั้น) ยืนยัน: tsc + vitest 215 tests ผ่าน · ทดสอบบน dev server ด้วย curl 3 โหมด — direct ได้ http://localhost:3000 (Word ยกเว้น localhost), ผ่าน proxy ส่ง header ครบได้ https://<โฮสต์>, ผ่าน proxy ไม่ส่ง proto (กรณี IIS) ได้ https://<โฮสต์> จากการ bump · next build compile route ผ่าน (ขั้น standalone symlink พังเฉพาะเครื่อง local Windows สิทธิ์ symlink — ไม่เกี่ยวกับโค้ด Docker build ผ่าน) Co-Authored-By: Claude Code <noreply@anthropic.com>
86 lines
5.1 KiB
TypeScript
86 lines
5.1 KiB
TypeScript
import { and, asc, eq } from "drizzle-orm";
|
|
|
|
import { db } from "@/db";
|
|
import { files } from "@/db/schema";
|
|
import { externalApiError, requireExternalApiKey } from "@/lib/external-api-auth";
|
|
import { loadDocumentBundles, parseQueryBody } from "@/lib/external-api";
|
|
import { requestOrigin } from "@/utils/request-origin";
|
|
|
|
// N10 (PLAN-API.md D2) — POST /api/note/external/documents
|
|
// (เดิมชื่อ /api/external/query ถึงรอบสาม — เปลี่ยนชื่อตาม resource ที่คืน 2026-09-25)
|
|
// เส้นเดียวถามด้วย JSON body { fileId และ/หรือ typeId, filter } — response
|
|
// รูปเดียวเสมอ { documents: [bundle…] } (bundle ยุบชั้น — field เอกสารอยู่
|
|
// ระดับเดียวกับ annotations/bookmarks): ถาม fileId = ยาว 1 ชิ้น (ไม่เคยเปิด
|
|
// ในระบบ = 404 — ไฟล์ url: fallback ของ N8 ค้นด้วย ecourt: prefix ไม่ได้ =
|
|
// 404 ตามสัญญาลิงก์), ถาม typeId = ทุกไฟล์ในกลุ่ม N9 (กลุ่มว่าง = documents: []),
|
|
// ส่งทั้งคู่ = AND เหลือเฉพาะเอกสารเดียวที่ตรงทั้งสองเงื่อนไข (2026-09-25 —
|
|
// รอบเดียวกันเคยเป็น OR รวมผลแล้วเปลี่ยนหลังทดสอบจริง: ผู้เรียกต้องการเอกสาร
|
|
// เดียวตามที่ส่งมา ไม่ใช่ทั้งกลุ่ม)
|
|
// read-only: ไม่ publish SSE ไม่ revalidatePath — snapshot ณ ตอนเรียก
|
|
|
|
const JSON_HEADERS = { "content-type": "application/json; charset=utf-8" };
|
|
|
|
// origin เต็มของ request สำหรับ url เต็มใน response — ถอดเป็น helper ร่วม
|
|
// (utils/request-origin.ts) ให้ route manifest ของ wordaddin ใช้เหมือนกัน (2026-10-16)
|
|
|
|
export async function POST(req: Request) {
|
|
const unauthorized = requireExternalApiKey(req);
|
|
if (unauthorized) return unauthorized;
|
|
|
|
let body: unknown;
|
|
try {
|
|
body = await req.json();
|
|
} catch {
|
|
return externalApiError("body ต้องเป็น JSON ที่ถูกต้อง", 400);
|
|
}
|
|
|
|
const query = parseQueryBody(body);
|
|
if (!query.ok) return externalApiError(query.error, 400);
|
|
|
|
const origin = requestOrigin(req);
|
|
|
|
// ครอบทุก DB call (code review 2026-09-25) — ล้มไม่ว่าเหตุใด (connection
|
|
// หลุด/timeout/table มีปัญหา) ต้องได้ 500 JSON ตาม contract เดียวกับ error
|
|
// อื่นของ endpoint นี้ ไม่ใช่ 500 ของ framework ที่ format ต่างออกไป; log
|
|
// รายละเอียดฝั่ง server อย่างเดียว — ข้อความตอบกลาง ๆ กัน leak SQL/path
|
|
try {
|
|
if (query.mode === "file") {
|
|
// ส่งทั้งคู่ = AND กรองทั้ง externalId และกลุ่มใน query เดียว เหลือเอกสารเดียว
|
|
// (external_id unique — match ได้มากสุด 1 ฉบับ) ไม่ตรง = 404 โดยแยกสาเหตุ:
|
|
// ไม่เคยเปิดในระบบ หรืออยู่คนละกลุ่มกับ typeId ที่ระบุ
|
|
const [file] = await db
|
|
.select()
|
|
.from(files)
|
|
.where(
|
|
query.typeId
|
|
? and(
|
|
eq(files.externalId, `ecourt:${query.fileId}`),
|
|
eq(files.typeId, query.typeId),
|
|
)
|
|
: eq(files.externalId, `ecourt:${query.fileId}`),
|
|
)
|
|
.limit(1);
|
|
if (!file) {
|
|
return externalApiError(
|
|
query.typeId
|
|
? "ไม่พบเอกสารนี้ในระบบ (ยังไม่เคยเปิด หรือไม่ได้อยู่ในกลุ่ม typeId ที่ระบุ)"
|
|
: "ไม่พบเอกสารนี้ในระบบ (ยังไม่เคยเปิด)",
|
|
404,
|
|
);
|
|
}
|
|
const documents = await loadDocumentBundles([file], query.parts, origin);
|
|
return new Response(JSON.stringify({ documents }), { headers: JSON_HEADERS });
|
|
}
|
|
|
|
const fileRows = await db
|
|
.select()
|
|
.from(files)
|
|
.where(eq(files.typeId, query.typeId))
|
|
.orderBy(asc(files.createdAt));
|
|
const documents = await loadDocumentBundles(fileRows, query.parts, origin);
|
|
return new Response(JSON.stringify({ documents }), { headers: JSON_HEADERS });
|
|
} catch (err) {
|
|
console.error("external/documents: query ฐานข้อมูลล้ม", err);
|
|
return externalApiError("เกิดข้อผิดพลาดของระบบ โปรดลองใหม่อีกครั้ง", 500);
|
|
}
|
|
}
|