Mobile Address Management System
Overview
This document outlines the comprehensive address management system implemented for the ADHD medication management app. The solution provides unified address handling for both patient settings and order delivery addresses with offline-first architecture and automatic sync.Architecture Components
1. Unified Type System (lib/types/Address.ts)
BaseAddress: Core address fields shared across all address typesLocalAddress: Extends BaseAddress with sync tracking for local storagePatientAddress: Database representation with audit fieldsOrderDeliveryAddress: Specialized for order delivery with additional fields
2. Local Storage Service (lib/services/LocalAddressService.ts)
Key Features:
- CRUD operations for patient addresses
- Automatic default address management (only one default per patient)
- Sync status tracking (
pending,synced,error) - Local UID generation for offline creation
- Data persistence using AsyncStorage
getAddresses(): Load all addressescreateAddress(): Create new addressupdateAddress(): Update existing addressdeleteAddress(): Delete addresssetDefaultAddress(): Set address as defaultgetAddressesNeedingSync(): Get addresses pending sync
3. Database Schema
patient_addresses Table:
- Full address information with audit trails
- Foreign key to
patientstable - RLS policies for patient and doctor access
- Triggers for default address enforcement
- Automatic timestamp updates
- Patients can only access their own addresses
- Doctors can read addresses for their clinic’s patients
- Automatic enforcement of single default address per patient
4. Sync Integration (lib/services/SyncService.ts)
Enhanced Sync Process:
- Prepare address sync payload from pending local changes
- Send to Edge Function with medication sync
- Process address CRUD operations on server
- Return updated address list
- Update local storage with synced data
addresses_to_create: New addresses to insertaddresses_to_update: Existing addresses to updateaddresses_to_delete: Address IDs to remove
5. Edge Function Updates (packages/supabase/functions/mobile-application-patient-sync/index.ts)
New Functionality:
- Process address sync operations before data fetch
- Handle address creation, updates, and deletion
- Maintain data integrity with patient ownership checks
- Return complete address list with sync response
6. React Hook (hooks/usePatientAddresses.ts)
Comprehensive Address Management:
- Load addresses from local storage
- Create, update, delete operations with automatic sync
- Default address management
- Error handling and loading states
- Sync status tracking
- Event-driven updates from sync service
Integration Points
1. Settings Page Integration
Replace the existing mock data inapp/delivery-addresses.tsx:
2. Order Flow Integration
Updateapp/reorder/delivery-address.tsx:
3. Order Context Integration
The existingOrderContext already supports delivery addresses:
Data Flow
1. Address Creation Flow
2. Order Address Selection Flow
3. Sync Flow
Benefits
1. Unified Management
- Single source of truth for all address types
- Consistent data structure across settings and orders
- Shared validation and business logic
2. Offline-First
- Addresses work without internet connection
- Automatic sync when connection restored
- Conflict resolution through server-side validation
3. User Experience
- Seamless address reuse between settings and orders
- Default address selection
- Real-time sync status indicators
4. Data Integrity
- Single default address enforcement
- Patient data isolation
- Audit trail for all changes
5. Scalability
- Easy to extend with additional address types
- Supports multiple addresses per patient
- Ready for future features (address validation, geolocation, etc.)
Implementation Status
✅ Completed:- Type system and interfaces
- Local storage service
- Database schema and migrations
- Sync service integration
- Edge function updates
- React hook implementation
- Update settings page to use real addresses
- Update order flow to load existing addresses
- Test sync functionality with database
- Add address validation (optional)
- Implement address search/autocomplete (optional)
Testing
To test the address system:-
Database Setup:
-
Create Test Addresses:
-
Verify Sync:
- Check local storage for address data
- Verify database insertion after sync
- Test offline/online scenarios