feat: Add profile picture uploads, Redis escrow locking, and fix Socket.io and ETA edge cases - #161
Merged
Conversation
…vent concurrent double-spending
…tion and timestamp validation
…al distance calculation
|
@Rofeeah-Tijani 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! 🚀 |
Collaborator
|
@Rofeeah-Tijani Please resolve conflict and Rename your PR Title to a descriptive name of the issues you implemented |
Collaborator
|
@Rofeeah-Tijani Well done job Thanks |
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.
Combined PR: Multiple Enhancements and Bug Fixes
This PR combines 4 separate issues addressing critical enhancements and bug fixes for the SwiftChain backend.
📋 Issues Resolved
🎯 Overview
This combined PR implements four independent features that enhance the reliability, functionality, and accuracy of the SwiftChain backend:
🔧 Feature 1: Redis Redlock for Escrow Release (#142)
🎯 Goal
Prevent concurrent requests from releasing the same Escrow twice (double-spending prevention).
📋 Implementation
Added Files
src/config/redis.ts- Redis client configuration and Redlock setupsrc/services/escrow.service.ts- New escrow service with distributed lockingsrc/controllers/escrow.controller.ts- Controller for escrow release endpointREDIS_REDLOCK_IMPLEMENTATION.md- Comprehensive documentationModified Files
src/routes/escrow.routes.ts- Added POST/api/v1/escrow/releaseendpointsrc/server.ts- Redis initialization and graceful shutdownsrc/app.ts- Health check with Redis statussrc/config/env.ts- Redis environment variables.env.example- Redis configuration templateKey Features
Technical Details
Environment Variables
🔧 Feature 2: Socket.io Reconnection Fix (#140)
🎯 Goal
Ensure location updates are processed idempotently to prevent duplicate updates during reconnections.
📋 Implementation
Modified Files
src/sockets/location.service.ts- Three-layer deduplication systemsrc/sockets/socket.types.ts- AddedisDuplicateandisStaleflagsSOCKET_DEDUPLICATION_FIX.md- Comprehensive documentationThree-Layer Defense System
Layer 1: Redis-Based Deduplication
location:dedup:{driverId}:{deliveryId}:{lat}:{lng}:{timestamp}Layer 2: Timestamp Validation
Layer 3: Stale Update Detection
Key Features
Technical Details
🔧 Feature 3: Profile Picture Upload (#139)
🎯 Goal
Allow users and drivers to upload profile pictures with automatic processing and secure storage.
📋 Implementation
Added Files
src/services/profilePicture.service.ts- Image processing and upload servicesrc/controllers/profileController.ts- Profile management endpointssrc/routes/profileRoutes.ts- Profile routes with Multer configurationPROFILE_PICTURE_UPLOAD.md- Comprehensive documentationModified Files
src/routes/index.ts- Mounted profile routes at/api/v1/profilesrc/services/storage.service.ts- Enhanced with custom path supportsrc/interfaces/IUser.ts- AddedprofilePictureandprofilePictureKeyfieldssrc/models/User.ts- Updated schema with profile picture fieldssrc/config/env.ts- Profile picture configuration variables.env.example- Profile picture settingsKey Features
API Endpoints
POST
/api/v1/profile/pictureDELETE
/api/v1/profile/pictureGET
/api/v1/profileImage Processing
Environment Variables
🔧 Feature 4: Haversine Anti-Meridian Fix (#146)
🎯 Goal
Ensure accurate distance calculation globally by fixing edge cases near the anti-meridian (±180° longitude).
📋 Implementation
Modified Files
src/services/routingService.ts- Fixed Haversine formulaAdded Files
tests/routingService.test.ts- 27 comprehensive unit testsHAVERSINE_ANTI_MERIDIAN_FIX.md- Comprehensive documentationThe Problem
The original Haversine formula didn't handle the anti-meridian correctly:
Before Fix:
After Fix:
The Solution
Normalize longitude difference to always take the shortest path:
Key Features
Test Coverage
✅ Acceptance Criteria
All features meet the project's acceptance criteria:
Architecture
/api/v1/...formatCode Quality
Testing
Documentation
📦 Dependencies Added
{ "dependencies": { "redlock": "^5.0.0-beta.2", "redis": "^6.2.1", "ioredis": "^6.0.0", "sharp": "^0.35.4" }, "devDependencies": { "@types/sharp": "^0.32.0" } }🔒 Environment Variables
Redis Configuration
Socket.io Configuration
Profile Picture Configuration
🧪 Testing
Run All Tests
npm testRun Specific Tests
npm test -- routingService.test.tsTest Results
📊 Performance Impact
Redis Redlock
Socket.io Deduplication
Profile Picture Upload
Haversine Fix
🔄 Migration Guide
1. Install Dependencies
2. Update Environment Variables
Copy the new variables from
.env.exampleto your.envfile.3. Start Redis (if not already running)
4. Database Migration
No database migrations required. The User schema fields are optional and backward-compatible.
5. Restart Application
📝 API Changes
New Endpoints
Escrow Release
POST /api/v1/escrow/release- Release escrow with distributed lockingProfile Management
POST /api/v1/profile/picture- Upload profile pictureDELETE /api/v1/profile/picture- Remove profile pictureGET /api/v1/profile- Get user profileModified Endpoints
No breaking changes to existing endpoints.
🎨 Code Structure
🚀 Deployment Checklist
npm testnpm run build📚 Documentation
Each feature has comprehensive documentation:
REDIS_REDLOCK_IMPLEMENTATION.mdSOCKET_DEDUPLICATION_FIX.mdPROFILE_PICTURE_UPLOAD.mdHAVERSINE_ANTI_MERIDIAN_FIX.md🤝 Review Checklist
Code Quality
Architecture
/api/v1/...versioningTesting
Documentation
Security
📄 License
This project is licensed under the terms specified in the repository.
🎉 Summary
This PR successfully implements 4 critical features:
All features are production-ready, well-tested, and fully documented.