How adir-jscode/walletxpress-api works
1. What this repo is
- A TypeScript/Express backend for a digital wallet platform.
- The source of truth is in src/; dist/ is the compiled JavaScript build artifact for deployment/runtime.
- The app is organized as a modular monolith: one API process, multiple feature modules, shared middleware, and shared infrastructure utilities.
2. Runtime and entry points
- src/server.ts is the process entry point.
- src/app.ts builds the Express application, applies middleware, mounts routes, and likely initializes sockets.
- src/app/routes/index.ts acts as the route composition layer that wires feature routers under the API prefix.
- src/app/socket/socketManager.ts is a separate integration point for realtime events.
3. Core architectural pattern
- Each domain is split into controller, service, route, and where needed model, validation, and interface.
- Controllers are the HTTP adapter layer.
- Services hold business rules and transactional logic.
- Routes define endpoint grouping and attach middleware.
- This keeps request handling, domain logic, and persistence concerns separated.
4. Main feature boundaries
- auth: login, token refresh, logout, password change/reset.
- user: registration and admin-facing user/agent listings.
- otp: email-based OTP sending and verification.
- wallet: money movement operations such as add money, withdraw, send money.
- transaction: transaction history and ledger access.
- admin: wallet blocking/unblocking and agent approval/suspension.
- notification: persisted notification records and delivery support.
- ai: isolated AI-facing endpoints/services, kept separate from the wallet core.
5. Data and persistence layer
- MongoDB with Mongoose is the primary database layer.
- The modules with .model.ts files define the persisted entities: user, wallet, transaction, and notification.
- Business operations appear to read and mutate these models through services rather than directly from controllers.
- This is the main persistence boundary for the system’s state.
6. Money movement and transaction flow
- Wallet actions are central to the architecture and are handled in the wallet service.
- Transactions are described as atomic: balance updates and transaction records are coordinated together.
- The system tracks transaction type, amount, fee, commission, initiator, and status.
- Status progression is part of the domain model: pending, completed, reversed.
- Admin wallet blocking affects whether downstream money operations can proceed.
7. Identity, roles, and authorization
- Authentication uses JWTs.
- Role-based access control is enforced by checkAuth middleware.
- Roles are explicitly part of the domain model: admin, user, agent.
- Access boundaries are role-specific: users move money, agents perform cash-in/cash-out actions, admins supervise and control the system.
8. OTP, password reset, and email delivery
- OTP flows are split into their own module and use Redis as a temporary store for short-lived codes.
- Email delivery is handled via sendEmail utilities and EJS templates in src/app/utils/templates.
- This creates a clear async boundary: request triggers OTP generation, Redis stores it, email is sent, then verification updates user state.
9. Validation and error handling
- Request validation is layered in through validateRequest and Zod-based schemas in module-specific *.validation.ts files.
- The app has centralized error normalization for common failure types: cast errors, duplicate key errors, validation errors, and Zod errors.
- globalErrorHandler and notFound form the final HTTP error boundary.
- AppError is the common application-level exception type.
10. Shared utilities and cross-cutting concerns
- catchAsync standardizes async controller error propagation.
- sendResponse centralizes response formatting.
- setCookie and JWT helpers support session/token handling.
- seedAdmin suggests bootstrap logic for provisioning a default admin account.
- These utilities reduce duplication across modules and keep controllers thin.
11. Realtime and notifications
- The presence of socketManager plus a notification module indicates a secondary event-delivery path beyond plain REST.
- Notifications are modeled separately from transactions, which implies the system can persist events and optionally push them in realtime.
- This is a distinct integration boundary from the core money and auth flows.
12. Deployment shape
- vercel.json indicates the API is deployed in a serverless-friendly environment.
- dist/ is the production build output, so the runtime likely executes compiled JavaScript rather than TypeScript directly.
- Environment configuration is centralized in src/app/config/env.ts, with .env.example documenting required variables.
Main entry points: index.ts Routes · app.ts App · sendResponse.ts Response · wallet.route.ts Wallet
Components 25
Runtime
Process entry, app bootstrap, routes, and realtime integration.
Cross-cutting
Middleware, helpers, configuration, and shared infrastructure.
- Auth Guard · JWT and role-based access control.
src/app/middlewares/checkAuth.ts - Validate · Request schema validation boundary.
src/app/middlewares/validateRequest.ts - Errors · Final HTTP error normalization and handling.
src/app/middlewares/globalErrorHandler.ts - Env · Central environment configuration.
src/app/config/env.ts - Redis · Short-lived OTP and cache store configuration.
src/app/config/redis.config.ts - JWT · Token creation and verification helpers.
src/app/utils/jwt.ts - Email · Email delivery and template rendering.
src/app/utils/sendEmail.ts - Response · Standardized API response formatting.
src/app/utils/sendResponse.ts - Async Wrap · Async controller error propagation helper.
src/app/utils/catchAsync.ts
Feature modules
Domain modules exposed through routes and services.
- Auth · Login, refresh, logout, and password flows.
src/app/modules/auth/auth.route.ts - OTP · Email OTP generation and verification.
src/app/modules/Otp/otp.route.ts - Wallet · Cash-in, cash-out, send-money, and balance operations.
src/app/modules/wallet/wallet.route.ts - Txn · Ledger and transaction history access.
src/app/modules/transaction/transaction.route.ts - Admin · Wallet controls and agent supervision.
src/app/modules/admin/admin.route.ts - User · Registration and user/agent management.
src/app/modules/user/user.route.ts - Notify · Persisted notifications and delivery support.
src/app/modules/notification/notification.route.ts - AI · Isolated AI-facing endpoints and services.
src/app/modules/ai/ai.route.ts
Data layer
Primary persisted entities managed through Mongoose models.
- User Model · Persistent user and role state.
src/app/modules/user/user.model.ts - Wallet Model · Account balance and wallet state.
src/app/modules/wallet/wallet.model.ts - Txn Model · Atomic transaction records and status.
src/app/modules/transaction/transaction.model.ts - Notif Model · Stored notification records.
src/app/modules/notification/notification.model.ts
Connections 34
- Server → App: starts · Bootstraps the API process.
- App → Routes: mounts · Composes and mounts the API routers.
- App → Socket: initializes · Attaches realtime event handling.
- App → Auth Guard: uses · Applies auth and role controls.
- App → Validate: uses · Applies request validation.
- App → Errors: terminates with · Centralized error boundary.
- Routes → Auth: wires · Mounts auth endpoints.
- Routes → OTP: wires · Mounts OTP endpoints.
- Routes → Wallet: wires · Mounts wallet endpoints.
- Routes → Txn: wires · Mounts transaction endpoints.
- Routes → Admin: wires · Mounts admin endpoints.
- Routes → User: wires · Mounts user endpoints.
- Routes → Notify: wires · Mounts notification endpoints.
- Routes → AI: wires · Mounts AI endpoints.
- Auth → JWT: issues · Creates and verifies JWT sessions.
- Auth → Response: responds · Uses shared response formatting.
- Auth → Async Wrap: wraps · Async controller safety.
- OTP → Redis: stores · Keeps short-lived OTP codes.
- OTP → Email: delivers · Sends OTP and reset emails.
- OTP → Validate: validates · Schema validation for OTP requests.
- Wallet → Wallet Model: mutates · Updates balances and wallet state.
- Wallet → Txn Model: records · Persists transactional outcomes.
- Wallet → Auth Guard: protects · Role-gated money movement.
- Txn → Txn Model: reads · Serves ledger and history.
- Admin → Wallet Model: controls · Blocks and unblocks wallets.
- Admin → User Model: governs · Approves and suspends agents/users.
- User → User Model: creates · Registers and manages user state.
- Notify → Notif Model: persists · Stores notification records.
- Notify → Socket: pushes · Optional realtime delivery path.
- Wallet → Response: responds · Uses shared response formatting.
- Txn → Response: responds · Uses shared response formatting.
- Admin → Response: responds · Uses shared response formatting.
- User → Response: responds · Uses shared response formatting.
- Notify → Response: responds · Uses shared response formatting.