8.5 KiB
Task B Template Audit
1. Executive Summary
Task B implemented the shared foundation layer for ALLA OS CRM vNext without introducing CRM business modules such as Customer, Contact, Enquiry, Quotation, Approval, KPI, Report, or PDF generation.
The foundation now provides:
- Current user context helpers
- Organization context helpers
- Permission helpers
- Branch scope abstraction
- Master options service and API
- Document sequence service
- Audit log helper
This work follows the Task A convention freeze:
organizationIdremains the tenant boundary- membership and permissions remain organization-scoped
branchIdis treated as business sub-scope only- existing Auth.js and shared auth helpers are reused rather than replaced
2. Files Added
Documentation
docs/implementation/task-b-template-audit.md
Schema / Migration
drizzle/0001_orange_mandarin.sqldrizzle/meta/0001_snapshot.json
Foundation Helpers and Services
src/features/foundation/auth-context/types.tssrc/features/foundation/auth-context/service.tssrc/features/foundation/organization-context/service.tssrc/features/foundation/permission/service.tssrc/features/foundation/branch-scope/types.tssrc/features/foundation/branch-scope/service.tssrc/features/foundation/master-options/types.tssrc/features/foundation/master-options/service.tssrc/features/foundation/master-options/api/types.tssrc/features/foundation/master-options/api/service.tssrc/features/foundation/master-options/api/queries.tssrc/features/foundation/master-options/api/mutations.tssrc/features/foundation/master-options/components/columns.tsxsrc/features/foundation/master-options/components/master-options-table.tsxsrc/features/foundation/master-options/components/master-options-listing.tsxsrc/features/foundation/document-sequence/types.tssrc/features/foundation/document-sequence/service.tssrc/features/foundation/audit-log/types.tssrc/features/foundation/audit-log/service.ts
API Route
src/app/api/foundation/master-options/route.ts
3. Files Modified
src/db/schema.tssrc/app/dashboard/crm/settings/master-options/page.tsxdrizzle/meta/_journal.jsondrizzle/meta/0000_snapshot.json
4. Current User Context Output
Implemented in:
src/features/foundation/auth-context/service.ts
Helpers added:
getCurrentUser()requireCurrentUser()getCurrentUserContext()
Returned context shape:
useractiveOrganizationmembershippermissionsbusinessRole
Implementation notes:
- Reuses
auth()fromsrc/auth.ts - Reuses
requireSession()fromsrc/lib/auth/session.ts - Does not duplicate sign-in or session enrichment logic
5. Organization Context Output
Implemented in:
src/features/foundation/organization-context/service.ts
Helpers added:
getCurrentOrganization()getActiveOrganizationId()requireOrganizationAccess()
Implementation notes:
- Re-exports the existing shared
requireOrganizationAccess()helper - Reuses current Auth.js active organization state
- Preserves
organizationIdas the only tenant boundary
6. Permission Layer Output
Implemented in:
src/features/foundation/permission/service.ts
Helpers added:
hasPermission(permission)requirePermission(permission)hasBusinessRole(role)
Implementation notes:
- Reuses
memberships.permissions - Reuses
activeBusinessRole - Gives
super_admina bypass through existing session semantics - Does not introduce a second RBAC model
7. Branch Scope Output
Implemented in:
src/features/foundation/branch-scope/service.ts
Helpers added:
getUserBranches()getActiveBranch()validateBranchAccess()
Implementation notes:
- Branches are currently abstracted through master options category
crm_branch - No CRM branch UI was added
- No direct schema migration for a full branch domain model was added
- Branch remains a business scope, not a tenant model
8. Master Options Output
Implemented in:
src/features/foundation/master-options/service.tssrc/features/foundation/master-options/api/**src/app/api/foundation/master-options/route.ts
Schema added in:
src/db/schema.tsasmsOptions
Helpers and services added:
listMasterOptions()getOptionsByCategory()getActiveOptionsByCategory()
Supported characteristics:
- organization-scoped option records
- category-based lookup
- parent / child option structure
- active / inactive state
- soft delete via
deletedAt - reusable query layer for future CRM modules
Supported category constants:
crm_branchcrm_customer_statuscrm_customer_typecrm_enquiry_statuscrm_quotation_statuscrm_product_typecrm_currencycrm_payment_termcrm_prioritycrm_lead_channel
9. Document Sequence Output
Implemented in:
src/features/foundation/document-sequence/service.ts
Schema added in:
src/db/schema.tsasdocumentSequences
Helpers added:
previewNextDocumentCode()generateNextDocumentCode()
Implementation notes:
previewdoes not incrementgenerateincrements- generation uses transaction plus row lock behavior
- code generation stays on the server
Current default prefixes:
customer -> CUScontact -> CONenquiry -> ENQquotation -> QTapproval -> APV
10. Audit Log Output
Implemented in:
src/features/foundation/audit-log/service.ts
Schema added in:
src/db/schema.tsastrAuditLogs
Helpers added:
auditCreate()auditUpdate()auditDelete()auditAction()
Supported fields:
beforeDataafterDatarequestIdorganizationIdbranchIduserIdentityTypeentityIdaction
11. UI Output
Task B only touched the allowed foundation testing page:
src/app/dashboard/crm/settings/master-options/page.tsx
What changed:
- the page no longer points at the old CRM mock reference query
- it now uses the new foundation master options listing flow
- it keeps the existing dashboard shell and
PageContainerpattern - it keeps the existing React Query plus DataTable pattern
What was intentionally not added:
- no new dashboard shell
- no new design system
- no CRM business forms or CRUD screens
12. Reuse From Template / Existing Repo
Reused foundation from the current repo:
src/auth.tssrc/lib/auth/session.tssrc/lib/auth/rbac.tssrc/lib/api-client.tssrc/lib/query-client.tssrc/lib/searchparams.tsPageContainer- existing DataTable primitives
- existing React Query hydration pattern
- existing Auth.js session enrichment
Production reference patterns reused:
src/features/products/**src/features/users/**
13. Migration Still Needed
Future work still required after Task B:
- seed data or admin flows for
ms_options - seed or setup flows for
document_sequences - actual CRM entity tables and route handlers
- mutation-level integration that calls
auditCreate,auditUpdate,auditDelete - branch-to-user assignment model if the product needs something stricter than option-based abstraction
- replacement of remaining CRM mock services and pages under
src/features/crm/**
14. Risks
Known risks after Task B:
- the foundation tables exist in schema and migration, but runtime depends on applying the migration
- branch scope is still abstraction-only and not yet backed by a dedicated operational model
- master options page is migrated, but the rest of CRM still points at mock data
- some compile errors still exist in legacy CRM mock files outside the Task B foundation scope
15. Verification Notes
Completed during Task B:
- generated Drizzle migration for the new foundation tables
- formatted the new and changed files
- checked TypeScript compilation
Compilation note:
- foundation-layer code added for Task B was cleaned up against its own type issues
- project-wide
tsc --noEmitstill reports existing errors in legacy CRM mock files outside the new foundation path
16. Task C Readiness
Task C can now start on top of this foundation with the following ready:
- shared user and organization context helpers
- permission guard helpers
- branch access abstraction
- option lookup abstraction for statuses and enumerations
- server-side document code generation
- shared audit logging helper
Recommended Task C direction:
- build
CustomerandContacton top of the new foundation services - keep
organizationIdmandatory on all new CRM entities - use
branchIdonly as business sub-scope where needed - wire all mutations through sequence and audit helpers where relevant