feat: store & display real-time driver location via MongoDB telemetry (#571)#631
Conversation
- Add Telemetry schema matching GeoJSON point geometry specifications - Implement high-speed atomic update/upsert pipeline in telemetryController - Add endpoints for polling or connecting active webhooks to map interfaces - Establish 2dsphere indexing for efficient real-time distance calculations Closes KanishJebaMathewM#571
|
🎉 Thank you for your contribution! Your pull request has been received and will be reviewed shortly. If you enjoy the project, please consider giving the repository a ⭐. You can also follow my GitHub profile to stay updated on future open-source projects. Thanks for being part of the community! 🚀 |
|
![Review Change Stack [link removed for security]]([link removed for security]) Caution Review failedPull request was closed or merged during review 📝 WalkthroughWalkthroughImplements end-to-end real-time driver location tracking. A new ChangesEnd-to-end real-time driver location tracking
Sequence Diagram(s)sequenceDiagram
rect rgba(100, 150, 200, 0.5)
note over LocationService,BackendTracker: Driver Side
LocationService->>BackendTracker: WebSocket location_ping {driver_id, orderId, lat, lng}
BackendTracker->>Supabase: resolve orderUUID by id/order_display_id
Supabase-->>BackendTracker: orderUUID, orderDisplayId
BackendTracker->>MongoDB: buffer telemetry record {driver_id, order_id, lat, lng}
BackendTracker->>Redis: cache {lat, lng}
BackendTracker->>WSClients: broadcast {latitude, longitude, order_display_id}
BackendTracker->>SupabaseRealtime: broadcast driver-location:{orderUUID} {lat, lng}
end
rect rgba(100, 200, 150, 0.5)
note over LiveTrackingScreen,MongoDB: Customer Side (initial load)
LiveTrackingScreen->>OrderService: fetchDriverLocation(orderId)
OrderService->>BackendREST: GET /orders/:id/driver-location
BackendREST->>MongoDB: find latest telemetry by driver_id
MongoDB-->>BackendREST: {lat, lng, timestamp}
BackendREST-->>OrderService: 200 {lat, lng}
OrderService-->>LiveTrackingScreen: initial coordinates → _updateTruckPosition
end
rect rgba(200, 150, 100, 0.5)
note over LiveTrackingScreen,SupabaseRealtime: Customer Side (live updates)
LiveTrackingScreen->>SupabaseRealtime: subscribe driver-location:{orderUUID}
SupabaseRealtime-->>LiveTrackingScreen: broadcast event {lat, lng}
LiveTrackingScreen->>LiveTrackingScreen: _updateTruckPosition(lat, lng)
end
Estimated code review effort🎯 4 (Complex) | ⏱️ ~60 minutes Possibly related PRs
Suggested labels
Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Comment
|
There was a problem hiding this comment.
Actionable comments posted: 6
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@server/controllers/telemetryController.js`:
- Around line 24-25: The error responses in the telemetryController.js file
expose sensitive internal error details to clients through error.message,
creating a security vulnerability. Replace the error.message values in both
error response locations (around lines 24 and 39) with a generic client-safe
message such as "An error occurred" or "An error occurred while processing your
request". Log the actual error details (including error.message) server-side
using your logging mechanism so that debugging information is available to
developers but not exposed to clients.
- Around line 30-32: The code is missing validation for the driverId parameter
before passing it to Telemetry.findOne(), which causes MongoDB cast errors to
result in 500 responses instead of 400 errors for invalid client input. Add
validation logic after extracting driverId from req.params to check if the ID
format is valid (typically checking if it matches MongoDB ObjectId format), and
return a 400 response with an appropriate error message if the validation fails.
This validation should occur before the Telemetry.findOne() call to prevent
MongoDB from attempting to cast an invalid ID.
- Around line 5-13: The driverId extracted from req.body is used directly in the
Telemetry.findOneAndUpdate query filter without type validation, creating a
NoSQL injection vulnerability where an attacker could pass MongoDB operators
like $ne or $gt instead of a string. Add a type check to ensure driverId is a
string (or appropriate primitive type) and reject the request if it is an object
or contains unexpected properties before using it in the findOneAndUpdate filter
with { driverId }.
- Around line 14-20: The upsert operation is missing coordinate validation and
schema validators. Add validation checks after parsing longitude and latitude to
ensure they are finite numbers within valid geographic ranges (longitude: -180
to 180, latitude: -90 to 90) before using them in the location coordinates
array. Additionally, add the `runValidators: true` option to the second
parameter of the upsert call alongside the existing `upsert: true, new: true`
options to enable schema-level validation during the update operation.
In `@server/models/Telemetry.js`:
- Around line 16-19: The coordinates field in the Telemetry model currently
accepts any number array without validating GeoJSON structure or valid
geospatial ranges. Add a custom validator function to the coordinates field that
enforces: the array contains exactly two numbers (longitude and latitude),
longitude values are between -180 and 180, and latitude values are between -90
and 90. This application-level validation should be added before MongoDB
processes the data to catch invalid geospatial coordinates early rather than
failing at write time.
- Around line 4-24: The Telemetry schema is missing the orderId field that
tracker.js is buffering with order_display_id, causing data loss at write time.
Remove the unique: true constraint from the driverId field since telemetry is
time-series data requiring multiple location updates per driver, not a live
cache. Add an orderId field with a reference to the Order model to persist order
linkage. Finally, add a TTL index to the schema using the index method with
expireAfterSeconds to automatically expire old telemetry records according to
the architecture documentation requirements, ensuring that updatedAt field is
used as the TTL reference point.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro Plus
Run ID: e8ea57d0-2e87-481e-af48-a47da0eb6db3
📒 Files selected for processing (3)
server/controllers/telemetryController.jsserver/models/Telemetry.jsserver/routes/telemetryRoutes.js
19486a2
into
KanishJebaMathewM:main
|
🎉 Thank you for your contribution! Your pull request has been merged successfully. We appreciate your work and look forward to your future contributions. 🚀 |
…db-telemetry feat: store & display real-time driver location via MongoDB telemetry (#571)
Pull Request Description
This PR integrates a backend pipeline to store, update, and read real-time driver telemetry coordinates via MongoDB. It creates a scalable data model optimized for geospatial processing to support live tracking rendering across the app interface.
Closes #571
Type of Change
Submission Checklist
Telemetrycollection structure following explicit GeoJSON conventions[longitude, latitude]/api/telemetry/location)2dspherespatial indexing for future proximity mapping featuresArchitecture Breakdown
location.coordinates[Lng, Lat]2dspheredriverId