Frontend-Backend Workflow Alignment¶
This document provides guidance on aligning the frontend and backend workflow implementations in the PRS system, with a focus on the Zustand stores in the frontend and database status fields in the backend.
Current Architecture¶
The PRS system uses a layered architecture with distinct frontend and backend components:
- Frontend: React application with Zustand for state management and React Query for API integration
- Backend: Node.js application with Fastify framework and Sequelize ORM for database access
Workflow Visualization¶
The following diagram illustrates the overall workflow architecture, showing how user actions flow through the system:
graph TD
User([User]) --> UI[Frontend UI]
UI --> Actions[Action Handlers]
Actions --> ZustandStores[Zustand Stores]
Actions --> ReactQuery[React Query]
ReactQuery --> Backend[Backend API]
Backend --> Database[(Database)]
ReactQuery --> UI
ZustandStores --> UI
Draft[Draft] --> Submitted[Submitted]
Submitted --> Approved[Approved]
Submitted --> Rejected[Rejected]
CSDraft[CS Draft] --> CSForApproval[For CS Approval]
CSForApproval --> CSApproved[CS Approved]
Workflow Sequence Diagram¶
The following sequence diagram shows the interaction between frontend and backend components during a typical workflow:
sequenceDiagram
participant User
participant UI
participant ZS as Zustand
participant RQ as ReactQuery
participant API
participant DB as Database
User->>UI: Create Requisition
UI->>ZS: Initialize store
User->>UI: Add items
UI->>ZS: Update store
User->>UI: Submit
UI->>RQ: Submit mutation
RQ->>API: PUT request
API->>DB: Update status
API->>RQ: Return response
RQ->>UI: Update UI
Current Challenges¶
The PRS system currently faces several challenges related to frontend-backend workflow alignment:
- Inconsistent Status Representation:
- Frontend and backend use different status values
- Status mapping is scattered across multiple components
-
No centralized status constants
-
No Central Workflow Management:
- Status transitions are handled independently in frontend and backend
- No shared validation logic between frontend and backend
-
Workflow rules are duplicated across components
-
Optimistic Updates Without Validation:
- Frontend may update UI before backend confirms status change
- No rollback mechanism if backend rejects status change
-
Potential for inconsistent state
-
Incomplete Permission Checks:
- Frontend may show actions that backend would reject
- Permission checks are not consistently applied
-
No centralized permission management
-
Zustand Store and Database Synchronization:
- Zustand stores in frontend don't properly synchronize with backend database status fields
- Local state may become out of sync with server state
- No clear mechanism for handling concurrent updates
Recommendations for Improving Alignment¶
1. Create Shared Status Constants¶
Create a shared module for status constants:
2. Create a Status Display Utility¶
Create a utility for consistent status display:
3. Implement a Workflow State Service¶
Create a service to manage workflow state:
4. Enhance React Query Mutations¶
Enhance React Query mutations with proper error handling and invalidation:
Implementation Roadmap¶
To improve frontend-backend workflow alignment, follow this phased approach:
Phase 1: Documentation and Analysis¶
- Document current status values and transitions
- Identify inconsistencies between frontend and backend
- Create a status mapping document
- Define valid status transitions for each workflow
Phase 2: Standardization¶
- Create shared status constants
- Implement status display utilities
- Update UI components to use the utilities
- Standardize status values in the backend
Phase 3: Workflow Management¶
- Implement workflow state service
- Add validation for status transitions
- Enhance error handling for invalid transitions
- Implement proper query invalidation
Phase 4: Testing and Verification¶
- Test all workflow transitions
- Verify that frontend and backend stay in sync
- Test edge cases like concurrent updates
- Document the improved workflow management system