You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The EquipChain platform requires administrative endpoints for managing protocol configuration, user accounts, device registrations, and system parameters. These endpoints must be restricted to authenticated users with the admin role (authentication from Issue #5) and provide a complete CRUD interface for backend administration.
The admin API should include the following endpoint groups: User Management — list all users (GET /api/admin/users), get user details (GET /api/admin/users/:id), create user (POST /api/admin/users), update user roles (PATCH /api/admin/users/:id), deactivate user (DELETE /api/admin/users/:id); Configuration Management — get protocol configuration (GET /api/admin/config), update configuration values (PATCH /api/admin/config), reset to defaults (POST /api/admin/config/reset); Device Management — register device (POST /api/admin/devices), list devices (GET /api/admin/devices), update device metadata (PATCH /api/admin/devices/:id), remove device (DELETE /api/admin/devices/:id); System Monitoring — view system health (GET /api/admin/system/health), view connection pool stats (GET /api/admin/system/stats), view active WebSocket connections (GET /api/admin/system/ws-connections).
All endpoints must follow RESTful conventions, support pagination for list endpoints (leveraging Issue #17), return consistent JSON responses, and include proper error handling. Changes to configuration should be logged with the admin identity and timestamp for audit trail purposes.
Architecture: New routes under src/routes/admin/ directory with separate files for users.js, config.js, devices.js, system.js. A shared requireAdmin middleware checks req.user.roles includes 'admin'.
Impact: These endpoints enable platform operators to manage the system without direct database access. This is essential for production operations. The configuration management endpoints allow dynamic protocol parameter updates without code deployments.
Create Admin Routes: Create src/routes/admin/users.js with CRUD operations (initially using in-memory storage, ready for database integration). Create src/routes/admin/config.js with get/update/reset operations for protocol configuration. Create src/routes/admin/devices.js with registration and management endpoints. Create src/routes/admin/system.js with read-only monitoring endpoints.
Wire Routes in Application: Update src/index.js to mount admin routes under /api/admin with authenticate and requireAdmin middleware applied to all routes. Ensure proper error handling for database/storage failures.
Write Tests: Create tests/unit/admin.test.js testing business logic for user management, config operations. Create tests/integration/admin.test.js testing full request/response cycle with authentication.
Description
The EquipChain platform requires administrative endpoints for managing protocol configuration, user accounts, device registrations, and system parameters. These endpoints must be restricted to authenticated users with the
adminrole (authentication from Issue #5) and provide a complete CRUD interface for backend administration.The admin API should include the following endpoint groups: User Management — list all users (
GET /api/admin/users), get user details (GET /api/admin/users/:id), create user (POST /api/admin/users), update user roles (PATCH /api/admin/users/:id), deactivate user (DELETE /api/admin/users/:id); Configuration Management — get protocol configuration (GET /api/admin/config), update configuration values (PATCH /api/admin/config), reset to defaults (POST /api/admin/config/reset); Device Management — register device (POST /api/admin/devices), list devices (GET /api/admin/devices), update device metadata (PATCH /api/admin/devices/:id), remove device (DELETE /api/admin/devices/:id); System Monitoring — view system health (GET /api/admin/system/health), view connection pool stats (GET /api/admin/system/stats), view active WebSocket connections (GET /api/admin/system/ws-connections).All endpoints must follow RESTful conventions, support pagination for list endpoints (leveraging Issue #17), return consistent JSON responses, and include proper error handling. Changes to configuration should be logged with the admin identity and timestamp for audit trail purposes.
Technical Context & Impact
src/routes/admin/directory with separate files forusers.js,config.js,devices.js,system.js. A sharedrequireAdminmiddleware checksreq.user.rolesincludes 'admin'.Step-by-Step Implementation Guide
src/middleware/requireAdmin.jsthat readsreq.user(set by auth middleware from Issue [Testing] Expand Test Coverage with Integration Tests, API Tests, and Mock Blockchain Layer #5), checks forrolescontaining'admin', returns 403 if not authorized.src/routes/admin/users.jswith CRUD operations (initially using in-memory storage, ready for database integration). Createsrc/routes/admin/config.jswith get/update/reset operations for protocol configuration. Createsrc/routes/admin/devices.jswith registration and management endpoints. Createsrc/routes/admin/system.jswith read-only monitoring endpoints.src/schemas/admin.schema.js(if not already done in Issue [Security] Implement Input Validation with Zod Schemas and Request Sanitization #8) with schemas for user creation, config updates, device registration — each with appropriate constraints and required fields.src/index.jsto mount admin routes under/api/adminwithauthenticateandrequireAdminmiddleware applied to all routes. Ensure proper error handling for database/storage failures.tests/unit/admin.test.jstesting business logic for user management, config operations. Createtests/integration/admin.test.jstesting full request/response cycle with authentication.Verification & Testing Steps
GET /api/admin/userswith the admin JWT — expect 200 with a list of users (initially empty or seeded).POST /api/admin/userswith valid user data and admin JWT — expect 201 with the created user object.PATCH /api/admin/configwith valid configuration changes — expect 200 and verify that subsequent reads reflect the changes.DELETE /api/admin/users/:idto deactivate a user — expect 200 and verify the user is marked inactive in subsequent reads.