This document explains the agents (long-running services, jobs, and helper modules) that make up the Gorda automation stack. Each agent encapsulates specific responsibilities so the team can reason about scaling, alerting, and on-call runbooks.
- Boots the Express server, HTTPS mirror, and attaches Socket.IO.
- Wires controllers under
/src/Api/Controllers/**for WhatsApp webhooks, notifications, polygon uploads, and admin dashboard routes. - Registers Sentry, CORS, static assets, and JSON parsing middleware.
- Owns the lifecycle of WhatsApp client instances (see below) via the dependency
Store+Containerbootstrap.
- Spins up one instance per configured
WpCliententry. - Bridges outbound messages from repositories/services to WhatsApp transports (Baileys, whatsapp-web.js, or official API depending on client metadata).
- Emits session state (QR, connection status, events) to Socket.IO for the admin UI under
wpServices[client.id]. - Delegates message classification to the chatbot service and persists audit trails via repositories.
- Parses inbound
ChatBotMessagepayloads, routes them throughMessagesEnum, and calls the correct handler strategy. - Relies on repository interfaces (
ChatRepository,ClientRepository, etc.) to pull context before formulating replies. - Pushes follow-up work to the BullMQ queues when a conversation requires async processing (e.g., fee recalculation, driver search).
- Caches branches, WhatsApp clients, and settings so that the Socket/HTTP layers can read them without repeating DB calls.
- Emits hydrated data into memory upon app start (
store.getBranches(),store.getWpClients()inapp.ts). - Acts as a central registry for downstream agents needing tenant-level configuration.
| Job | File | Purpose | Trigger |
|---|---|---|---|
| RemoveConnectedDrivers | RemoveConnectedDrivers.ts |
Clears lingering driver sessions to prevent ghost availability. | Cron via node-cron in Schedule.ts. |
| CloseSessionsJob | CloseSessionsJob.ts |
Auto-closes stale chat sessions and notifies admins. | Cron. |
| PopulateMetrics | PopulateMetrics.ts |
Aggregates usage metrics into analytics tables or Firebase. | Cron / manual. |
| SetDynamicMinFeeJob | SetDynamicMinFeeJob.ts |
Adjusts minimum ride fee based on demand windows. | Cron. |
| SetDynamicMultiplierFeeJob | SetDynamicMultiplierFeeJob.ts |
Maintains surge multipliers per branch. | Cron. |
| CancelPendingServicesJob | CancelPendingServicesJob.ts |
Auto-cancels pending services older than 15 minutes to prevent stale ride requests. | Cron (every 5 minutes). |
| RemoveConnectedDrivers | RemoveConnectedDrivers.ts |
Runs both as scheduled task and callable helper for incident response. | Cron + manual. |
Schedule.ts exports the cron definitions that wire these jobs to node-cron. Each job usually coordinates with repositories (SessionRepository, DriverRepository, etc.) and may enqueue notifications through WhatsApp or Firebase.
- Built on BullMQ (
bullmqdependency) for Redis-backed background processing. - Typical queues: message delivery retries, Firebase notifications, large polygon/KML imports, and heavy reporting tasks.
- Workers pull helpers from
/src/Helpers(file I/O, date utilities) and persist transactional data via Sequelize models in/src/Models. - Configure new workers by extending the base queue service and registering them inside the container so they share logging and configuration.
sequelize.tsexports the configured Sequelize instance (PostgreSQL driver). Migration files live underDatabase/Migrationswhile seeders reflect initial data loads.- Models under
/src/Modelsadhere to interfaces in/src/Interfaces, ensuring that services stay strongly typed.
- Each repository (
ClientRepository,PlaceRepository, etc.) acts as an internal API for queries. Services never hit Sequelize directly; they request data through repositories so that caching, eager-loading, and auditing are centralized.
- Pushes FCM notifications for drivers and admins using
firebase-admincredentials fromfirebaseAccount.json. - Works with
NotificationControllerto broadcast alerts triggered by HTTP calls or background jobs.
- Reuses the WhatsApp client agent but exposes helper methods (see
WpNotificationRepositoryandServices/whatsapp) that translate domain events into WhatsApp templates or plain text.
- Locale assets live under
src/Localeswithlocale.jswiring thei18nmodule. Locale.getInstance()is invoked at bootstrap so every agent can call translation helpers without reconfiguration.
- Bootstrap:
npm run buildfollowed bynpm run serve(or PM2 usingecosystem.config.example.js). Ensure environment variables, Firebase credentials, and SSL certs (src/Helpers/SSL.ts) are available. - Monitoring: Sentry DSN configured; check Socket.IO logs for WhatsApp reconnect loops.
- Scaling WhatsApp Clients: Add rows through the admin panel or seeders; the
Storehot-reloads clients andapp.tsinstantiates aWhatsAppClientper tenant. - Queue Health: Inspect BullMQ dashboard (if configured) or Redis metrics, especially before marketing campaigns.
- Backups: Keep current dumps of PostgreSQL (migrations + data) and Firebase service accounts. Rotate SSL certificates referenced in
config.APP_DOMAIN.
- Comments: Avoid unnecessary comments. Code should be self-explanatory through clear naming and structure.
- Necessary Comments: When comments are needed (complex business logic, non-obvious behavior, or API quirks), write them in English.
- Language: All code, variable names, function names, and documentation must be in English for consistency across the codebase.
- Documentation & Plans: All planning documents, technical specifications, and implementation plans must be written in English and stored in the
/docsdirectory. Plans should include problem description, proposed solution, implementation steps, test cases, and rollback procedures.
- Define the responsibility and data flow (HTTP, queue, cron, or helper).
- Create a service module under
src/Services/<agent>with clear interfaces. - Register any scheduled tasks in
src/Jobs/Schedule.tsand expose configuration throughStoreif tenant-specific. - Update this
agents.mdfile outlining the new agent’s trigger, dependencies, and failure modes.
Keeping agents loosely coupled (controllers → services → repositories) makes migrations, scaling, and debugging significantly easier. Always document how new agents are started, monitored, and stopped so the on-call team can respond quickly.