Skip to content

Latest commit

 

History

History
273 lines (206 loc) · 6.69 KB

File metadata and controls

273 lines (206 loc) · 6.69 KB

Profile Picture URL Fix - Undefined Bucket Issue

Issue Summary

Problem: The job_completion_selfies table was storing an incorrect profile picture URL:

https://undefined.s3.ap-south-1.amazonaws.com/provider_profile_pictures/4/1761742280247_TGCphoto.jpg

Root Cause: The code was reconstructing the S3 URL using process.env.AWS_S3_BUCKET which was:

  1. Not set when the code ran (before we added it)
  2. Unnecessary since the full URL is already stored in the providers table

✅ Fixes Applied

1. Use Database URL Instead of Reconstructing

Before (WRONG):

// Reconstructing URL - causes "undefined" if env var not loaded
profilePictureS3Url: `https://${process.env.AWS_S3_BUCKET}.s3.${process.env.AWS_REGION}.amazonaws.com/${profilePictureKey}`

After (CORRECT):

// Use the actual URL from database
profilePictureS3Url: profilePictureData.url

2. Created New Function: getWorkerProfilePictureData()

File: backend/services/faceComparisonService.js

New Function:

async getWorkerProfilePictureData(workerId) {
    // Returns: { url: "full S3 URL", key: "S3 key" }
    // Gets data from providers.profile_picture_url column
}

Returns:

{
    url: "https://files-and-image-storage-bucket.s3.ap-south-1.amazonaws.com/provider_profile_pictures/4/1761742280247_TGCphoto.jpg",
    key: "provider_profile_pictures/4/1761742280247_TGCphoto.jpg"
}

3. Updated Selfie Verification Route

File: backend/routes/worker_management/selfieVerification.js

Changes:

  1. Changed from getWorkerProfilePictureKey() to getWorkerProfilePictureData()
  2. Use profilePictureData.url for saving to database
  3. Use profilePictureData.key for Rekognition comparison

Code:

// Get both URL and key from database
const profilePictureData = await faceComparisonService.getWorkerProfilePictureData(workerId);

// Use key for face comparison
const faceComparison = await faceComparisonService.compareFaces(s3Key, profilePictureData.key);

// Use URL for database storage
const verificationData = {
    profilePictureS3Url: profilePictureData.url,  // ✅ Correct URL from database
    // ... other fields
};

📊 Database Schema

providers Table:

profile_picture_url VARCHAR(500)

Stores full S3 URL:

https://files-and-image-storage-bucket.s3.ap-south-1.amazonaws.com/provider_profile_pictures/4/1761742280247_TGCphoto.jpg

job_completion_selfies Table:

profile_picture_s3_url VARCHAR(500)

Now stores correct URL:

https://files-and-image-storage-bucket.s3.ap-south-1.amazonaws.com/provider_profile_pictures/4/1761742280247_TGCphoto.jpg

Before (WRONG):

https://undefined.s3.ap-south-1.amazonaws.com/provider_profile_pictures/4/1761742280247_TGCphoto.jpg

🔍 How It Works Now

Data Flow:

1. Worker uploads profile picture
   ↓
2. Stored in: files-and-image-storage-bucket
   ↓
3. Full URL saved to: providers.profile_picture_url
   ↓
4. Worker completes job and uploads selfie
   ↓
5. System calls: getWorkerProfilePictureData(workerId)
   ↓
6. Returns: { url: "full URL", key: "S3 key" }
   ↓
7. Face comparison uses: profilePictureData.key
   ↓
8. Database saves: profilePictureData.url
   ↓
9. job_completion_selfies.profile_picture_s3_url = correct URL ✅

🧪 Testing

Step 1: Restart Backend

# Backend should auto-reload with nodemon
# Check console for confirmation

Step 2: Test Selfie Upload

Worker ID 4 (has profile picture):

  1. Complete a job
  2. Upload selfie
  3. Check database

Step 3: Verify Database

SELECT 
    booking_id,
    worker_id,
    profile_picture_s3_url,
    verification_status
FROM job_completion_selfies
WHERE worker_id = 4
ORDER BY created_at DESC
LIMIT 1;

Expected Result:

profile_picture_s3_url: https://files-and-image-storage-bucket.s3.ap-south-1.amazonaws.com/provider_profile_pictures/4/1761742280247_TGCphoto.jpg

NOT:

profile_picture_s3_url: https://undefined.s3.ap-south-1.amazonaws.com/...

📝 Console Logs

You should see:

[Selfie Verification] Checking profile picture for worker ID: 4

[Profile Picture] Found URL for provider 4: https://files-and-image-storage-bucket.s3.ap-south-1.amazonaws.com/provider_profile_pictures/4/1761742280247_TGCphoto.jpg

[Profile Picture] Extracted S3 key: provider_profile_pictures/4/1761742280247_TGCphoto.jpg

[Selfie Verification] Profile picture found for worker ID: 4 {
  key: 'provider_profile_pictures/4/1761742280247_TGCphoto.jpg',
  url: 'https://files-and-image-storage-bucket.s3.ap-south-1.amazonaws.com/provider_profile_pictures/4/1761742280247_TGCphoto.jpg'
}

[Face Comparison] Starting face comparison: {
  selfieKey: 'selfie_verifications/4/...',
  selfieBucket: 'worker-verification-images',
  profileKey: 'provider_profile_pictures/4/1761742280247_TGCphoto.jpg',
  profileBucket: 'files-and-image-storage-bucket',
  threshold: 80,
  region: 'ap-south-1'
}

🎯 Benefits

1. Correct URLs

  • ✅ No more "undefined" in bucket name
  • ✅ Uses actual URL from database
  • ✅ Consistent with how profile pictures are stored

2. Single Source of Truth

  • ✅ Database is the source of truth for URLs
  • ✅ No need to reconstruct URLs
  • ✅ Handles different bucket names automatically

3. Better Debugging

  • ✅ Logs show both URL and key
  • ✅ Easy to verify what's being used
  • ✅ Clear error messages

4. Backward Compatible

  • ✅ Old function still exists (deprecated)
  • ✅ New function provides more data
  • ✅ No breaking changes to other code

🔗 Related Files

Modified:

  • backend/routes/worker_management/selfieVerification.js - Uses new function
  • backend/services/faceComparisonService.js - Added getWorkerProfilePictureData()
  • backend/.env - Added AWS_S3_BUCKET (for Rekognition access)

Database Tables:

  • providers - Source of profile picture URLs
  • job_completion_selfies - Stores verification data

✨ Summary

The Problem:

// Code was doing this:
profilePictureS3Url: `https://${process.env.AWS_S3_BUCKET}.s3...`
// Result: https://undefined.s3.ap-south-1.amazonaws.com/...

The Solution:

// Now doing this:
profilePictureS3Url: profilePictureData.url
// Result: https://files-and-image-storage-bucket.s3.ap-south-1.amazonaws.com/...

Key Insight:

Don't reconstruct what you already have! The full URL is stored in the database, so just use it directly.


🚀 Status

  • ✅ Code fixed
  • ✅ New function created
  • ✅ Logging enhanced
  • ✅ Ready to test

Next selfie upload will have the correct profile picture URL! 🎉