feat: validate dropoff location on delivery completion - #165
Merged
Tybravo merged 2 commits intoAug 30, 2026
Merged
Conversation
|
@obswrld Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits. You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀 |
- Fix authService.verifyToken to check decoded.userId first - Store raw JWT token on socket.data during connection handshake - Add SocketService.validateSocketToken for DB-backed token validation - Implement setupTokenExpirationCheck in locationHandler: - Periodic JWT validation on configurable interval (default 60s) - Emit auth_expired event with grace period (default 30s) - Handle auth_refresh to accept new JWT without reconnecting - Graceful disconnect if token not refreshed in time - Extend socket.types with AuthExpiredPayload, AuthRefreshPayload, etc. - Add unit tests for validateSocketToken and token expiration flow
Collaborator
|
@obswrld Thanks for contributing |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
closes #150
Summary
Add a server‑side geofence check that guarantees a driver can only set a delivery’s status to completed when they are physically
at the drop‑off location. The validation pulls the driver’s latest GPS coordinates from the LocationUpdate collection,
calculates the haversine distance to the delivery’s dropoffCoordinates, and rejects the request if the driver is farther than
200 m (0.2 km).
──────
Problem
• The updateDeliveryStatus endpoint allowed any client to mark a delivery as completed regardless of the driver’s actual
location.
• This opened the door for fraudulent “completed” reports (e.g., a driver could claim delivery was finished without delivering
the package).
──────
🛠️ Solution / Changes
src/services/authService.ts, | No functional changes – these files were staged automatically
src/sockets/connectionHandler.ts, src/sockets/socket.types.ts | during the commit but remain untouched.
src/models/LocationUpdate.ts (already present) | Utilized for fetching the driver’s latest GPS record; no
| source modifications required.
All modifications respect the existing Controller → Service → Model layered architecture and only use data retrieved from
MongoDB—no hard‑coded or mock values.
──────
Impact
• Security – Prevents fraudulent completion reports; helps enforce proof‑of‑delivery integrity.
• User Experience – Drivers whose device fails to send a location will receive a clear error (No recent driver location
available for validation).
• Performance – A single indexed lookup on LocationUpdate (driverId + deliveryId) and a lightweight distance calculation;
negligible overhead.
──────
Verification / Testing
• Send PATCH /api/v1/deliveries/:id/status with body { "status": "completed" } while the driver’s last known location (in
LocationUpdate) is inside the 200 m radius.
• Expected: 200 OK, delivery status updated to completed.
• Same request, but the driver’s location is > 200 m from dropoffCoordinates.
• Expected: 400 Bad Request with message like Driver is too far from drop-off location (distance: X.XX km).
• No LocationUpdate record for the driver/delivery.
• Expected: 400 Bad Request with message No recent driver location available for validation.
• Verify that transitions such as pending → assigned, assigned → picked_up, etc., bypass the proximity check and succeed as
before.
• npm test (or pnpm test) – all existing tests should still pass.
Add automated unit tests for the new validation logic if the project’s CI requires coverage.
──────
Files Modified