15 KiB
Training System Implementation Plan
เอกสารนี้สรุป implementation plan แบบ phase-by-phase สำหรับพัฒนาระบบ Training System บน stack ของโปรเจ็กต์นี้
- Next.js 16 App Router
- React 19
- TypeScript
- Auth.js
- PostgreSQL + Drizzle ORM
- TanStack React Query
- nuqs
- TanStack Form + Zod
- shadcn/ui + Tailwind CSS
เอกสารนี้อ้างอิงจาก req.md และออกแบบให้สอดคล้องกับ feature-based architecture ของรีโปนี้
Phase 0: Foundation Alignment
เป้าหมาย:
- ทำให้ requirement, domain language, และโครงสร้างข้อมูลสอดคล้องกับ stack และ architecture ของโปรเจ็กต์
งานหลัก:
- ยืนยันคำศัพท์โดเมนหลัก:
organizationmembershipuseremployeecoursetraining_recordcertificatetraining_matrix
- แยกความหมายของ
userกับemployeeให้ชัด - ยืนยัน status values กลางของระบบ เช่น:
pendingapprovedrejectedneeds_revision
- ยืนยัน training types:
onlineonsiteinternalexternal
- กำหนด naming convention สำหรับ:
- database tables
- route handlers
- query keys
- feature folders
ผลลัพธ์:
- domain language ชัดเจน
- requirement พร้อมสำหรับลงมือทำ
Phase 1: Auth, Organization, and RBAC Core
เป้าหมาย:
- วาง auth และ multi-tenant foundation ให้ทุกฟีเจอร์ใหม่ใช้งานร่วมกันได้
งานหลัก:
- ใช้
Auth.jsเป็น auth boundary หลัก - ใช้ตาราง:
usersorganizationsmemberships
- ทำ server-side helpers:
requireSession()requireOrganizationAccess()requireRole()เมื่อจำเป็น
- ปรับ route protection สำหรับ dashboard และ protected API
- ทำ session shape ให้ client ใช้:
activeOrganizationIdactiveOrganizationNamerolepermissions
- ปรับ navigation filtering ให้ดึงจาก app-owned session
ผลลัพธ์:
- ระบบ login/logout ใช้งานได้
- organization switching ใช้งานได้
- role-based access เริ่มทำงานได้
Acceptance Criteria:
- ผู้ใช้ที่ไม่ login เข้า dashboard ไม่ได้
- ผู้ใช้ที่ไม่มี active organization เข้า org-scoped page ไม่ได้
- visibility ของ nav เปลี่ยนตาม session และ role
Phase 2: Shared Domain Schema
เป้าหมาย:
- วาง schema กลางของระบบ Training System ใน PostgreSQL ด้วย Drizzle
งานหลัก:
- เพิ่ม schema สำหรับ:
departmentspositionsemployeescoursestraining_recordscertificatestraining_matrices
- ทุก business table ต้องมี
organizationId - กำหนด foreign keys ให้ครบ
- เพิ่ม timestamps และ audit-friendly fields ตามความเหมาะสม
- สร้างและ apply migration
ผลลัพธ์:
- database model ครบพอสำหรับสร้างฟีเจอร์หลัก
Acceptance Criteria:
- schema build ผ่าน
- migration ใช้งานได้
- relation หลัก query ได้จริง
Phase 3: Employee Management
เป้าหมาย:
- มี master data พนักงานที่ใช้เป็นฐานของการบันทึกอบรม
งานหลัก:
- สร้าง feature
employees - เพิ่มไฟล์:
src/features/employees/api/types.tssrc/features/employees/api/service.tssrc/features/employees/api/queries.tssrc/features/employees/api/mutations.tssrc/features/employees/components/*src/features/employees/schemas/*
- เพิ่ม route handlers:
GET /api/employeesPOST /api/employeesGET /api/employees/:idPATCH /api/employees/:id
- สร้าง dashboard page สำหรับ list และ create/edit
- ใช้ React Query + nuqs table pattern
ข้อมูลหลัก:
- employee code
- full name
- department
- position
- company
- active status
ผลลัพธ์:
- admin จัดการข้อมูล employee ได้
Acceptance Criteria:
- list/filter/search/pagination ทำงาน
- create/update/disable employee ทำงาน
- จำกัดข้อมูลตาม organization
Phase 4: Course Management
เป้าหมาย:
- มี course catalog กลางของ organization
งานหลัก:
- สร้าง feature
courses - เพิ่ม CRUD route handlers และ Drizzle queries
- สร้าง list page และ create/edit form
- ใช้ table pattern เดียวกับ
productsและusers
ข้อมูลหลัก:
- course name
- category
- organizer
- standard hours
- certificate validity
- required flag
ผลลัพธ์:
- admin จัดการหลักสูตรได้
Acceptance Criteria:
- create/update/delete course ทำงาน
- list/filter/search/sort ทำงาน
- course ถูกจำกัดตาม organization
Phase 5: Training Record CRUD
เป้าหมาย:
- ทำ flow หลักของระบบให้ผู้ใช้บันทึกประวัติการอบรมได้จริง
งานหลัก:
- สร้าง feature
training-records - เพิ่ม route handlers:
GET /api/training-recordsPOST /api/training-recordsGET /api/training-records/:idPATCH /api/training-records/:idDELETE /api/training-records/:id
- สร้างหน้า:
- list page
- detail page
- create/edit form
- ใช้ TanStack Form + Zod
- ใช้ React Query mutations + invalidation
ข้อมูลหลัก:
- employee
- course
- training date
- training type
- hours
- organizer
- note
- approval status
ผลลัพธ์:
- ผู้ใช้เพิ่มและจัดการประวัติการอบรมได้
Acceptance Criteria:
- user เพิ่ม record ได้
- user เห็นเฉพาะ record ที่เกี่ยวข้องตาม policy
- admin เห็น records ภายใน organization
Phase 6: Certificate Upload and Metadata
เป้าหมาย:
- รองรับไฟล์ใบรับรองและ metadata อย่างเป็นระบบ
งานหลัก:
- เพิ่ม certificate model และ route handlers ที่เกี่ยวข้อง
- เก็บ metadata ใน PostgreSQL:
fileNamefileTypefileSizestorageKeyuploadedAt
- ผูก
certificateกับtraining_record - เพิ่ม upload flow และ preview/open flow
- กำหนดข้อจำกัดไฟล์:
pdfjpgjpegpng
ผลลัพธ์:
- training record มีหลักฐานแนบได้จริง
Acceptance Criteria:
- upload สำเร็จ
- metadata ถูกบันทึก
- เปิดดูย้อนหลังได้
- validation ประเภทและขนาดไฟล์ทำงาน
Phase 7: Approval Workflow
เป้าหมาย:
- ให้ admin ตรวจสอบและอนุมัติรายการอบรมได้
งานหลัก:
- เพิ่ม admin queue สำหรับรายการ
pending - เพิ่ม actions:
- approve
- reject
- request changes
- เก็บ reviewer note
- กำหนด policy การแก้ไข record หลัง approve
- เพิ่ม filtering ตาม status
ผลลัพธ์:
- ระบบมี workflow ตรวจสอบจริง
Acceptance Criteria:
- admin เปลี่ยนสถานะได้
- user เห็นสถานะล่าสุดได้
- unauthorized user เปลี่ยนสถานะไม่ได้
Phase 8: Training Matrix
เป้าหมาย:
- ทำ logic ความครบถ้วนตาม requirement ของแต่ละตำแหน่งหรือแผนก
งานหลัก:
- สร้าง feature
training-matrix - รองรับ mapping:
- department -> required courses
- position -> required courses
- เตรียมรองรับ employee-specific overrides ในอนาคต
- เพิ่มหน้า admin สำหรับจัดการ matrix
- เพิ่ม query/helper สำหรับคำนวณ compliance
คำตัดสินเชิงกติกาที่ต้อง implement:
- ใช้เฉพาะ approved training records
- course ที่หมดอายุสามารถถือว่าไม่ครบได้ ถ้าระบบกำหนด validity
- matrix ต้องคำนวณในระดับ organization เสมอ
ผลลัพธ์:
- ระบบสามารถตอบได้ว่าใครครบหรือยังไม่ครบ
Acceptance Criteria:
- admin กำหนด matrix ได้
- employee compliance คำนวณได้
- dashboard/report เรียกใช้ข้อมูลนี้ได้
Phase 9: Dashboards and Analytics
เป้าหมาย:
- ทำ dashboard ให้สะท้อนข้อมูลธุรกิจจริง
งานหลัก:
- User dashboard:
- total courses
- total training hours
- total certificates
- missing required courses
- training chart by month/year
- Admin dashboard:
- employee count
- total hours
- pending approvals
- compliance percentage
- charts by department/category/time
- ใช้ Recharts
- ใช้ server prefetch +
HydrationBoundary+useSuspenseQuery
ผลลัพธ์:
- dashboard ใช้ข้อมูลจาก DB จริง
Acceptance Criteria:
- metrics ถูกต้อง
- charts render ได้
- organization scoping ถูกต้อง
Phase 10: Reports and Export
เป้าหมาย:
- เพิ่ม reporting สำหรับใช้งานจริงของ admin
งานหลัก:
- สร้าง feature
reports - ทำรายงาน:
- individual training history
- department training summary
- accumulated hours
- missing required courses
- expiring certificates
- organization training matrix summary
- เริ่มจาก on-screen report pages
- เพิ่ม export ใน phase ย่อยถัดไป เช่น CSV หรือ PDF
ผลลัพธ์:
- admin ตรวจสอบข้อมูลย้อนหลังและสรุปผลได้
Acceptance Criteria:
- รายงานโหลดได้
- filter/report criteria ใช้งานได้
- export อย่างน้อย 1 รูปแบบเมื่อรวมใน scope
Phase 11: UX, Theme, and Hardening
เป้าหมาย:
- polish ระบบให้พร้อมใช้งานและขยายต่อ
งานหลัก:
- ปรับ visual theme ตาม requirement ผ่าน theme system ของโปรเจ็กต์
- ใช้สีหลัก:
#336b4a#808285#e26631#413b60
- จัด empty/loading/error/access states ให้ครบ
- ปรับ mobile behavior ของ table และ form
- เพิ่ม validation messages ให้สม่ำเสมอ
- เก็บ lint/type/build issues ให้สะอาด
- เพิ่ม test coverage ใน critical flows
ผลลัพธ์:
- ระบบพร้อม demo และพร้อมพัฒนาต่อ
Acceptance Criteria:
buildผ่าน- ไม่มี lint issue ใหม่จากส่วนที่เพิ่ม
- flow หลักใช้งานได้ทั้ง desktop และ mobile
Milestones ที่แนะนำ
Milestone A
- Phase 1
- Phase 2
- Phase 3
- Phase 4
ผลลัพธ์:
- organization foundation พร้อม
- employee และ course CRUD พร้อม
Milestone B
- Phase 5
- Phase 6
ผลลัพธ์:
- training records และ certificate flow ใช้งานได้
Milestone C
- Phase 7
- Phase 8
ผลลัพธ์:
- approval workflow และ training matrix ใช้งานได้
Milestone D
- Phase 9
- Phase 10
- Phase 11
ผลลัพธ์:
- dashboard, reports, และ UX polish พร้อมใช้งาน
ลำดับลงมือที่แนะนำ
- ทำ shared schema และ domain foundation ให้ครบก่อน
- ทำ master data คือ
employeesและcourses - ทำ
training-recordsและcertificates - ทำ approval workflow
- ทำ training matrix
- ทำ dashboard และ reports
- ทำ polish และ hardening
Mapping กับโครงสร้างรีโป
feature ที่ควรมีใน src/features/:
employeescoursestraining-recordscertificatestraining-matrixreports
page routes ที่ควรมีใน src/app/dashboard/:
employeescoursestraining-recordstraining-matrixreports
API routes ที่ควรมีใน src/app/api/:
employeescoursestraining-recordscertificatestraining-matrixreportsเมื่อจำเป็น
หมายเหตุสำหรับการ implement
- ใช้
PageContainerสำหรับ page header ทุกหน้า - ใช้
apiClientสำหรับเรียก local route handlers จากservice.ts - ใช้
queryOptionsและmutationOptionsตาม pattern เดิม - ใช้
Iconsจาก@/components/icons - อย่าใช้ mock data สำหรับฟีเจอร์ใหม่
- อย่าใช้ vendor UI ภายนอกแทน component patterns ของโปรเจ็กต์