Scrap Car Leads Feature

Overview

The sell-car-for-scrap pages allow users to submit their vehicles for scrap and connect with scrapyard suppliers.

Key Files

Pages

Components

API Endpoints

Important: RLS Considerations

The sell-car.ts API uses getAdminSupabase() (service role) for database inserts to bypass RLS policies. This is necessary because server-side API routes don’t have proper anonymous session context for RLS policies.

RLS Policies (Dec 2024): Multiple INSERT policies exist as fallbacks:

This belt-and-suspenders approach ensures inserts work regardless of how Vercel/Supabase recognizes the client role.

Image Attachment Handling

Photos uploaded via the form go through validation:

  1. Supabase Storage upload: Skips files < 1KB or > 5MB
  2. Email attachments: Same validation applied to filter corrupt/empty files

Fix (Dec 2024): Email attachments now filter out empty/corrupt images (< 1KB decoded) to prevent empty JPG files appearing in supplier emails.

Fix (Jan 2025): Mobile Browser GC Issue Photos were arriving as empty attachments on mobile browsers because File objects can lose their blob data if there’s a delay between file selection and form submission (browser garbage collection).

Solution: Photos are now converted to base64 immediately when selected (not at submission time):

Console logging: Photos now log size on selection:

Successfully stored photo: 1000001744.jpg (847293 base64 chars)

If you see “Skipping empty or invalid attachment” in server logs, the fix isn’t deployed or there’s a different issue.

iPhone HEIC Image Support

Added: Dec 2024

iPhones save photos in HEIC/HEIF format by default, which has poor browser/email compatibility. The form now:

  1. Accepts HEIC/HEIF files - File input accepts .heic, .heif extensions
  2. Auto-converts to JPEG - Uses heic2any library for client-side conversion
  3. Shows progress indicator - Displays “Converting iPhone photos…” with progress bar during conversion
  4. Quality setting: 0.85 (85%) - good balance of quality vs file size

Supported formats:

Troubleshooting:

API Validation Rules

Contact Method Validation (Dec 2024 Fix)

The API now matches frontend validation - at least one contact method is required (email OR phone):

FieldRequiredNotes
Vehicle MakeYesAlways required
EmailNo*Optional if phone provided
PhoneNo*Optional if email provided
Full NameYesRequired (validated on frontend)
CityYesRequired (validated on frontend)

*At least ONE contact method must be provided.

Backend behavior when no email:

Database

Table: scrap_car_leads

Stores all scrap car enquiries submitted through the form.

Key columns:

Query Examples

-- Count leads with photos (photos is JSONB)
SELECT COUNT(*) FROM scrap_car_leads
WHERE photos IS NOT NULL AND jsonb_array_length(photos) > 0;

-- Get recent leads with images
SELECT id, make, model, year, city, photos, created_at
FROM scrap_car_leads
WHERE photos IS NOT NULL AND jsonb_array_length(photos) > 0
ORDER BY created_at DESC
LIMIT 12;

-- Check total leads
SELECT COUNT(*) FROM scrap_car_leads;

-- Find leads with broken image URLs (old Supabase project)
SELECT id, make, model, city FROM scrap_car_leads
WHERE photos::text LIKE '%bjywqecxfbuxlachkzia%';

Current Status

Live Scrap Car Leads Section

Status: ENABLED (as of Dec 2024)

The RecentScrapCarEnquiries component displays recent scrap car enquiries with:

Query Filter

-- Fetch all recent leads (with or without photos)
SELECT * FROM scrap_car_leads
WHERE make IS NOT NULL
  AND city IS NOT NULL
  AND city NOT ILIKE '%test%'
ORDER BY created_at DESC
LIMIT 12;

The component filters out:

Known Issue: Old Supabase Image URLs

Some older leads may have photos pointing to a previous Supabase project (bjywqecxfbuxlachkzia.supabase.co) instead of the current one (hytuuxolxmlqntgpudcd.supabase.co). These images are inaccessible.

Fix: Clear the photos array for affected leads:

UPDATE scrap_car_leads
SET photos = '[]'::jsonb
WHERE photos::text LIKE '%bjywqecxfbuxlachkzia%';

Trusted Scrapyard Partners Grid

Updated Dec 2024: Changed from infinite-scroll carousel to a responsive grid to eliminate duplicate display issues.

The page displays a grid of scrapyard suppliers from the suppliers table:

SEO Structure

B2B Lead Service

The page includes a callout section linking to /scrap-car-leads-service/ for scrapyards wanting to purchase leads.



🤖 Subagent Rule

Before modifying any files in this feature:

  1. Spawn a subagent with subagent_type: "general-purpose"
  2. Include this CLAUDE.md path in the prompt
  3. After work is complete, update this CLAUDE.md with changes