Volume 6 · benefit-api Deep Dive
เล่มนี้เจาะลึก benefit-api/ — service สวัสดิการและการเบิกจ่ายของสภากาชาดไทย เป็น NestJS 9 + TypeORM 0.3 + MSSQL ที่เพิ่มเข้ามาภายหลังจากระบบ legacy (vtrc-api, cu-central-api)
เป้าหมายคือให้อ่านจบแล้วเข้าใจโครงสร้าง รู้จุดที่ต้องระวัง (ซึ่งมีเยอะ) และสามารถแก้ไขหรือเพิ่มฟีเจอร์ได้โดยไม่ทำลาย pattern ที่มีอยู่ — หรืออย่างน้อยก็รู้ว่า pattern ที่มีอยู่นั้นเปราะบางเพียงใด
benefit-api คืออะไร
benefit-api คือ REST API service ที่จัดการสวัสดิการ (welfare) และการเบิกจ่ายเงิน (withdrawal/disbursement) ของพนักงานสภากาชาด — ครอบคลุมการเบิกค่ารักษาพยาบาล (WDHospital), สวัสดิการภัยพิบัติ (disaster), สวัสดิการศึกษา (studyLeave), สวัสดิการมรดก/ฌาปนกิจ (inheritance/funeral) รวมถึงการส่งข้อมูลการเบิกไป FMIS ผ่าน SFTP
ตัว service รันที่ http://10.188.128.61:9320/benefit-api/api/v1/ (internal production) — ไม่ได้ expose ผ่าน domain สาธารณะโดยตรง แต่ถูกเรียกจาก frontend (vtrc-web, vtrc-rc-backoffice) และทำงานร่วมกับ vtrc-api, centralize-api (ผ่าน HTTP), และระบบ HRMI (ผ่าน cron job)
ข้อสำคัญที่ทำให้ benefit-api แตกต่างจาก service อื่น:
- benefit-api เขียนตรงไปยัง schema
vtrc(legacy core database) ผ่าน raw SQL โดย bypassvtrc-apiโดยสิ้นเชิง — เป็นการละเมิด bounded context ของระบบเดิม - benefit-api มี 3 TypeORM connections ไปยัง MSSQL host เดียวกัน (
main,vtrc,hrmi) — ทั้งสามล็อกอินด้วย usersa - benefit-api มี SQL injection หลายสิบจุด เพราะใช้ template literal ใน raw query โดยไม่ parameterize
- benefit-api commit
.envพร้อม production credentials เข้า git และ baked เข้า Docker image
เส้นทางการอ่าน
เล่มนี้แบ่งเป็น 9 บท:
- 6.1 Repository map — โครงสร้างโฟลเดอร์, ขนาดไฟล์, dead code
- 6.2 Bootstrap + TypeORM 3 connections — port, global prefix, Swagger, 3 DB pools
- 6.3 REST surface + DTOs — controllers,
@Public()endpoints, validation - 6.4 Service patterns + raw SQL + SQL injection — รูปแบบ raw query, 20+ injection sites
- 6.5 Auth pipeline (JWT guard + SECRET_KEY) — passport-jwt,
@Public(), ปัญหา SECRET_KEY - 6.6 Entities + persistence patterns —
paymentTransaction(~90 fields), cross-schema writes - 6.7 External integrations + cron + SFTP — SFTP credentials, cron jobs, Puppeteer, ExcelJS
- 6.8 Security debt register — รายการ debt 14 รายการ พร้อม severity
- 6.9 Scorecard + เปรียบเทียบ service อื่น — เทียบกับ
centralize-api(Volume 5)
ขอบเขตและสิ่งที่ไม่อยู่ในเล่มนี้
เล่มนี้เน้นที่ตัว benefit-api service เอง ไม่ลงรายละเอียด:
- GraphQL schema ของ vtrc-api — cover ใน Volume 3 และ Volume 11 · API Reference
- Legacy data model ของ
vtrcschema — cover ใน Volume 9 · Data Model - Security vulnerability เชิงลึกพร้อม exploit scenario ของทั้งระบบ — cover ใน Volume 10 · Security
- Deployment pipeline ของ benefit-api — cover ใน Volume 13 · Infrastructure
- Frontend ที่เรียก benefit-api — cover ใน Volume 7 · vtrc-web และ Volume 8 · vtrc-rc-backoffice
คำเตือนสำหรับผู้อ่าน
เนื้อหาในเล่มนี้บันทึกสภาพจริงของ codebase ณ commit e3be391 บน branch prod — หลายส่วนเปราะบางและมี debt สะสมหนักในด้าน security ก่อนนำข้อมูลใดไปใช้ ให้เปิดไฟล์ที่อ้างถึงเพื่อยืนยันว่ายังเป็นจริงอยู่ — โดยเฉพาะปัญหา security ที่อาจได้รับการแก้ไขบางส่วนหลังจากที่รายงานนี้เขียน
ถ้าจะทำ security review — อ่าน 6.8 ก่อน แล้ว cross-check กับ Volume 10 · Security