# Task B.1 – Foundation Stabilization & Production Isolation ## Context Task B Foundation Layer เสร็จแล้ว ปัจจุบันระบบมี: * Organization Context * Membership Context * Permission Layer * Branch Scope * Master Options * Document Sequence * Audit Log Helper เป้าหมายของ Task B.1 คือทำให้ Foundation พร้อมใช้งานจริงก่อนเริ่ม Task C (Customer + Contact) --- # สิ่งที่ต้องอ่านก่อน อ่านและยึดตาม: ```txt docs/implementation/task-a-template-audit.md docs/implementation/task-b-template-audit.md Layout.md AGENTS.md .agents/skills/kiranism-shadcn-dashboard src/auth.ts src/lib/auth/** src/db/schema.ts src/features/products/** src/features/users/** ``` ใช้ products และ users เป็น production reference --- # เป้าหมาย ทำ 2 เรื่องหลัก: 1. Foundation Stabilization 2. Production Isolation โดยยังไม่เริ่ม Customer Module --- # Part A: Foundation Stabilization ## A.1 Review Foundation Tables ตรวจสอบ schema ใหม่ที่เพิ่มจาก Task B ```txt msOptions documentSequences trAuditLogs ``` ตรวจสอบ: * unique constraint * indexes * foreign keys * organizationId consistency * soft delete support หากพบจุดที่ควรปรับปรุง ให้เขียนเป็น Migration Note ห้ามแก้ schema โดยไม่จำเป็น --- ## A.2 Create Foundation Seed สร้าง seed script ตัวอย่าง path: ```txt src/db/seeds/foundation.seed.ts ``` Seed เฉพาะ foundation data ### crm_branch ```txt สำนักงานใหญ่ Factory Service ``` ### crm_customer_status ```txt active inactive suspended ``` ### crm_customer_type ```txt company individual ``` ### crm_enquiry_status ```txt new qualifying requirement follow_up converted closed_lost cancelled ``` ### crm_quotation_status ```txt draft pending_approval approved rejected sent accepted lost cancelled revised ``` ### crm_product_type ```txt crane dockdoor solarcell ``` ### crm_currency ```txt THB USD EUR ``` ### crm_payment_term ```txt cash credit_30 credit_60 ``` ### crm_priority ```txt low normal high urgent ``` ### crm_lead_channel ```txt website referral sales facebook line other ``` --- ## A.3 Seed Document Sequences Seed initial document types ```txt customer contact enquiry quotation approval ``` Mapping ```txt CUS CON ENQ QT APV ``` Requirements: * idempotent * run ซ้ำได้ * ไม่สร้างข้อมูลซ้ำ * ใช้ organizationId จาก database * ห้าม hardcode organizationId หากไม่พบ organization ให้ throw error พร้อมข้อความชัดเจน --- ## A.4 Verify Foundation Services ตรวจสอบ service ที่สร้างใน Task B ```txt auth-context organization-context permission branch-scope master-options document-sequence audit-log ``` ตรวจสอบ: * naming consistency * return type consistency * error handling * organization-first design หากพบปัญหา ให้แก้เฉพาะ foundation layer --- # Part B: Production Isolation ## เป้าหมาย แยก legacy CRM mock ออกจาก production path เพื่อให้ Task C เริ่มบน production architecture เท่านั้น --- ## B.1 Audit Existing CRM Module ตรวจสอบ ```txt src/features/crm/** src/app/dashboard/crm/** ``` จำแนกเป็น: ### Production-ready ใช้ต่อได้ ### Legacy Mock ยังใช้ * mock data * in-memory state * placeholder service * fake API --- ## B.2 Move Mock Out of Production Path ห้ามลบไฟล์ ให้ย้ายไป: ```txt src/features/crm-mock/** ``` หรือ ```txt src/features/crm-demo/** ``` แล้วแต่ convention ที่เหมาะกับโปรเจ็ค --- ## B.3 Route Isolation Production route: ```txt /dashboard/crm ``` Mock route: ```txt /dashboard/crm-demo ``` Requirements: * ห้าม production route import mock service * ห้าม production route import mock API * ห้าม production component พึ่ง mock state --- ## B.4 Import Audit ตรวจสอบ import ทั้งระบบ ค้นหา: ```txt mock-api mock-api-users crm mock service fake data in-memory store ``` ถ้ายังถูก import ใน production path ให้แก้ --- ## B.5 Compile Error Cleanup แก้เฉพาะ: ```txt type errors missing imports unused exports broken references ``` ห้าม: * เปลี่ยน business flow * rewrite CRM module * เริ่ม Customer production --- # Deliverables สร้างเอกสาร ```txt docs/implementation/task-b1-foundation-stabilization.md ``` ต้องมี: ## Foundation Review * schema review * seed review * service review ## Production Isolation Review * production modules * demo modules * route separation ## Risks * remaining mock dependencies * migration concerns ## Task C Readiness --- # ห้ามทำ ```txt Customer CRUD Contact CRUD Enquiry CRUD Quotation CRUD Approval Workflow Dashboard KPI PDF Generator Reporting Notification ``` --- # ห้ามรัน ```txt npm run build npm run lint npm run test pnpm build pnpm lint pnpm test ``` จนกว่าจะได้รับคำสั่ง --- # Output ที่ต้องการ หลังทำเสร็จให้สรุป: 1. Files Added 2. Files Modified 3. Seed Scripts Added 4. Mock Modules Moved 5. Production Routes Verified 6. Remaining Risks 7. Task C Readiness --- # Definition of Done ✔ Foundation tables reviewed ✔ Foundation seed script created ✔ Document sequence seed created ✔ Foundation services reviewed ✔ Mock CRM isolated from production path ✔ Production route no longer depends on mock services ✔ Compile issues related to isolation fixed ✔ Task C can begin safely ✔ No Customer module created ✔ No Quotation module created