Workout Window Feature - Implementation Summary
Overview
The Workout Window feature is a complete accountability system that allows users to:
- Set a 2-hour daily commitment window for activities (workout, meditation, reading, etc.)
- Get matched with other users based on location and time windows
- Form chains where users "pass the baton" to the next person
- Join Red or Blue teams and compete in weekly battles
- Check in with photos to verify completion
- Earn battle chips for successful check-ins
Completed Components
1. Database Schema ✅
- Migration:
supabase/migrations/025_add_workout_window.sql - Tables:
workout_windows- User workout window configurationsworkout_window_locations- User location dataworkout_window_chains- Daily chain assignmentsworkout_window_squads- 3-person squad assignmentsworkout_window_checkins- User check-in recordsworkout_window_team_battles- Weekly battle recordsworkout_window_mosaic_status- Daily mosaic state
2. Backend API ✅
- Routes:
apps/backend/src/routes/workout-window.ts - Endpoints:
POST /api/workout-window/setup- Set up workout windowGET /api/workout-window/my-window- Get user's windowPUT /api/workout-window/update-location- Update locationGET /api/workout-window/chain/:date- Get chain for datePOST /api/workout-window/checkin- Submit check-inGET /api/workout-window/checkin/:date- Get check-in statusPOST /api/workout-window/cron/generate-chains- Cron endpointPOST /api/workout-window/generate-chains- Manual generationGET /api/workout-window/team-stats/:date- Team statistics
3. Core Services ✅
- Chain Matching:
apps/backend/src/services/workout-window/chain-matching.ts- Distance calculation (Haversine formula)
- Time window matching
- Team assignment (Red/Blue)
- Squad creation (3-person teams)
- Daily Chain Generator:
apps/backend/src/services/workout-window/daily-chain-generator.ts- Generates chains for specific dates
- Creates squads and assigns teams
- Updates existing chains
- Location Tracker:
apps/backend/src/services/workout-window/location-tracker.ts- IP-based location detection
- Coordinate-based location updates
- Location retrieval
4. Web UI ✅
- Setup Page:
apps/frontend/src/app/dashboard/workout-window/page.tsx- Activity type selection
- Time window picker
- Location display
- Timezone detection
- Chain View:
apps/frontend/src/app/dashboard/workout-window/chain/page.tsx- Before/after user display
- Check-in status
- Team color badge
- Squad information
- Check-In:
apps/frontend/src/app/dashboard/workout-window/checkin/page.tsx- Photo upload with preview
- Activity type display
- Location capture
- Duplicate prevention
- Dashboard:
apps/frontend/src/app/dashboard/workout-window/dashboard/page.tsx- Today's status
- Team statistics
- Quick actions
- Navigation: Added to
apps/frontend/src/components/layout/Navbar.tsx
5. Mobile UI ✅
- Setup Screen:
apps/mobile/src/screens/workout-window/WorkoutWindowSetupScreen.tsx- Activity selection with icons
- Time window input
- Custom activity input
- Info section
- Check-In Screen:
apps/mobile/src/screens/workout-window/WorkoutWindowCheckInScreen.tsx- Camera integration (
expo-image-picker) - Gallery selection
- Location permissions (
expo-location) - Photo preview
- Camera integration (
- Navigation: Added to
apps/mobile/src/navigation/MainNavigator.tsx - Settings Link: Added to
apps/mobile/src/screens/settings/SettingsScreen.tsx
6. Tests ✅
- Test Suite:
apps/backend/src/routes/__tests__/workout-window.test.ts - Comprehensive tests for all endpoints
- Mock Supabase client
- Test coverage for edge cases
7. Documentation ✅
- Cron Setup:
docs/WORKOUT_WINDOW_CRON_SETUP.md - Storage Setup:
docs/WORKOUT_WINDOW_STORAGE_SETUP.md - Implementation Plan:
docs/WORKOUT_WINDOW_PLAN.md
Setup Requirements
1. Database Migration
Run the migration:
supabase migration up
2. Storage Bucket
Create the workout-checkins bucket in Supabase Storage:
- Public bucket: Yes
- File size limit: 10MB
- Allowed MIME types:
image/jpeg, image/png, image/gif, image/webp
See docs/WORKOUT_WINDOW_STORAGE_SETUP.md for detailed RLS policies.
3. Environment Variables
Add to backend .env:
WORKOUT_WINDOW_CRON_SECRET=your-secret-token-here
4. Cron Job Setup
Set up a daily cron job to generate chains:
- Schedule: Daily at midnight UTC
- Endpoint:
POST /api/workout-window/cron/generate-chains - Header:
x-cron-secret: your-secret-token-here
See docs/WORKOUT_WINDOW_CRON_SETUP.md for details.
5. Mobile Dependencies
Install required packages:
cd apps/mobile
npm install expo-image-picker expo-location
How It Works
1. User Setup
- User sets up workout window (activity type, time window, timezone)
- System captures user location (IP or GPS)
- Chains are automatically generated for today and tomorrow
2. Daily Chain Generation
- Cron job runs daily at midnight
- Algorithm matches users based on:
- Location proximity (Haversine distance)
- Time window sequence (before/after)
- Users assigned to Red or Blue teams
- 3-person squads created from chains
3. Check-In Process
- User completes activity during their window
- User uploads photo via web or mobile
- Photo stored in Supabase Storage
- Check-in recorded with:
- Photo URL
- Timestamp
- Location (optional)
- Status (pending → verified)
- Battle chips calculated:
- Base: 1 chip
- Bonus: +1 if chain continued (before user checked in)
4. Chain Visualization
- Users see their "before" and "after" partners
- Check-in status displayed for all chain members
- Team color badge shows Red/Blue assignment
- Squad information shows 3-person team
5. Team Competition
- Red vs Blue teams compete daily
- Stats tracked: completion rate, battle chips, members
- Weekly battles (future feature)
Future Enhancements
Phase 2: Mosaic Generation
- Daily low-poly mosaic image
- Each section represents a participant
- Clickable sections for details
- Visual representation of chain success
Phase 3: Battle System
- Weekly showdowns
- Robot vs bad guy battles
- Battle chips power robot
- Team with most chips wins
Phase 4: Notifications
- Notify next user in chain
- Reminder notifications
- Battle updates
- Chain break alerts
Phase 5: Photo Verification
- Automated verification (AI)
- Manual admin verification
- EXIF data extraction
- Content moderation
Testing Checklist
- User can set up workout window
- Location is captured correctly
- Chains are generated for new users
- User can check in with photo
- Chain view shows before/after users
- Team stats display correctly
- Battle chips calculated correctly
- Mobile check-in works
- Photo uploads to storage
- RLS policies work correctly
- Cron job generates chains daily
Known Limitations
- Squad Creation: Incomplete chains may create 2-person squads that need to be filled later
- Location Accuracy: IP-based location is approximate (city-level)
- Timezone Handling: Timezone conversion could be improved for global users
- Photo Verification: Currently manual, no automated verification
- Chain Matching: May not find matches for isolated users (low user density areas)
Support
For issues or questions:
- Check documentation in
docs/folder - Review test files for usage examples
- Check backend logs for errors
- Verify storage bucket and RLS policies
Next Steps
- Set up storage bucket and RLS policies
- Configure cron job for daily chain generation
- Test with multiple users
- Monitor chain matching algorithm performance
- Gather user feedback for improvements