How parthi952/pvms_backend works
Purpose and entry points
This is a REST backend for visitor-pass lifecycle management: account administration, login/token issuance, host approval, check-in/out, and pass querying. The Express process starts in Setup.js, establishes MongoDB connectivity through DB_Connaction.js, mounts API routes, and applies global error handling. Database bootstrap/seeding is handled by Setup.js and package scripts.
HTTP routing boundary
Routes expose three resource groups under /api: authentication, users, and visitors. Route modules bind HTTP verbs to controllers and attach protection/role middleware before business handlers.
• Routes/Login.js
• Routes/User.js
• Routes/Visitors.js
Authentication and authorization
Login validates user credentials and returns access/refresh JWTs. Protected requests pass through token verification, which populates req.user; role authorization gates administrative user operations and selected visitor mutations. The authentication middleware also supports request-header fallback identities, an important non-production/integration boundary.
• Controller/LoginControll.js
• Middleware/AuthMiddleware.js
• Models/UserDB.js
User account domain
Users represent admins, receptionists, and employees/hosts. Creation is admin-controlled, normalizes and enforces email uniqueness, assigns employee IDs when omitted, and relies on a Mongoose save hook to hash passwords. User records are the identity source for login, RBAC, and visitor-host association.
• Controller/UserController.js
• Models/UserDB.js
• Routes/User.js
Visitor-pass lifecycle
Visitor requests are created, listed, fetched, edited, status-transitioned, and deleted through the visitor controller. The core state machine includes Pending, Approved, Rejected, CheckedIn, CheckedOut, and Cancelled; transitions enforce approval before check-in and checked-in state before checkout. Creation additionally enforces one active/same-day visit per phone number, valid future/current scheduling, and a maximum of three pending requests per host.
• Controller/VisitorController.js
• Models/VisitorData.js
• Routes/Visitors.js
Persistence and audit model
MongoDB is the sole persistence service, accessed via Mongoose. User and visitor schemas define document validation/defaults; the visitor model includes nested activity/audit information for lifecycle events. Controllers form the application boundary between HTTP inputs and MongoDB queries/updates.
• DB_Connaction.js
• Models/UserDB.js
• Models/VisitorData.js
Request/error flow and runtime
The primary control flow is: Express request → route middleware (JWT/RBAC) → async controller → Mongoose model → JSON response; thrown async errors are forwarded to the centralized formatter. The service runs on Node.js 18+ with Express 5, MongoDB (local or Atlas), Mongoose, JWT, bcryptjs, and permissive CORS configured in the server entry point. Secrets and connection configuration come from environment variables, including MongoDB URI and JWT signing keys.
• Setup.js
• Middleware/ErrorHandle.js
• Middleware/Constants.js
Main entry points: Setup.js Express Bootstrap · ErrorHandle.js Error Formatter · UserDB.js User Model · VisitorController.js Visitor Controller
Components 14
Runtime
- Express Bootstrap · Starts the service, mounts APIs, connects MongoDB, and installs global errors.
Setup.js - MongoDB Connection
DB_Connaction.js - Error Formatter
Middleware/ErrorHandle.js - Error Constants
Middleware/Constants.js
HTTP API
- Auth Routes · Exposes login and token issuance endpoints.
Routes/Login.js - User Routes · Administrative account endpoints.
Routes/User.js - Visitor Routes · Visitor-pass lifecycle endpoints.
Routes/Visitors.js - JWT and RBAC Middleware · Verifies tokens, populates req.user, and supports header fallback identities.
Middleware/AuthMiddleware.js
Domain Logic
- Login Controller · Validates credentials and issues access and refresh JWTs.
Controller/LoginControll.js - User Controller · Admin-controlled user creation and management.
Controller/UserController.js - Visitor Controller · Enforces scheduling, host limits, and visitor state transitions.
Controller/VisitorController.js
Persistence
- User Model · Identity, roles, host association, and password hashing hook.
Models/UserDB.js - Visitor Model · Visitor pass state and nested lifecycle audit activity.
Models/VisitorData.js - MongoDB · Local or Atlas document persistence.
Connections 22
- Express Bootstrap → MongoDB Connection: connects
- Express Bootstrap → Auth Routes: mounts /api
- Express Bootstrap → User Routes: mounts /api
- Express Bootstrap → Visitor Routes: mounts /api
- Express Bootstrap → Error Formatter: installs
- Error Formatter → Error Constants: formats
- Auth Routes → Login Controller: dispatches
- User Routes → JWT and RBAC Middleware: protects and authorizes
- Visitor Routes → JWT and RBAC Middleware: protects selected mutations
- User Routes → User Controller: dispatches
- Visitor Routes → Visitor Controller: dispatches
- Login Controller → User Model: validates credentials
- JWT and RBAC Middleware → User Model: resolves identity and roles
- User Controller → User Model: manages accounts
- Visitor Controller → Visitor Model: manages passes and audit
- Visitor Controller → User Model: validates host
- MongoDB Connection → MongoDB: opens connection
- User Model → MongoDB: persists
- Visitor Model → MongoDB: persists
- Login Controller → Error Formatter: forwards async failures
- User Controller → Error Formatter: forwards async failures
- Visitor Controller → Error Formatter: forwards async failures