Introduction
MicroCBM uses Next.js 15 Server Actions to handle all data operations with the backend API. These server-side functions are marked with"use server" and provide type-safe, authenticated communication with the REST API.
Architecture
All Server Actions are located insrc/app/actions/ and follow a consistent pattern:
- Authentication: Automatic token injection via
requestWithAuthhelper - Error Handling: Consistent error responses with status codes and messages
- Type Safety: Full TypeScript types for requests and responses
- Pagination: Standard pagination with
page,limit, and metadata
Common Patterns
Authentication Flow
All actions automatically include the authentication token from cookies:Response Format
All mutation actions return anApiResponse object:
Pagination Format
List operations return paginated results:Error Handling
Server Actions handle errors gracefully:- 403 Forbidden: Returns empty data array (permission denied)
- Network Errors: Returns error with appropriate status code
- Validation Errors: Returns error message from backend
Available Actions
Asset Management
CRUD operations for assets and sites, plus analytics. View Asset Actions →Sample Management
Manage oil samples with wear metals, contaminants, and particle counts. View Sample Actions →Sampling Points
Create and manage sampling points on assets for oil analysis collection. View Sampling Point Actions →Sampling Routes
Organize sampling points into routes for systematic data collection. View Sampling Route Actions →Alarm Management
Monitor and acknowledge alarms with linked recommendations. View Alarm Actions →Recommendations
Create and manage maintenance recommendations with severity levels. View Recommendation Actions →Organizations & Sites
Manage organizational hierarchy: organizations, sites, and departments. View Organization Actions →User Management
Manage user accounts, activation, and profiles. View User Actions →Role & Permission Management
Control role-based access with roles and permissions. View Role Actions →Authentication
Handle user authentication, OTP verification, and password reset. View Auth Actions →Usage in Components
Server Actions can be used in both Server Components and Client Components:Best Practices
Always Check Response Status
Verifyresponse.success before accessing data:
Handle Empty Results
List operations return empty arrays on permission errors:Use Type Inference
Import payload types from@/schema for form validation:
Next Steps
Asset Actions
Learn about asset and site management actions
Sample Actions
Explore oil sample analysis actions
Sampling Points
Manage sampling points on assets
Sampling Routes
Organize sampling collection routes
Alarm Actions
Discover alarm monitoring and acknowledgment
Recommendation Actions
View maintenance recommendation actions
User Management
Manage user accounts and profiles
Roles & Permissions
Control role-based access