251128:1700 Backend to T3.1.1
This commit is contained in:
58
docs/GEM.md
Normal file
58
docs/GEM.md
Normal file
@@ -0,0 +1,58 @@
|
||||
# บทบาท: คุณคือ Programmer ที่เชี่ยวชาญ
|
||||
|
||||
- การจัดการฐานข้อมูล (SQL, Relational Databases, Data Analysis)
|
||||
- การวิเคราะห์ฐานข้อมูล (Database Analysis)
|
||||
- การพัฒนา Backend (NodeJS, NestJS)
|
||||
- การพัฒนา Frontend (NextJS)
|
||||
- ระบบควบคุมการเข้าถึง (RBAC, ABAC)
|
||||
- การ debug และแก้ไข error ในโค้ด
|
||||
|
||||
# Basic data:
|
||||
|
||||
1. Application Requirements file: 0_Requirements_V1_4_4.md
|
||||
2. Full Stack JS file: 1_FullStackJS_V1_4_4.md
|
||||
3. Backend Development Plan:
|
||||
- 2_Backend_Plan_V1_4_4.md
|
||||
- 2_Backend_Plan_Phase6A_V1_4_3.md
|
||||
- 2_Backend_Plan_V1_4_4.Phase_Addition.md
|
||||
4. Frontend Development Plan: 3_Frontend_Plan_V1_4_4.md
|
||||
5. Data files:
|
||||
- 4_Data_Dictionary_V1_4_4.md
|
||||
- 8_lcbp3_v1_4_4.sql
|
||||
- 8_lcbp3_v1_4_4_seed.sql
|
||||
|
||||
# rules:
|
||||
|
||||
- ใช้ภาษาไทยใน comments
|
||||
- เขียนโค้ดให้อ่านง่าย, ใส่ path/filename ในบรรทัดแรกของโค้ด
|
||||
- การอัพเดทโค้ด ให้แก้ไขจากต้นฉบับเป็น โค้ดที่สมบูรณ์
|
||||
- เขียน documentation สำหรับ function สำคัญ
|
||||
- ใช้ข้อมูลพื้นฐานที่ให้มา (Basic data) เพื่อให้คำแนะนำและโค้ดที่สอดคล้องกับเอกสารโครงการ
|
||||
- ต้องตรวจสอบกับ 8_lcbp3_v1_4_4_seed.sql ก่อน สร้างข้อมูลสมมุติ (Mockup data) ถ้ามีข้อมูลอยู่แล้วให้ใช้ข้อมูลที่มีอยู่
|
||||
|
||||
# เป้าหมายและจุดประสงค์:
|
||||
|
||||
- ให้ความช่วยเหลือผู้ใช้ในงานที่เกี่ยวข้องกับการพัฒนาซอฟต์แวร์ โดยเฉพาะอย่างยิ่งในส่วนของ JavaScript (NodeJS, NestJS, NextJS) และฐานข้อมูล (SQL, Relational Databases)
|
||||
- ให้คำปรึกษาเกี่ยวกับการจัดการข้อมูล, การออกแบบฐานข้อมูลเชิงสัมพันธ์, และการใช้โมเดลการควบคุมการเข้าถึง (RBAC, ABAC)
|
||||
- ให้คำแนะนำเกี่ยวกับการแก้ไขปัญหาหรือการพัฒนาซอฟต์แวร์
|
||||
- ช่วยเหลือในการวิเคราะห์และแก้ไขข้อผิดพลาด (debug และ error) ในโค้ดตามที่ผู้ใช้ระบุ
|
||||
|
||||
# พฤติกรรมและกฎเพิ่มเติม:
|
||||
|
||||
1. การเริ่มต้นและการโต้ตอบ:
|
||||
a) ทักทายผู้ใช้ด้วยภาษาไทยอย่างเป็นมิตร และสอบถามเกี่ยวกับปัญหาหรือความช่วยเหลือที่ต้องการในด้านการเขียนโปรแกรมหรือฐานข้อมูล
|
||||
b) ตอบคำถามทางเทคนิคอย่างแม่นยำและเป็นมืออาชีพ โดยใช้ศัพท์เฉพาะทางที่ถูกต้อง
|
||||
c) จำกัดจำนวนประโยคในการตอบกลับแต่ละครั้งให้กระชับและตรงประเด็นเพื่อความรวดเร็วในการสื่อสาร
|
||||
d) ให้วิเคราะห์ปัญกาอย่างละเอียดและรอบคอบ ไม่ต้องรีบตอบ
|
||||
|
||||
2. การจัดการโค้ดและข้อมูล:
|
||||
a) เมื่อผู้ใช้ขอให้อัพเดทโค้ด ให้ทำการแสดงโค้ดฉบับเสมบูรณ์ที่ได้รับการแก้ไขแล้ว
|
||||
b) ต้องแน่ใจว่าโค้ดที่สร้างขึ้นมานั้นอ่านง่ายและมี comments เป็นภาษาไทยตามที่ระบุใน rules
|
||||
c) สำหรับฟังก์ชันที่มีความซับซ้อนหรือมีความสำคัญต่อระบบ ต้องเขียน documentation อธิบายวัตถุประสงค์, พารามิเตอร์, และผลลัพธ์ของฟังก์ชันนั้นๆ ด้วยภาษาไทย
|
||||
d) หากต้องอ้างอิงถึงโครงสร้างข้อมูลหรือข้อกำหนดใดๆ ให้ตรวจสอบจากไฟล์ Basic data ที่ผู้ใช้ให้มาก่อนเสมอ ถ้าไม่พบ ให้แจ้งผู้ใช้ทราบ
|
||||
e) ถ้ามีการอ้างอิงถึงโค้ดที่อยู่ใน Phase หรือ Task ก่อนหน้า ให้สอบถามผู้ใช้เพื่อให้ upload ไฟล์โค้ดที่อ้างอิง (ไม่สร้างใหม่ เพื่อประหยัดเวลา)
|
||||
|
||||
3. โทนโดยรวม:
|
||||
- ใช้ภาษาไทยในการสื่อสารเป็นหลัก ยกเว้นศัพท์เทคนิค
|
||||
- มีความมั่นใจและแสดงออกถึงความเชี่ยวชาญในฐานะโปรแกรมเมอร์ผู้เชี่ยวชาญ
|
||||
- มีความเป็นระเบียบและให้คำแนะนำที่เป็นขั้นตอน
|
||||
@@ -1,162 +0,0 @@
|
||||
# **🌳 โครงสร้างไฟล์และโฟลเดอร์ทั้งหมด \- DMS v1.3.0 Backend**
|
||||
|
||||
DMS\_Backend\_Project/
|
||||
├── Dockerfile
|
||||
├── docker-compose.yml
|
||||
├── nest-cli.json
|
||||
├── package.json
|
||||
├── tsconfig.json
|
||||
└── src/
|
||||
├── app.module.ts
|
||||
├── main.ts
|
||||
├── common/
|
||||
│ ├── common.module.ts
|
||||
│ ├── audit-log/
|
||||
│ │ ├── audit-log.interceptor.ts
|
||||
│ │ └── audit-log.service.ts
|
||||
│ ├── auth/
|
||||
│ │ ├── auth.controller.ts
|
||||
│ │ ├── auth.module.ts
|
||||
│ │ ├── auth.service.spec.ts
|
||||
│ │ ├── auth.service.ts
|
||||
│ │ ├── decorators/
|
||||
│ │ │ ├── current-user.decorator.ts
|
||||
│ │ │ └── require-permission.decorator.ts
|
||||
│ │ ├── dto/
|
||||
│ │ │ └── login.dto.ts
|
||||
│ │ ├── guards/
|
||||
│ │ │ ├── rbac.guard.spec.ts
|
||||
│ │ │ └── rbac.guard.ts
|
||||
│ │ └── strategies/
|
||||
│ │ └── jwt.strategy.ts
|
||||
│ ├── config/
|
||||
│ │ └── typeorm.config.ts (ตามที่ระบุใน app.module)
|
||||
│ ├── entities/
|
||||
│ │ ├── attachment.entity.ts
|
||||
│ │ ├── audit-log.entity.ts
|
||||
│ │ ├── circulation-action.entity.ts
|
||||
│ │ ├── circulation-assignee.entity.ts
|
||||
│ │ ├── circulation-attachment.entity.ts
|
||||
│ │ ├── circulation-recipient.entity.ts
|
||||
│ │ ├── circulation-status-code.entity.ts
|
||||
│ │ ├── circulation.entity.ts
|
||||
│ │ ├── contract-drawing-attachment.entity.ts
|
||||
│ │ ├── contract-drawing-category.entity.ts
|
||||
│ │ ├── contract-drawing-sub-category.entity.ts
|
||||
│ │ ├── contract-drawing-volume.entity.ts
|
||||
│ │ ├── contract-drawing.entity.ts
|
||||
│ │ ├── correspondence-attachment.entity.ts
|
||||
│ │ ├── correspondence-recipient.entity.ts
|
||||
│ │ ├── correspondence-revision.entity.ts
|
||||
│ │ ├── correspondence-status.entity.ts
|
||||
│ │ ├── correspondence-type.entity.ts
|
||||
│ │ ├── correspondence.entity.ts
|
||||
│ │ ├── document-number-counter.entity.ts
|
||||
│ │ ├── document-number-format.entity.ts
|
||||
│ │ ├── permission.entity.ts
|
||||
│ │ ├── rfa-approve-code.entity.ts
|
||||
│ │ ├── rfa-item.entity.ts
|
||||
│ │ ├── rfa-revision.entity.ts
|
||||
│ │ ├── rfa-status-code.entity.ts
|
||||
│ │ ├── rfa-type.entity.ts
|
||||
│ │ ├── rfa.entity.ts
|
||||
│ │ ├── role.entity.ts
|
||||
│ │ ├── shop-drawing-main-category.entity.ts
|
||||
│ │ ├── shop-drawing-revision-attachment.entity.ts
|
||||
│ │ ├── shop-drawing-revision-contract-ref.entity.ts
|
||||
│ │ ├── shop-drawing-revision.entity.ts
|
||||
│ │ ├── shop-drawing-sub-category.entity.ts
|
||||
│ │ ├── shop-drawing.entity.ts
|
||||
│ │ ├── tag.entity.ts
|
||||
│ │ ├── transmittal-item.entity.ts
|
||||
│ │ ├── transmittal.entity.ts
|
||||
│ │ ├── user.entity.ts
|
||||
│ │ └── views/
|
||||
│ │ ├── view-current-correspondence.entity.ts
|
||||
│ │ └── view-current-rfa.entity.ts
|
||||
│ ├── exceptions/
|
||||
│ │ └── http-exception.filter.ts
|
||||
│ ├── file-storage/
|
||||
│ │ ├── file-storage.service.ts
|
||||
│ │ └── file.controller.ts
|
||||
│ └── security/
|
||||
│ └── rate-limiter.module.ts
|
||||
└── modules/
|
||||
├── caching/
|
||||
│ └── caching.module.ts
|
||||
├── circulation/
|
||||
│ ├── circulation.controller.ts
|
||||
│ ├── circulation.module.ts
|
||||
│ ├── circulation.service.ts
|
||||
│ └── dto/
|
||||
│ ├── add-action.dto.ts
|
||||
│ ├── assignee.dto.ts
|
||||
│ ├── attachment.dto.ts
|
||||
│ ├── create-circulation.dto.ts
|
||||
│ └── recipient.dto.ts
|
||||
├── correspondence/
|
||||
│ ├── correspondence.controller.ts
|
||||
│ ├── correspondence.module.ts
|
||||
│ ├── correspondence.service.ts
|
||||
│ └── dto/
|
||||
│ ├── create-correspondence.dto.ts
|
||||
│ └── query-correspondence.dto.ts
|
||||
├── document-numbering/
|
||||
│ ├── admin-numbering.controller.ts
|
||||
│ ├── document-numbering.module.ts
|
||||
│ ├── document-numbering.service.ts
|
||||
│ └── dto/
|
||||
│ ├── admin-create-number-format.dto.ts
|
||||
│ └── admin-update-number-format.dto.ts
|
||||
├── drawing/
|
||||
│ ├── drawing.controller.ts
|
||||
│ ├── drawing.module.ts
|
||||
│ ├── drawing.service.ts
|
||||
│ └── dto/
|
||||
│ ├── attachment.dto.ts
|
||||
│ ├── create-contract-drawing.dto.ts
|
||||
│ ├── create-shop-drawing-revision.dto.ts
|
||||
│ └── create-shop-drawing.dto.ts
|
||||
├── health/
|
||||
│ ├── health.controller.ts
|
||||
│ └── health.module.ts
|
||||
├── master-data/
|
||||
│ ├── admin-master-data.controller.ts
|
||||
│ ├── master-data.module.ts
|
||||
│ ├── master-data.service.ts
|
||||
│ └── dto/
|
||||
│ └── create-tag.dto.ts
|
||||
├── notification/
|
||||
│ ├── notification.module.ts
|
||||
│ └── notification.service.ts
|
||||
├── project/
|
||||
│ └── (ยังไม่ได้สร้างไฟล์)
|
||||
├── rfa/
|
||||
│ ├── rfa.controller.ts
|
||||
│ ├── rfa.module.ts
|
||||
│ ├── rfa.service.ts
|
||||
│ └── dto/
|
||||
│ └── create-rfa.dto.ts
|
||||
├── search/
|
||||
│ ├── search.controller.ts
|
||||
│ ├── search.module.ts
|
||||
│ ├── search.service.ts
|
||||
│ └── dto/
|
||||
│ └── advanced-search.dto.ts
|
||||
├── transmittal/
|
||||
│ ├── transmittal.controller.ts
|
||||
│ ├── transmittal.module.ts
|
||||
│ ├── transmittal.service.ts
|
||||
│ └── dto/
|
||||
│ ├── create-transmittal-item.dto.ts
|
||||
│ └── create-transmittal.dto.ts
|
||||
└── user/
|
||||
├── admin-roles.controller.ts
|
||||
├── admin-users.controller.ts
|
||||
├── roles.service.ts
|
||||
├── user.module.ts
|
||||
├── user.service.ts
|
||||
└── dto/
|
||||
├── admin-assign-permissions.dto.ts
|
||||
├── admin-create-role.dto.ts
|
||||
└── admin-create-user.dto.ts
|
||||
@@ -1,100 +0,0 @@
|
||||
# **🗂️ สรุปโครงสร้างไฟล์และโฟลเดอร์ (Backend NestJS) \- DMS v1.3.0**
|
||||
|
||||
นี่คือภาพรวมสถาปัตยกรรมโฟลเดอร์ทั้งหมดของโปรเจกต์ NestJS API ที่เราได้พัฒนาขึ้น โดยแบ่งตามหลักการ Separation of Concerns
|
||||
|
||||
## **📂 (Root) โฟลเดอร์หลักของโปรเจกต์**
|
||||
|
||||
ไฟล์เหล่านี้จะอยู่ที่ระดับบนสุดของโปรเจกต์ (นอก src/)
|
||||
|
||||
* Dockerfile
|
||||
* **หน้าที่:** คำสั่งสำหรับ Build Docker Image (Multi-stage build) เพื่อนำไป Deploy
|
||||
* docker-compose.yml
|
||||
* **หน้าที่:** ไฟล์ตั้งค่าสำหรับ QNAP Container Station ใช้กำหนด Services, Networks, และ (สำคัญที่สุด) Environment Variables (เช่น DATABASE\_HOST, JWT\_SECRET)
|
||||
* package.json
|
||||
* **หน้าที่:** เก็บรายการ Dependencies ทั้งหมด (เช่น @nestjs/core, typeorm, bcrypt, @nestjs/elasticsearch)
|
||||
* .gitignore, tsconfig.json, nest-cli.json
|
||||
* **หน้าที่:** ไฟล์ตั้งค่าพื้นฐานของโปรเจกต์ TypeScript และ NestJS
|
||||
|
||||
## **📂 src/ (Source Code)**
|
||||
|
||||
นี่คือหัวใจหลักของแอปพลิเคชัน
|
||||
|
||||
### **1\. src/ (Root Files)**
|
||||
|
||||
* main.ts
|
||||
* **หน้าที่:** จุดเริ่มต้น (Entry Point) ของแอปพลิเคชัน
|
||||
* **การตั้งค่า:**
|
||||
* ใช้ helmet() (Security)
|
||||
* ใช้ RateLimiterGuard (Security)
|
||||
* ใช้ ValidationPipe (Global Pipe สำหรับ DTO)
|
||||
* ใช้ HttpExceptionFilter (Global Filter สำหรับ Error Handling)
|
||||
* ตั้งค่า SwaggerModule (สำหรับสร้าง API Docs)
|
||||
* ตั้งค่า Global Prefix (/api/v1)
|
||||
* app.module.ts
|
||||
* **หน้าที่:** โมดูลหลัก (Root Module)
|
||||
* **การตั้งค่า:**
|
||||
* Import ConfigModule (สำหรับ .env)
|
||||
* Import TypeOrmModule (เชื่อมต่อฐานข้อมูล)
|
||||
* **Import โมดูลหลักและโมดูล Feature ทั้งหมด** (เช่น CommonModule, UserModule, CorrespondenceModule, SearchModule ฯลฯ)
|
||||
|
||||
### **2\. src/common/ (The Foundation)**
|
||||
|
||||
โฟลเดอร์นี้คือ "รากฐาน" เก็บโค้ดที่โมดูลอื่นต้องใช้ร่วมกัน (Shared Logic)
|
||||
|
||||
* common.module.ts
|
||||
* **หน้าที่:** ไฟล์ที่รวบรวมและ export Service/Module ทั้งหมดใน common/ ให้โมดูลอื่นเรียกใช้ง่ายๆ
|
||||
* auth/
|
||||
* **หน้าที่:** จัดการการยืนยันตัวตน (Authentication) และสิทธิ์ (RBAC)
|
||||
* auth.module.ts, auth.service.ts, auth.controller.ts (สำหรับ /login, /me)
|
||||
* strategies/jwt.strategy.ts (ตรรกะการถอดรหัส JWT)
|
||||
* guards/rbac.guard.ts (ตัวตรวจสอบสิทธิ์ RBACGuard)
|
||||
* decorators/require-permission.decorator.ts (@RequirePermission)
|
||||
* decorators/current-user.decorator.ts (@CurrentUser)
|
||||
* entities/
|
||||
* **หน้าที่:** **ศูนย์รวม Entities ทั้งหมด (44+ ไฟล์)** ที่ Map กับตารางใน MariaDB
|
||||
* เช่น user.entity.ts, role.entity.ts, correspondence.entity.ts, rfa.entity.ts, shop-drawing.entity.ts, circulation.entity.ts, attachment.entity.ts, audit-log.entity.ts ฯลฯ
|
||||
* views/: โฟลเดอร์ย่อยสำหรับ View Entities (เช่น view-current-correspondence.entity.ts)
|
||||
* config/
|
||||
* **หน้าที่:** เก็บ Config ที่ซับซ้อน เช่น typeorm.config.ts (ตั้งค่าการเชื่อมต่อ TypeORM)
|
||||
* exceptions/
|
||||
* http-exception.filter.ts (Global Error Handler)
|
||||
* file-storage/
|
||||
* file-storage.service.ts (Logic การบันทึกไฟล์ลง Disk)
|
||||
* file.controller.ts (API Endpoint /files/upload)
|
||||
* audit-log/
|
||||
* audit-log.service.ts (Service บันทึก Log)
|
||||
* audit-log.interceptor.ts (Interceptor ดักจับการกระทำ)
|
||||
* security/
|
||||
* rate-limiter.module.ts (ตั้งค่า Rate Limiting)
|
||||
|
||||
### **3\. src/modules/ (The Features)**
|
||||
|
||||
โฟลเดอร์นี้เก็บ "Business Logic" โดยแยกเป็น Feature Modules อย่างชัดเจน
|
||||
|
||||
* user/ (Phase 1\)
|
||||
* **หน้าที่:** API สำหรับ Admin Panel (จัดการ Users, Roles, Permissions)
|
||||
* admin-users.controller.ts, admin-roles.controller.ts, user.service.ts, roles.service.ts
|
||||
* project/ (Phase 2\)
|
||||
* **หน้าที่:** API สำหรับจัดการโปรเจกต์ (ยังไม่ได้สร้างไฟล์ แต่มีในแผน)
|
||||
* document-numbering/ (Phase 2\)
|
||||
* **หน้าที่:** API สำหรับ Admin (ตั้งค่า Format เลขที่) และ Service (generateNextDocumentNumber)
|
||||
* master-data/ (Phase 2\)
|
||||
* **หน้าที่:** API สำหรับ Admin (จัดการ Master Data เช่น Tags, RFA Types, Corr. Types)
|
||||
* correspondence/ (Phase 2\)
|
||||
* **หน้าที่:** Logic หลักในการสร้าง/ค้นหา เอกสารโต้ตอบ
|
||||
* rfa/ (Phase 3\)
|
||||
* **หน้าที่:** Logic หลักในการสร้าง/ค้นหา เอกสารขออนุมัติ (RFA)
|
||||
* drawing/ (Phase 3\)
|
||||
* **หน้าที่:** Logic หลักในการสร้าง/ค้นหา แบบแปลน (Contract & Shop Drawing)
|
||||
* circulation/ (Phase 3\)
|
||||
* **หน้าที่:** Logic หลักในการสร้าง/ค้นหา ใบเวียนภายใน
|
||||
* transmittal/ (Phase 3\)
|
||||
* **หน้าที่:** Logic หลักในการสร้าง/ค้นหา เอกสารนำส่ง
|
||||
* search/ (Phase 4\)
|
||||
* **หน้าที่:** API (/search) และ Service ที่เชื่อมต่อกับ Elasticsearch
|
||||
* notification/ (Phase 4\)
|
||||
* **หน้าที่:** Service สำหรับยิง Webhook ไปยัง N8N
|
||||
* caching/ (Phase 4\)
|
||||
* **หน้าที่:** โมดูลตั้งค่า Caching (Global)
|
||||
* health/ (Phase 5 \- Deploy)
|
||||
* **หน้าที่:** API (/health) สำหรับ Health Check
|
||||
436
docs/T0-T6.2.md
436
docs/T0-T6.2.md
@@ -1,436 +0,0 @@
|
||||
# โครงสร้างโฟลเดอร์และไฟล์ทั้งหมดสำหรับ **Backend (NestJS)** ตามแผนงาน **LCBP3-DMS v1.4.3** ตั้งแต่ Phase 0 ถึง Phase 6 (T0-T6.2) ที่ได้ดำเนินการไปแล้ว
|
||||
|
||||
โครงสร้างนี้ออกแบบตามหลัก **Domain-Driven Design** และ **Modular Architecture** ที่ระบุไว้ในแผนพัฒนา
|
||||
|
||||
---
|
||||
|
||||
## 📂 **backend/** (Backend Application)
|
||||
|
||||
* [x] `.env` (สำหรับ Local Dev เท่านั้น ห้าม commit)
|
||||
* [x] `.gitignore`
|
||||
* [x] `docker-compose.yml` (Configuration หลักสำหรับ Deploy)
|
||||
* [x] `docker-compose.override.yml` (สำหรับ Inject Secrets ตอน Dev)
|
||||
* [x] `package.json`
|
||||
* [x] `pnpm-lock.yaml`
|
||||
* [x] `tsconfig.json`
|
||||
* [x] `nest-cli.json`
|
||||
* [x] `README.md`
|
||||
|
||||
---
|
||||
|
||||
## 📂 **backend/src/** (Source Code)
|
||||
|
||||
### **📄 Entry Points**
|
||||
|
||||
* [x] `main.ts` (Application Bootstrap, Swagger, Global Pipes)
|
||||
* [x] `app.module.ts` (Root Module ที่รวมทุก Modules เข้าด้วยกัน)
|
||||
|
||||
### **📁 src/common/** (Shared Resources)
|
||||
|
||||
* [x] **common.module.ts**
|
||||
* **auth/**
|
||||
* **dto/**
|
||||
* [x] **login.dto.ts**
|
||||
* [x] **register.dto.ts**
|
||||
* [x] **auth.controller.spec.ts**
|
||||
* [x] **auth.controller.ts**
|
||||
* [x] **auth.module.ts**
|
||||
* [x] **auth.service.spec.ts**
|
||||
* [x] **auth.service.ts**
|
||||
* **config/** (Configuration Service)
|
||||
* [x] **env.validation.ts**
|
||||
* **decorators/**
|
||||
* [x] **audit.decorator.ts**
|
||||
* [x] `current-user.decorator.ts`
|
||||
* [x] `require-permission.decorator.ts`
|
||||
* **entities/**
|
||||
* [x] **audit-log.entity.ts**
|
||||
* [x] **base.entity.ts**
|
||||
* **exceptions/**
|
||||
* [x] `http-exception.filter.ts` (Global Filter)
|
||||
* **file-storage/** (Two-Phase Storage System)
|
||||
* **entities/**
|
||||
* [x] **attachment.entity.ts**
|
||||
* [x] **file-storage.controller.spec.ts**
|
||||
* [x] **file-storage.controller.ts**
|
||||
* [x] **file-storage.module.ts**
|
||||
* [x] **file-storage.service.spec.ts**
|
||||
* [x] `file-storage.service.ts` (Upload, Scan Virus, Commit)
|
||||
* [x] `guards/`
|
||||
* [x] `jwt-auth.guard.ts`
|
||||
* [x] **jwt.strategy.ts**
|
||||
* [x] `rbac.guard.ts` (ตรวจสอบสิทธิ์ 4 ระดับ)
|
||||
* **interceptors/**
|
||||
* [x] `audit-log.interceptor.ts` (เก็บ Log ลง DB)
|
||||
* [x] `transform.interceptor.ts` (Standard Response Format)
|
||||
* **resilience/** (Circuit Breaker & Retry)
|
||||
|
||||
### **📁 src/modules/** (Feature Modules)
|
||||
|
||||
1. **user/** (User Management & RBAC)
|
||||
* [x] `dto/`
|
||||
* [x] **assign-user-role.dto.ts**
|
||||
* [x] `create-user.dto.ts`
|
||||
* [x] `update-user.dto.ts`
|
||||
* [x] `entities/`
|
||||
* [x] `user.entity.ts`
|
||||
* [x] `role.entity.ts`
|
||||
* [x] `permission.entity.ts`
|
||||
* [x] `user-preference.entity.ts`
|
||||
* [x] **user-assignment.service.ts**
|
||||
* [x] `user.controller.ts`
|
||||
* [x] `user.module.ts`
|
||||
* [x] `user.service.ts`
|
||||
* [x] **user.service.spec.ts**
|
||||
|
||||
2. **project/** (Project Structure)
|
||||
* [x] `dto/`
|
||||
* [x] **create-project.dto.ts**
|
||||
* [x] `search-project.dto.ts`
|
||||
* [x] `update-project.dto.ts`
|
||||
* [x] `entities/`
|
||||
* [x] `contract-organization.entity.ts`
|
||||
* [x] `contract.entity.ts`
|
||||
* [x] `organization.entity.ts`
|
||||
* [x] `project-organization.entity.ts` (Junction)
|
||||
* [x] **project.entity.ts**
|
||||
* [x] **project.controller.spec.ts**
|
||||
* [x] `project.controller.ts`
|
||||
* [x] `project.module.ts`
|
||||
* [x] **project.service.spec.ts**
|
||||
* [x] `project.service.ts`
|
||||
|
||||
3. **correspondence/** (Core Document System)
|
||||
* [x] `dto/`
|
||||
* [x] `add-reference.dto.ts`
|
||||
* [x] `create-correspondence.dto.ts`
|
||||
* [x] `search-correspondence.dto.ts`
|
||||
* [x] **submit-correspondence.dto.ts**
|
||||
* [x] `workflow-action.dto.ts`
|
||||
* [x] `entities/`
|
||||
* [x] `correspondence-reference.entity.ts`
|
||||
* [x] `correspondence-revision.entity.ts`
|
||||
* [x] `correspondence-routing.entity.ts` (Unified Workflow)
|
||||
* [x] `correspondence-status.entity.ts`
|
||||
* [x] `correspondence-type.entity.ts`
|
||||
* [x] `correspondence.entity.ts`
|
||||
* [x] **routing-template-step.entity.ts**
|
||||
* [x] `routing-template.entity.ts`
|
||||
* [x] **correspondence.controller.spec.ts**
|
||||
* [x] `correspondence.controller.ts`
|
||||
* [x] `correspondence.module.ts`
|
||||
* [x] **correspondence.service.spec.ts**
|
||||
* [x] `correspondence.service.ts` (Impersonation & Workflow Logic)
|
||||
|
||||
4. **drawing/** (Contract & Shop Drawings)
|
||||
* [x] `dto/`
|
||||
* [x] `create-contract-drawing.dto.ts`
|
||||
* [x] `create-shop-drawing-revision.dto.ts`
|
||||
* [x] `create-shop-drawing.dto.ts`
|
||||
* [x] `search-contract-drawing.dto.ts`
|
||||
* [x] `search-shop-drawing.dto.ts`
|
||||
* [x] `update-contract-drawing.dto.ts`
|
||||
* [x] `entities/`
|
||||
* [x] `contract-drawing-sub-category.entity.ts`
|
||||
* [x] `contract-drawing-volume.entity.ts`
|
||||
* [x] `contract-drawing.entity.ts`
|
||||
* [x] `shop-drawing-main-category.entity.ts`
|
||||
* [x] `shop-drawing-revision.entity.ts`
|
||||
* [x] `shop-drawing-sub-category.entity.ts`
|
||||
* [x] `shop-drawing.entity.ts`
|
||||
* [x] `contract-drawing.controller.ts`
|
||||
* [x] `contract-drawing.service.ts`
|
||||
* [x] `drawing-master-data.controller.ts`
|
||||
* [x] `drawing-master-data.service.ts`
|
||||
* [x] `drawing.module.ts`
|
||||
* [x] `shop-drawing.controller.ts`
|
||||
* [x] `shop-drawing.service.ts`
|
||||
|
||||
4. **rfa/** (Request for Approval & Advanced Workflow)
|
||||
* [x] `dto/`
|
||||
* [x] `create-rfa.dto.ts`
|
||||
* [x] `search-rfa.dto.ts`
|
||||
* [x] `update-rfa.dto.ts`
|
||||
* [x] `entities/`
|
||||
* [x] `rfa-approve-code.entity.ts`
|
||||
* [x] `rfa-item.entity.ts`
|
||||
* [x] `rfa-revision.entity.ts`
|
||||
* [x] `rfa-status-code.entity.ts`
|
||||
* [x] `rfa-type.entity.ts`
|
||||
* [x] `rfa-workflow-template-step.entity.ts`
|
||||
* [x] `rfa-workflow-template.entity.ts`
|
||||
* [x] `rfa-workflow.entity.ts`
|
||||
* [x] `rfa.entity.ts`
|
||||
* [x] `rfa.controller.ts`
|
||||
* [x] `rfa.module.ts`
|
||||
* [x] `rfa.service.ts` (Unified Workflow Integration)
|
||||
|
||||
5. **circulation/** (Internal Routing)
|
||||
* [x] `dto/`
|
||||
* [x] `create-circulation.dto.ts`
|
||||
* [x] `update-circulation-routing.dto.ts`
|
||||
* [x] `search-circulation.dto.ts`
|
||||
* [x] `entities/`
|
||||
* [x] `circulation-routing.entity.ts`
|
||||
* [x] `circulation-status-code.entity.ts`
|
||||
* [x] `circulation.entity.ts`
|
||||
* [x] `circulation.controller.ts`
|
||||
* [x] `circulation.module.ts`
|
||||
* [x] `circulation.service.ts`
|
||||
|
||||
6. **transmittal/** (Document Forwarding)
|
||||
* [x] `dto/`
|
||||
* [x] `create-transmittal.dto.ts`
|
||||
* [x] `search-transmittal.dto.ts`
|
||||
* [x] **update-transmittal.dto.ts**
|
||||
* [x] `entities/`
|
||||
* [x] `transmittal-item.entity.ts`
|
||||
* [x] `transmittal.entity.ts`
|
||||
* [x] `transmittal.controller.ts`
|
||||
* [x] `transmittal.module.ts`
|
||||
* [x] `transmittal.service.ts`
|
||||
|
||||
7. **notification/** (System Alerts)
|
||||
* [x] `dto/`
|
||||
* [x] `create-notification.dto.ts`
|
||||
* [x] `search-notification.dto.ts`
|
||||
* [x] `entities/`
|
||||
* [x] `notification.entity.ts`
|
||||
* [x] `notification-cleanup.service.ts` (Cron Job)
|
||||
* [x] `notification.controller.ts`
|
||||
* [x] `notification.gateway.ts`
|
||||
* [x] `notification.module.ts` (Real-time WebSocket)
|
||||
* [x] `notification.processor.ts` (Consumer/Worker for Email & Line)
|
||||
* [x] `notification.service.ts` (Producer)
|
||||
|
||||
8. **search/** (Elasticsearch)
|
||||
* [x] `dto/search-query.dto.ts`
|
||||
* [x] `search.controller.ts`
|
||||
* [x] `search.module.ts`
|
||||
* [x] `search.service.ts` (Indexing & Searching)
|
||||
|
||||
9. **document-numbering/** (Internal Service)
|
||||
* [x] `entities/`
|
||||
* [x] `document-number-format.entity.ts`
|
||||
* [x] `document-number-counter.entity.ts`
|
||||
* [x] `document-numbering.module.ts`
|
||||
* [x] **document-numbering.service.spec.ts**
|
||||
* [x] `document-numbering.service.ts` (Double-Lock Mechanism)
|
||||
|
||||
10. **workflow-engine/** (Unified Logic)
|
||||
* [x] `interfaces/workflow.interface.ts`
|
||||
* [x] `workflow-engine.module.ts`
|
||||
* [x] **workflow-engine.service.spec.ts**
|
||||
* [x] `workflow-engine.service.ts` (State Machine Logic)
|
||||
|
||||
11. **json-schema/** (Validation)
|
||||
* [x] `dto/`
|
||||
* [x] `create-json-schema.dto.ts`+
|
||||
* [x] `search-json-schema.dto.ts`
|
||||
* [x] `update-json-schema.dto.ts`
|
||||
* [x] `entities/`
|
||||
* [x] `json-schema.entity.ts`
|
||||
* [x] **json-schema.controller.spec.ts**
|
||||
* [x] **json-schema.controller.ts**
|
||||
* [x] `json-schema.module.ts`
|
||||
* [x] **json-schema.service.spec.ts**
|
||||
* [x] `json-schema.service.ts`
|
||||
|
||||
---
|
||||
|
||||
นี่คือโครงสร้าง **Folder Structure** ของโปรเจกต์ Backend (NestJS) ฉบับสมบูรณ์ ที่รวบรวมจาก **Requirements**, **FullStackJS Guidelines**, **Data Dictionary** และสิ่งที่เราได้ **Implement ไปแล้วตั้งแต่ Phase 0 ถึง Phase 6.2** ครับ
|
||||
|
||||
โครงสร้างนี้เป็นแบบ **Domain-Driven Modular Architecture** ครับ
|
||||
|
||||
```text
|
||||
lcbp3-backend/
|
||||
├── .env # Environment variables (Local Dev)
|
||||
├── .gitignore
|
||||
├── docker-compose.yml # Main config for services (DB, Redis, ES, App)
|
||||
├── docker-compose.override.yml # Secrets injection for Dev
|
||||
├── nest-cli.json
|
||||
├── package.json
|
||||
├── pnpm-lock.yaml
|
||||
├── README.md
|
||||
├── tsconfig.json
|
||||
│
|
||||
└── src/
|
||||
├── main.ts # Entry point (Swagger, Helmet, Global Pipes)
|
||||
├── app.module.ts # Root Module
|
||||
│
|
||||
├── common/ # 🛠️ Shared Resources
|
||||
│ ├── auth/
|
||||
│ │ ├── guards/
|
||||
│ │ │ ├── jwt-auth.guard.ts
|
||||
│ │ │ └── rbac.guard.ts
|
||||
│ │ └── strategies/
|
||||
│ │ └── jwt.strategy.ts
|
||||
│ ├── config/ # Config Service
|
||||
│ ├── decorators/
|
||||
│ │ ├── current-user.decorator.ts
|
||||
│ │ └── require-permission.decorator.ts
|
||||
│ ├── exceptions/
|
||||
│ │ └── http-exception.filter.ts
|
||||
│ ├── file-storage/ # 📂 Two-Phase Storage System
|
||||
│ │ ├── dto/
|
||||
│ │ ├── entities/
|
||||
│ │ │ └── attachment.entity.ts
|
||||
│ │ ├── file-storage.module.ts
|
||||
│ │ └── file-storage.service.ts
|
||||
│ ├── interceptors/
|
||||
│ │ ├── audit-log.interceptor.ts
|
||||
│ │ └── transform.interceptor.ts
|
||||
│ └── resilience/ # Circuit Breaker & Retry logic
|
||||
│
|
||||
└── modules/ # 📦 Feature Modules
|
||||
│
|
||||
├── user/ # 👤 User & RBAC
|
||||
│ ├── dto/
|
||||
│ │ ├── create-user.dto.ts
|
||||
│ │ └── update-user.dto.ts
|
||||
│ ├── entities/
|
||||
│ │ ├── permission.entity.ts
|
||||
│ │ ├── role.entity.ts
|
||||
│ │ ├── user-preference.entity.ts
|
||||
│ │ └── user.entity.ts
|
||||
│ ├── user.controller.ts
|
||||
│ ├── user.module.ts
|
||||
│ └── user.service.ts
|
||||
│
|
||||
├── project/ # 🏢 Projects & Organizations
|
||||
│ ├── dto/
|
||||
│ │ ├── create-project.dto.ts
|
||||
│ │ ├── search-project.dto.ts
|
||||
│ │ └── update-project.dto.ts
|
||||
│ ├── entities/
|
||||
│ │ ├── contract-organization.entity.ts
|
||||
│ │ ├── contract.entity.ts
|
||||
│ │ ├── organization.entity.ts
|
||||
│ │ ├── project-organization.entity.ts
|
||||
│ │ └── project.entity.ts
|
||||
│ ├── project.controller.ts
|
||||
│ ├── project.module.ts
|
||||
│ └── project.service.ts
|
||||
│
|
||||
├── correspondence/ # ✉️ Core Document
|
||||
│ ├── dto/
|
||||
│ │ ├── add-reference.dto.ts
|
||||
│ │ ├── create-correspondence.dto.ts
|
||||
│ │ ├── search-correspondence.dto.ts
|
||||
│ │ └── workflow-action.dto.ts
|
||||
│ ├── entities/
|
||||
│ │ ├── correspondence-reference.entity.ts
|
||||
│ │ ├── correspondence-revision.entity.ts
|
||||
│ │ ├── correspondence-routing.entity.ts
|
||||
│ │ ├── correspondence-status.entity.ts
|
||||
│ │ ├── correspondence-type.entity.ts
|
||||
│ │ ├── correspondence.entity.ts
|
||||
│ │ └── routing-template.entity.ts
|
||||
│ ├── correspondence.controller.ts
|
||||
│ ├── correspondence.module.ts
|
||||
│ └── correspondence.service.ts
|
||||
│
|
||||
├── drawing/ # 📐 Shop & Contract Drawings
|
||||
│ ├── dto/
|
||||
│ │ ├── create-contract-drawing.dto.ts
|
||||
│ │ ├── create-shop-drawing-revision.dto.ts
|
||||
│ │ ├── create-shop-drawing.dto.ts
|
||||
│ │ ├── search-contract-drawing.dto.ts
|
||||
│ │ ├── search-shop-drawing.dto.ts
|
||||
│ │ └── update-contract-drawing.dto.ts
|
||||
│ ├── entities/
|
||||
│ │ ├── contract-drawing-sub-category.entity.ts
|
||||
│ │ ├── contract-drawing-volume.entity.ts
|
||||
│ │ ├── contract-drawing.entity.ts
|
||||
│ │ ├── shop-drawing-main-category.entity.ts
|
||||
│ │ ├── shop-drawing-revision.entity.ts
|
||||
│ │ ├── shop-drawing-sub-category.entity.ts
|
||||
│ │ └── shop-drawing.entity.ts
|
||||
│ ├── contract-drawing.controller.ts
|
||||
│ ├── contract-drawing.service.ts
|
||||
│ ├── drawing-master-data.controller.ts
|
||||
│ ├── drawing-master-data.service.ts
|
||||
│ ├── drawing.module.ts
|
||||
│ ├── shop-drawing.controller.ts
|
||||
│ └── shop-drawing.service.ts
|
||||
│
|
||||
├── rfa/ # ✅ Request for Approval
|
||||
│ ├── dto/
|
||||
│ │ ├── create-rfa.dto.ts
|
||||
│ │ ├── search-rfa.dto.ts
|
||||
│ │ └── update-rfa.dto.ts
|
||||
│ ├── entities/
|
||||
│ │ ├── rfa-approve-code.entity.ts
|
||||
│ │ ├── rfa-item.entity.ts
|
||||
│ │ ├── rfa-revision.entity.ts
|
||||
│ │ ├── rfa-status-code.entity.ts
|
||||
│ │ ├── rfa-type.entity.ts
|
||||
│ │ ├── rfa-workflow-template-step.entity.ts
|
||||
│ │ ├── rfa-workflow-template.entity.ts
|
||||
│ │ ├── rfa-workflow.entity.ts
|
||||
│ │ └── rfa.entity.ts
|
||||
│ ├── rfa.controller.ts
|
||||
│ ├── rfa.module.ts
|
||||
│ └── rfa.service.ts
|
||||
│
|
||||
├── circulation/ # 🔄 Internal Routing
|
||||
│ ├── dto/
|
||||
│ │ ├── create-circulation.dto.ts
|
||||
│ │ ├── search-circulation.dto.ts
|
||||
│ │ └── update-circulation-routing.dto.ts
|
||||
│ ├── entities/
|
||||
│ │ ├── circulation-routing.entity.ts
|
||||
│ │ ├── circulation-status-code.entity.ts
|
||||
│ │ └── circulation.entity.ts
|
||||
│ ├── circulation.controller.ts
|
||||
│ ├── circulation.module.ts
|
||||
│ └── circulation.service.ts
|
||||
│
|
||||
├── transmittal/ # 📤 Outgoing Documents
|
||||
│ ├── dto/
|
||||
│ │ ├── create-transmittal.dto.ts
|
||||
│ │ └── search-transmittal.dto.ts
|
||||
│ ├── entities/
|
||||
│ │ ├── transmittal-item.entity.ts
|
||||
│ │ └── transmittal.entity.ts
|
||||
│ ├── transmittal.controller.ts
|
||||
│ ├── transmittal.module.ts
|
||||
│ └── transmittal.service.ts
|
||||
│
|
||||
├── notification/ # 🔔 Real-time & Queue
|
||||
│ ├── dto/
|
||||
│ │ ├── create-notification.dto.ts
|
||||
│ │ └── search-notification.dto.ts
|
||||
│ ├── entities/
|
||||
│ │ └── notification.entity.ts
|
||||
│ ├── notification-cleanup.service.ts
|
||||
│ ├── notification.controller.ts
|
||||
│ ├── notification.gateway.ts
|
||||
│ ├── notification.module.ts
|
||||
│ ├── notification.processor.ts
|
||||
│ └── notification.service.ts
|
||||
│
|
||||
├── search/ # 🔍 Elasticsearch
|
||||
│ ├── dto/
|
||||
│ │ └── search-query.dto.ts
|
||||
│ ├── search.controller.ts
|
||||
│ ├── search.module.ts
|
||||
│ └── search.service.ts
|
||||
│
|
||||
├── document-numbering/ # 🔢 Internal Numbering Service
|
||||
│ ├── entities/
|
||||
│ │ ├── document-number-counter.entity.ts
|
||||
│ │ └── document-number-format.entity.ts
|
||||
│ ├── document-numbering.module.ts
|
||||
│ └── document-numbering.service.ts
|
||||
│
|
||||
├── workflow-engine/ # ⚙️ Unified Logic
|
||||
│ ├── interfaces/
|
||||
│ │ └── workflow.interface.ts
|
||||
│ ├── workflow-engine.module.ts
|
||||
│ └── workflow-engine.service.ts
|
||||
│
|
||||
└── json-schema/ # 📋 Validation Logic
|
||||
├── json-schema.module.ts
|
||||
└── json-schema.service.ts
|
||||
```
|
||||
367
docs/prompt.md
367
docs/prompt.md
@@ -4,7 +4,7 @@
|
||||
|
||||
## VSCode Shortcut
|
||||
|
||||
Markdown preview Ctrl+Shift+V
|
||||
Markdown preview Ctrl+Shift+V
|
||||
|
||||
## สร้างโครงสร้างโฟลเดอร์สำหรับ lcbp3-backend
|
||||
|
||||
@@ -80,14 +80,16 @@ git push -u origin main
|
||||
|
||||
## **สร้าง NestJS Project ใหม่**
|
||||
|
||||
* ขั้นตอนที่ 1: ติดตั้ง NestJS CLI (ถ้ายังไม่ได้ติดตั้ง)
|
||||
* npm install -g @nestjs/cli
|
||||
- ขั้นตอนที่ 1: ติดตั้ง NestJS CLI (ถ้ายังไม่ได้ติดตั้ง)
|
||||
|
||||
* ขั้นตอนที่ 2: สร้างโปรเจกต์ใหม่
|
||||
* nest new backend
|
||||
* nest new . /อยู่ในโฟลเดอร์ที่สร้างไว้แล้ว และต้องการสร้างโปรเจกต์ลงในโฟลเดอร์นั้นโดยตรง:
|
||||
- npm install -g @nestjs/cli
|
||||
|
||||
* ขั้นตอนที่ 3: ติดตั้ง Dependencies เพิ่มเติมสำหรับ DMS
|
||||
- ขั้นตอนที่ 2: สร้างโปรเจกต์ใหม่
|
||||
|
||||
- nest new backend
|
||||
- nest new . /อยู่ในโฟลเดอร์ที่สร้างไว้แล้ว และต้องการสร้างโปรเจกต์ลงในโฟลเดอร์นั้นโดยตรง:
|
||||
|
||||
- ขั้นตอนที่ 3: ติดตั้ง Dependencies เพิ่มเติมสำหรับ DMS
|
||||
|
||||
```bash
|
||||
# Core & Database
|
||||
@@ -123,302 +125,143 @@ npm install --save-dev @nestjs/testing jest @types/jest @types/passport-jwt @typ
|
||||
การควบคุมพฤติกรรมของผมต้องทำผ่าน **"คำสั่ง (Prompt)"** ที่คุณพิมพ์เข้ามาเท่านั้นครับ เพื่อป้องกันไม่ให้เกิดเหตุการณ์แบบนี้อีก คุณสามารถใช้เทคนิคการสั่งงานดังนี้เพื่อ **"บังคับ"** ให้ผมตรวจสอบข้อมูลครับ:
|
||||
|
||||
1. **สั่งให้ "อ่านและสรุปก่อน" (Verify First):**
|
||||
* ก่อนให้ผมเขียนโค้ด ให้สั่งว่า *"ช่วยอ่านไฟล์ `01_lcbp3_v1_4_3.sql` แล้วสรุปโครงสร้างตาราง audit_logs ที่มีอยู่จริงให้ดูก่อน"*
|
||||
* วิธีนี้จะบังคับให้ผมต้องไปดึงข้อมูลจากไฟล์มาประมวลผลก่อนที่จะเริ่ม "จินตนาการ" หรือเขียนโค้ดใหม่ครับ
|
||||
|
||||
- ก่อนให้ผมเขียนโค้ด ให้สั่งว่า _"ช่วยอ่านไฟล์ `01_lcbp3_v1_4_3.sql` แล้วสรุปโครงสร้างตาราง audit_logs ที่มีอยู่จริงให้ดูก่อน"_
|
||||
- วิธีนี้จะบังคับให้ผมต้องไปดึงข้อมูลจากไฟล์มาประมวลผลก่อนที่จะเริ่ม "จินตนาการ" หรือเขียนโค้ดใหม่ครับ
|
||||
|
||||
2. **ใช้คำสั่ง "ห้ามเดา" (Strict Constraints):**
|
||||
* เติมประโยคท้ายคำสั่งว่า *"ห้ามใช้โครงสร้างสมมติ ให้ยึดตามไฟล์แนบ `ชื่อไฟล์` เท่านั้น หากไม่เจอข้อมูลให้ถามกลับ"*
|
||||
|
||||
- เติมประโยคท้ายคำสั่งว่า _"ห้ามใช้โครงสร้างสมมติ ให้ยึดตามไฟล์แนบ `ชื่อไฟล์` เท่านั้น หากไม่เจอข้อมูลให้ถามกลับ"_
|
||||
|
||||
3. **อ้างอิงชื่อไฟล์ชัดเจน:**
|
||||
* ระบุชื่อไฟล์ในคำสั่งเสมอ เช่น *"อ้างอิงจาก `Backend Plan`..."* หรือ *"ตาม `Data Dictionary`..."* (ซึ่งคุณทำได้ดีแล้วครับ แต่ผมประมวลผลพลาดเองในรอบนี้)
|
||||
- ระบุชื่อไฟล์ในคำสั่งเสมอ เช่น _"อ้างอิงจาก `Backend Plan`..."_ หรือ _"ตาม `Data Dictionary`..."_ (ซึ่งคุณทำได้ดีแล้วครับ แต่ผมประมวลผลพลาดเองในรอบนี้)
|
||||
|
||||
**สรุป:**
|
||||
คุณไม่ต้องไปตั้งค่าที่ไหนครับ แต่ใช้การ **"สั่งให้ผมทวนข้อมูลจากไฟล์"** เป็นขั้นตอนแรกก่อนเริ่มงาน จะช่วยลดโอกาสผิดพลาดได้มากที่สุดครับ
|
||||
|
||||
ตอนนี้ผมพร้อมทำงานต่อโดยยึดข้อมูลจากไฟล์ `01_lcbp3_v1_4_3.sql` และไฟล์อื่นๆ ที่คุณให้มาอย่างเคร่งครัดครับ มีส่วนไหนให้ผมดำเนินการต่อไหมครับ?
|
||||
|
||||
ช่วยอ่านไฟล์ 2_Backend_Plan_V1_4_4.md ในหัวข้อ T2.5.1-T2.5.9 และ โครงสร้างโมดูล แล้วสรุป รายการที่ต้องการสร้างหรือแก้ไขโมดูล ในแต่ละข้อก่อนเริ่มดำเนินการต่อครับ
|
||||
|
||||
สวัสดีครับ ผมได้ทำการตรวจสอบและวิเคราะห์ความครบถ้วนของไฟล์เอกสารแผนงาน (Plans), โครงสร้างฐานข้อมูล (SQL), และโครงสร้างไฟล์ (File Structure) เทียบกับ Requirements ของ **LCBP3-DMS v1.4.3** เรียบร้อยแล้วครับ
|
||||
สวัสดีครับ ผมได้วิเคราะห์ไฟล์ **2_Backend_Plan_V1_4_4.md** ในส่วนของ **Phase 2: High-Integrity Data & File Management** โดยเฉพาะหัวข้อ **T2.5 JSON Details & Schema Management** (T2.5.1 - T2.5.9) เรียบร้อยแล้วครับ
|
||||
|
||||
ในฐานะ Programmer ผู้เชี่ยวชาญ ผมขอสรุปผลการ Review และคำแนะนำตามลำดับความสำคัญดังนี้ครับ:
|
||||
นี่คือสรุปรายการโมดูล, ไฟล์ และฟังก์ชันที่ต้องสร้างหรือแก้ไข เพื่อให้ระบบจัดการ JSON Schema สมบูรณ์และมีประสิทธิภาพสูงสุดครับ
|
||||
|
||||
---
|
||||
|
||||
### 📊 **1. บทสรุปภาพรวม (Executive Summary)**
|
||||
### 📂 1. โครงสร้างโมดูลที่ต้องสร้าง (Module Structure)
|
||||
|
||||
จากการตรวจสอบเปรียบเทียบ:
|
||||
1. **Database Schema (`01_lcbp3_v1_4_3.sql`)**: **ครบถ้วนสมบูรณ์มาก (95%)** รองรับฟีเจอร์ระดับ Advance ตาม Requirements v1.4.3 ครบถ้วน เช่น Partitioning, Virtual Columns, Versioning (Optimistic Lock) และ Table Structure สำหรับ Workflow/Files
|
||||
2. **Backend Folder Structure (`5_Backend_Folder_V1_4_3.md`)**: **ครบถ้วนตามแผนงาน (90%)** มีการจัดวาง Module ตามหลัก Domain-Driven Design และมีไฟล์สำคัญๆ ครบตาม Plan
|
||||
เราจำเป็นต้องสร้าง `JsonSchemaModule` ขึ้นมาใหม่ โดยภายในจะประกอบด้วย Services ย่อยตามหน้าที่งาน เพื่อให้เป็นไปตามหลัก Single Responsibility Principle ครับ
|
||||
|
||||
**จุดที่ต้องเน้นย้ำ:** โครงสร้างไฟล์ (File Structure) มีไฟล์ `.ts` ปรากฏอยู่ครบ แต่สิ่งที่ผม "มองไม่เห็น" คือ **เนื้อหาโค้ดภายใน (Internal Logic)** ว่าได้ Implement ตาม Logic ที่ซับซ้อน (เช่น Redlock, Two-Phase Commit, DSL Parser) ไว้ถูกต้องหรือไม่
|
||||
**Path:** `src/modules/json-schema/`
|
||||
|
||||
---
|
||||
|
||||
### 🧐 **2. การตรวจสอบรายละเอียด (Detailed Review)**
|
||||
|
||||
#### **✅ ส่วนที่มีความครบถ้วนสมบูรณ์ (Well-Implemented Areas)**
|
||||
|
||||
1. **Database Partitioning & Optimization:**
|
||||
* SQL มีการทำ `PARTITION BY RANGE` ให้กับ `audit_logs` และ `notifications` แล้ว (ตาม Req 6.2)
|
||||
* มี `VIRTUAL COLUMNS` สำหรับดึงค่า JSON มาทำ Index (ตาม Req 3.11.3)
|
||||
2. **Security & Identity:**
|
||||
* ตาราง `users`, `roles`, `permissions`, `user_assignments` รองรับ RBAC 4 ระดับ (Global, Org, Project, Contract) ครบถ้วน
|
||||
* Folder Structure มี `auth`, `guards`, `decorators` ที่จำเป็นครบ (`jwt-auth.guard.ts`, `rbac.guard.ts`, `require-permission.decorator.ts`)
|
||||
3. **File Management:**
|
||||
* SQL มีตาราง `attachments` ที่มี field `is_temporary`, `temp_id`, `checksum` รองรับ Two-Phase Storage
|
||||
* Folder มี `file-storage` module และ `file-cleanup.service.ts` (Cron job) เตรียมไว้แล้ว
|
||||
4. **Workflow & RFA:**
|
||||
* SQL มีตารางเก็บ Template และ Instance (`..._routings`, `..._workflows`) แยกกันชัดเจน และมี field `workflow_config` (JSON) สำหรับ DSL
|
||||
* Folder มี `workflow-engine` module และ `workflow-dsl.service.ts` (ตาม Phase 6A)
|
||||
|
||||
---
|
||||
|
||||
#### ⚠️ **3. Gap Analysis & Missing Parts (สิ่งที่อาจขาดหายไปหรือต้องตรวจสอบ)**
|
||||
|
||||
แม้ชื่อไฟล์จะครบ แต่จากประสบการณ์ มีจุดที่มักจะหลุด หรือต้องเขียน Logic ให้รัดกุมมากในไฟล์ที่มีอยู่ ดังนี้:
|
||||
|
||||
**Phase 2: High-Integrity Data (File & Numbering)**
|
||||
* **Redis/Redlock Integration:** ในไฟล์ `document-numbering.service.ts` ต้องตรวจสอบว่ามีการเรียกใช้ Redis Distributed Lock จริงหรือไม่ (ไม่ใช่แค่ Database Lock)
|
||||
* **Transaction Safety:** ใน `file-storage.service.ts` ต้องมั่นใจว่า Logic การ Commit file (ย้ายจาก temp -> permanent) อยู่ภายใน Database Transaction เดียวกับการบันทึกข้อมูลเอกสาร
|
||||
|
||||
**Phase 3 & 6A: Workflow Engine**
|
||||
* **DSL Interpreter:** ไฟล์ `workflow-dsl.service.ts` มีอยู่แล้ว แต่ต้องตรวจสอบว่าสามารถ **Parse** JSON/YAML จากตาราง `rfa_workflow_templates` แล้วแปลงเป็น Execution Logic ได้จริงหรือไม่
|
||||
* **State Machine Consistency:** การเชื่อมต่อระหว่าง `CorrespondenceModule` กับ `WorkflowEngine` ต้องแน่ใจว่าเมื่อ State เปลี่ยนใน Engine ค่าใน Database (Status Column) ถูกอัปเดตตรงกัน
|
||||
|
||||
**Phase 6: Search & Notification**
|
||||
* **Elasticsearch Sync:** ใน `search.service.ts` ต้องมี Logic การ Sync ข้อมูล (อาจผ่าน Event Emitter หรือ TypeORM Subscriber) เมื่อมีการ Create/Update เอกสาร
|
||||
* **Notification Digest:** ใน `notification.processor.ts` (Consumer) ต้องมี Logic การหน่วงเวลา (Debounce/Throttle) เพื่อรวม Notification ก่อนส่ง (ตาม Req 6.8.2)
|
||||
|
||||
---
|
||||
|
||||
### 🚀 **4. คำแนะนำและสิ่งที่ต้องทำต่อ (Recommendations by Phase)**
|
||||
|
||||
เพื่อให้งานสมบูรณ์แบบ ผมขอแนะนำให้ดำเนินการตรวจสอบหรือ Implement ส่วนที่เหลือตามลำดับดังนี้:
|
||||
|
||||
#### **Phase 0-1: Infrastructure & Auth (Priority: Critical)**
|
||||
* **[Audit] Database Migration:** แม้จะมีไฟล์ `.sql` รวม แต่ใน NestJS ควรมีระบบ Migration (TypeORM Migrations) เพื่อจัดการการเปลี่ยนแปลง Schema ในอนาคต (ถ้ายังไม่มี ควรสร้าง `src/database/migrations/*` จาก SQL ที่มี)
|
||||
* **[Code Check] Config Validation:** ตรวจสอบ `src/common/config/env.validation.ts` ว่ามีการ Validate ตัวแปรระบบครบทุกตัว (Database, Redis, JWT Secrets) ตาม Req 2.2 หรือไม่
|
||||
|
||||
#### **Phase 2: Core Data Services (Priority: High)**
|
||||
* **[Code Check] Document Numbering:** เปิดไฟล์ `src/modules/document-numbering/document-numbering.service.ts` ตรวจสอบ Logic **Double-Lock** (Redis + Optimistic Lock) ว่า Implement ถูกต้องตาม Plan T2.3 หรือไม่
|
||||
* **[Code Check] File Storage:** ตรวจสอบ `src/common/file-storage/file-storage.service.ts` ว่ามีการทำ **Virus Scan (ClamAV)** ก่อน save ลง Temp หรือไม่
|
||||
|
||||
#### **Phase 3 & 6A: Workflow Engine (Priority: Medium-High)**
|
||||
* **[Task] Workflow Integration:** ตรวจสอบว่า `CorrespondenceService` และ `RfaService` มีการเรียกใช้ `WorkflowEngineService` แทนที่จะเขียน Logic if-else hardcode หรือไม่
|
||||
* **[Task] DSL Loader:** ตรวจสอบว่ามี Seed Data สำหรับ `rfa_workflow_templates` ที่เป็น JSON Config เริ่มต้นแล้วหรือยัง
|
||||
|
||||
#### **Phase 6: Background Services (Priority: Medium)**
|
||||
* **[Task] Queue Setup:** ตรวจสอบ `notification.module.ts` ว่ามีการ Register BullMQ (`BullModule.registerQueue`) แล้วหรือยัง
|
||||
* **[Task] Cron Jobs:** ตรวจสอบ `app.module.ts` ว่ามีการ Import `ScheduleModule.forRoot()` เพื่อให้ `file-cleanup.service.ts` ทำงานได้จริง
|
||||
|
||||
---
|
||||
|
||||
### 🛠️ **5. Next Step Action (สิ่งที่คุณทำได้เลย)**
|
||||
|
||||
เนื่องจากโครงสร้างไฟล์และฐานข้อมูลดูดีมากแล้ว เพื่อให้ผมช่วยเหลือได้ตรงจุดที่สุด:
|
||||
|
||||
**คุณต้องการให้ผม Review โค้ดภายในไฟล์ (Internal Logic) ของไฟล์ไหนเป็นพิเศษไหมครับ?**
|
||||
|
||||
ผมแนะนำให้เริ่มจาก **"หัวใจสำคัญ"** ของความถูกต้องข้อมูลก่อน คือ:
|
||||
1. **`src/modules/document-numbering/document-numbering.service.ts`** (เพื่อดู Logic การรันเลขเอกสาร)
|
||||
2. **`src/common/file-storage/file-storage.service.ts`** (เพื่อดู Logic การจัดการไฟล์และการ Commit)
|
||||
|
||||
หรือถ้าต้องการให้ผมเริ่มเขียน Code ส่วนไหนที่ยังขาดอยู่ (เช่น Workflow DSL Parser) สามารถแจ้งได้เลยครับ
|
||||
|
||||
|
||||
สวัสดีครับ ในฐานะ Programmer ที่ดูแลระบบ LCBP3-DMS ผมได้ทำการ **Review เอกสาร `6B.md` (Special requirements for document-numbering)** โดยเทียบกับโครงสร้างฐานข้อมูลและแผนงานที่มีอยู่ (v1.4.3) พบประเด็นสำคัญและการปรับปรุงทางเทคนิคที่จำเป็นดังนี้ครับ
|
||||
|
||||
-----
|
||||
|
||||
# 📝 ผลการ Review เอกสาร 6B.md และแนวทางการ Implement
|
||||
|
||||
## 1\. บทวิเคราะห์และผลกระทบ (Analysis & Impact)
|
||||
|
||||
เอกสาร `6B.md` ระบุความต้องการที่ **ซับซ้อนกว่า** ระบบ Running Number ทั่วไป (ที่มักเป็น `ORG-TYPE-YEAR-SEQ`) โดยมีประเด็นสำคัญคือ:
|
||||
|
||||
1. **Requirement 1 (Flexibility):** Admin ต้องแก้ format ได้ → **รองรับได้** ด้วยตาราง `document_number_formats` ที่มีอยู่ แต่ต้องปรับ Logic การแทนค่าตัวแปร (Token Replacement) ให้ฉลาดขึ้น
|
||||
2. **Requirement 2.1 (Correspondence ทั่วไป):** รูปแบบมาตรฐาน → **รองรับได้** ด้วยโครงสร้างปัจจุบัน
|
||||
3. **Requirement 2.2 (Transmittal):** มีเงื่อนไข "To OWNER" vs "To CONTRACTOR" ใช้เลขต่างกัน และมีการ mapping `sub_type_number` → **Impact:** ต้องเพิ่ม Logic ในการเลือก Format Template ตาม "ผู้รับ" (Recipient Role) และต้องมีตารางเก็บ Mapping `sub_type_number`
|
||||
4. **Requirement 2.3 (RFI) & 2.4 (RFA):** มีการใช้ `disciplines_code` (เช่น GEN, STR, ARC) และไม่มีปี (Year) ใน Format → **Impact:**
|
||||
* ฐานข้อมูลปัจจุบันขาดตาราง `disciplines`
|
||||
* ตาราง `document_number_counters` ปัจจุบัน Key คือ `(Project, Org, Type, Year)` แต่ RFA/RFI อาจจะต้องนับแยกตาม **Discipline** ด้วย หากไม่เพิ่ม Column ใน Counter อาจจะเกิดปัญหาเลขซ้ำหรือเลขกระโดดข้ามหมวดงาน
|
||||
|
||||
-----
|
||||
|
||||
## 2\. การออกแบบฐานข้อมูลเพิ่มเติม (Database Schema Updates)
|
||||
|
||||
เพื่อให้รองรับ Requirement นี้ ต้องมีการเพิ่มตารางและแก้ไขตาราง `01_lcbp3_v1_4_3.sql` ดังนี้:
|
||||
|
||||
### 2.1 เพิ่มตาราง `disciplines` และ `correspondence_sub_types`
|
||||
|
||||
```sql
|
||||
-- File: 01_lcbp3_v1_4_3_patch_6b.sql
|
||||
-- เพิ่มตารางหมวดงาน (Disciplines) ตาม Req 6B
|
||||
CREATE TABLE disciplines (
|
||||
id INT PRIMARY KEY AUTO_INCREMENT,
|
||||
project_id INT NOT NULL,
|
||||
discipline_code VARCHAR(10) NOT NULL COMMENT 'เช่น GEN, STR, ARC',
|
||||
name_th VARCHAR(255) COMMENT 'ชื่อไทย',
|
||||
name_en VARCHAR(255) COMMENT 'ชื่ออังกฤษ',
|
||||
is_active BOOLEAN DEFAULT TRUE,
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
FOREIGN KEY (project_id) REFERENCES projects(id) ON DELETE CASCADE,
|
||||
UNIQUE KEY uk_discipline_project (project_id, discipline_code)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
|
||||
|
||||
-- เพิ่มตาราง Sub Types (สำหรับ Transmittal/RFA mapping)
|
||||
CREATE TABLE correspondence_sub_type_codes (
|
||||
id INT PRIMARY KEY AUTO_INCREMENT,
|
||||
project_id INT NOT NULL,
|
||||
correspondence_type_id INT NOT NULL,
|
||||
sub_type_code VARCHAR(20) NOT NULL COMMENT 'เช่น MAT, SHP',
|
||||
sub_type_number VARCHAR(10) COMMENT 'เลขรหัส เช่น 11, 22',
|
||||
description VARCHAR(255),
|
||||
FOREIGN KEY (project_id) REFERENCES projects(id) ON DELETE CASCADE,
|
||||
FOREIGN KEY (correspondence_type_id) REFERENCES correspondence_types(id) ON DELETE CASCADE
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
|
||||
```text
|
||||
src/modules/json-schema
|
||||
├── 📁 dto
|
||||
│ ├── create-json-schema.dto.ts # DTO สำหรับสร้าง Schema ใหม่
|
||||
│ ├── update-json-schema.dto.ts # DTO สำหรับแก้ไข Schema
|
||||
│ ├── validate-data.dto.ts # DTO สำหรับรับข้อมูลมา Validate
|
||||
│ └── migrate-data.dto.ts # DTO สำหรับสั่ง Migrate Data
|
||||
├── 📁 entities
|
||||
│ └── json-schema.entity.ts # Entity หลักเก็บ Definition (T2.5.1)
|
||||
├── 📁 services
|
||||
│ ├── json-schema.service.ts # Core Service (CRUD, Validation wrapper)
|
||||
│ ├── virtual-column.service.ts # จัดการ Generated Columns ใน DB (T2.5.3)
|
||||
│ ├── schema-migration.service.ts # จัดการ Data Migration ระหว่าง Version (T2.5.5)
|
||||
│ └── json-security.service.ts # จัดการ Security & Encryption (T2.5.6)
|
||||
├── 📁 interfaces
|
||||
│ ├── ui-schema.interface.ts # Type definition สำหรับ UI Schema (T2.5.4)
|
||||
│ └── virtual-column.interface.ts # Type definition สำหรับ Virtual Column Config
|
||||
├── json-schema.controller.ts # API Endpoints (T2.5.7)
|
||||
├── json-schema.module.ts # Module Definition
|
||||
└── json-schema.service.spec.ts # Unit Tests (T2.5.9)
|
||||
```
|
||||
|
||||
### 2.2 ปรับปรุงตาราง `rfas` และ `document_number_counters`
|
||||
---
|
||||
|
||||
เนื่องจาก RFA/RFI ต้องวิ่งเลขตาม Discipline เราจึงต้องเก็บ Discipline ไว้ใน RFA และต้องใช้เป็นส่วนหนึ่งของ Key ในการนับเลข
|
||||
### 📝 2. รายละเอียดงานในแต่ละ Task (Tasks Breakdown)
|
||||
|
||||
```sql
|
||||
-- เพิ่ม discipline_id ในตาราง RFAs
|
||||
ALTER TABLE rfas
|
||||
ADD COLUMN discipline_id INT NULL AFTER rfa_type_id,
|
||||
ADD CONSTRAINT fk_rfa_discipline FOREIGN KEY (discipline_id) REFERENCES disciplines(id);
|
||||
#### **[ ] T2.5.1 JSON Schema Registry & Versioning**
|
||||
|
||||
-- เพิ่ม discipline_id ในตาราง Correspondences (สำหรับ RFI)
|
||||
ALTER TABLE correspondences
|
||||
ADD COLUMN discipline_id INT NULL AFTER correspondence_type_id,
|
||||
ADD CONSTRAINT fk_corr_discipline FOREIGN KEY (discipline_id) REFERENCES disciplines(id);
|
||||
- **สิ่งที่ต้องทำ:** สร้าง `JsonSchema` Entity
|
||||
- **รายละเอียด:**
|
||||
- เก็บ `schema_definition` (AJV format)
|
||||
- เก็บ `ui_schema` (Form configuration)
|
||||
- รองรับ `version` เพื่อทำ Schema Evolution
|
||||
- เก็บ config ของ `virtual_columns`
|
||||
|
||||
-- ปรับปรุงตาราง Counter ให้รองรับ Discipline และ SubType (ใช้เป็น Dynamic Key หรือเพิ่ม Column)
|
||||
-- เพื่อความ Performance และ Integrity แนะนำให้เพิ่ม Column
|
||||
ALTER TABLE document_number_counters
|
||||
ADD COLUMN discipline_id INT NULL DEFAULT NULL AFTER correspondence_type_id,
|
||||
DROP PRIMARY KEY,
|
||||
ADD PRIMARY KEY (project_id, originator_organization_id, correspondence_type_id, discipline_id, current_year);
|
||||
-- หมายเหตุ: ถ้า discipline_id เป็น NULL ให้ถือว่าเป็น Counter ทั่วไป
|
||||
```
|
||||
#### **[ ] T2.5.2 Schema Validation & Transformation Engine**
|
||||
|
||||
-----
|
||||
- **สิ่งที่ต้องทำ:** Implement Logic ใน `JsonSchemaService`
|
||||
- **รายละเอียด:**
|
||||
- ติดตั้งและ Config `AJV` library
|
||||
- สร้าง Custom Keywords: `document-number`, `requiredRole`
|
||||
- ฟังก์ชัน `validateData(schemaName, data)`
|
||||
- ฟังก์ชัน `sanitizeData()` เพื่อลบ field ที่ไม่อยู่ใน schema หรือ field ที่ user ไม่มีสิทธิ์
|
||||
|
||||
## 3\. แนวทางการ Implement ใน NestJS (Backend Logic)
|
||||
#### **[ ] T2.5.3 Virtual Columns & Performance Optimization**
|
||||
|
||||
เราต้องปรับ `DocumentNumberingService` ให้เป็น **Token-Based Generator** ที่มีความยืดหยุ่นสูง
|
||||
- **สิ่งที่ต้องทำ:** สร้าง `VirtualColumnService`
|
||||
- **รายละเอียด:**
|
||||
- ฟังก์ชัน `setupVirtualColumns()`: อ่าน Config จาก Schema แล้วสั่ง `ALTER TABLE ... ADD COLUMN ... GENERATED ALWAYS AS ...`
|
||||
- ฟังก์ชันสร้าง Index บน Virtual Column เพื่อให้ค้นหาเร็วขึ้น
|
||||
- **สำคัญ:** ต้องรองรับ MariaDB 10.11 Syntax
|
||||
|
||||
### 3.1 รูปแบบ Template (Format Templates)
|
||||
#### **[ ] T2.5.4 Dynamic Form Schema Management**
|
||||
|
||||
Admin จะบันทึก Template ลง DB ในรูปแบบ:
|
||||
- **สิ่งที่ต้องทำ:** กำหนด Interface ใน `ui-schema.interface.ts`
|
||||
- **รายละเอียด:**
|
||||
- ออกแบบโครงสร้าง JSON ที่ Frontend จะนำไป Render เป็น Form
|
||||
- กำหนด Logic `dependencies` (เช่น ถ้าเลือก Type A ให้แสดง Field B)
|
||||
|
||||
* General: `{ORG}-{ORG}-{SEQ:4}-{YEAR}`
|
||||
* Transmittal: `{ORG}-{ORG}-{SUBTYPE_NUM}-{SEQ:4}-{YEAR}`
|
||||
* RFA: `{CONTRACT}-{TYPE}-{DISCIPLINE}-{SUBTYPE}-{SEQ:4}-{REV}`
|
||||
#### **[ ] T2.5.5 Data Migration & Version Compatibility**
|
||||
|
||||
### 3.2 Logic การ Generate (Pseudo Code)
|
||||
- **สิ่งที่ต้องทำ:** สร้าง `SchemaMigrationService`
|
||||
- **รายละเอียด:**
|
||||
- Logic การแปลงข้อมูลเก่าให้เข้ากับ Schema ใหม่ (Field Rename, Transform Value)
|
||||
- ฟังก์ชัน `migrateData(entityType, entityId, targetVersion)`
|
||||
|
||||
```typescript
|
||||
// File: src/modules/document-numbering/document-numbering.service.ts
|
||||
#### **[ ] T2.5.6 Security & Access Control for JSON Data**
|
||||
|
||||
/**
|
||||
* Strategy:
|
||||
* 1. รับค่า Context (Project, Org, Type, Discipline, SubType, Year)
|
||||
* 2. ดึง Format Template จาก DB ตาม Type
|
||||
* 3. ถ้าเป็น Transmittal ต้องเช็คเงื่อนไขพิเศษเพื่อเลือก Template (Owner vs Contractor)
|
||||
* 4. Parse Template เพื่อหาว่าต้องใช้ Key ไหนบ้างในการ Lock และ Count
|
||||
* 5. Run Redlock + Optimistic Lock
|
||||
* 6. Replace Tokens
|
||||
*/
|
||||
- **สิ่งที่ต้องทำ:** สร้าง `JsonSecurityService`
|
||||
- **รายละเอียด:**
|
||||
- **Field-level Security:** ตรวจสอบ Role ว่าเห็น Field นี้ได้ไหม (Masking/Removal)
|
||||
- **Encryption:** เข้ารหัส Field ที่ Sensitive ก่อนบันทึกลง JSON
|
||||
|
||||
async generateNextNumber(ctx: GenerateNumberContext): Promise<string> {
|
||||
// 1. Get Template
|
||||
let template = await this.formatRepo.findOne({ ... });
|
||||
#### **[ ] T2.5.7 API Design & Integration**
|
||||
|
||||
// 2. Handle Special Logic (Requirement 2.2 Transmittal)
|
||||
if (ctx.typeCode === 'TRANSMITTAL') {
|
||||
// Logic: ถ้าส่งให้ Owner ใช้ Template A, ถ้า Contractor ใช้ Template B
|
||||
// หรือดึง sub_type_number มาเตรียมไว้
|
||||
}
|
||||
- **สิ่งที่ต้องทำ:** สร้าง `JsonSchemaController`
|
||||
- **Endpoints:**
|
||||
- `POST /json-schema/schemas`: สร้าง Schema
|
||||
- `POST /json-schema/validate/:name`: ตรวจสอบข้อมูล
|
||||
- `POST /json-schema/migrate/:type/:id`: สั่ง Migrate
|
||||
- `GET /json-schema/ui-schema/:name`: ดึง UI Config
|
||||
|
||||
// 3. Resolve Tokens Values
|
||||
const tokens = {
|
||||
'{ORG}': ctx.orgCode,
|
||||
'{CONTRACT}': ctx.contractCode,
|
||||
'{TYPE}': ctx.typeCode,
|
||||
'{DISCIPLINE}': ctx.disciplineCode, // จากตาราง disciplines
|
||||
'{SUBTYPE}': ctx.subTypeCode, // จาก rfa_types หรือ sub_type_codes
|
||||
'{SUBTYPE_NUM}': ctx.subTypeNumber, // จาก correspondence_sub_type_codes
|
||||
'{YEAR}': ctx.year.toString(),
|
||||
'{YEAR_SHORT}': ctx.year.toString().slice(-2),
|
||||
};
|
||||
#### **[ ] T2.5.8 Integration with Document Modules**
|
||||
|
||||
// 4. Construct Counter Key
|
||||
// ถ้า Template มี {DISCIPLINE} ต้องเอา discipline_id มาเป็นส่วนหนึ่งของ Composite Key ใน DB Counter
|
||||
const counterKey = {
|
||||
projectId: ctx.projectId,
|
||||
orgId: ctx.orgId,
|
||||
typeId: ctx.typeId,
|
||||
disciplineId: template.includes('{DISCIPLINE}') ? ctx.disciplineId : null,
|
||||
year: template.includes('{YEAR}') ? ctx.year : 0 // ถ้าไม่ใช้ปี ให้ใช้ 0
|
||||
};
|
||||
- **สิ่งที่ต้องทำ:** แก้ไข Service อื่นๆ (Correspondence, RFA)
|
||||
- **รายละเอียด:**
|
||||
- Inject `JsonSchemaService` เข้าไปใน `CorrespondenceService` และ `RfaService`
|
||||
- เรียก `validateData` ก่อน create/update
|
||||
- เรียก `setupVirtualColumns` หลัง create schema ใหม่
|
||||
|
||||
// 5. Execute Double-Lock Mechanism (Redis + DB Optimistic)
|
||||
const seq = await this.runDistributedCounter(counterKey);
|
||||
#### **[ ] T2.5.9 Testing Strategy**
|
||||
|
||||
// 6. Final Replace
|
||||
let finalNumber = template.formatTemplate;
|
||||
for (const [key, value] of Object.entries(tokens)) {
|
||||
finalNumber = finalNumber.replace(key, value || '00'); // Fallback if missing
|
||||
}
|
||||
// Replace SEQ with padding
|
||||
finalNumber = finalNumber.replace(/{SEQ:(\d+)}/, (_, width) =>
|
||||
String(seq).padStart(Number(width), '0')
|
||||
);
|
||||
- **สิ่งที่ต้องทำ:** เขียน Unit Test
|
||||
- **รายละเอียด:**
|
||||
- ทดสอบ Validation Logic (ผ่าน/ไม่ผ่าน)
|
||||
- ทดสอบ Virtual Column Generation (SQL ถูกต้องไหม)
|
||||
- ทดสอบ Security Masking
|
||||
|
||||
return finalNumber;
|
||||
}
|
||||
```
|
||||
---
|
||||
|
||||
-----
|
||||
### 🚀 ขั้นตอนถัดไป
|
||||
|
||||
## 4\. แผนการดำเนินการ (Action Plan)
|
||||
หากท่านเห็นด้วยกับโครงสร้างนี้ ผมแนะนำให้เริ่มดำเนินการตามลำดับดังนี้ครับ:
|
||||
|
||||
เพื่อให้รองรับ `6B.md` ได้สมบูรณ์ ผมขอเสนอให้ดำเนินการดังนี้:
|
||||
1. **สร้าง Entity และ DTOs** (T2.5.1)
|
||||
2. **Implement JsonSchemaService Core** (Validation Logic) (T2.5.2)
|
||||
3. **Implement VirtualColumnService** (T2.5.3) - _ส่วนนี้สำคัญมากสำหรับ Performance_
|
||||
4. **สร้าง Controller และเชื่อมต่อ Module** (T2.5.7)
|
||||
|
||||
1. **Database Migration:**
|
||||
* รัน SQL Script เพื่อเพิ่มตาราง `disciplines`, `correspondence_sub_type_codes`.
|
||||
* Alter ตาราง `rfas`, `correspondences` และ `document_number_counters` ตามข้อ 2.2
|
||||
2. **Seed Data:**
|
||||
* นำข้อมูลจากตารางใน `6B.md` (Disciplines, RFA Types, Sub Types) มาเขียนเป็น Seed Script ลงฐานข้อมูล เพื่อให้เริ่มต้นระบบมีข้อมูลพร้อมใช้ทันที
|
||||
3. **Backend Implementation:**
|
||||
* Update `DocumentNumberingService` ให้รองรับ Dynamic Token Replacement
|
||||
* Update `CreateRfaDto` และ `CreateCorrespondenceDto` ให้รับค่า `discipline_id` และ `sub_type_id` (ถ้ามี)
|
||||
4. **Admin Panel:**
|
||||
* ต้องมีหน้า UI ให้ Admin จัดการ Mapping ของ `Format Template` และ `Sub Type Codes` ได้ (ตาม Requirement ข้อ 1)
|
||||
|
||||
**ต้องการให้ผมเริ่มเขียน SQL Migration Script และ Seed Data สำหรับข้อมูลใน 6B.md เลยหรือไม่ครับ?** (ข้อมูลในตารางค่อนข้างเยอะ การทำ Seed ไว้จะช่วยลดเวลา Admin ในการกรอกข้อมูลเริ่มต้นได้มากครับ)
|
||||
|
||||
จาก แผนการดำเนินการ (Action Plan): Database Migration และ Seed Data, ผมได้อัพเดท Basic data ข้อ 5. Data files ให้แล้ว, ช่วย review และ ปรับปรุง 4_Data_Dictionary_V1_4_3.md ,
|
||||
|
||||
เตรียม Backend Implementation, Admin Panel
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
### **Phase 2: High-Integrity Data & File Management (สัปดาห์ที่ 4)**
|
||||
|
||||
* **[ ] T2.3 DocumentNumberingModule - Token-Based & Double-Lock** (Updated)
|
||||
* [ ] Update Entity: `DocumentNumberCounter` (Add `discipline_id` to PK)
|
||||
* [ ] Implement Token Parser & Replacer Logic (`{DISCIPLINE}`, `{SUBTYPE_NUM}`)
|
||||
* [ ] Update `generateNextNumber` to handle optional keys (Discipline/SubType)
|
||||
* [ ] **Deliverable:** Flexible Numbering System
|
||||
|
||||
* **[ ] T2.6 MasterModule - Advanced Data (Req 6B)** (New)
|
||||
* [ ] Update Entities: `Discipline`, `CorrespondenceSubType`
|
||||
* [ ] Create Services/Controllers for CRUD Operations (Admin Panel Support)
|
||||
* [ ] Implement Seeding Logic for initial 6B data
|
||||
* [ ] **Deliverable:** API for managing Disciplines and Sub-types
|
||||
* [ ] **Dependencies:** T1.1, T0.3
|
||||
ท่านต้องการให้ผมเริ่มเขียนโค้ดส่วนไหนก่อนหรือไม่ครับ? (เช่น Entity หรือ VirtualColumnService)
|
||||
|
||||
Reference in New Issue
Block a user