Implement User Profile Data Layer with Supabase
Description
Implement the data layer for StellarMarket's user profile functionality using Supabase. This includes setting up the necessary database tables, relationships, and APIs to manage user profiles, orders, reviews, wishlists, and delivery tracking.
Acceptance Criteria
Database Schema Implementation
-
Extended User Profiles Table
- Extend the existing profiles table with:
membership_level: enum ('standard', 'premium', 'platinum')
membership_since: timestamp
display_name: string
avatar_url: string
email_public: boolean
last_active: timestamp
-
Orders Table
- Create table with schema:
id: UUID primary key
user_id: FK to auth.users
status: enum ('processing', 'shipped', 'in_transit', 'delivered', 'cancelled')
total_amount: numeric
currency: string default 'XLM'
created_at: timestamp
updated_at: timestamp
shipping_address_id: FK to addresses
payment_intent_id: string (for payment processor reference)
-
Order Items Table
id: UUID primary key
order_id: FK to orders
product_id: FK to products
quantity: integer
price_at_purchase: numeric
product_name_at_purchase: string
product_options: JSONB (for size, color, etc.)
-
Reviews Table
id: UUID primary key
user_id: FK to auth.users
product_id: FK to products
order_item_id: FK to order_items (optional)
rating: integer (1-5)
comment: text
created_at: timestamp
updated_at: timestamp
helpful_votes: integer
reported: boolean
-
Wishlist Items Table
id: UUID primary key
user_id: FK to auth.users
product_id: FK to products
added_at: timestamp
notes: text
-
Shipment Tracking Table
id: UUID primary key
order_id: FK to orders
tracking_number: string
carrier: string
estimated_delivery: timestamp
status: enum ('processing', 'shipped', 'in_transit', 'out_for_delivery', 'delivered', 'exception')
status_updated_at: timestamp
last_location: string
delivery_notes: text
Row-Level Security Policies
-
Profiles Table Policies
- Users can read their own profile
- Admin can read all profiles
- Users can update only their own profile
- Public fields accessible to everyone
-
Orders Table Policies
- Users can read only their own orders
- Admin can read all orders
- Orders can only be created/updated by system or admin
-
Reviews Table Policies
- Anyone can read published reviews
- Users can create/update only their own reviews
- Users can only create one review per product
-
Wishlist Table Policies
- Users can only see their own wishlist items
- Users can only add/remove from their own wishlist
-
Shipment Tracking Policies
- Users can view tracking for their own orders
- Admin and system can update tracking info
API Implementation
-
Profile Management
- Get user profile with membership details
- Update profile information
- Upload and manage profile avatar
- Get activity metrics (order count, review count, wishlist count)
-
Order History
- Get paginated order history
- Get order details by ID
- Get recent orders (last 3-5)
- Filter orders by status
- Search orders by product name or order ID
-
Review Management
- Get user's submitted reviews
- Create/update reviews for purchased products
- Delete user's own reviews
- Get review metrics (total, average rating given)
-
Wishlist Management
- Add/remove items to wishlist
- Get wishlist items with product details
- Get wishlist count
- Move wishlist item to cart
-
Shipment Tracking
- Get upcoming deliveries
- Get delivery calendar (next 30 days)
- Subscribe to shipment updates
- Get shipment history
Database Functions and Triggers
-
Update User Activity
- Function to update
last_active timestamp
- Trigger on user actions (login, purchase, etc.)
-
Calculate Review Metrics
- Function to update product ratings when reviews change
- Function to calculate user review metrics
-
Order Status Updates
- Function to update order status based on shipment status
- Trigger email notifications on status change
-
Auto-Archive Old Orders
- Function to move old orders to archive table
- Scheduled via pgcron
Security Implementation
-
Data Access Control
- Implement proper RLS policies
- Create service roles with appropriate permissions
- Audit logging for sensitive operations
-
Personal Data Handling
- Implement GDPR-compliant data access/removal
- Personal data encryption where appropriate
- Data retention policies
Performance Considerations
-
Indexing Strategy
- Create indexes for:
orders(user_id, created_at)
reviews(user_id, product_id)
wishlist_items(user_id, added_at)
shipment_tracking(order_id, estimated_delivery)
-
Query Optimization
- Optimize joins for order history
- Use materialized views for activity metrics
- Implement efficient pagination
-
Caching Strategy
- Cache profile data (short TTL)
- Cache activity metrics (medium TTL)
- Cache recent orders (short TTL)
Integration Requirements
-
Authentication Integration
- Hook into auth events for profile creation/updates
- Handle account merging/linking
- Support social login profile data
-
Notification System
- Trigger notifications on:
- Order status changes
- Upcoming deliveries
- Review requests (post-delivery)
- Wishlist item price changes
-
Analytics Integration
- Track profile completeness
- Measure user engagement metrics
- Record order frequency and value
Testing Requirements
-
Unit Tests
- Test database functions and triggers
- Test RLS policies effectiveness
- Test data validation
-
Integration Tests
- Test API endpoints with various scenarios
- Test concurrent data modifications
- Test performance under load
-
Security Tests
- Verify RLS prevents unauthorized access
- Test for SQL injection vulnerabilities
- Verify proper error handling (no data leakage)
Migration Plan
- Create database migration files for new tables
- Create data migration for existing users
- Implement backward compatibility for existing API endpoints
- Deploy in phases with feature flags
Implement User Profile Data Layer with Supabase
Description
Implement the data layer for StellarMarket's user profile functionality using Supabase. This includes setting up the necessary database tables, relationships, and APIs to manage user profiles, orders, reviews, wishlists, and delivery tracking.
Acceptance Criteria
Database Schema Implementation
Extended User Profiles Table
membership_level: enum ('standard', 'premium', 'platinum')membership_since: timestampdisplay_name: stringavatar_url: stringemail_public: booleanlast_active: timestampOrders Table
id: UUID primary keyuser_id: FK to auth.usersstatus: enum ('processing', 'shipped', 'in_transit', 'delivered', 'cancelled')total_amount: numericcurrency: string default 'XLM'created_at: timestampupdated_at: timestampshipping_address_id: FK to addressespayment_intent_id: string (for payment processor reference)Order Items Table
id: UUID primary keyorder_id: FK to ordersproduct_id: FK to productsquantity: integerprice_at_purchase: numericproduct_name_at_purchase: stringproduct_options: JSONB (for size, color, etc.)Reviews Table
id: UUID primary keyuser_id: FK to auth.usersproduct_id: FK to productsorder_item_id: FK to order_items (optional)rating: integer (1-5)comment: textcreated_at: timestampupdated_at: timestamphelpful_votes: integerreported: booleanWishlist Items Table
id: UUID primary keyuser_id: FK to auth.usersproduct_id: FK to productsadded_at: timestampnotes: textShipment Tracking Table
id: UUID primary keyorder_id: FK to orderstracking_number: stringcarrier: stringestimated_delivery: timestampstatus: enum ('processing', 'shipped', 'in_transit', 'out_for_delivery', 'delivered', 'exception')status_updated_at: timestamplast_location: stringdelivery_notes: textRow-Level Security Policies
Profiles Table Policies
Orders Table Policies
Reviews Table Policies
Wishlist Table Policies
Shipment Tracking Policies
API Implementation
Profile Management
Order History
Review Management
Wishlist Management
Shipment Tracking
Database Functions and Triggers
Update User Activity
last_activetimestampCalculate Review Metrics
Order Status Updates
Auto-Archive Old Orders
Security Implementation
Data Access Control
Personal Data Handling
Performance Considerations
Indexing Strategy
orders(user_id, created_at)reviews(user_id, product_id)wishlist_items(user_id, added_at)shipment_tracking(order_id, estimated_delivery)Query Optimization
Caching Strategy
Integration Requirements
Authentication Integration
Notification System
Analytics Integration
Testing Requirements
Unit Tests
Integration Tests
Security Tests
Migration Plan