Skip to main content

Overview

Maintenance mode locks non-admin users out of the system during deployments, schema migrations, or critical updates. This ensures data consistency and prevents user actions from interfering with system changes.
Only users with the ADMIN role can access the system during maintenance mode.

How It Works

When maintenance mode is enabled:
  • Admin users: Full system access continues
  • All other users (Supervisors, Credit Officers): Receive a 503 error with message:
    System is currently under maintenance. Please try again later.
    
The check occurs at the authentication middleware level (backend/src/middlewares/auth.middleware.ts:78-89).

API Endpoint

Get Maintenance Status

GET /api/settings/maintenance
Response:
{
  "success": true,
  "data": {
    "maintenanceMode": false
  }
}

Toggle Maintenance Mode

PUT /api/settings/maintenance
Content-Type: application/json

{
  "enabled": true
}
Response:
{
  "success": true,
  "data": {
    "maintenanceMode": true
  },
  "message": "Maintenance mode enabled successfully"
}
Only ADMIN users can enable/disable maintenance mode. This endpoint is protected by the requireRole(Role.ADMIN) middleware.

Database Schema

Maintenance mode state is stored in the CompanySetting model:
model CompanySetting {
  id              String   @id
  maintenanceMode Boolean  @default(false)
  // ... other fields
}
Location: backend/prisma/schema.prisma:366

Use Cases

1. Safe Schema Migrations

# Step 1: Enable maintenance mode via API or UI
curl -X PUT https://your-api.com/api/settings/maintenance \
  -H "Authorization: Bearer YOUR_ADMIN_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{"enabled": true}'

# Step 2: Run database migration
npx prisma migrate deploy

# Step 3: Deploy new code
git push production main

# Step 4: Disable maintenance mode
curl -X PUT https://your-api.com/api/settings/maintenance \
  -H "Authorization: Bearer YOUR_ADMIN_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{"enabled": false}'

2. Emergency System Lockdown

If critical issues are detected:
  1. Enable maintenance mode immediately
  2. Investigate and resolve the issue
  3. Verify system integrity
  4. Disable maintenance mode

3. Data Restoration

When restoring from backup:
  1. Enable maintenance mode
  2. Run restore operation (see Backups)
  3. Verify data integrity
  4. Disable maintenance mode

Implementation Details

Backend Check Flow

  1. Authentication Middleware (auth.middleware.ts:78):
    // Check maintenance mode (allow admins only)
    if (user.role !== "ADMIN") {
      const isMaintenance = await SettingsService.isMaintenanceModeActive();
      if (isMaintenance) {
        return ApiResponseUtil.error(
          res,
          "System is currently under maintenance. Please try again later.",
          503
        );
      }
    }
    
  2. Settings Service (settings.service.ts:358):
    static async isMaintenanceModeActive(): Promise<boolean> {
      const settings = await prisma.companySetting.findFirst({
        select: { maintenanceMode: true },
      });
      return settings?.maintenanceMode ?? false;
    }
    

Audit Logging

Maintenance mode toggles are automatically logged via the auditLog middleware:
auditLog("MAINTENANCE_MODE_TOGGLED", "Settings")
View audit trail:
GET /api/audit-logs?action=MAINTENANCE_MODE_TOGGLED

Best Practices

1

Communicate in advance

Notify users 24-48 hours before scheduled maintenance via email or system announcement.
2

Choose off-peak hours

Enable maintenance mode during periods of lowest user activity (typically late night/early morning).
3

Test in staging first

Always test deployment procedures in a staging environment before production.
4

Monitor actively

Keep the health check endpoint available during maintenance: GET /api/health
5

Document the change

Record the reason for maintenance in audit logs or deployment notes.

Frontend Integration

When the frontend receives a 503 response, display a user-friendly maintenance page:
if (error.response?.status === 503) {
  // Show maintenance banner
  return {
    message: "System is under maintenance. We'll be back shortly!",
    retryAfter: 300 // seconds
  };
}

Build docs developers (and LLMs) love