Skip to content

Latest commit

 

History

History
177 lines (139 loc) · 4.91 KB

File metadata and controls

177 lines (139 loc) · 4.91 KB

Type Safety Enhancement for booking_requests.status

Overview

Enhanced type safety for the booking_requests.status field by replacing generic string type with a specific TypeScript enum that enforces valid database values.

Changes Made

1. Added Enum Definition (Line 2197)

booking_request_status_enum: "sent" | "viewed" | "accepted" | "rejected" | "expired"

This enum was added to the Database["public"]["Enums"] section to match the database constraint:

  • sent: Initial state when request is sent to mover
  • viewed: Mover has viewed the request
  • accepted: Mover has accepted the booking
  • rejected: Mover has declined the booking
  • expired: Request expired without response

2. Updated Type References

Row Type (Line 29)

Before:

status: string

After:

status: Database["public"]["Enums"]["booking_request_status_enum"]

Insert Type (Line 44)

Before:

status?: string

After:

status?: Database["public"]["Enums"]["booking_request_status_enum"]

Update Type (Line 59)

Before:

status?: string

After:

status?: Database["public"]["Enums"]["booking_request_status_enum"]

Benefits

1. Compile-Time Type Safety

TypeScript will now catch invalid status values at compile time:

// ✅ Valid - will compile
await supabase
  .from('booking_requests')
  .update({ status: 'accepted' })

// ❌ Invalid - TypeScript error
await supabase
  .from('booking_requests')
  .update({ status: 'invalid_status' }) // Error: Type '"invalid_status"' is not assignable

2. IDE Autocomplete

IDEs will provide autocomplete suggestions for valid status values:

  • sent
  • viewed
  • accepted
  • rejected
  • expired

3. Database Consistency

Type definitions now match the database constraints defined in migration:

-- From: supabase/migrations/20251008_part2_marketplace_schema.sql
status TEXT NOT NULL DEFAULT 'sent', -- 'sent', 'viewed', 'accepted', 'rejected', 'expired'

4. Refactoring Safety

When refactoring code that uses booking_requests.status, TypeScript will highlight all usages that need updating if enum values change.

Usage Examples

Creating a Booking Request

const { data, error } = await supabase
  .from('booking_requests')
  .insert({
    booking_id: '123',
    mover_id: '456',
    status: 'sent', // ✅ Type-safe
    expires_at: new Date().toISOString()
  });

Updating Status

const { data, error } = await supabase
  .from('booking_requests')
  .update({ 
    status: 'accepted', // ✅ Autocomplete available
    responded_at: new Date().toISOString() 
  })
  .eq('id', requestId);

Filtering by Status

const { data, error } = await supabase
  .from('booking_requests')
  .select('*')
  .eq('status', 'sent') // ✅ Type-safe
  .lt('expires_at', new Date().toISOString());

Type Guards

type BookingRequestStatus = Database["public"]["Enums"]["booking_request_status_enum"];

function isValidStatus(status: string): status is BookingRequestStatus {
  return ['sent', 'viewed', 'accepted', 'rejected', 'expired'].includes(status);
}

Regenerating Types

To regenerate type definitions after database schema changes:

# Using Supabase CLI
supabase gen types typescript --project-id YOUR_PROJECT_ID > src/integrations/supabase/types.ts

# Or with local development
supabase gen types typescript --local > src/integrations/supabase/types.ts

Important: After regenerating, manually verify that enum types are correctly referenced in Row/Insert/Update types.

Related Files

  • Database Migration: supabase/migrations/20251008_part2_marketplace_schema.sql
  • Type Definitions: src/integrations/supabase/types.ts
  • Usage Examples:
    • src/pages/MoverDashboard.tsx (mover responds to requests)
    • src/services/api.ts (booking request API functions)

Known Issues

The types.ts file may show some TypeScript errors related to the Database structure. These are pre-existing issues with the generated types and do not affect the functionality of the enum types added in this enhancement.

Next Steps

Consider applying the same pattern to other string fields that have constrained values:

  • booking_requests.status (completed)
  • ⏳ Other potential candidates in the codebase

Testing

After making these changes, test the following scenarios:

  1. Creating new booking requests with valid status values
  2. Updating booking request status
  3. Filtering by status in queries
  4. Verify IDE autocomplete works correctly
  5. Confirm TypeScript catches invalid status values at compile time

References