Architecture Overview
upLegal is built as a modern full-stack web application with a client-server architecture:Core Components
Frontend Layer (React SPA)
The frontend is a single-page application built with React and Vite:UI Framework
- React 18: Component-based architecture with hooks
- TypeScript: Type-safe development
- Vite: Fast development server and optimized builds
- React Router: Client-side routing with protected routes
State Management
- React Query: Server state caching and synchronization
- Context API: Auth, messages, notifications
- Local State: Component-level state with hooks
UI Components
- shadcn/ui: Radix UI primitives with Tailwind styling
- Tailwind CSS: Utility-first styling
- Framer Motion: Animations and transitions
- Recharts: Data visualization
Forms & Validation
- React Hook Form: Form state management
- Zod: Schema validation
- @hookform/resolvers: Validation integration
Backend Layer (Express + Supabase)
- Express Server
- Supabase
The Express server (
server.mjs) handles:- Payment Processing: MercadoPago preference creation and webhooks
- Lawyer Verification: PJUD RUT validation and scraping
- Booking Management: Appointment creation and confirmation
- Email Notifications: Resend integration for transactional emails
- File Uploads: Avatar and document handling
POST /create-payment- Create MercadoPago payment preferencePOST /api/mercadopago/webhook- Payment confirmation webhookPOST /verify-lawyer- PJUD lawyer verificationPOST /api/bookings/create- Create booking with payment
Database Architecture
The database uses PostgreSQL with Supabase, organized into key tables:- profiles: User accounts (clients and lawyers)
- appointments: Scheduled consultations
- bookings: Payment-linked appointment records
- consultations: Service requests and cases
- messages: Real-time chat messages
- conversations: Message threads
- notifications: In-app notification queue
- reviews: Client reviews and ratings
- payments: Payment transaction records
- lawyer_services: Lawyer-offered legal services
Design Patterns
Authentication Flow
Real-time Subscriptions
upLegal uses Supabase real-time channels for live updates:Payment Flow
- Client selects lawyer and time slot
- Frontend calls
POST /create-paymenton Express server - Server creates MercadoPago preference with 10% platform fee
- Client redirected to MercadoPago checkout
- MercadoPago sends webhook to
POST /api/mercadopago/webhook - Server confirms booking and sends email notifications
- Appointment record created in database
Deployment Architecture
Frontend Deployment
Netlify
- Static site hosting
- Automatic deployments from Git
- CDN distribution
- Serverless redirects
Backend Deployment
Render
- Express server hosting
- Environment variables
- Health check endpoint
- Auto-scaling
Security Measures
Row Level Security (RLS)
Row Level Security (RLS)
Every database table has RLS policies that restrict access:
- Users can only read/write their own profile
- Lawyers can only see their own appointments
- Messages are visible only to conversation participants
- Admin operations require service role key
API Authentication
API Authentication
- Supabase JWT tokens for authenticated requests
- Service role key for server-side operations
- CORS whitelist for allowed origins
- Request validation and sanitization
Payment Security
Payment Security
- MercadoPago handles all sensitive payment data
- No credit card information stored in database
- Webhook signature verification
- Payment reconciliation for failed webhooks
Data Privacy
Data Privacy
- User emails and phone numbers are private
- File uploads scoped to authenticated users
- Message encryption in transit (TLS)
- GDPR-compliant data handling
Performance Optimizations
- Code Splitting: React lazy loading for routes
- Image Optimization: Supabase Storage CDN
- Database Indexing: Indexed foreign keys and search fields
- Query Optimization: React Query caching reduces API calls
- Infinite Scroll: Virtual scrolling for large lists
- Debouncing: Search input debouncing (300ms)
Monitoring & Logging
- Google Analytics: User behavior tracking
- Payment Logging: Custom payment event logger
- Error Logging: Error boundary for React crashes
- Server Logs: Express logging for API requests
- Health Checks:
/healthendpoint for uptime monitoring
Next Steps
Tech Stack Details
Explore the complete technology stack
Database Schema
Detailed database table structures
Local Setup
Set up upLegal for local development
Deployment Guide
Deploy upLegal to production