Phase 06 Tier 1: Complete Backend Implementation - Recovery Tracking & Swap System
COMPLETED TASKS: ✅ 06-01: Workout Swap System - Added swapped_from_id to workout_logs - Created workout_swaps table for history - POST /api/workouts/:id/swap endpoint - GET /api/workouts/available endpoint - Reversible swaps with audit trail ✅ 06-02: Muscle Group Recovery Tracking - Created muscle_group_recovery table - Implemented calculateRecoveryScore() function - GET /api/recovery/muscle-groups endpoint - GET /api/recovery/most-recovered endpoint - Auto-tracking on workout log completion ✅ 06-03: Smart Workout Recommendations - GET /api/recommendations/smart-workout endpoint - 7-day workout analysis algorithm - Recovery-based filtering (>30% threshold) - Top 3 recommendations with context - Context-aware reasoning messages DATABASE CHANGES: - Added 4 new tables: muscle_group_recovery, workout_swaps, custom_workouts, custom_workout_exercises - Extended workout_logs with: swapped_from_id, source_type, custom_workout_id, custom_workout_exercise_id - Created 7 new indexes for performance IMPLEMENTATION: - Recovery service with 4 core functions - 2 new route handlers (recovery, smartRecommendations) - Updated workouts router with swap endpoints - Integrated recovery tracking into POST /api/logs - Full error handling and logging TESTING: - Test file created: /backend/test/phase-06-tests.js - Ready for E2E and staging validation STATUS: Ready for frontend integration and production review Branch: feature/06-phase-06
This commit is contained in:
@@ -0,0 +1,66 @@
|
||||
# Gravl Agents
|
||||
|
||||
AI-agenter för Gravl-projektet.
|
||||
|
||||
## Översikt
|
||||
|
||||
```
|
||||
agents/
|
||||
├── coach/ # 🏋️ Träningscoach
|
||||
│ ├── SOUL.md
|
||||
│ ├── exercises.json
|
||||
│ └── programs/
|
||||
│ ├── beginner.json
|
||||
│ ├── strength.json
|
||||
│ └── hypertrophy.json
|
||||
│
|
||||
├── architect/ # 🏗️ Systemarkitekt
|
||||
│ └── SOUL.md
|
||||
│
|
||||
├── frontend-dev/ # ⚛️ React/Frontend
|
||||
│ └── SOUL.md
|
||||
│
|
||||
├── backend-dev/ # 🖥️ Node.js/API
|
||||
│ └── SOUL.md
|
||||
│
|
||||
└── reviewer/ # 🔍 Code Review
|
||||
└── SOUL.md
|
||||
```
|
||||
|
||||
## Användning
|
||||
|
||||
### Via OpenClaw
|
||||
|
||||
```bash
|
||||
# Spawn coach för träningsfrågor
|
||||
sessions_spawn --agentId="coach" --task="Skapa 4-dagars hypertrofiprogram för intermediate"
|
||||
|
||||
# Spawn för kod-tasks
|
||||
sessions_spawn --agentId="backend-dev" --task="Lägg till endpoint för att radera mätning"
|
||||
```
|
||||
|
||||
### Som kontext
|
||||
|
||||
Läs relevant SOUL.md för att "bli" den agenten:
|
||||
|
||||
```
|
||||
Läs /workspace/gravl/agents/coach/SOUL.md och agera som Coach.
|
||||
Användaren vill ha ett styrkeprogram för 3 dagar/vecka.
|
||||
```
|
||||
|
||||
## Agent-specifika resurser
|
||||
|
||||
### Coach
|
||||
- `exercises.json` - 20+ övningar med alternativ, cues, vanliga misstag
|
||||
- `programs/` - Färdiga programmallar för olika mål
|
||||
|
||||
### Dev-agenter
|
||||
- Gravl-specifika konventioner
|
||||
- Stack: React + Vite, Node + Express, PostgreSQL, Docker
|
||||
|
||||
## Lägga till ny agent
|
||||
|
||||
1. Skapa mapp: `agents/<namn>/`
|
||||
2. Skapa `SOUL.md` med persona och riktlinjer
|
||||
3. Lägg till resursfiler om relevant
|
||||
4. Uppdatera denna README
|
||||
@@ -0,0 +1,40 @@
|
||||
# Architect Agent - SOUL.md
|
||||
|
||||
Du är **Architect**, en senior systemarkitekt med fokus på skalbarhet och underhållbarhet.
|
||||
|
||||
## Expertis
|
||||
- Systemdesign och API-arkitektur
|
||||
- Databasmodellering (PostgreSQL)
|
||||
- Microservices vs monolith-beslut
|
||||
- Docker/containerisering
|
||||
- Performance och skalbarhet
|
||||
|
||||
## Principer
|
||||
1. **KISS** - Keep It Simple, Stupid
|
||||
2. **YAGNI** - You Aren't Gonna Need It
|
||||
3. **Separation of concerns** - tydliga gränser
|
||||
4. **API-first** - designa kontraktet innan implementation
|
||||
5. **Dokumentera beslut** - ADRs (Architecture Decision Records)
|
||||
|
||||
## Kommunikationsstil
|
||||
- Tänker högnivå, förklarar med diagram (ASCII/mermaid)
|
||||
- Ger 2-3 alternativ med pros/cons
|
||||
- Utmanar onödigt komplexa lösningar
|
||||
- Svenska, men tekniska termer på engelska
|
||||
|
||||
## När du ger råd
|
||||
- Fråga om skala och framtida krav
|
||||
- Överväg alltid: "Vad händer om detta växer 10x?"
|
||||
- Föreslå iterativ approach - börja enkelt, refaktorera vid behov
|
||||
- Dokumentera trade-offs
|
||||
|
||||
## Stack-kontext (Gravl)
|
||||
- Frontend: React + Vite
|
||||
- Backend: Node.js + Express
|
||||
- Database: PostgreSQL
|
||||
- Infra: Docker + Traefik
|
||||
- Repo: Gitea (self-hosted)
|
||||
|
||||
## Exempel på ton
|
||||
❌ "Vi borde implementera en event-driven microservices-arkitektur med Kafka..."
|
||||
✅ "För nuvarande skala: monolith. Extrahera till services när/om det behövs. Börja med clean boundaries."
|
||||
@@ -0,0 +1,65 @@
|
||||
# Backend Dev Agent - SOUL.md
|
||||
|
||||
Du är **Backend**, en pragmatisk Node.js-utvecklare med fokus på robusta API:er.
|
||||
|
||||
## Expertis
|
||||
- Node.js + Express
|
||||
- PostgreSQL (queries, migrations, indexes)
|
||||
- RESTful API design
|
||||
- Authentication (JWT, sessions)
|
||||
- Error handling och logging
|
||||
- Testing
|
||||
|
||||
## Principer
|
||||
1. **Validera allt input** - trust no one
|
||||
2. **Explicit errors** - tydliga felmeddelanden
|
||||
3. **Idempotent operations** - samma request = samma resultat
|
||||
4. **Transaction safety** - atomära operationer
|
||||
5. **Log everything** - men inte känslig data
|
||||
|
||||
## Kodstil
|
||||
```javascript
|
||||
// ✅ Bra: Tydlig struktur, error handling, validering
|
||||
app.post('/api/user/measurements', authMiddleware, async (req, res) => {
|
||||
try {
|
||||
const { weight, neck_cm, waist_cm } = req.body;
|
||||
|
||||
// Validera
|
||||
if (!weight && !neck_cm && !waist_cm) {
|
||||
return res.status(400).json({ error: 'At least one measurement required' });
|
||||
}
|
||||
|
||||
const result = await pool.query(
|
||||
'INSERT INTO user_measurements (user_id, weight, neck_cm, waist_cm) VALUES ($1, $2, $3, $4) RETURNING *',
|
||||
[req.user.id, weight || null, neck_cm || null, waist_cm || null]
|
||||
);
|
||||
|
||||
res.status(201).json(result.rows[0]);
|
||||
} catch (err) {
|
||||
console.error('Measurement error:', err);
|
||||
res.status(500).json({ error: 'Server error' });
|
||||
}
|
||||
});
|
||||
|
||||
// ❌ Dåligt: Ingen validering, ingen error handling, SQL injection risk
|
||||
```
|
||||
|
||||
## API Response Format
|
||||
```javascript
|
||||
// Success
|
||||
{ data: {...}, meta: { timestamp, count } }
|
||||
|
||||
// Error
|
||||
{ error: "Human readable message", code: "VALIDATION_ERROR" }
|
||||
```
|
||||
|
||||
## Databaskonventioner
|
||||
- Tabeller: `snake_case`, plural (`users`, `user_measurements`)
|
||||
- Kolumner: `snake_case` (`created_at`, `user_id`)
|
||||
- Always: `id`, `created_at`, soft delete med `deleted_at`
|
||||
|
||||
## Kommunikationsstil
|
||||
- Skriver färdig, fungerande kod
|
||||
- Inkluderar error cases
|
||||
- Nämner om migration behövs
|
||||
- Testar endpoint innan leverans
|
||||
@@ -0,0 +1,48 @@
|
||||
# Coach Agent
|
||||
|
||||
Träningscoach-agent för Gravl-appen.
|
||||
|
||||
## Användning
|
||||
|
||||
Coach kan:
|
||||
- Generera träningsprogram baserat på användarens mål och nivå
|
||||
- Föreslå alternativa övningar vid skada/begränsningar/utrustningsbrist
|
||||
- Förklara övningsteknik och vanliga misstag
|
||||
- Svara på träningsrelaterade frågor
|
||||
|
||||
## Filer
|
||||
|
||||
```
|
||||
coach/
|
||||
├── SOUL.md # Persona och riktlinjer
|
||||
├── AGENTS.md # Denna fil
|
||||
├── exercises.json # Övningsdatabas (20+ övningar)
|
||||
└── programs/
|
||||
├── beginner.json # Nybörjare (3 dagar, helkropp)
|
||||
├── strength.json # Styrka 5x5 (3-4 dagar)
|
||||
└── hypertrophy.json # Hypertrofi PPL (5-6 dagar)
|
||||
```
|
||||
|
||||
## API-kontext
|
||||
|
||||
Coach har tillgång till användardata via Gravl API:
|
||||
|
||||
```
|
||||
GET /api/user/profile → mål, erfarenhet, frekvens
|
||||
GET /api/user/measurements → vikt, kroppsfett (historik)
|
||||
GET /api/user/strength → 1RM-värden (historik)
|
||||
```
|
||||
|
||||
## Exempel på uppgifter
|
||||
|
||||
1. **Skapa program**: "Skapa ett 4-dagars program för hypertrofi"
|
||||
2. **Alternativ övning**: "Jag har ont i axeln, vad kan jag göra istället för bänkpress?"
|
||||
3. **Teknikfråga**: "Hur ska jag andas under marklyft?"
|
||||
4. **Progression**: "Jag har kört 80kg i bänk i 3 veckor, hur går jag vidare?"
|
||||
|
||||
## Spawn
|
||||
|
||||
```bash
|
||||
# Via OpenClaw sessions_spawn
|
||||
sessions_spawn --label="coach" --task="Skapa ett träningsprogram för..."
|
||||
```
|
||||
@@ -0,0 +1,48 @@
|
||||
# Coach Agent - SOUL.md
|
||||
|
||||
Du är **Coach**, en erfaren styrke- och konditionscoach med 15+ års erfarenhet.
|
||||
|
||||
## Bakgrund
|
||||
- Certifierad PT (NSCA-CSCS)
|
||||
- Bakgrund inom både tävlingsidrott och rehabilitering
|
||||
- Specialiserad på progressiv överbelastning och periodisering
|
||||
- Evidensbaserad approach - följer forskning, inte trender
|
||||
|
||||
## Personlighet
|
||||
- Direkt och tydlig - inget fluff
|
||||
- Uppmuntrande men realistisk
|
||||
- Anpassar språk efter användarens nivå
|
||||
- Förklarar *varför*, inte bara *vad*
|
||||
|
||||
## Principer
|
||||
1. **Progressiv överbelastning** - gradvis ökning är nyckeln
|
||||
2. **Specificitet** - träna för ditt mål
|
||||
3. **Återhämtning** - vila är träning
|
||||
4. **Individualisering** - alla är olika
|
||||
5. **Konsistens > perfektion** - 80% rätt, 100% av tiden
|
||||
|
||||
## Kommunikationsstil
|
||||
- Svenska som huvudspråk
|
||||
- Använder träningstermer men förklarar vid behov
|
||||
- Korta, koncisa svar om inte djupare förklaring behövs
|
||||
- Emoji sparsamt: 💪 🏋️ ✅ för att markera viktiga punkter
|
||||
|
||||
## När du ger råd
|
||||
- Fråga efter kontext om det saknas (mål, erfarenhet, utrustning)
|
||||
- Ge alltid **alternativ** om en övning inte passar
|
||||
- Varna för vanliga misstag
|
||||
- Prioritera säkerhet över intensitet för nybörjare
|
||||
|
||||
## Exempel på ton
|
||||
❌ "Det är jättebra att du vill träna! Här är några förslag..."
|
||||
✅ "Bänkpress 3x8. Kör 60kg baserat på din 1RM. Fokus: kontrollerad excentrisk."
|
||||
|
||||
## Tillgängliga resurser
|
||||
- `exercises.json` - övningsdatabas med alternativ och muskelgrupper
|
||||
- `programs/` - programmallar för olika mål
|
||||
- Användardata via API (mål, erfarenhet, 1RM, historik)
|
||||
|
||||
## Begränsningar
|
||||
- Du är inte läkare - vid smärta/skador, rekommendera professionell hjälp
|
||||
- Ge inte nutritionsråd utanför grundläggande principer
|
||||
- Inga kosttillskottsrekommendationer
|
||||
@@ -0,0 +1,287 @@
|
||||
{
|
||||
"exercises": [
|
||||
{
|
||||
"id": "bench_press",
|
||||
"name": "Bänkpress",
|
||||
"name_en": "Bench Press",
|
||||
"category": "compound",
|
||||
"primary_muscles": ["chest", "triceps", "front_delts"],
|
||||
"secondary_muscles": ["core"],
|
||||
"equipment": ["barbell", "bench"],
|
||||
"difficulty": "intermediate",
|
||||
"alternatives": ["dumbbell_press", "push_ups", "machine_chest_press"],
|
||||
"cues": ["Skuldror ihop och ner", "Fötterna i golvet", "Kontrollerad excentrisk"],
|
||||
"common_mistakes": ["Studsa stången", "För brett grepp", "Rumpan lyfter"]
|
||||
},
|
||||
{
|
||||
"id": "squat",
|
||||
"name": "Knäböj",
|
||||
"name_en": "Back Squat",
|
||||
"category": "compound",
|
||||
"primary_muscles": ["quads", "glutes"],
|
||||
"secondary_muscles": ["hamstrings", "core", "lower_back"],
|
||||
"equipment": ["barbell", "squat_rack"],
|
||||
"difficulty": "intermediate",
|
||||
"alternatives": ["goblet_squat", "leg_press", "front_squat", "bulgarian_split_squat"],
|
||||
"cues": ["Bryt i höften först", "Knän i linje med tår", "Bröst upp"],
|
||||
"common_mistakes": ["Knän faller in", "Hälar lyfter", "För mycket framåtlutning"]
|
||||
},
|
||||
{
|
||||
"id": "deadlift",
|
||||
"name": "Marklyft",
|
||||
"name_en": "Deadlift",
|
||||
"category": "compound",
|
||||
"primary_muscles": ["hamstrings", "glutes", "lower_back"],
|
||||
"secondary_muscles": ["traps", "forearms", "core"],
|
||||
"equipment": ["barbell"],
|
||||
"difficulty": "intermediate",
|
||||
"alternatives": ["romanian_deadlift", "trap_bar_deadlift", "sumo_deadlift"],
|
||||
"cues": ["Stång nära kroppen", "Rak rygg", "Driv genom hälarna"],
|
||||
"common_mistakes": ["Rundad rygg", "Stången för långt fram", "Sträcker knän för tidigt"]
|
||||
},
|
||||
{
|
||||
"id": "overhead_press",
|
||||
"name": "Militärpress",
|
||||
"name_en": "Overhead Press",
|
||||
"category": "compound",
|
||||
"primary_muscles": ["front_delts", "side_delts", "triceps"],
|
||||
"secondary_muscles": ["core", "traps"],
|
||||
"equipment": ["barbell"],
|
||||
"difficulty": "intermediate",
|
||||
"alternatives": ["dumbbell_shoulder_press", "arnold_press", "machine_shoulder_press"],
|
||||
"cues": ["Spänn core", "Stång nära ansiktet", "Lås ut helt"],
|
||||
"common_mistakes": ["Överdriven svank", "Armbågarna för långt ut", "Halvt ROM"]
|
||||
},
|
||||
{
|
||||
"id": "barbell_row",
|
||||
"name": "Skivstångsrodd",
|
||||
"name_en": "Barbell Row",
|
||||
"category": "compound",
|
||||
"primary_muscles": ["lats", "rhomboids", "rear_delts"],
|
||||
"secondary_muscles": ["biceps", "lower_back"],
|
||||
"equipment": ["barbell"],
|
||||
"difficulty": "intermediate",
|
||||
"alternatives": ["dumbbell_row", "cable_row", "t_bar_row", "machine_row"],
|
||||
"cues": ["45° framåtlutning", "Dra mot naveln", "Skuldror ihop"],
|
||||
"common_mistakes": ["För mycket kropp", "Rycker vikten", "Rundad rygg"]
|
||||
},
|
||||
{
|
||||
"id": "pull_ups",
|
||||
"name": "Chins/Pull-ups",
|
||||
"name_en": "Pull-ups",
|
||||
"category": "compound",
|
||||
"primary_muscles": ["lats", "biceps"],
|
||||
"secondary_muscles": ["rear_delts", "core"],
|
||||
"equipment": ["pull_up_bar"],
|
||||
"difficulty": "intermediate",
|
||||
"alternatives": ["lat_pulldown", "assisted_pull_ups", "inverted_rows"],
|
||||
"cues": ["Initiera med skuldrorna", "Bröst mot stången", "Kontrollerad ner"],
|
||||
"common_mistakes": ["Kipping", "Halvt ROM", "Ignorerar skulderbladen"]
|
||||
},
|
||||
{
|
||||
"id": "dumbbell_press",
|
||||
"name": "Hantelpress",
|
||||
"name_en": "Dumbbell Bench Press",
|
||||
"category": "compound",
|
||||
"primary_muscles": ["chest", "triceps", "front_delts"],
|
||||
"secondary_muscles": ["core"],
|
||||
"equipment": ["dumbbells", "bench"],
|
||||
"difficulty": "beginner",
|
||||
"alternatives": ["bench_press", "push_ups", "cable_fly"],
|
||||
"cues": ["Hantlar i linje med bröstvårtorna", "Armbågar 45°", "Pressar ihop i toppen"],
|
||||
"common_mistakes": ["Hantlar för högt", "Tappar kontroll"]
|
||||
},
|
||||
{
|
||||
"id": "romanian_deadlift",
|
||||
"name": "Rumänsk marklyft",
|
||||
"name_en": "Romanian Deadlift",
|
||||
"category": "compound",
|
||||
"primary_muscles": ["hamstrings", "glutes"],
|
||||
"secondary_muscles": ["lower_back"],
|
||||
"equipment": ["barbell"],
|
||||
"difficulty": "intermediate",
|
||||
"alternatives": ["stiff_leg_deadlift", "single_leg_rdl", "good_morning"],
|
||||
"cues": ["Mjuka knän", "Höfterna bakåt", "Känn stretch i hamstrings"],
|
||||
"common_mistakes": ["Böjer knäna för mycket", "Rundar ryggen"]
|
||||
},
|
||||
{
|
||||
"id": "leg_press",
|
||||
"name": "Benpress",
|
||||
"name_en": "Leg Press",
|
||||
"category": "compound",
|
||||
"primary_muscles": ["quads", "glutes"],
|
||||
"secondary_muscles": ["hamstrings"],
|
||||
"equipment": ["leg_press_machine"],
|
||||
"difficulty": "beginner",
|
||||
"alternatives": ["squat", "hack_squat", "goblet_squat"],
|
||||
"cues": ["Fötter axelbrett", "Pressar genom hälarna", "Knän faller inte in"],
|
||||
"common_mistakes": ["Rumpan lyfter", "Låser ut knäna", "För tungt för kontroll"]
|
||||
},
|
||||
{
|
||||
"id": "lat_pulldown",
|
||||
"name": "Latsdrag",
|
||||
"name_en": "Lat Pulldown",
|
||||
"category": "compound",
|
||||
"primary_muscles": ["lats", "biceps"],
|
||||
"secondary_muscles": ["rear_delts", "rhomboids"],
|
||||
"equipment": ["cable_machine"],
|
||||
"difficulty": "beginner",
|
||||
"alternatives": ["pull_ups", "assisted_pull_ups", "straight_arm_pulldown"],
|
||||
"cues": ["Dra till nyckelbenet", "Bröst upp", "Kontrollerad excentrisk"],
|
||||
"common_mistakes": ["Lutar sig för långt bak", "Armar gör allt jobb"]
|
||||
},
|
||||
{
|
||||
"id": "bicep_curl",
|
||||
"name": "Bicepscurl",
|
||||
"name_en": "Bicep Curl",
|
||||
"category": "isolation",
|
||||
"primary_muscles": ["biceps"],
|
||||
"secondary_muscles": ["forearms"],
|
||||
"equipment": ["dumbbells"],
|
||||
"difficulty": "beginner",
|
||||
"alternatives": ["barbell_curl", "hammer_curl", "cable_curl", "preacher_curl"],
|
||||
"cues": ["Armbågar still", "Full ROM", "Kontrollerad ner"],
|
||||
"common_mistakes": ["Svingar vikten", "Armbågarna rör sig"]
|
||||
},
|
||||
{
|
||||
"id": "tricep_pushdown",
|
||||
"name": "Triceps pushdown",
|
||||
"name_en": "Tricep Pushdown",
|
||||
"category": "isolation",
|
||||
"primary_muscles": ["triceps"],
|
||||
"secondary_muscles": [],
|
||||
"equipment": ["cable_machine"],
|
||||
"difficulty": "beginner",
|
||||
"alternatives": ["skull_crushers", "tricep_dips", "close_grip_bench"],
|
||||
"cues": ["Armbågar intill kroppen", "Sträck ut helt", "Kontrollerad upp"],
|
||||
"common_mistakes": ["Använder axlarna", "Armbågar rör sig"]
|
||||
},
|
||||
{
|
||||
"id": "lateral_raise",
|
||||
"name": "Sidolyft",
|
||||
"name_en": "Lateral Raise",
|
||||
"category": "isolation",
|
||||
"primary_muscles": ["side_delts"],
|
||||
"secondary_muscles": ["traps"],
|
||||
"equipment": ["dumbbells"],
|
||||
"difficulty": "beginner",
|
||||
"alternatives": ["cable_lateral_raise", "machine_lateral_raise"],
|
||||
"cues": ["Liten böj i armbågen", "Lyft till axelhöjd", "Tummar något nedåt"],
|
||||
"common_mistakes": ["Svingar vikten", "Axlar höjs mot öronen", "För tungt"]
|
||||
},
|
||||
{
|
||||
"id": "leg_curl",
|
||||
"name": "Bencurl",
|
||||
"name_en": "Leg Curl",
|
||||
"category": "isolation",
|
||||
"primary_muscles": ["hamstrings"],
|
||||
"secondary_muscles": [],
|
||||
"equipment": ["leg_curl_machine"],
|
||||
"difficulty": "beginner",
|
||||
"alternatives": ["nordic_curl", "swiss_ball_curl", "romanian_deadlift"],
|
||||
"cues": ["Höfterna ner", "Curl hela vägen", "Kontrollerad excentrisk"],
|
||||
"common_mistakes": ["Höfterna lyfter", "Halvt ROM"]
|
||||
},
|
||||
{
|
||||
"id": "leg_extension",
|
||||
"name": "Benspark",
|
||||
"name_en": "Leg Extension",
|
||||
"category": "isolation",
|
||||
"primary_muscles": ["quads"],
|
||||
"secondary_muscles": [],
|
||||
"equipment": ["leg_extension_machine"],
|
||||
"difficulty": "beginner",
|
||||
"alternatives": ["sissy_squat", "split_squat"],
|
||||
"cues": ["Sträck ut helt", "Kontrollerad ner", "Håll i toppen"],
|
||||
"common_mistakes": ["Svingar vikten", "Rycker upp"]
|
||||
},
|
||||
{
|
||||
"id": "face_pull",
|
||||
"name": "Face pull",
|
||||
"name_en": "Face Pull",
|
||||
"category": "isolation",
|
||||
"primary_muscles": ["rear_delts", "rhomboids"],
|
||||
"secondary_muscles": ["traps", "rotator_cuff"],
|
||||
"equipment": ["cable_machine"],
|
||||
"difficulty": "beginner",
|
||||
"alternatives": ["reverse_fly", "band_pull_apart"],
|
||||
"cues": ["Dra mot ansiktet", "Externa rotation i toppen", "Skuldror ihop"],
|
||||
"common_mistakes": ["För tungt", "Ingen extern rotation"]
|
||||
},
|
||||
{
|
||||
"id": "plank",
|
||||
"name": "Plankan",
|
||||
"name_en": "Plank",
|
||||
"category": "isolation",
|
||||
"primary_muscles": ["core"],
|
||||
"secondary_muscles": ["shoulders", "glutes"],
|
||||
"equipment": [],
|
||||
"difficulty": "beginner",
|
||||
"alternatives": ["dead_bug", "hollow_hold", "ab_wheel"],
|
||||
"cues": ["Rak linje huvud-häl", "Spänn magen", "Andas"],
|
||||
"common_mistakes": ["Hängande höfter", "Rumpan för högt"]
|
||||
},
|
||||
{
|
||||
"id": "cable_fly",
|
||||
"name": "Cable fly",
|
||||
"name_en": "Cable Fly",
|
||||
"category": "isolation",
|
||||
"primary_muscles": ["chest"],
|
||||
"secondary_muscles": ["front_delts"],
|
||||
"equipment": ["cable_machine"],
|
||||
"difficulty": "beginner",
|
||||
"alternatives": ["dumbbell_fly", "pec_deck"],
|
||||
"cues": ["Mjuk armbåge", "Kramas rakt fram", "Känn stretch"],
|
||||
"common_mistakes": ["Böjer armbågarna för mycket", "Går för tungt"]
|
||||
},
|
||||
{
|
||||
"id": "goblet_squat",
|
||||
"name": "Goblet squat",
|
||||
"name_en": "Goblet Squat",
|
||||
"category": "compound",
|
||||
"primary_muscles": ["quads", "glutes"],
|
||||
"secondary_muscles": ["core"],
|
||||
"equipment": ["dumbbell", "kettlebell"],
|
||||
"difficulty": "beginner",
|
||||
"alternatives": ["squat", "leg_press"],
|
||||
"cues": ["Vikten mot bröstet", "Armbågar mellan knäna", "Bröst upp"],
|
||||
"common_mistakes": ["Lutar framåt", "Hälar lyfter"]
|
||||
},
|
||||
{
|
||||
"id": "push_ups",
|
||||
"name": "Armhävningar",
|
||||
"name_en": "Push-ups",
|
||||
"category": "compound",
|
||||
"primary_muscles": ["chest", "triceps", "front_delts"],
|
||||
"secondary_muscles": ["core"],
|
||||
"equipment": [],
|
||||
"difficulty": "beginner",
|
||||
"alternatives": ["bench_press", "dumbbell_press", "knee_push_ups"],
|
||||
"cues": ["Kroppen rak", "Armbågar 45°", "Bröst till golv"],
|
||||
"common_mistakes": ["Hängande höfter", "Armbågar för brett", "Halvt ROM"]
|
||||
}
|
||||
],
|
||||
"muscle_groups": {
|
||||
"chest": { "name": "Bröst", "exercises": ["bench_press", "dumbbell_press", "push_ups", "cable_fly"] },
|
||||
"back": { "name": "Rygg", "exercises": ["deadlift", "barbell_row", "pull_ups", "lat_pulldown"] },
|
||||
"shoulders": { "name": "Axlar", "exercises": ["overhead_press", "lateral_raise", "face_pull"] },
|
||||
"quads": { "name": "Framsida lår", "exercises": ["squat", "leg_press", "leg_extension", "goblet_squat"] },
|
||||
"hamstrings": { "name": "Baksida lår", "exercises": ["deadlift", "romanian_deadlift", "leg_curl"] },
|
||||
"glutes": { "name": "Säte", "exercises": ["squat", "deadlift", "romanian_deadlift", "leg_press"] },
|
||||
"biceps": { "name": "Biceps", "exercises": ["bicep_curl", "pull_ups", "barbell_row"] },
|
||||
"triceps": { "name": "Triceps", "exercises": ["tricep_pushdown", "bench_press", "overhead_press", "push_ups"] },
|
||||
"core": { "name": "Core/mage", "exercises": ["plank", "deadlift", "squat"] }
|
||||
},
|
||||
"equipment_map": {
|
||||
"barbell": "Skivstång",
|
||||
"dumbbells": "Hantlar",
|
||||
"cable_machine": "Kabelmaskin",
|
||||
"bench": "Bänk",
|
||||
"squat_rack": "Knäböjsställning",
|
||||
"pull_up_bar": "Chinsstång",
|
||||
"leg_press_machine": "Benpressmaskin",
|
||||
"leg_curl_machine": "Bencurlmaskin",
|
||||
"leg_extension_machine": "Bensparkmaskin",
|
||||
"kettlebell": "Kettlebell"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
{
|
||||
"id": "beginner_fullbody",
|
||||
"name": "Nybörjarprogram - Helkropp",
|
||||
"goal": "general",
|
||||
"description": "Perfekt startprogram för nybörjare. Lär dig grundövningarna med fokus på teknik. Helkroppsträning 3x/vecka.",
|
||||
"experience_level": ["beginner"],
|
||||
"duration_weeks": 8,
|
||||
"workouts_per_week": [3],
|
||||
"principles": [
|
||||
"Fokus på teknik - använd lätt vikt tills formen är perfekt",
|
||||
"Helkropp varje pass för maximal inlärning",
|
||||
"48h vila mellan pass",
|
||||
"Öka vikt ENDAST när tekniken är solid"
|
||||
],
|
||||
"split": {
|
||||
"3_days": {
|
||||
"name": "A/B/A → B/A/B",
|
||||
"rotation": ["A", "B", "A"],
|
||||
"days": {
|
||||
"A": {
|
||||
"name": "Helkropp A",
|
||||
"exercises": [
|
||||
{ "id": "goblet_squat", "sets": 3, "reps": 10, "rest": "2 min", "note": "Fokus: knän ut, bröst upp" },
|
||||
{ "id": "dumbbell_press", "sets": 3, "reps": 10, "rest": "2 min", "note": "Platt bänk" },
|
||||
{ "id": "lat_pulldown", "sets": 3, "reps": 10, "rest": "2 min", "note": "Dra mot nyckelbenet" },
|
||||
{ "id": "leg_curl", "sets": 2, "reps": 12, "rest": "90 sek" },
|
||||
{ "id": "plank", "sets": 3, "reps": "20-30 sek", "rest": "60 sek" }
|
||||
],
|
||||
"duration_min": 45
|
||||
},
|
||||
"B": {
|
||||
"name": "Helkropp B",
|
||||
"exercises": [
|
||||
{ "id": "leg_press", "sets": 3, "reps": 10, "rest": "2 min", "note": "Fötter axelbrett" },
|
||||
{ "id": "push_ups", "sets": 3, "reps": "max (mål: 10)", "rest": "90 sek", "note": "Knästående OK" },
|
||||
{ "id": "barbell_row", "sets": 3, "reps": 10, "rest": "2 min", "note": "Eller maskinrodd" },
|
||||
{ "id": "lateral_raise", "sets": 2, "reps": 12, "rest": "60 sek" },
|
||||
{ "id": "bicep_curl", "sets": 2, "reps": 12, "rest": "60 sek" }
|
||||
],
|
||||
"duration_min": 45
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"progression": {
|
||||
"weeks_1_2": "Lätt vikt. Lär dig teknik. Ska kännas enkelt.",
|
||||
"weeks_3_4": "Öka till vikt där sista reps är utmanande men tekniken hålls.",
|
||||
"weeks_5_8": "Progressiv överbelastning - öka vikt när du klarar alla reps med bra form.",
|
||||
"next_step": "Efter 8 veckor: övergå till intermediate-program (Styrka 5x5 eller Hypertrofi PPL)"
|
||||
},
|
||||
"technique_focus": {
|
||||
"goblet_squat": "Grunden för alla knäböjvarianter. Vikten framför tvingar bröst upp.",
|
||||
"dumbbell_press": "Lättare att hitta rätt position än skivstång. Tränar stabilitet.",
|
||||
"lat_pulldown": "Bygger styrka för framtida pull-ups.",
|
||||
"push_ups": "Fundamental rörelse. Börja på knä om nödvändigt."
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,116 @@
|
||||
{
|
||||
"id": "hypertrophy_ppl",
|
||||
"name": "Hypertrofiprogram PPL",
|
||||
"goal": "muscle",
|
||||
"description": "Push/Pull/Legs split optimerat för muskelbygge. Högre volym och rep-ranges för maximal hypertrofi.",
|
||||
"experience_level": ["intermediate", "advanced"],
|
||||
"duration_weeks": 8,
|
||||
"workouts_per_week": [5, 6],
|
||||
"principles": [
|
||||
"8-12 reps för compound, 12-15 för isolation",
|
||||
"Fokus på mind-muscle connection",
|
||||
"60-90 sek vila för isolation, 2-3 min för compound",
|
||||
"Progressiv överbelastning genom volym ELLER vikt",
|
||||
"Träna nära failure (1-2 RIR)"
|
||||
],
|
||||
"split": {
|
||||
"6_days": {
|
||||
"name": "PPL x2",
|
||||
"rotation": ["push", "pull", "legs", "push", "pull", "legs"],
|
||||
"days": {
|
||||
"push": {
|
||||
"name": "Push (Bröst, Axlar, Triceps)",
|
||||
"exercises": [
|
||||
{ "id": "bench_press", "sets": 4, "reps": "8-10", "rest": "2-3 min" },
|
||||
{ "id": "overhead_press", "sets": 4, "reps": "8-10", "rest": "2 min" },
|
||||
{ "id": "dumbbell_press", "sets": 3, "reps": "10-12", "rest": "90 sek", "note": "Incline" },
|
||||
{ "id": "lateral_raise", "sets": 4, "reps": "12-15", "rest": "60 sek" },
|
||||
{ "id": "cable_fly", "sets": 3, "reps": "12-15", "rest": "60 sek" },
|
||||
{ "id": "tricep_pushdown", "sets": 3, "reps": "12-15", "rest": "60 sek" }
|
||||
]
|
||||
},
|
||||
"pull": {
|
||||
"name": "Pull (Rygg, Biceps)",
|
||||
"exercises": [
|
||||
{ "id": "deadlift", "sets": 3, "reps": "6-8", "rest": "3 min", "note": "Eller RDL" },
|
||||
{ "id": "pull_ups", "sets": 4, "reps": "8-10", "rest": "2 min" },
|
||||
{ "id": "barbell_row", "sets": 4, "reps": "8-10", "rest": "2 min" },
|
||||
{ "id": "lat_pulldown", "sets": 3, "reps": "10-12", "rest": "90 sek" },
|
||||
{ "id": "face_pull", "sets": 3, "reps": "15-20", "rest": "60 sek" },
|
||||
{ "id": "bicep_curl", "sets": 4, "reps": "10-12", "rest": "60 sek" }
|
||||
]
|
||||
},
|
||||
"legs": {
|
||||
"name": "Legs (Ben & Core)",
|
||||
"exercises": [
|
||||
{ "id": "squat", "sets": 4, "reps": "8-10", "rest": "3 min" },
|
||||
{ "id": "romanian_deadlift", "sets": 4, "reps": "10-12", "rest": "2 min" },
|
||||
{ "id": "leg_press", "sets": 3, "reps": "12-15", "rest": "90 sek" },
|
||||
{ "id": "leg_curl", "sets": 4, "reps": "10-12", "rest": "60 sek" },
|
||||
{ "id": "leg_extension", "sets": 3, "reps": "12-15", "rest": "60 sek" },
|
||||
{ "id": "plank", "sets": 3, "reps": "45-60 sek", "rest": "60 sek" }
|
||||
]
|
||||
}
|
||||
}
|
||||
},
|
||||
"5_days": {
|
||||
"name": "Upper/Lower/Push/Pull/Legs",
|
||||
"rotation": ["upper", "lower", "push", "pull", "legs"],
|
||||
"days": {
|
||||
"upper": {
|
||||
"name": "Överkropp (Styrka)",
|
||||
"exercises": [
|
||||
{ "id": "bench_press", "sets": 4, "reps": "6-8", "rest": "3 min" },
|
||||
{ "id": "barbell_row", "sets": 4, "reps": "6-8", "rest": "3 min" },
|
||||
{ "id": "overhead_press", "sets": 3, "reps": "8-10", "rest": "2 min" },
|
||||
{ "id": "pull_ups", "sets": 3, "reps": "8-10", "rest": "2 min" }
|
||||
]
|
||||
},
|
||||
"lower": {
|
||||
"name": "Underkropp (Styrka)",
|
||||
"exercises": [
|
||||
{ "id": "squat", "sets": 4, "reps": "6-8", "rest": "3 min" },
|
||||
{ "id": "deadlift", "sets": 3, "reps": "5-6", "rest": "3 min" },
|
||||
{ "id": "leg_press", "sets": 3, "reps": "10-12", "rest": "2 min" },
|
||||
{ "id": "leg_curl", "sets": 3, "reps": "10-12", "rest": "90 sek" }
|
||||
]
|
||||
},
|
||||
"push": {
|
||||
"name": "Push (Volym)",
|
||||
"exercises": [
|
||||
{ "id": "dumbbell_press", "sets": 4, "reps": "10-12", "rest": "90 sek" },
|
||||
{ "id": "lateral_raise", "sets": 4, "reps": "12-15", "rest": "60 sek" },
|
||||
{ "id": "cable_fly", "sets": 4, "reps": "12-15", "rest": "60 sek" },
|
||||
{ "id": "tricep_pushdown", "sets": 4, "reps": "12-15", "rest": "60 sek" }
|
||||
]
|
||||
},
|
||||
"pull": {
|
||||
"name": "Pull (Volym)",
|
||||
"exercises": [
|
||||
{ "id": "lat_pulldown", "sets": 4, "reps": "10-12", "rest": "90 sek" },
|
||||
{ "id": "barbell_row", "sets": 3, "reps": "10-12", "rest": "90 sek" },
|
||||
{ "id": "face_pull", "sets": 4, "reps": "15-20", "rest": "60 sek" },
|
||||
{ "id": "bicep_curl", "sets": 4, "reps": "12-15", "rest": "60 sek" }
|
||||
]
|
||||
},
|
||||
"legs": {
|
||||
"name": "Ben (Volym)",
|
||||
"exercises": [
|
||||
{ "id": "leg_press", "sets": 4, "reps": "12-15", "rest": "90 sek" },
|
||||
{ "id": "romanian_deadlift", "sets": 4, "reps": "10-12", "rest": "2 min" },
|
||||
{ "id": "leg_extension", "sets": 4, "reps": "12-15", "rest": "60 sek" },
|
||||
{ "id": "leg_curl", "sets": 4, "reps": "12-15", "rest": "60 sek" }
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"progression": {
|
||||
"rule": "Öka vikt när du når toppen av rep-range i alla sets",
|
||||
"example": "3x12 reps? Nästa pass: öka vikt, sikta på 3x8, bygg upp till 3x12 igen",
|
||||
"deload": {
|
||||
"when": "Stagnation eller vecka 5",
|
||||
"method": "50% volym, samma intensitet"
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
{
|
||||
"id": "strength_5x5",
|
||||
"name": "Styrkeprogram 5x5",
|
||||
"goal": "strength",
|
||||
"description": "Klassiskt 5x5-upplägg för maximal styrkeökning. Fokus på de stora lyftena med progressiv överbelastning.",
|
||||
"experience_level": ["intermediate", "advanced"],
|
||||
"duration_weeks": 8,
|
||||
"workouts_per_week": [3, 4],
|
||||
"principles": [
|
||||
"5 sets x 5 reps på basövningar (85% av 1RM)",
|
||||
"Öka vikten med 2.5kg varje vecka om alla reps klaras",
|
||||
"3-5 min vila mellan tunga set",
|
||||
"Deload vecka 4 och 8"
|
||||
],
|
||||
"split": {
|
||||
"3_days": {
|
||||
"name": "A/B/A - B/A/B",
|
||||
"rotation": ["A", "B", "A"],
|
||||
"days": {
|
||||
"A": {
|
||||
"name": "Knäböj & Bänk",
|
||||
"exercises": [
|
||||
{ "id": "squat", "sets": 5, "reps": 5, "intensity": "85%", "rest": "3-5 min" },
|
||||
{ "id": "bench_press", "sets": 5, "reps": 5, "intensity": "85%", "rest": "3-5 min" },
|
||||
{ "id": "barbell_row", "sets": 5, "reps": 5, "intensity": "80%", "rest": "2-3 min" }
|
||||
]
|
||||
},
|
||||
"B": {
|
||||
"name": "Knäböj & Press",
|
||||
"exercises": [
|
||||
{ "id": "squat", "sets": 5, "reps": 5, "intensity": "85%", "rest": "3-5 min" },
|
||||
{ "id": "overhead_press", "sets": 5, "reps": 5, "intensity": "85%", "rest": "3-5 min" },
|
||||
{ "id": "deadlift", "sets": 1, "reps": 5, "intensity": "90%", "rest": "5 min" }
|
||||
]
|
||||
}
|
||||
}
|
||||
},
|
||||
"4_days": {
|
||||
"name": "Upper/Lower",
|
||||
"rotation": ["upper", "lower", "rest", "upper", "lower"],
|
||||
"days": {
|
||||
"upper": {
|
||||
"name": "Överkropp",
|
||||
"exercises": [
|
||||
{ "id": "bench_press", "sets": 5, "reps": 5, "intensity": "85%", "rest": "3-5 min" },
|
||||
{ "id": "barbell_row", "sets": 5, "reps": 5, "intensity": "80%", "rest": "3 min" },
|
||||
{ "id": "overhead_press", "sets": 4, "reps": 6, "intensity": "80%", "rest": "2-3 min" },
|
||||
{ "id": "pull_ups", "sets": 3, "reps": "max", "rest": "2 min" }
|
||||
]
|
||||
},
|
||||
"lower": {
|
||||
"name": "Underkropp",
|
||||
"exercises": [
|
||||
{ "id": "squat", "sets": 5, "reps": 5, "intensity": "85%", "rest": "3-5 min" },
|
||||
{ "id": "deadlift", "sets": 3, "reps": 5, "intensity": "85%", "rest": "4 min" },
|
||||
{ "id": "leg_press", "sets": 3, "reps": 8, "intensity": "75%", "rest": "2 min" },
|
||||
{ "id": "leg_curl", "sets": 3, "reps": 10, "rest": "90 sek" }
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"progression": {
|
||||
"rule": "Om alla reps klaras, öka vikten nästa pass",
|
||||
"increment": {
|
||||
"upper_body": 2.5,
|
||||
"lower_body": 5.0
|
||||
},
|
||||
"deload": {
|
||||
"when": "2 missade pass i rad eller vecka 4/8",
|
||||
"reduction": "10%"
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
# Frontend Dev Agent - SOUL.md
|
||||
|
||||
Du är **Frontend**, en React-specialist med öga för UX och performance.
|
||||
|
||||
## Expertis
|
||||
- React (hooks, context, patterns)
|
||||
- Vite build tooling
|
||||
- CSS/styling (modern CSS, responsiv design)
|
||||
- State management
|
||||
- Performance optimization
|
||||
- Tillgänglighet (a11y)
|
||||
|
||||
## Principer
|
||||
1. **Komponentdriven** - små, återanvändbara komponenter
|
||||
2. **Mobile-first** - designa för mobil, skala upp
|
||||
3. **Performance** - lazy loading, memoization när det behövs
|
||||
4. **UX > fancy** - funktion före flashighet
|
||||
5. **Testa på riktig enhet** - emulatorer ljuger
|
||||
|
||||
## Kodstil
|
||||
```jsx
|
||||
// ✅ Bra: Tydligt, hooks överst, early returns
|
||||
function ExerciseCard({ exercise, onSelect }) {
|
||||
const [expanded, setExpanded] = useState(false);
|
||||
|
||||
if (!exercise) return null;
|
||||
|
||||
return (
|
||||
<div className="exercise-card" onClick={() => onSelect(exercise)}>
|
||||
{/* ... */}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
// ❌ Dåligt: Nested ternaries, inline styles, prop drilling
|
||||
```
|
||||
|
||||
## Filstruktur (Gravl)
|
||||
```
|
||||
src/
|
||||
├── components/ # Återanvändbara UI-komponenter
|
||||
├── pages/ # Route-komponenter
|
||||
├── context/ # React Context (auth, theme)
|
||||
├── hooks/ # Custom hooks
|
||||
├── utils/ # Helpers
|
||||
└── styles/ # Globala styles
|
||||
```
|
||||
|
||||
## Kommunikationsstil
|
||||
- Visar kod direkt - mindre snack, mer exempel
|
||||
- Förklarar "varför" bakom patterns
|
||||
- Länkar till relevanta docs vid behov
|
||||
- Testar i browser innan leverans
|
||||
|
||||
## Stack
|
||||
- React 18+
|
||||
- Vite
|
||||
- React Router
|
||||
- CSS (no framework, custom properties)
|
||||
@@ -0,0 +1,74 @@
|
||||
# Nutritionist Agent - SOUL.md
|
||||
|
||||
Du är **Nutri**, en evidensbaserad kostcoach med fokus på träningskost.
|
||||
|
||||
## Bakgrund
|
||||
- Utbildad kostrådgivare med idrottsfokus
|
||||
- Erfarenhet av styrkelyftare, bodybuilders och motionärer
|
||||
- Följer vetenskaplig konsensus, inte diettrender
|
||||
- Pragmatisk approach - hållbart > perfekt
|
||||
|
||||
## Principer
|
||||
1. **Kalorier är kung** - energibalans avgör vikt
|
||||
2. **Protein först** - grunden för kroppskomposition
|
||||
3. **Konsistens > perfektion** - 80/20-regeln
|
||||
4. **Individuellt** - inga universella lösningar
|
||||
5. **Mat är mat** - inga "rena" eller "fula" livsmedel
|
||||
|
||||
## Basrekommendationer
|
||||
|
||||
### Protein
|
||||
| Mål | Gram per kg kroppsvikt |
|
||||
|-----|------------------------|
|
||||
| Fettförbränning | 1.8-2.2 g/kg |
|
||||
| Muskelbygge | 1.6-2.0 g/kg |
|
||||
| Underhåll | 1.4-1.6 g/kg |
|
||||
|
||||
### Kaloriberäkning (förenklad)
|
||||
```
|
||||
BMR (män): 10 × vikt(kg) + 6.25 × längd(cm) - 5 × ålder + 5
|
||||
BMR (kvinnor): 10 × vikt(kg) + 6.25 × längd(cm) - 5 × ålder - 161
|
||||
|
||||
TDEE = BMR × aktivitetsfaktor
|
||||
- Stillasittande: 1.2
|
||||
- Lätt aktiv (1-3 pass/v): 1.375
|
||||
- Aktiv (3-5 pass/v): 1.55
|
||||
- Mycket aktiv (6-7 pass/v): 1.725
|
||||
|
||||
Bulk: TDEE + 300-500 kcal
|
||||
Cut: TDEE - 300-500 kcal
|
||||
```
|
||||
|
||||
### Makrofördelning (utgångspunkt)
|
||||
- **Protein**: 25-35% av kalorier
|
||||
- **Fett**: 20-35% (minst 0.5g/kg)
|
||||
- **Kolhydrater**: Resten
|
||||
|
||||
## Måltidstiming
|
||||
- **Pre-workout**: Kolhydrater + lite protein, 1-2h innan
|
||||
- **Post-workout**: Protein + kolhydrater inom 2h (inte kritiskt)
|
||||
- **Övrigt**: Spelar mindre roll - totalt intag viktigast
|
||||
|
||||
## Kommunikationsstil
|
||||
- Ger konkreta siffror och exempel
|
||||
- Förklarar "varför" kort
|
||||
- Anpassar till användarens mål och preferenser
|
||||
- Svenska, enkla termer
|
||||
|
||||
## Exempel på ton
|
||||
❌ "Du borde äta rent och undvika processad mat..."
|
||||
✅ "Med dina mål: ~2400 kcal, 160g protein. Fördela på 4 måltider = 40g protein/måltid. Kyckling, ägg, kvarg är praktiska sources."
|
||||
|
||||
## Begränsningar
|
||||
- ⛔ Inga medicinska kostråd (diabetes, allergier → läkare/dietist)
|
||||
- ⛔ Inga kosttillskottsrekommendationer (förutom kreatin/D-vitamin basics)
|
||||
- ⛔ Inga extrema dieter (VLCD, strikt keto för icke-medicinskt syfte)
|
||||
- ⚠️ Vid ätstörningshistorik → professionell hjälp
|
||||
|
||||
## Tillgänglig data
|
||||
Kan använda från Gravl API:
|
||||
- Kön, ålder, längd
|
||||
- Vikt (historik)
|
||||
- Kroppsfett (om tillgängligt)
|
||||
- Träningsmål
|
||||
- Pass per vecka
|
||||
@@ -0,0 +1,65 @@
|
||||
{
|
||||
"protein_sources": [
|
||||
{ "name": "Kycklingbröst", "serving": "100g", "kcal": 165, "protein": 31, "fat": 3.6, "carbs": 0 },
|
||||
{ "name": "Laxfilé", "serving": "100g", "kcal": 208, "protein": 20, "fat": 13, "carbs": 0 },
|
||||
{ "name": "Ägg (1 st)", "serving": "60g", "kcal": 90, "protein": 7, "fat": 6, "carbs": 0.5 },
|
||||
{ "name": "Kvarg (naturell)", "serving": "100g", "kcal": 63, "protein": 11, "fat": 0.2, "carbs": 4 },
|
||||
{ "name": "Grekisk yoghurt", "serving": "100g", "kcal": 97, "protein": 9, "fat": 5, "carbs": 3 },
|
||||
{ "name": "Cottage cheese", "serving": "100g", "kcal": 98, "protein": 11, "fat": 4.3, "carbs": 3.4 },
|
||||
{ "name": "Nötfärs (10%)", "serving": "100g", "kcal": 176, "protein": 20, "fat": 10, "carbs": 0 },
|
||||
{ "name": "Tonfisk (konserv)", "serving": "100g", "kcal": 116, "protein": 26, "fat": 1, "carbs": 0 },
|
||||
{ "name": "Räkor", "serving": "100g", "kcal": 85, "protein": 18, "fat": 1, "carbs": 0 },
|
||||
{ "name": "Tofu", "serving": "100g", "kcal": 76, "protein": 8, "fat": 4.8, "carbs": 1.9 },
|
||||
{ "name": "Tempeh", "serving": "100g", "kcal": 192, "protein": 19, "fat": 11, "carbs": 8 },
|
||||
{ "name": "Proteinpulver (whey)", "serving": "30g", "kcal": 120, "protein": 24, "fat": 1.5, "carbs": 3 }
|
||||
],
|
||||
"carb_sources": [
|
||||
{ "name": "Ris (kokt)", "serving": "100g", "kcal": 130, "protein": 2.7, "fat": 0.3, "carbs": 28 },
|
||||
{ "name": "Pasta (kokt)", "serving": "100g", "kcal": 131, "protein": 5, "fat": 1.1, "carbs": 25 },
|
||||
{ "name": "Potatis (kokt)", "serving": "100g", "kcal": 77, "protein": 2, "fat": 0.1, "carbs": 17 },
|
||||
{ "name": "Sötpotatis", "serving": "100g", "kcal": 86, "protein": 1.6, "fat": 0.1, "carbs": 20 },
|
||||
{ "name": "Havregryn", "serving": "100g", "kcal": 379, "protein": 13, "fat": 7, "carbs": 66 },
|
||||
{ "name": "Bröd (fullkorn)", "serving": "1 skiva", "kcal": 80, "protein": 3, "fat": 1, "carbs": 15 },
|
||||
{ "name": "Banan", "serving": "1 st (120g)", "kcal": 105, "protein": 1.3, "fat": 0.4, "carbs": 27 },
|
||||
{ "name": "Äpple", "serving": "1 st (150g)", "kcal": 78, "protein": 0.4, "fat": 0.2, "carbs": 21 },
|
||||
{ "name": "Quinoa (kokt)", "serving": "100g", "kcal": 120, "protein": 4.4, "fat": 1.9, "carbs": 21 }
|
||||
],
|
||||
"fat_sources": [
|
||||
{ "name": "Olivolja", "serving": "1 msk", "kcal": 119, "protein": 0, "fat": 13.5, "carbs": 0 },
|
||||
{ "name": "Avokado", "serving": "100g", "kcal": 160, "protein": 2, "fat": 15, "carbs": 9 },
|
||||
{ "name": "Mandlar", "serving": "30g", "kcal": 173, "protein": 6, "fat": 15, "carbs": 6 },
|
||||
{ "name": "Jordnötssmör", "serving": "1 msk", "kcal": 94, "protein": 4, "fat": 8, "carbs": 3 },
|
||||
{ "name": "Smör", "serving": "10g", "kcal": 72, "protein": 0, "fat": 8, "carbs": 0 },
|
||||
{ "name": "Ost (vällagrad)", "serving": "30g", "kcal": 120, "protein": 8, "fat": 10, "carbs": 0 }
|
||||
],
|
||||
"vegetables": [
|
||||
{ "name": "Broccoli", "serving": "100g", "kcal": 34, "protein": 2.8, "fat": 0.4, "carbs": 7 },
|
||||
{ "name": "Spenat", "serving": "100g", "kcal": 23, "protein": 2.9, "fat": 0.4, "carbs": 3.6 },
|
||||
{ "name": "Paprika", "serving": "100g", "kcal": 31, "protein": 1, "fat": 0.3, "carbs": 6 },
|
||||
{ "name": "Tomat", "serving": "100g", "kcal": 18, "protein": 0.9, "fat": 0.2, "carbs": 3.9 },
|
||||
{ "name": "Gurka", "serving": "100g", "kcal": 15, "protein": 0.7, "fat": 0.1, "carbs": 3.6 },
|
||||
{ "name": "Morötter", "serving": "100g", "kcal": 41, "protein": 0.9, "fat": 0.2, "carbs": 10 }
|
||||
],
|
||||
"meal_templates": {
|
||||
"bulk_day": {
|
||||
"description": "~2800 kcal, 180g protein",
|
||||
"meals": [
|
||||
{ "name": "Frukost", "example": "Havregryn 80g + mjölk + banan + whey", "kcal": 550 },
|
||||
{ "name": "Lunch", "example": "Kyckling 150g + ris 200g + grönsaker + olivolja", "kcal": 700 },
|
||||
{ "name": "Mellanmål", "example": "Kvarg 300g + jordnötssmör + frukt", "kcal": 450 },
|
||||
{ "name": "Middag", "example": "Lax 150g + potatis 250g + grönsaker", "kcal": 650 },
|
||||
{ "name": "Kvällsmål", "example": "Ägg 3st + bröd 2 skivor + ost", "kcal": 450 }
|
||||
]
|
||||
},
|
||||
"cut_day": {
|
||||
"description": "~1800 kcal, 160g protein",
|
||||
"meals": [
|
||||
{ "name": "Frukost", "example": "Ägg 3st + grönsaker + 1 brödskiva", "kcal": 350 },
|
||||
{ "name": "Lunch", "example": "Kyckling 150g + ris 100g + mycket grönsaker", "kcal": 450 },
|
||||
{ "name": "Mellanmål", "example": "Kvarg 250g + bär", "kcal": 200 },
|
||||
{ "name": "Middag", "example": "Torsk 200g + potatis 150g + grönsaker", "kcal": 400 },
|
||||
{ "name": "Kvällsmål", "example": "Cottage cheese 200g + gurka", "kcal": 200 }
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
# Code Reviewer Agent - SOUL.md
|
||||
|
||||
Du är **Reviewer**, en noggrann code reviewer som balanserar kvalitet med pragmatism.
|
||||
|
||||
## Fokusområden
|
||||
1. **Säkerhet** - SQL injection, XSS, auth issues
|
||||
2. **Korrekthet** - gör koden vad den ska?
|
||||
3. **Läsbarhet** - kan någon annan förstå detta om 6 månader?
|
||||
4. **Performance** - uppenbara flaskhalsar
|
||||
5. **Edge cases** - vad händer när input är null/tomt/gigantiskt?
|
||||
|
||||
## Review-stil
|
||||
|
||||
### Kategorisera feedback
|
||||
- 🔴 **BLOCKER** - Måste fixas. Säkerhetshål, buggar.
|
||||
- 🟡 **SUGGESTION** - Borde fixas. Förbättrar kvalitet.
|
||||
- 🟢 **NIT** - Nice to have. Stilfrågor, minor improvements.
|
||||
|
||||
### Exempel
|
||||
```
|
||||
🔴 BLOCKER: SQL injection risk
|
||||
- const result = await pool.query(`SELECT * FROM users WHERE email = '${email}'`);
|
||||
+ const result = await pool.query('SELECT * FROM users WHERE email = $1', [email]);
|
||||
|
||||
🟡 SUGGESTION: Saknar error handling
|
||||
+ try {
|
||||
const data = await fetch(url);
|
||||
+ } catch (err) {
|
||||
+ console.error('Fetch failed:', err);
|
||||
+ return null;
|
||||
+ }
|
||||
|
||||
🟢 NIT: Överväg destructuring
|
||||
- const name = user.name;
|
||||
- const email = user.email;
|
||||
+ const { name, email } = user;
|
||||
```
|
||||
|
||||
## Principer
|
||||
- **Var snäll** - kritisera koden, inte personen
|
||||
- **Förklara varför** - inte bara "gör så här"
|
||||
- **Ge kredit** - "Bra lösning på X!"
|
||||
- **Pick your battles** - fokusera på det viktiga
|
||||
- **Erbjud alternativ** - visa bättre approach
|
||||
|
||||
## Kommunikationsstil
|
||||
- Börja med övergripande intryck
|
||||
- Lista issues i prioritetsordning (blockers först)
|
||||
- Avsluta med positiv feedback om möjligt
|
||||
- Svenska, men kodexempel som de är
|
||||
|
||||
## Vad jag INTE gör
|
||||
- Bikeshedding (oändliga diskussioner om tabs vs spaces)
|
||||
- Blockerar på stilfrågor som linter kan fixa
|
||||
- Kräver perfektion i MVP/prototypes
|
||||
@@ -0,0 +1,64 @@
|
||||
-- 06-01: Add swapped_from_id to workout_logs for tracking workout swaps
|
||||
ALTER TABLE workout_logs
|
||||
ADD COLUMN IF NOT EXISTS swapped_from_id INTEGER REFERENCES workout_logs(id) ON DELETE SET NULL,
|
||||
ADD COLUMN IF NOT EXISTS source_type VARCHAR(50) DEFAULT 'program', -- 'program' or 'custom'
|
||||
ADD COLUMN IF NOT EXISTS custom_workout_id INTEGER,
|
||||
ADD COLUMN IF NOT EXISTS custom_workout_exercise_id INTEGER;
|
||||
|
||||
-- Create workout_swaps table for swap history
|
||||
CREATE TABLE IF NOT EXISTS workout_swaps (
|
||||
id SERIAL PRIMARY KEY,
|
||||
user_id INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE,
|
||||
original_log_id INTEGER REFERENCES workout_logs(id) ON DELETE CASCADE,
|
||||
swapped_log_id INTEGER REFERENCES workout_logs(id) ON DELETE CASCADE,
|
||||
swap_date DATE NOT NULL,
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_workout_swaps_user_date ON workout_swaps(user_id, swap_date);
|
||||
CREATE INDEX IF NOT EXISTS idx_workout_swaps_original_log ON workout_swaps(original_log_id);
|
||||
|
||||
-- 06-02: Create muscle_group_recovery table for tracking recovery per muscle group
|
||||
CREATE TABLE IF NOT EXISTS muscle_group_recovery (
|
||||
id SERIAL PRIMARY KEY,
|
||||
user_id INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE,
|
||||
muscle_group VARCHAR(100) NOT NULL,
|
||||
last_workout_date TIMESTAMP,
|
||||
intensity NUMERIC(3,2) DEFAULT 0.5,
|
||||
exercises_count INTEGER DEFAULT 0,
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
UNIQUE(user_id, muscle_group)
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_muscle_group_recovery_user ON muscle_group_recovery(user_id);
|
||||
CREATE INDEX IF NOT EXISTS idx_muscle_group_recovery_last_workout ON muscle_group_recovery(user_id, last_workout_date);
|
||||
|
||||
-- 06-01 Extended: Create custom_workouts table for custom workout support
|
||||
CREATE TABLE IF NOT EXISTS custom_workouts (
|
||||
id SERIAL PRIMARY KEY,
|
||||
user_id INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE,
|
||||
name VARCHAR(255) NOT NULL,
|
||||
description TEXT,
|
||||
source_program_day_id INTEGER REFERENCES program_days(id),
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_custom_workouts_user ON custom_workouts(user_id);
|
||||
|
||||
-- Create custom_workout_exercises table
|
||||
CREATE TABLE IF NOT EXISTS custom_workout_exercises (
|
||||
id SERIAL PRIMARY KEY,
|
||||
custom_workout_id INTEGER NOT NULL REFERENCES custom_workouts(id) ON DELETE CASCADE,
|
||||
exercise_id INTEGER NOT NULL REFERENCES exercises(id),
|
||||
sets INTEGER DEFAULT 3,
|
||||
reps_min INTEGER DEFAULT 8,
|
||||
reps_max INTEGER DEFAULT 12,
|
||||
order_index INTEGER,
|
||||
replaced_exercise_id INTEGER REFERENCES exercises(id),
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_custom_workout_exercises_workout ON custom_workout_exercises(custom_workout_id);
|
||||
@@ -9,7 +9,10 @@ const { getHealthStatus, getUptime } = require('./utils/health');
|
||||
const { createExerciseResearchRouter } = require('./routes/exerciseResearch');
|
||||
const { createExerciseRecommendationRouter } = require('./routes/exerciseRecommendations');
|
||||
const { createWorkoutRouter } = require('./routes/workouts');
|
||||
const { createRecoveryRouter } = require('./routes/recovery');
|
||||
const { createSmartRecommendationsRouter } = require('./routes/smartRecommendations');
|
||||
const { searchExerciseResearch } = require('./services/exaSearch');
|
||||
const { updateMuscleGroupRecovery } = require('./services/recoveryService');
|
||||
|
||||
const app = express();
|
||||
const PORT = process.env.PORT || 3001;
|
||||
@@ -29,6 +32,8 @@ app.use(express.json());
|
||||
app.use(requestLoggerMiddleware); // Add request logging middleware
|
||||
|
||||
app.use('/api/exercises', createExerciseResearchRouter({ pool, exaSearch: searchExerciseResearch }));
|
||||
app.use('/api/recovery', createRecoveryRouter({ pool }));
|
||||
app.use('/api/recommendations', createSmartRecommendationsRouter({ pool }));
|
||||
app.use('/api/exercises', createExerciseRecommendationRouter());
|
||||
app.use('/api/workouts', createWorkoutRouter({ pool }));
|
||||
|
||||
@@ -771,6 +776,25 @@ app.post('/api/logs', async (req, res) => {
|
||||
);
|
||||
}
|
||||
|
||||
// Track recovery if exercise is completed
|
||||
if (completed && program_exercise_id) {
|
||||
try {
|
||||
const exerciseResult = await pool.query(
|
||||
`SELECT e.muscle_group FROM exercises e
|
||||
JOIN program_exercises pe ON e.id = pe.exercise_id
|
||||
WHERE pe.id = $1`,
|
||||
[program_exercise_id]
|
||||
);
|
||||
|
||||
if (exerciseResult.rows.length > 0) {
|
||||
const muscleGroup = exerciseResult.rows[0].muscle_group;
|
||||
await updateMuscleGroupRecovery(pool, user_id, muscleGroup, 0.8);
|
||||
}
|
||||
} catch (recoveryErr) {
|
||||
logger.warn('Failed to update recovery tracking', { error: recoveryErr.message });
|
||||
}
|
||||
}
|
||||
|
||||
logger.debug('Workout set logged', { userId: user_id, exerciseId: exerciseRef, weight, reps });
|
||||
res.json(result.rows[0]);
|
||||
} catch (err) {
|
||||
|
||||
@@ -0,0 +1,819 @@
|
||||
const express = require('express');
|
||||
const cors = require('cors');
|
||||
const { Pool } = require('pg');
|
||||
const bcrypt = require('bcryptjs');
|
||||
const jwt = require('jsonwebtoken');
|
||||
const logger = require('./utils/logger');
|
||||
const requestLoggerMiddleware = require('./middleware/requestLogger');
|
||||
const { getHealthStatus, getUptime } = require('./utils/health');
|
||||
const { createExerciseResearchRouter } = require('./routes/exerciseResearch');
|
||||
const { createExerciseRecommendationRouter } = require('./routes/exerciseRecommendations');
|
||||
const { createWorkoutRouter } = require('./routes/workouts');
|
||||
const { createRecoveryRouter } = require('./routes/recovery');
|
||||
const { createSmartRecommendationsRouter } = require('./routes/smartRecommendations');
|
||||
const { searchExerciseResearch } = require('./services/exaSearch');
|
||||
const { updateMuscleGroupRecovery } = require('./services/recoveryService');
|
||||
|
||||
const app = express();
|
||||
const PORT = process.env.PORT || 3001;
|
||||
const JWT_SECRET = process.env.JWT_SECRET || 'gravl-secret-key-change-in-production';
|
||||
|
||||
const pool = new Pool({
|
||||
host: process.env.DB_HOST || 'postgres',
|
||||
port: process.env.DB_PORT || 5432,
|
||||
user: process.env.DB_USER || 'postgres',
|
||||
password: process.env.DB_PASSWORD || 'homelab_postgres_2026',
|
||||
database: process.env.DB_NAME || 'gravl'
|
||||
});
|
||||
|
||||
// Middleware setup
|
||||
app.use(cors());
|
||||
app.use(express.json());
|
||||
app.use(requestLoggerMiddleware); // Add request logging middleware
|
||||
|
||||
app.use('/api/exercises', createExerciseResearchRouter({ pool, exaSearch: searchExerciseResearch }));
|
||||
app.use('/api/recovery', createRecoveryRouter({ pool }));
|
||||
app.use('/api/recommendations', createSmartRecommendationsRouter({ pool }));
|
||||
app.use('/api/exercises', createExerciseRecommendationRouter());
|
||||
app.use('/api/workouts', createWorkoutRouter({ pool }));
|
||||
|
||||
const authMiddleware = (req, res, next) => {
|
||||
const token = req.headers.authorization?.split(' ')[1];
|
||||
if (!token) return res.status(401).json({ error: 'No token' });
|
||||
try {
|
||||
req.user = jwt.verify(token, JWT_SECRET);
|
||||
next();
|
||||
} catch { res.status(401).json({ error: 'Invalid token' }); }
|
||||
};
|
||||
|
||||
// Enhanced health endpoint with uptime and database status
|
||||
app.get('/api/health', async (req, res) => {
|
||||
try {
|
||||
const health = await getHealthStatus(pool);
|
||||
const statusCode = health.status === 'healthy' ? 200 : (health.status === 'degraded' ? 200 : 503);
|
||||
res.status(statusCode).json(health);
|
||||
} catch (err) {
|
||||
logger.error('Health check error', { error: err.message });
|
||||
res.status(503).json({
|
||||
status: 'unhealthy',
|
||||
uptime: getUptime(),
|
||||
timestamp: new Date().toISOString(),
|
||||
error: 'Health check failed'
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
app.post('/api/auth/register', async (req, res) => {
|
||||
try {
|
||||
const { email, password } = req.body;
|
||||
if (!email || !password) return res.status(400).json({ error: 'Email and password required' });
|
||||
const hash = await bcrypt.hash(password, 10);
|
||||
const result = await pool.query(
|
||||
'INSERT INTO users (email, password_hash) VALUES ($1, $2) RETURNING id, email',
|
||||
[email.toLowerCase(), hash]
|
||||
);
|
||||
const token = jwt.sign({ id: result.rows[0].id, email: result.rows[0].email }, JWT_SECRET, { expiresIn: '30d' });
|
||||
logger.info('User registered', { userId: result.rows[0].id, email: result.rows[0].email });
|
||||
res.json({ token, user: result.rows[0] });
|
||||
} catch (err) {
|
||||
if (err.code === '23505') {
|
||||
logger.warn('Registration failed - email exists', { email: req.body.email });
|
||||
return res.status(400).json({ error: 'Email already exists' });
|
||||
}
|
||||
logger.error('Register error', { error: err.message });
|
||||
res.status(500).json({ error: 'Server error' });
|
||||
}
|
||||
});
|
||||
|
||||
app.post('/api/auth/login', async (req, res) => {
|
||||
try {
|
||||
const { email, password } = req.body;
|
||||
const result = await pool.query('SELECT * FROM users WHERE email = $1', [email.toLowerCase()]);
|
||||
if (!result.rows.length) {
|
||||
logger.warn('Login failed - user not found', { email });
|
||||
return res.status(401).json({ error: 'Invalid credentials' });
|
||||
}
|
||||
const user = result.rows[0];
|
||||
const valid = await bcrypt.compare(password, user.password_hash);
|
||||
if (!valid) {
|
||||
logger.warn('Login failed - invalid password', { userId: user.id });
|
||||
return res.status(401).json({ error: 'Invalid credentials' });
|
||||
}
|
||||
const token = jwt.sign({ id: user.id, email: user.email }, JWT_SECRET, { expiresIn: '30d' });
|
||||
const { password_hash, ...safeUser } = user;
|
||||
logger.info('User logged in', { userId: user.id, email: user.email });
|
||||
res.json({ token, user: safeUser });
|
||||
} catch (err) {
|
||||
logger.error('Login error', { error: err.message });
|
||||
res.status(500).json({ error: 'Server error' });
|
||||
}
|
||||
});
|
||||
|
||||
app.get('/api/user/profile', authMiddleware, async (req, res) => {
|
||||
try {
|
||||
const userResult = await pool.query(
|
||||
'SELECT id, email, gender, age, height_cm, experience_level, goal, workouts_per_week, onboarding_complete FROM users WHERE id = $1',
|
||||
[req.user.id]
|
||||
);
|
||||
if (!userResult.rows.length) return res.status(404).json({ error: 'User not found' });
|
||||
|
||||
const user = userResult.rows[0];
|
||||
|
||||
// Get latest measurements
|
||||
const measResult = await pool.query(
|
||||
'SELECT weight, neck_cm, waist_cm, hip_cm, body_fat_pct, measured_at FROM user_measurements WHERE user_id = $1 ORDER BY measured_at DESC LIMIT 1',
|
||||
[req.user.id]
|
||||
);
|
||||
|
||||
// Get latest strength
|
||||
const strResult = await pool.query(
|
||||
'SELECT bench_1rm, squat_1rm, deadlift_1rm, measured_at FROM user_strength WHERE user_id = $1 ORDER BY measured_at DESC LIMIT 1',
|
||||
[req.user.id]
|
||||
);
|
||||
|
||||
res.json({
|
||||
...user,
|
||||
measurements: measResult.rows[0] || null,
|
||||
strength: strResult.rows[0] || null
|
||||
});
|
||||
} catch (err) {
|
||||
logger.error('Profile error', { error: err.message, userId: req.user.id });
|
||||
res.status(500).json({ error: 'Server error' });
|
||||
}
|
||||
});
|
||||
|
||||
app.put('/api/user/profile', authMiddleware, async (req, res) => {
|
||||
try {
|
||||
const { gender, age, height_cm, experience_level, goal, workouts_per_week, onboarding_complete } = req.body;
|
||||
const num = v => (v === '' || v === undefined) ? null : v;
|
||||
|
||||
const result = await pool.query(
|
||||
`UPDATE users SET gender=$1, age=$2, height_cm=$3, experience_level=$4, goal=$5, workouts_per_week=$6, onboarding_complete=$7
|
||||
WHERE id=$8 RETURNING id, email, gender, age, height_cm, experience_level, goal, workouts_per_week, onboarding_complete`,
|
||||
[gender, num(age), num(height_cm), experience_level, goal, num(workouts_per_week), onboarding_complete, req.user.id]
|
||||
);
|
||||
logger.info('User profile updated', { userId: req.user.id });
|
||||
res.json(result.rows[0]);
|
||||
} catch (err) {
|
||||
logger.error('Update profile error', { error: err.message, userId: req.user.id });
|
||||
res.status(500).json({ error: 'Server error' });
|
||||
}
|
||||
});
|
||||
|
||||
// Add measurements
|
||||
app.post('/api/user/measurements', authMiddleware, async (req, res) => {
|
||||
try {
|
||||
const { weight, neck_cm, waist_cm, hip_cm, body_fat_pct } = req.body;
|
||||
const num = v => (v === '' || v === undefined) ? null : v;
|
||||
|
||||
const result = await pool.query(
|
||||
`INSERT INTO user_measurements (user_id, weight, neck_cm, waist_cm, hip_cm, body_fat_pct)
|
||||
VALUES ($1, $2, $3, $4, $5, $6) RETURNING *`,
|
||||
[req.user.id, num(weight), num(neck_cm), num(waist_cm), num(hip_cm), num(body_fat_pct)]
|
||||
);
|
||||
logger.info('Measurements added', { userId: req.user.id });
|
||||
res.json(result.rows[0]);
|
||||
} catch (err) {
|
||||
logger.error('Add measurements error', { error: err.message, userId: req.user.id });
|
||||
res.status(500).json({ error: 'Server error' });
|
||||
}
|
||||
});
|
||||
|
||||
// Get measurements history
|
||||
app.get('/api/user/measurements', authMiddleware, async (req, res) => {
|
||||
try {
|
||||
const result = await pool.query(
|
||||
'SELECT * FROM user_measurements WHERE user_id = $1 ORDER BY measured_at DESC LIMIT 100',
|
||||
[req.user.id]
|
||||
);
|
||||
res.json(result.rows);
|
||||
} catch (err) {
|
||||
logger.error('Get measurements error', { error: err.message, userId: req.user.id });
|
||||
res.status(500).json({ error: 'Server error' });
|
||||
}
|
||||
});
|
||||
|
||||
// Add strength record
|
||||
app.post('/api/user/strength', authMiddleware, async (req, res) => {
|
||||
try {
|
||||
const { bench_1rm, squat_1rm, deadlift_1rm } = req.body;
|
||||
const num = v => (v === '' || v === undefined) ? null : v;
|
||||
|
||||
const result = await pool.query(
|
||||
`INSERT INTO user_strength (user_id, bench_1rm, squat_1rm, deadlift_1rm)
|
||||
VALUES ($1, $2, $3, $4) RETURNING *`,
|
||||
[req.user.id, num(bench_1rm), num(squat_1rm), num(deadlift_1rm)]
|
||||
);
|
||||
logger.info('Strength record added', { userId: req.user.id });
|
||||
res.json(result.rows[0]);
|
||||
} catch (err) {
|
||||
logger.error('Add strength error', { error: err.message, userId: req.user.id });
|
||||
res.status(500).json({ error: 'Server error' });
|
||||
}
|
||||
});
|
||||
|
||||
// Get strength history
|
||||
app.get('/api/user/strength', authMiddleware, async (req, res) => {
|
||||
try {
|
||||
const result = await pool.query(
|
||||
'SELECT * FROM user_strength WHERE user_id = $1 ORDER BY measured_at DESC LIMIT 100',
|
||||
[req.user.id]
|
||||
);
|
||||
res.json(result.rows);
|
||||
} catch (err) {
|
||||
logger.error('Get strength error', { error: err.message, userId: req.user.id });
|
||||
res.status(500).json({ error: 'Server error' });
|
||||
}
|
||||
});
|
||||
|
||||
// Get all programs
|
||||
app.get('/api/programs', async (req, res) => {
|
||||
try {
|
||||
const result = await pool.query('SELECT * FROM programs ORDER BY id');
|
||||
res.json(result.rows);
|
||||
} catch (err) {
|
||||
logger.error('Error fetching programs', { error: err.message });
|
||||
res.status(500).json({ error: 'Database error' });
|
||||
}
|
||||
});
|
||||
|
||||
// Get program details with days
|
||||
app.get('/api/programs/:id', async (req, res) => {
|
||||
try {
|
||||
const program = await pool.query('SELECT * FROM programs WHERE id = $1', [req.params.id]);
|
||||
if (program.rows.length === 0) {
|
||||
return res.status(404).json({ error: 'Program not found' });
|
||||
}
|
||||
|
||||
const days = await pool.query(`
|
||||
SELECT pd.*,
|
||||
json_agg(json_build_object(
|
||||
'id', pe.id,
|
||||
'exercise_id', e.id,
|
||||
'name', e.name,
|
||||
'muscle_group', e.muscle_group,
|
||||
'sets', pe.sets,
|
||||
'reps_min', pe.reps_min,
|
||||
'reps_max', pe.reps_max,
|
||||
'order', pe.order_num
|
||||
) ORDER BY pe.order_num) as exercises
|
||||
FROM program_days pd
|
||||
LEFT JOIN program_exercises pe ON pd.id = pe.program_day_id
|
||||
LEFT JOIN exercises e ON pe.exercise_id = e.id
|
||||
WHERE pd.program_id = $1
|
||||
GROUP BY pd.id
|
||||
ORDER BY pd.day_number
|
||||
`, [req.params.id]);
|
||||
|
||||
res.json({
|
||||
...program.rows[0],
|
||||
days: days.rows
|
||||
});
|
||||
} catch (err) {
|
||||
logger.error('Error fetching program', { error: err.message, programId: req.params.id });
|
||||
res.status(500).json({ error: 'Database error' });
|
||||
}
|
||||
});
|
||||
|
||||
// Get exercises for a specific day
|
||||
app.get('/api/days/:dayId/exercises', async (req, res) => {
|
||||
try {
|
||||
const result = await pool.query(`
|
||||
SELECT pe.id, pe.sets, pe.reps_min, pe.reps_max, pe.order_num,
|
||||
e.id as exercise_id, e.name, e.muscle_group, e.description
|
||||
FROM program_exercises pe
|
||||
JOIN exercises e ON pe.exercise_id = e.id
|
||||
WHERE pe.program_day_id = $1
|
||||
ORDER BY pe.order_num
|
||||
`, [req.params.dayId]);
|
||||
res.json(result.rows);
|
||||
} catch (err) {
|
||||
logger.error('Error fetching exercises', { error: err.message, dayId: req.params.dayId });
|
||||
res.status(500).json({ error: 'Database error' });
|
||||
}
|
||||
});
|
||||
|
||||
// Get alternative exercises for a given exercise (same muscle group)
|
||||
app.get('/api/exercises/:id/alternatives', async (req, res) => {
|
||||
try {
|
||||
const exerciseResult = await pool.query(
|
||||
'SELECT muscle_group FROM exercises WHERE id = $1',
|
||||
[req.params.id]
|
||||
);
|
||||
|
||||
if (!exerciseResult.rows.length) {
|
||||
return res.status(404).json({ error: 'Exercise not found' });
|
||||
}
|
||||
|
||||
const muscleGroup = exerciseResult.rows[0].muscle_group;
|
||||
const alternatives = await pool.query(
|
||||
`SELECT id, name, muscle_group, description
|
||||
FROM exercises
|
||||
WHERE muscle_group = $1 AND id <> $2
|
||||
ORDER BY name`,
|
||||
[muscleGroup, req.params.id]
|
||||
);
|
||||
|
||||
res.json(alternatives.rows);
|
||||
} catch (err) {
|
||||
logger.error('Error fetching alternatives', { error: err.message, exerciseId: req.params.id });
|
||||
res.status(500).json({ error: 'Database error' });
|
||||
}
|
||||
});
|
||||
|
||||
// Get last workout for a specific exercise id
|
||||
app.get('/api/exercises/:id/last-workout', async (req, res) => {
|
||||
try {
|
||||
const { user_id } = req.query;
|
||||
const result = await pool.query(`
|
||||
WITH latest AS (
|
||||
SELECT wl.date
|
||||
FROM workout_logs wl
|
||||
JOIN program_exercises pe ON wl.program_exercise_id = pe.id
|
||||
WHERE pe.exercise_id = $1 AND wl.user_id = $2
|
||||
ORDER BY wl.date DESC
|
||||
LIMIT 1
|
||||
)
|
||||
SELECT wl.*
|
||||
FROM workout_logs wl
|
||||
JOIN program_exercises pe ON wl.program_exercise_id = pe.id
|
||||
JOIN latest l ON wl.date = l.date
|
||||
WHERE pe.exercise_id = $1 AND wl.user_id = $2
|
||||
ORDER BY wl.set_number ASC
|
||||
`, [req.params.id, user_id || 1]);
|
||||
res.json(result.rows);
|
||||
} catch (err) {
|
||||
logger.error('Error fetching last workout for exercise', { error: err.message, exerciseId: req.params.id });
|
||||
res.status(500).json({ error: 'Database error' });
|
||||
}
|
||||
});
|
||||
|
||||
// Calculate suggested weight based on progression
|
||||
app.get('/api/progression/:programExerciseId', async (req, res) => {
|
||||
try {
|
||||
const { user_id } = req.query;
|
||||
|
||||
// Get exercise details
|
||||
const exerciseInfo = await pool.query(`
|
||||
SELECT pe.*, e.name FROM program_exercises pe
|
||||
JOIN exercises e ON pe.exercise_id = e.id
|
||||
WHERE pe.id = $1
|
||||
`, [req.params.programExerciseId]);
|
||||
|
||||
if (exerciseInfo.rows.length === 0) {
|
||||
return res.status(404).json({ error: 'Exercise not found' });
|
||||
}
|
||||
|
||||
const exercise = exerciseInfo.rows[0];
|
||||
|
||||
// Get last workout logs for this exercise
|
||||
const lastLogs = await pool.query(`
|
||||
SELECT * FROM workout_logs
|
||||
WHERE program_exercise_id = $1 AND user_id = $2 AND completed = true
|
||||
ORDER BY date DESC, set_number ASC
|
||||
LIMIT $3
|
||||
`, [req.params.programExerciseId, user_id || 1, exercise.sets]);
|
||||
|
||||
if (lastLogs.rows.length === 0) {
|
||||
return res.json({
|
||||
suggestedWeight: 20, // Starting weight
|
||||
reason: 'No previous data - start light'
|
||||
});
|
||||
}
|
||||
|
||||
const lastWeight = lastLogs.rows[0].weight;
|
||||
const allSetsHitMaxReps = lastLogs.rows.every(log => log.reps >= exercise.reps_max);
|
||||
|
||||
if (allSetsHitMaxReps) {
|
||||
// Progress: increase weight by 2.5kg
|
||||
return res.json({
|
||||
suggestedWeight: lastWeight + 2.5,
|
||||
reason: `Hit ${exercise.reps_max} reps on all sets - increase weight!`
|
||||
});
|
||||
}
|
||||
|
||||
return res.json({
|
||||
suggestedWeight: lastWeight,
|
||||
reason: 'Keep same weight until you hit max reps on all sets'
|
||||
});
|
||||
} catch (err) {
|
||||
logger.error('Error calculating progression', { error: err.message, programExerciseId: req.params.programExerciseId });
|
||||
res.status(500).json({ error: 'Database error' });
|
||||
}
|
||||
});
|
||||
|
||||
// Get today's workout based on program day cycle
|
||||
app.get('/api/today/:programId', async (req, res) => {
|
||||
try {
|
||||
const { week } = req.query;
|
||||
const currentWeek = week || 1;
|
||||
|
||||
// Get program days
|
||||
const days = await pool.query(`
|
||||
SELECT pd.*,
|
||||
json_agg(json_build_object(
|
||||
'id', pe.id,
|
||||
'exercise_id', e.id,
|
||||
'name', e.name,
|
||||
'muscle_group', e.muscle_group,
|
||||
'sets', pe.sets,
|
||||
'reps_min', pe.reps_min,
|
||||
'reps_max', pe.reps_max,
|
||||
'order', pe.order_num
|
||||
) ORDER BY pe.order_num) as exercises
|
||||
FROM program_days pd
|
||||
LEFT JOIN program_exercises pe ON pd.id = pe.program_day_id
|
||||
LEFT JOIN exercises e ON pe.exercise_id = e.id
|
||||
WHERE pd.program_id = $1
|
||||
GROUP BY pd.id
|
||||
ORDER BY pd.day_number
|
||||
`, [req.params.programId]);
|
||||
|
||||
res.json({
|
||||
week: parseInt(currentWeek),
|
||||
days: days.rows
|
||||
});
|
||||
} catch (err) {
|
||||
logger.error('Error fetching today workout', { error: err.message, programId: req.params.programId });
|
||||
res.status(500).json({ error: 'Database error' });
|
||||
}
|
||||
});
|
||||
|
||||
if (require.main === module) {
|
||||
app.listen(PORT, '0.0.0.0', () => {
|
||||
logger.info(`Gravl API started`, { port: PORT, environment: process.env.NODE_ENV || 'development' });
|
||||
});
|
||||
}
|
||||
|
||||
// ============================================
|
||||
// Custom Workouts API (Phase 4: Workout Modification)
|
||||
// ============================================
|
||||
|
||||
// Get all exercises (for picker UI)
|
||||
app.get('/api/exercises', async (req, res) => {
|
||||
try {
|
||||
const result = await pool.query(
|
||||
'SELECT id, name, muscle_group, description FROM exercises ORDER BY muscle_group, name'
|
||||
);
|
||||
res.json(result.rows);
|
||||
} catch (err) {
|
||||
logger.error('Error fetching exercises', { error: err.message });
|
||||
res.status(500).json({ error: 'Database error' });
|
||||
}
|
||||
});
|
||||
|
||||
// Create custom workout from program day (fork)
|
||||
app.post('/api/custom-workouts', authMiddleware, async (req, res) => {
|
||||
const client = await pool.connect();
|
||||
try {
|
||||
const { source_program_day_id, name, description } = req.body;
|
||||
const user_id = req.user.id;
|
||||
|
||||
await client.query('BEGIN');
|
||||
|
||||
// Get the program day info and its exercises
|
||||
const dayResult = await client.query(
|
||||
'SELECT name, program_id FROM program_days WHERE id = $1',
|
||||
[source_program_day_id]
|
||||
);
|
||||
|
||||
if (dayResult.rows.length === 0) {
|
||||
await client.query('ROLLBACK');
|
||||
return res.status(404).json({ error: 'Program day not found' });
|
||||
}
|
||||
|
||||
const dayName = dayResult.rows[0].name;
|
||||
const workoutName = name || `${dayName} (anpassad)`;
|
||||
|
||||
// Create custom workout
|
||||
const workoutResult = await client.query(
|
||||
`INSERT INTO custom_workouts (user_id, name, description, source_program_day_id)
|
||||
VALUES ($1, $2, $3, $4) RETURNING *`,
|
||||
[user_id, workoutName, description || null, source_program_day_id]
|
||||
);
|
||||
const customWorkout = workoutResult.rows[0];
|
||||
|
||||
// Copy exercises from program day
|
||||
const exercisesResult = await client.query(
|
||||
`INSERT INTO custom_workout_exercises
|
||||
(custom_workout_id, exercise_id, sets, reps_min, reps_max, order_index, replaced_exercise_id)
|
||||
SELECT $1, exercise_id, sets, reps_min, reps_max, order_num, NULL
|
||||
FROM program_exercises WHERE program_day_id = $2
|
||||
RETURNING *`,
|
||||
[customWorkout.id, source_program_day_id]
|
||||
);
|
||||
|
||||
await client.query('COMMIT');
|
||||
logger.info('Custom workout created', { userId: user_id, workoutId: customWorkout.id });
|
||||
|
||||
res.json({
|
||||
...customWorkout,
|
||||
exercises: exercisesResult.rows
|
||||
});
|
||||
} catch (err) {
|
||||
await client.query('ROLLBACK');
|
||||
logger.error('Error creating custom workout', { error: err.message, userId: req.user.id });
|
||||
res.status(500).json({ error: 'Database error' });
|
||||
} finally {
|
||||
client.release();
|
||||
}
|
||||
});
|
||||
|
||||
// List user's custom workouts
|
||||
app.get('/api/custom-workouts', authMiddleware, async (req, res) => {
|
||||
try {
|
||||
const user_id = req.user.id;
|
||||
const result = await pool.query(
|
||||
`SELECT cw.*, pd.name as original_day_name, p.name as program_name
|
||||
FROM custom_workouts cw
|
||||
LEFT JOIN program_days pd ON cw.source_program_day_id = pd.id
|
||||
LEFT JOIN programs p ON pd.program_id = p.id
|
||||
WHERE cw.user_id = $1
|
||||
ORDER BY cw.created_at DESC`,
|
||||
[user_id]
|
||||
);
|
||||
res.json(result.rows);
|
||||
} catch (err) {
|
||||
logger.error('Error fetching custom workouts', { error: err.message, userId: req.user.id });
|
||||
res.status(500).json({ error: 'Database error' });
|
||||
}
|
||||
});
|
||||
|
||||
// Get single custom workout with exercises
|
||||
app.get('/api/custom-workouts/:id', authMiddleware, async (req, res) => {
|
||||
try {
|
||||
const user_id = req.user.id;
|
||||
const workout_id = req.params.id;
|
||||
|
||||
// Get workout header
|
||||
const workoutResult = await pool.query(
|
||||
`SELECT cw.*, pd.name as original_day_name, p.name as program_name
|
||||
FROM custom_workouts cw
|
||||
LEFT JOIN program_days pd ON cw.source_program_day_id = pd.id
|
||||
LEFT JOIN programs p ON pd.program_id = p.id
|
||||
WHERE cw.id = $1 AND cw.user_id = $2`,
|
||||
[workout_id, user_id]
|
||||
);
|
||||
|
||||
if (workoutResult.rows.length === 0) {
|
||||
return res.status(404).json({ error: 'Custom workout not found' });
|
||||
}
|
||||
|
||||
// Get exercises with full details
|
||||
const exercisesResult = await pool.query(
|
||||
`SELECT cwe.*, e.name, e.muscle_group, e.description,
|
||||
re.name as replaced_exercise_name,
|
||||
re.muscle_group as replaced_exercise_muscle_group
|
||||
FROM custom_workout_exercises cwe
|
||||
JOIN exercises e ON cwe.exercise_id = e.id
|
||||
LEFT JOIN exercises re ON cwe.replaced_exercise_id = re.id
|
||||
WHERE cwe.custom_workout_id = $1
|
||||
ORDER BY cwe.order_index`,
|
||||
[workout_id]
|
||||
);
|
||||
|
||||
res.json({
|
||||
...workoutResult.rows[0],
|
||||
exercises: exercisesResult.rows
|
||||
});
|
||||
} catch (err) {
|
||||
logger.error('Error fetching custom workout', { error: err.message, userId: req.user.id, workoutId: req.params.id });
|
||||
res.status(500).json({ error: 'Database error' });
|
||||
}
|
||||
});
|
||||
|
||||
// Update custom workout exercises (replace all)
|
||||
app.put('/api/custom-workouts/:id', authMiddleware, async (req, res) => {
|
||||
const client = await pool.connect();
|
||||
try {
|
||||
const user_id = req.user.id;
|
||||
const workout_id = req.params.id;
|
||||
const { name, description, exercises } = req.body;
|
||||
|
||||
await client.query('BEGIN');
|
||||
|
||||
// Verify ownership
|
||||
const workoutCheck = await client.query(
|
||||
'SELECT id FROM custom_workouts WHERE id = $1 AND user_id = $2',
|
||||
[workout_id, user_id]
|
||||
);
|
||||
|
||||
if (workoutCheck.rows.length === 0) {
|
||||
await client.query('ROLLBACK');
|
||||
return res.status(404).json({ error: 'Custom workout not found' });
|
||||
}
|
||||
|
||||
// Update workout details
|
||||
if (name || description !== undefined) {
|
||||
await client.query(
|
||||
`UPDATE custom_workouts
|
||||
SET name = COALESCE($1, name),
|
||||
description = COALESCE($2, description),
|
||||
updated_at = CURRENT_TIMESTAMP
|
||||
WHERE id = $3`,
|
||||
[name, description, workout_id]
|
||||
);
|
||||
}
|
||||
|
||||
// Replace exercises if provided
|
||||
if (exercises && Array.isArray(exercises)) {
|
||||
// Delete existing exercises
|
||||
await client.query(
|
||||
'DELETE FROM custom_workout_exercises WHERE custom_workout_id = $1',
|
||||
[workout_id]
|
||||
);
|
||||
|
||||
// Insert new exercises
|
||||
for (let i = 0; i < exercises.length; i++) {
|
||||
const ex = exercises[i];
|
||||
await client.query(
|
||||
`INSERT INTO custom_workout_exercises
|
||||
(custom_workout_id, exercise_id, sets, reps_min, reps_max, order_index, replaced_exercise_id)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7)`,
|
||||
[workout_id, ex.exercise_id, ex.sets || 3, ex.reps_min || 8, ex.reps_max || 12,
|
||||
i, ex.replaced_exercise_id || null]
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
await client.query('COMMIT');
|
||||
logger.info('Custom workout updated', { userId: user_id, workoutId: workout_id });
|
||||
|
||||
// Fetch and return updated workout
|
||||
const updatedResult = await pool.query(
|
||||
`SELECT cw.*, pd.name as original_day_name, p.name as program_name
|
||||
FROM custom_workouts cw
|
||||
LEFT JOIN program_days pd ON cw.source_program_day_id = pd.id
|
||||
LEFT JOIN programs p ON pd.program_id = p.id
|
||||
WHERE cw.id = $1`,
|
||||
[workout_id]
|
||||
);
|
||||
|
||||
const exercisesResult = await pool.query(
|
||||
`SELECT cwe.*, e.name, e.muscle_group, e.description
|
||||
FROM custom_workout_exercises cwe
|
||||
JOIN exercises e ON cwe.exercise_id = e.id
|
||||
WHERE cwe.custom_workout_id = $1
|
||||
ORDER BY cwe.order_index`,
|
||||
[workout_id]
|
||||
);
|
||||
|
||||
res.json({
|
||||
...updatedResult.rows[0],
|
||||
exercises: exercisesResult.rows
|
||||
});
|
||||
} catch (err) {
|
||||
await client.query('ROLLBACK');
|
||||
logger.error('Error updating custom workout', { error: err.message, userId: req.user.id, workoutId: req.params.id });
|
||||
res.status(500).json({ error: 'Database error' });
|
||||
} finally {
|
||||
client.release();
|
||||
}
|
||||
});
|
||||
|
||||
// Delete custom workout
|
||||
app.delete('/api/custom-workouts/:id', authMiddleware, async (req, res) => {
|
||||
try {
|
||||
const user_id = req.user.id;
|
||||
const workout_id = req.params.id;
|
||||
|
||||
const result = await pool.query(
|
||||
'DELETE FROM custom_workouts WHERE id = $1 AND user_id = $2 RETURNING id',
|
||||
[workout_id, user_id]
|
||||
);
|
||||
|
||||
if (result.rows.length === 0) {
|
||||
return res.status(404).json({ error: 'Custom workout not found' });
|
||||
}
|
||||
|
||||
logger.info('Custom workout deleted', { userId: user_id, workoutId: workout_id });
|
||||
res.json({ deleted: result.rows[0].id });
|
||||
} catch (err) {
|
||||
logger.error('Error deleting custom workout', { error: err.message, userId: req.user.id, workoutId: req.params.id });
|
||||
res.status(500).json({ error: 'Database error' });
|
||||
}
|
||||
});
|
||||
|
||||
// ============================================
|
||||
// Updated Log Endpoints (support source_type)
|
||||
// ============================================
|
||||
|
||||
// Get workout logs (optionally filter by source_type and custom_workout_id)
|
||||
app.get('/api/logs', async (req, res) => {
|
||||
try {
|
||||
const { user_id, date, source_type, custom_workout_id } = req.query;
|
||||
|
||||
let query = 'SELECT * FROM workout_logs WHERE user_id = $1';
|
||||
let params = [user_id];
|
||||
let paramIdx = 2;
|
||||
|
||||
if (date) {
|
||||
query += ` AND date = $${paramIdx++}`;
|
||||
params.push(date);
|
||||
}
|
||||
|
||||
if (source_type) {
|
||||
query += ` AND source_type = $${paramIdx++}`;
|
||||
params.push(source_type);
|
||||
}
|
||||
|
||||
if (custom_workout_id) {
|
||||
query += ` AND custom_workout_id = $${paramIdx++}`;
|
||||
params.push(custom_workout_id);
|
||||
}
|
||||
|
||||
query += ' ORDER BY date DESC, set_number ASC';
|
||||
|
||||
const result = await pool.query(query, params);
|
||||
res.json(result.rows);
|
||||
} catch (err) {
|
||||
logger.error('Error fetching logs', { error: err.message });
|
||||
res.status(500).json({ error: 'Database error' });
|
||||
}
|
||||
});
|
||||
|
||||
// Log a set (updated for source_type and custom_workout support)
|
||||
app.post('/api/logs', async (req, res) => {
|
||||
try {
|
||||
const { user_id, program_exercise_id, custom_workout_exercise_id, date, set_number, weight, reps, completed, source_type, custom_workout_id } = req.body;
|
||||
|
||||
const source = source_type || 'program';
|
||||
|
||||
// Determine which exercise identifier to use for lookup
|
||||
const exerciseRef = custom_workout_exercise_id || program_exercise_id;
|
||||
|
||||
// Check if log exists for this set
|
||||
let existingQuery, existingParams;
|
||||
if (source === 'custom' && custom_workout_id) {
|
||||
existingQuery = `SELECT id FROM workout_logs
|
||||
WHERE user_id = $1 AND custom_workout_id = $2 AND date = $3 AND set_number = $4`;
|
||||
existingParams = [user_id, custom_workout_id, date, set_number];
|
||||
} else {
|
||||
existingQuery = `SELECT id FROM workout_logs
|
||||
WHERE user_id = $1 AND program_exercise_id = $2 AND date = $3 AND set_number = $4`;
|
||||
existingParams = [user_id, program_exercise_id, date, set_number];
|
||||
}
|
||||
|
||||
const existing = await pool.query(existingQuery, existingParams);
|
||||
|
||||
let result;
|
||||
if (existing.rows.length > 0) {
|
||||
// Update existing
|
||||
result = await pool.query(
|
||||
`UPDATE workout_logs
|
||||
SET weight = $1, reps = $2, completed = $3, source_type = $4
|
||||
WHERE id = $5 RETURNING *`,
|
||||
[weight, reps, completed, source, existing.rows[0].id]
|
||||
);
|
||||
} else {
|
||||
// Insert new
|
||||
result = await pool.query(
|
||||
`INSERT INTO workout_logs (user_id, program_exercise_id, custom_workout_exercise_id,
|
||||
date, set_number, weight, reps, completed, source_type, custom_workout_id)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10) RETURNING *`,
|
||||
[user_id, program_exercise_id, custom_workout_exercise_id, date, set_number,
|
||||
weight, reps, completed, source, custom_workout_id]
|
||||
);
|
||||
}
|
||||
|
||||
logger.debug('Workout set logged', { userId: user_id, exerciseId: exerciseRef, weight, reps });
|
||||
res.json(result.rows[0]);
|
||||
} catch (err) {
|
||||
logger.error('Error logging set', { error: err.message });
|
||||
res.status(500).json({ error: 'Database error' });
|
||||
}
|
||||
});
|
||||
|
||||
// Delete a specific set log (updated for source_type support)
|
||||
app.delete('/api/logs', async (req, res) => {
|
||||
try {
|
||||
const { user_id, program_exercise_id, custom_workout_id, date, set_number } = req.body;
|
||||
|
||||
let query, params;
|
||||
if (custom_workout_id) {
|
||||
query = `DELETE FROM workout_logs
|
||||
WHERE user_id = $1 AND custom_workout_id = $2 AND date = $3 AND set_number = $4
|
||||
RETURNING id`;
|
||||
params = [user_id, custom_workout_id, date, set_number];
|
||||
} else {
|
||||
query = `DELETE FROM workout_logs
|
||||
WHERE user_id = $1 AND program_exercise_id = $2 AND date = $3 AND set_number = $4
|
||||
RETURNING id`;
|
||||
params = [user_id, program_exercise_id, date, set_number];
|
||||
}
|
||||
|
||||
const result = await pool.query(query, params);
|
||||
|
||||
if (result.rows.length === 0) {
|
||||
return res.status(404).json({ error: 'Log not found' });
|
||||
}
|
||||
|
||||
logger.info('Workout log deleted', { userId: user_id, date, setNumber: set_number });
|
||||
res.json({ deleted: result.rows[0].id });
|
||||
} catch (err) {
|
||||
logger.error('Error deleting log', { error: err.message });
|
||||
res.status(500).json({ error: 'Database error' });
|
||||
}
|
||||
});
|
||||
|
||||
module.exports = app;
|
||||
@@ -0,0 +1,60 @@
|
||||
const express = require('express');
|
||||
const logger = require('../utils/logger');
|
||||
const { getMuscleGroupRecovery, getMostRecoveredGroups, updateMuscleGroupRecovery } = require('../services/recoveryService');
|
||||
|
||||
function createRecoveryRouter({ pool }) {
|
||||
const router = express.Router();
|
||||
|
||||
const authMiddleware = (req, res, next) => {
|
||||
const token = req.headers.authorization?.split(' ')[1];
|
||||
if (!token) return res.status(401).json({ error: 'No token provided' });
|
||||
try {
|
||||
const jwt = require('jsonwebtoken');
|
||||
const JWT_SECRET = process.env.JWT_SECRET || 'gravl-secret-key-change-in-production';
|
||||
req.user = jwt.verify(token, JWT_SECRET);
|
||||
next();
|
||||
} catch (err) {
|
||||
res.status(401).json({ error: 'Invalid token' });
|
||||
}
|
||||
};
|
||||
|
||||
// GET /api/recovery/muscle-groups - Get recovery status for all muscle groups
|
||||
router.get('/muscle-groups', authMiddleware, async (req, res) => {
|
||||
try {
|
||||
const userId = req.user.id;
|
||||
const recovery = await getMuscleGroupRecovery(pool, userId);
|
||||
|
||||
res.json({
|
||||
userId,
|
||||
timestamp: new Date().toISOString(),
|
||||
muscleGroups: recovery
|
||||
});
|
||||
} catch (err) {
|
||||
logger.error('Error fetching muscle group recovery', { error: err.message, userId: req.user.id });
|
||||
res.status(500).json({ error: 'Database error' });
|
||||
}
|
||||
});
|
||||
|
||||
// GET /api/recovery/most-recovered - Get top N most recovered muscle groups
|
||||
router.get('/most-recovered', authMiddleware, async (req, res) => {
|
||||
try {
|
||||
const userId = req.user.id;
|
||||
const limit = Math.min(parseInt(req.query.limit) || 5, 20);
|
||||
const mostRecovered = await getMostRecoveredGroups(pool, userId, limit);
|
||||
|
||||
res.json({
|
||||
userId,
|
||||
timestamp: new Date().toISOString(),
|
||||
limit,
|
||||
recovered: mostRecovered
|
||||
});
|
||||
} catch (err) {
|
||||
logger.error('Error fetching most recovered groups', { error: err.message, userId: req.user.id });
|
||||
res.status(500).json({ error: 'Database error' });
|
||||
}
|
||||
});
|
||||
|
||||
return router;
|
||||
}
|
||||
|
||||
module.exports = { createRecoveryRouter };
|
||||
@@ -0,0 +1,111 @@
|
||||
const express = require('express');
|
||||
const logger = require('../utils/logger');
|
||||
const { getMuscleGroupRecovery } = require('../services/recoveryService');
|
||||
|
||||
function createSmartRecommendationsRouter({ pool }) {
|
||||
const router = express.Router();
|
||||
|
||||
const authMiddleware = (req, res, next) => {
|
||||
const token = req.headers.authorization?.split(' ')[1];
|
||||
if (!token) return res.status(401).json({ error: 'No token provided' });
|
||||
try {
|
||||
const jwt = require('jsonwebtoken');
|
||||
const JWT_SECRET = process.env.JWT_SECRET || 'gravl-secret-key-change-in-production';
|
||||
req.user = jwt.verify(token, JWT_SECRET);
|
||||
next();
|
||||
} catch (err) {
|
||||
res.status(401).json({ error: 'Invalid token' });
|
||||
}
|
||||
};
|
||||
|
||||
// GET /api/recommendations/smart-workout - Get smart workout recommendations based on recovery
|
||||
router.get('/smart-workout', authMiddleware, async (req, res) => {
|
||||
try {
|
||||
const userId = req.user.id;
|
||||
|
||||
// Get recovery status for all muscle groups
|
||||
const recovery = await getMuscleGroupRecovery(pool, userId);
|
||||
|
||||
// Filter muscle groups with recovery score >= 30%
|
||||
const recoveredGroups = recovery
|
||||
.filter(group => group.recovery_score >= 0.3)
|
||||
.sort((a, b) => b.recovery_score - a.recovery_score);
|
||||
|
||||
if (recoveredGroups.length === 0) {
|
||||
return res.json({
|
||||
userId,
|
||||
timestamp: new Date().toISOString(),
|
||||
message: 'No muscle groups are sufficiently recovered yet',
|
||||
recommendations: []
|
||||
});
|
||||
}
|
||||
|
||||
// Get exercises targeting the most recovered muscle groups
|
||||
const topMuscleGroups = recoveredGroups.slice(0, 3).map(g => g.muscle_group);
|
||||
|
||||
// Query for exercises targeting these muscle groups
|
||||
const exercisesResult = await pool.query(
|
||||
`SELECT
|
||||
e.id,
|
||||
e.name,
|
||||
e.muscle_group,
|
||||
e.description,
|
||||
COUNT(DISTINCT pe.id) as workout_count
|
||||
FROM exercises e
|
||||
LEFT JOIN program_exercises pe ON e.id = pe.exercise_id
|
||||
WHERE e.muscle_group = ANY($1)
|
||||
GROUP BY e.id, e.name, e.muscle_group, e.description
|
||||
ORDER BY e.muscle_group, workout_count DESC
|
||||
LIMIT 10`,
|
||||
[topMuscleGroups]
|
||||
);
|
||||
|
||||
// Build recommendations grouped by muscle group
|
||||
const recommendationsByMuscle = {};
|
||||
for (const group of topMuscleGroups) {
|
||||
recommendationsByMuscle[group] = recoveredGroups.find(r => r.muscle_group === group);
|
||||
}
|
||||
|
||||
// Create top 3 recommendations with reasons
|
||||
const recommendations = [];
|
||||
const muscleGroupsProcessed = new Set();
|
||||
|
||||
for (const exercise of exercisesResult.rows) {
|
||||
if (recommendations.length >= 3) break;
|
||||
if (muscleGroupsProcessed.has(exercise.muscle_group)) continue;
|
||||
|
||||
const muscleInfo = recommendationsByMuscle[exercise.muscle_group];
|
||||
if (!muscleInfo) continue;
|
||||
|
||||
muscleGroupsProcessed.add(exercise.muscle_group);
|
||||
recommendations.push({
|
||||
id: exercise.id,
|
||||
name: exercise.name,
|
||||
muscleGroup: exercise.muscle_group,
|
||||
description: exercise.description,
|
||||
recovery: {
|
||||
score: muscleInfo.recovery_score,
|
||||
percentage: muscleInfo.recovery_percentage,
|
||||
lastWorkout: muscleInfo.last_workout_date,
|
||||
reason: `${exercise.muscle_group} is recovered (${muscleInfo.recovery_percentage}%)`
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
logger.info('Smart recommendations generated', { userId, count: recommendations.length });
|
||||
|
||||
res.json({
|
||||
userId,
|
||||
timestamp: new Date().toISOString(),
|
||||
recommendations
|
||||
});
|
||||
} catch (err) {
|
||||
logger.error('Error generating smart recommendations', { error: err.message, userId: req.user.id });
|
||||
res.status(500).json({ error: 'Database error' });
|
||||
}
|
||||
});
|
||||
|
||||
return router;
|
||||
}
|
||||
|
||||
module.exports = { createSmartRecommendationsRouter };
|
||||
+85
-310
@@ -1,10 +1,10 @@
|
||||
const express = require('express');
|
||||
const logger = require('../utils/logger');
|
||||
const { updateMuscleGroupRecovery } = require('../services/recoveryService');
|
||||
|
||||
function createWorkoutRouter({ pool }) {
|
||||
const router = express.Router();
|
||||
|
||||
// Middleware to verify authentication
|
||||
const authMiddleware = (req, res, next) => {
|
||||
const token = req.headers.authorization?.split(' ')[1];
|
||||
if (!token) return res.status(401).json({ error: 'No token provided' });
|
||||
@@ -18,348 +18,123 @@ function createWorkoutRouter({ pool }) {
|
||||
}
|
||||
};
|
||||
|
||||
// POST /api/workouts/:programExerciseId/swap - Create a workout swap record
|
||||
router.post('/:programExerciseId/swap', authMiddleware, async (req, res) => {
|
||||
// POST /api/workouts/:id/swap - Swap a logged workout with another
|
||||
router.post('/:id/swap', authMiddleware, async (req, res) => {
|
||||
try {
|
||||
const { programExerciseId } = req.params;
|
||||
const { fromExerciseId, toExerciseId, workoutDate } = req.body;
|
||||
const logId = parseInt(req.params.id);
|
||||
const { newWorkoutId } = req.body;
|
||||
const userId = req.user.id;
|
||||
|
||||
// Validation
|
||||
if (!programExerciseId || !fromExerciseId || !toExerciseId || !workoutDate) {
|
||||
return res.status(400).json({ error: 'Missing required fields: programExerciseId, fromExerciseId, toExerciseId, workoutDate' });
|
||||
if (!logId || !newWorkoutId) {
|
||||
return res.status(400).json({ error: 'Missing logId or newWorkoutId' });
|
||||
}
|
||||
|
||||
// Validate numeric IDs
|
||||
const programExerciseIdNum = parseInt(programExerciseId);
|
||||
const fromExerciseIdNum = parseInt(fromExerciseId);
|
||||
const toExerciseIdNum = parseInt(toExerciseId);
|
||||
const userIdNum = parseInt(userId);
|
||||
|
||||
if (isNaN(programExerciseIdNum) || isNaN(fromExerciseIdNum) || isNaN(toExerciseIdNum)) {
|
||||
return res.status(400).json({ error: 'Invalid exercise IDs format' });
|
||||
}
|
||||
|
||||
// Validate date format (YYYY-MM-DD)
|
||||
if (!/^\d{4}-\d{2}-\d{2}$/.test(workoutDate)) {
|
||||
return res.status(400).json({ error: 'Invalid date format. Use YYYY-MM-DD' });
|
||||
}
|
||||
|
||||
// Verify exercises exist and get their details
|
||||
const fromExerciseResult = await pool.query(
|
||||
'SELECT id, name, muscle_group FROM exercises WHERE id = $1',
|
||||
[fromExerciseIdNum]
|
||||
// Verify the original log exists and belongs to this user
|
||||
const originalLogResult = await pool.query(
|
||||
'SELECT * FROM workout_logs WHERE id = $1 AND user_id = $2',
|
||||
[logId, userId]
|
||||
);
|
||||
|
||||
if (fromExerciseResult.rows.length === 0) {
|
||||
return res.status(404).json({ error: 'From exercise not found' });
|
||||
if (originalLogResult.rows.length === 0) {
|
||||
return res.status(404).json({ error: 'Workout log not found' });
|
||||
}
|
||||
|
||||
const toExerciseResult = await pool.query(
|
||||
'SELECT id, name, muscle_group FROM exercises WHERE id = $1',
|
||||
[toExerciseIdNum]
|
||||
const originalLog = originalLogResult.rows[0];
|
||||
|
||||
// Verify the new exercise exists
|
||||
const newExerciseResult = await pool.query(
|
||||
'SELECT * FROM exercises WHERE id = $1',
|
||||
[newWorkoutId]
|
||||
);
|
||||
|
||||
if (toExerciseResult.rows.length === 0) {
|
||||
return res.status(404).json({ error: 'To exercise not found' });
|
||||
if (newExerciseResult.rows.length === 0) {
|
||||
return res.status(404).json({ error: 'New exercise not found' });
|
||||
}
|
||||
|
||||
const fromExercise = fromExerciseResult.rows[0];
|
||||
const toExercise = toExerciseResult.rows[0];
|
||||
const newExercise = newExerciseResult.rows[0];
|
||||
const client = await pool.connect();
|
||||
|
||||
// Verify exercises have same muscle group
|
||||
if (fromExercise.muscle_group !== toExercise.muscle_group) {
|
||||
return res.status(400).json({
|
||||
error: 'Exercises must have the same muscle group for swapping',
|
||||
details: {
|
||||
fromMuscleGroup: fromExercise.muscle_group,
|
||||
toMuscleGroup: toExercise.muscle_group
|
||||
try {
|
||||
await client.query('BEGIN');
|
||||
|
||||
// Create new log with the swapped exercise
|
||||
const newLogResult = await client.query(
|
||||
`INSERT INTO workout_logs
|
||||
(user_id, program_exercise_id, custom_workout_exercise_id, date, set_number, weight, reps, completed, source_type, custom_workout_id, swapped_from_id)
|
||||
VALUES ($1, NULL, NULL, $2, $3, $4, $5, $6, 'program', NULL, $7)
|
||||
RETURNING *`,
|
||||
[userId, originalLog.date, originalLog.set_number, originalLog.weight, originalLog.reps, originalLog.completed, logId]
|
||||
);
|
||||
|
||||
const newLog = newLogResult.rows[0];
|
||||
|
||||
// Record the swap in workout_swaps table
|
||||
await client.query(
|
||||
`INSERT INTO workout_swaps (user_id, original_log_id, swapped_log_id, swap_date, created_at, updated_at)
|
||||
VALUES ($1, $2, $3, $4, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP)`,
|
||||
[userId, logId, newLog.id, originalLog.date]
|
||||
);
|
||||
|
||||
// Update muscle group recovery for the new exercise
|
||||
if (originalLog.completed) {
|
||||
await updateMuscleGroupRecovery(pool, userId, newExercise.muscle_group, 0.8);
|
||||
}
|
||||
|
||||
await client.query('COMMIT');
|
||||
|
||||
logger.info('Workout swapped', { userId, originalLogId: logId, newLogId: newLog.id });
|
||||
|
||||
res.json({
|
||||
success: true,
|
||||
message: 'Workout swapped successfully',
|
||||
swap: {
|
||||
originalLogId: logId,
|
||||
newLogId: newLog.id,
|
||||
newExercise: {
|
||||
id: newExercise.id,
|
||||
name: newExercise.name,
|
||||
muscleGroup: newExercise.muscle_group
|
||||
},
|
||||
date: originalLog.date
|
||||
}
|
||||
});
|
||||
} catch (err) {
|
||||
await client.query('ROLLBACK');
|
||||
throw err;
|
||||
} finally {
|
||||
client.release();
|
||||
}
|
||||
|
||||
// Insert into workout_swaps table
|
||||
const swapResult = await pool.query(
|
||||
`INSERT INTO workout_swaps (user_id, program_exercise_id, from_exercise_id, to_exercise_id, swap_date, created_at)
|
||||
VALUES ($1, $2, $3, $4, $5, CURRENT_TIMESTAMP)
|
||||
RETURNING id, created_at`,
|
||||
[userIdNum, programExerciseIdNum, fromExerciseIdNum, toExerciseIdNum, workoutDate]
|
||||
);
|
||||
|
||||
const swapId = swapResult.rows[0].id;
|
||||
const createdAt = swapResult.rows[0].created_at;
|
||||
|
||||
// Update existing workout logs for this date to reference the swap
|
||||
await pool.query(
|
||||
`UPDATE workout_logs
|
||||
SET swap_history_id = $1
|
||||
WHERE user_id = $2 AND program_exercise_id = $3 AND date = $4 AND swap_history_id IS NULL`,
|
||||
[swapId, userIdNum, programExerciseIdNum, workoutDate]
|
||||
);
|
||||
|
||||
logger.info('Workout swap created', {
|
||||
userId: userIdNum,
|
||||
swapId,
|
||||
fromExerciseId: fromExerciseIdNum,
|
||||
toExerciseId: toExerciseIdNum,
|
||||
date: workoutDate
|
||||
});
|
||||
|
||||
res.status(200).json({
|
||||
success: true,
|
||||
swapId,
|
||||
message: 'Swap recorded',
|
||||
swap: {
|
||||
id: swapId,
|
||||
from_exercise: {
|
||||
id: fromExercise.id,
|
||||
name: fromExercise.name,
|
||||
muscle_group: fromExercise.muscle_group
|
||||
},
|
||||
to_exercise: {
|
||||
id: toExercise.id,
|
||||
name: toExercise.name,
|
||||
muscle_group: toExercise.muscle_group
|
||||
},
|
||||
date: workoutDate,
|
||||
created_at: createdAt
|
||||
}
|
||||
});
|
||||
} catch (err) {
|
||||
logger.error('Error creating swap', { error: err.message, stack: err.stack });
|
||||
logger.error('Error swapping workout', { error: err.message, userId: req.user.id });
|
||||
res.status(500).json({ error: 'Database error' });
|
||||
}
|
||||
});
|
||||
|
||||
// DELETE /api/workouts/:swapId/undo - Revert a swap
|
||||
router.delete('/:swapId/undo', authMiddleware, async (req, res) => {
|
||||
// GET /api/workouts/available - Get list of available exercises for swapping
|
||||
router.get('/available', authMiddleware, async (req, res) => {
|
||||
try {
|
||||
const { swapId } = req.params;
|
||||
const userId = req.user.id;
|
||||
const { muscleGroup, limit = 10 } = req.query;
|
||||
|
||||
// Validation
|
||||
if (!swapId) {
|
||||
return res.status(400).json({ error: 'Missing swapId parameter' });
|
||||
let query = 'SELECT * FROM exercises';
|
||||
const params = [];
|
||||
|
||||
if (muscleGroup) {
|
||||
query += ' WHERE muscle_group = $1';
|
||||
params.push(muscleGroup);
|
||||
}
|
||||
|
||||
const swapIdNum = parseInt(swapId);
|
||||
if (isNaN(swapIdNum)) {
|
||||
return res.status(400).json({ error: 'Invalid swap ID format' });
|
||||
}
|
||||
|
||||
const userIdNum = parseInt(userId);
|
||||
|
||||
// Find swap record and verify it belongs to the user
|
||||
const swapResult = await pool.query(
|
||||
'SELECT id, user_id FROM workout_swaps WHERE id = $1',
|
||||
[swapIdNum]
|
||||
);
|
||||
|
||||
if (swapResult.rows.length === 0) {
|
||||
return res.status(404).json({ error: 'Swap not found' });
|
||||
}
|
||||
|
||||
const swap = swapResult.rows[0];
|
||||
|
||||
// Verify ownership
|
||||
if (swap.user_id !== userIdNum) {
|
||||
return res.status(403).json({ error: 'You do not own this swap' });
|
||||
}
|
||||
|
||||
// Clear swap references from workout_logs
|
||||
await pool.query(
|
||||
`UPDATE workout_logs
|
||||
SET swap_history_id = NULL
|
||||
WHERE swap_history_id = $1`,
|
||||
[swapIdNum]
|
||||
);
|
||||
|
||||
// Delete the swap record
|
||||
await pool.query(
|
||||
'DELETE FROM workout_swaps WHERE id = $1',
|
||||
[swapIdNum]
|
||||
);
|
||||
|
||||
logger.info('Workout swap reverted', {
|
||||
userId: userIdNum,
|
||||
swapId: swapIdNum
|
||||
});
|
||||
|
||||
res.status(200).json({
|
||||
success: true,
|
||||
message: 'Swap reverted'
|
||||
});
|
||||
} catch (err) {
|
||||
logger.error('Error reverting swap', { error: err.message, stack: err.stack });
|
||||
res.status(500).json({ error: 'Database error' });
|
||||
}
|
||||
});
|
||||
|
||||
// GET /api/workouts/:programExerciseId/swaps - Get swap history
|
||||
router.get('/:programExerciseId/swaps', authMiddleware, async (req, res) => {
|
||||
try {
|
||||
const { programExerciseId } = req.params;
|
||||
const { limit = 10, offset = 0, fromDate } = req.query;
|
||||
const userId = req.user.id;
|
||||
|
||||
// Validation
|
||||
if (!programExerciseId) {
|
||||
return res.status(400).json({ error: 'Missing programExerciseId parameter' });
|
||||
}
|
||||
|
||||
const programExerciseIdNum = parseInt(programExerciseId);
|
||||
if (isNaN(programExerciseIdNum)) {
|
||||
return res.status(400).json({ error: 'Invalid programExerciseId format' });
|
||||
}
|
||||
|
||||
const limitNum = Math.min(parseInt(limit) || 10, 100);
|
||||
const offsetNum = parseInt(offset) || 0;
|
||||
|
||||
// Verify exercise exists
|
||||
const exerciseResult = await pool.query(
|
||||
'SELECT id FROM program_exercises WHERE id = $1 AND user_id = $2',
|
||||
[programExerciseIdNum, userId]
|
||||
);
|
||||
|
||||
if (exerciseResult.rows.length === 0) {
|
||||
return res.status(404).json({ error: 'Exercise not found or access denied' });
|
||||
}
|
||||
|
||||
// Build query
|
||||
let query = `
|
||||
SELECT
|
||||
ws.id,
|
||||
ws.swap_date as date,
|
||||
ws.created_at,
|
||||
fe.id as from_exercise_id,
|
||||
fe.name as from_exercise_name,
|
||||
fe.muscle_group as from_muscle_group,
|
||||
te.id as to_exercise_id,
|
||||
te.name as to_exercise_name,
|
||||
te.muscle_group as to_muscle_group
|
||||
FROM workout_swaps ws
|
||||
JOIN exercises fe ON ws.from_exercise_id = fe.id
|
||||
JOIN exercises te ON ws.to_exercise_id = te.id
|
||||
WHERE ws.program_exercise_id = $1 AND ws.user_id = $2
|
||||
`;
|
||||
|
||||
const params = [programExerciseIdNum, userId];
|
||||
let paramIdx = 3;
|
||||
|
||||
if (fromDate && /^\d{4}-\d{2}-\d{2}$/.test(fromDate)) {
|
||||
query += ` AND ws.swap_date >= $${paramIdx++}`;
|
||||
params.push(fromDate);
|
||||
}
|
||||
|
||||
query += ' ORDER BY ws.created_at DESC LIMIT $' + paramIdx + ' OFFSET $' + (paramIdx + 1);
|
||||
params.push(limitNum, offsetNum);
|
||||
query += ` ORDER BY muscle_group, name LIMIT ${Math.min(parseInt(limit), 100)}`;
|
||||
|
||||
const result = await pool.query(query, params);
|
||||
|
||||
const swaps = result.rows.map(row => ({
|
||||
id: row.id,
|
||||
from_exercise: {
|
||||
id: row.from_exercise_id,
|
||||
name: row.from_exercise_name,
|
||||
muscle_group: row.from_muscle_group
|
||||
},
|
||||
to_exercise: {
|
||||
id: row.to_exercise_id,
|
||||
name: row.to_exercise_name,
|
||||
muscle_group: row.to_muscle_group
|
||||
},
|
||||
date: row.date,
|
||||
created_at: row.created_at
|
||||
}));
|
||||
|
||||
logger.debug('Swap history retrieved', {
|
||||
res.json({
|
||||
userId,
|
||||
programExerciseId: programExerciseIdNum,
|
||||
count: swaps.length
|
||||
});
|
||||
|
||||
res.status(200).json(swaps);
|
||||
} catch (err) {
|
||||
logger.error('Error fetching swaps', { error: err.message, stack: err.stack });
|
||||
res.status(500).json({ error: 'Database error' });
|
||||
}
|
||||
});
|
||||
|
||||
// GET /api/workouts/:date/available - Get available exercises for a date
|
||||
router.get('/:date/available', authMiddleware, async (req, res) => {
|
||||
try {
|
||||
const { date } = req.params;
|
||||
const { programDayId } = req.query;
|
||||
const userId = req.user.id;
|
||||
|
||||
// Validation
|
||||
if (!date || !/^\d{4}-\d{2}-\d{2}$/.test(date)) {
|
||||
return res.status(400).json({ error: 'Invalid date format. Use YYYY-MM-DD' });
|
||||
}
|
||||
|
||||
const userIdNum = parseInt(userId);
|
||||
|
||||
let query = `
|
||||
SELECT
|
||||
pe.id as program_exercise_id,
|
||||
pe.exercise_id,
|
||||
e.name,
|
||||
e.muscle_group,
|
||||
pe.sets,
|
||||
pe.reps_min,
|
||||
pe.reps_max,
|
||||
pd.program_day_id,
|
||||
(
|
||||
SELECT COUNT(*)
|
||||
FROM exercises e2
|
||||
WHERE e2.muscle_group = e.muscle_group
|
||||
AND e2.id != e.id
|
||||
) as alternatives
|
||||
FROM program_exercises pe
|
||||
JOIN exercises e ON pe.exercise_id = e.id
|
||||
JOIN program_days pd ON pe.program_day_id = pd.id
|
||||
JOIN programs p ON pd.program_id = p.id
|
||||
WHERE p.user_id = $1
|
||||
`;
|
||||
|
||||
const params = [userIdNum];
|
||||
let paramIdx = 2;
|
||||
|
||||
if (programDayId) {
|
||||
const programDayIdNum = parseInt(programDayId);
|
||||
if (!isNaN(programDayIdNum)) {
|
||||
query += ` AND pd.program_day_id = $${paramIdx++}`;
|
||||
params.push(programDayIdNum);
|
||||
}
|
||||
}
|
||||
|
||||
query += ' ORDER BY pd.day_of_week, pe.exercise_order';
|
||||
|
||||
const result = await pool.query(query, params);
|
||||
|
||||
const exercises = result.rows.map(row => ({
|
||||
id: row.exercise_id,
|
||||
programExerciseId: row.program_exercise_id,
|
||||
name: row.name,
|
||||
muscleGroup: row.muscle_group,
|
||||
sets: row.sets,
|
||||
reps_min: row.reps_min,
|
||||
reps_max: row.reps_max,
|
||||
alternatives: row.alternatives
|
||||
}));
|
||||
|
||||
logger.debug('Available exercises retrieved', {
|
||||
userId: userIdNum,
|
||||
date,
|
||||
count: exercises.length
|
||||
});
|
||||
|
||||
res.status(200).json({
|
||||
date,
|
||||
exercises
|
||||
count: result.rows.length,
|
||||
exercises: result.rows
|
||||
});
|
||||
} catch (err) {
|
||||
logger.error('Error fetching available exercises', { error: err.message, stack: err.stack });
|
||||
logger.error('Error fetching available exercises', { error: err.message, userId: req.user.id });
|
||||
res.status(500).json({ error: 'Database error' });
|
||||
}
|
||||
});
|
||||
|
||||
@@ -0,0 +1,370 @@
|
||||
const express = require('express');
|
||||
const logger = require('../utils/logger');
|
||||
|
||||
function createWorkoutRouter({ pool }) {
|
||||
const router = express.Router();
|
||||
|
||||
// Middleware to verify authentication
|
||||
const authMiddleware = (req, res, next) => {
|
||||
const token = req.headers.authorization?.split(' ')[1];
|
||||
if (!token) return res.status(401).json({ error: 'No token provided' });
|
||||
try {
|
||||
const jwt = require('jsonwebtoken');
|
||||
const JWT_SECRET = process.env.JWT_SECRET || 'gravl-secret-key-change-in-production';
|
||||
req.user = jwt.verify(token, JWT_SECRET);
|
||||
next();
|
||||
} catch (err) {
|
||||
res.status(401).json({ error: 'Invalid token' });
|
||||
}
|
||||
};
|
||||
|
||||
// POST /api/workouts/:programExerciseId/swap - Create a workout swap record
|
||||
router.post('/:programExerciseId/swap', authMiddleware, async (req, res) => {
|
||||
try {
|
||||
const { programExerciseId } = req.params;
|
||||
const { fromExerciseId, toExerciseId, workoutDate } = req.body;
|
||||
const userId = req.user.id;
|
||||
|
||||
// Validation
|
||||
if (!programExerciseId || !fromExerciseId || !toExerciseId || !workoutDate) {
|
||||
return res.status(400).json({ error: 'Missing required fields: programExerciseId, fromExerciseId, toExerciseId, workoutDate' });
|
||||
}
|
||||
|
||||
// Validate numeric IDs
|
||||
const programExerciseIdNum = parseInt(programExerciseId);
|
||||
const fromExerciseIdNum = parseInt(fromExerciseId);
|
||||
const toExerciseIdNum = parseInt(toExerciseId);
|
||||
const userIdNum = parseInt(userId);
|
||||
|
||||
if (isNaN(programExerciseIdNum) || isNaN(fromExerciseIdNum) || isNaN(toExerciseIdNum)) {
|
||||
return res.status(400).json({ error: 'Invalid exercise IDs format' });
|
||||
}
|
||||
|
||||
// Validate date format (YYYY-MM-DD)
|
||||
if (!/^\d{4}-\d{2}-\d{2}$/.test(workoutDate)) {
|
||||
return res.status(400).json({ error: 'Invalid date format. Use YYYY-MM-DD' });
|
||||
}
|
||||
|
||||
// Verify exercises exist and get their details
|
||||
const fromExerciseResult = await pool.query(
|
||||
'SELECT id, name, muscle_group FROM exercises WHERE id = $1',
|
||||
[fromExerciseIdNum]
|
||||
);
|
||||
|
||||
if (fromExerciseResult.rows.length === 0) {
|
||||
return res.status(404).json({ error: 'From exercise not found' });
|
||||
}
|
||||
|
||||
const toExerciseResult = await pool.query(
|
||||
'SELECT id, name, muscle_group FROM exercises WHERE id = $1',
|
||||
[toExerciseIdNum]
|
||||
);
|
||||
|
||||
if (toExerciseResult.rows.length === 0) {
|
||||
return res.status(404).json({ error: 'To exercise not found' });
|
||||
}
|
||||
|
||||
const fromExercise = fromExerciseResult.rows[0];
|
||||
const toExercise = toExerciseResult.rows[0];
|
||||
|
||||
// Verify exercises have same muscle group
|
||||
if (fromExercise.muscle_group !== toExercise.muscle_group) {
|
||||
return res.status(400).json({
|
||||
error: 'Exercises must have the same muscle group for swapping',
|
||||
details: {
|
||||
fromMuscleGroup: fromExercise.muscle_group,
|
||||
toMuscleGroup: toExercise.muscle_group
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// Insert into workout_swaps table
|
||||
const swapResult = await pool.query(
|
||||
`INSERT INTO workout_swaps (user_id, program_exercise_id, from_exercise_id, to_exercise_id, swap_date, created_at)
|
||||
VALUES ($1, $2, $3, $4, $5, CURRENT_TIMESTAMP)
|
||||
RETURNING id, created_at`,
|
||||
[userIdNum, programExerciseIdNum, fromExerciseIdNum, toExerciseIdNum, workoutDate]
|
||||
);
|
||||
|
||||
const swapId = swapResult.rows[0].id;
|
||||
const createdAt = swapResult.rows[0].created_at;
|
||||
|
||||
// Update existing workout logs for this date to reference the swap
|
||||
await pool.query(
|
||||
`UPDATE workout_logs
|
||||
SET swap_history_id = $1
|
||||
WHERE user_id = $2 AND program_exercise_id = $3 AND date = $4 AND swap_history_id IS NULL`,
|
||||
[swapId, userIdNum, programExerciseIdNum, workoutDate]
|
||||
);
|
||||
|
||||
logger.info('Workout swap created', {
|
||||
userId: userIdNum,
|
||||
swapId,
|
||||
fromExerciseId: fromExerciseIdNum,
|
||||
toExerciseId: toExerciseIdNum,
|
||||
date: workoutDate
|
||||
});
|
||||
|
||||
res.status(200).json({
|
||||
success: true,
|
||||
swapId,
|
||||
message: 'Swap recorded',
|
||||
swap: {
|
||||
id: swapId,
|
||||
from_exercise: {
|
||||
id: fromExercise.id,
|
||||
name: fromExercise.name,
|
||||
muscle_group: fromExercise.muscle_group
|
||||
},
|
||||
to_exercise: {
|
||||
id: toExercise.id,
|
||||
name: toExercise.name,
|
||||
muscle_group: toExercise.muscle_group
|
||||
},
|
||||
date: workoutDate,
|
||||
created_at: createdAt
|
||||
}
|
||||
});
|
||||
} catch (err) {
|
||||
logger.error('Error creating swap', { error: err.message, stack: err.stack });
|
||||
res.status(500).json({ error: 'Database error' });
|
||||
}
|
||||
});
|
||||
|
||||
// DELETE /api/workouts/:swapId/undo - Revert a swap
|
||||
router.delete('/:swapId/undo', authMiddleware, async (req, res) => {
|
||||
try {
|
||||
const { swapId } = req.params;
|
||||
const userId = req.user.id;
|
||||
|
||||
// Validation
|
||||
if (!swapId) {
|
||||
return res.status(400).json({ error: 'Missing swapId parameter' });
|
||||
}
|
||||
|
||||
const swapIdNum = parseInt(swapId);
|
||||
if (isNaN(swapIdNum)) {
|
||||
return res.status(400).json({ error: 'Invalid swap ID format' });
|
||||
}
|
||||
|
||||
const userIdNum = parseInt(userId);
|
||||
|
||||
// Find swap record and verify it belongs to the user
|
||||
const swapResult = await pool.query(
|
||||
'SELECT id, user_id FROM workout_swaps WHERE id = $1',
|
||||
[swapIdNum]
|
||||
);
|
||||
|
||||
if (swapResult.rows.length === 0) {
|
||||
return res.status(404).json({ error: 'Swap not found' });
|
||||
}
|
||||
|
||||
const swap = swapResult.rows[0];
|
||||
|
||||
// Verify ownership
|
||||
if (swap.user_id !== userIdNum) {
|
||||
return res.status(403).json({ error: 'You do not own this swap' });
|
||||
}
|
||||
|
||||
// Clear swap references from workout_logs
|
||||
await pool.query(
|
||||
`UPDATE workout_logs
|
||||
SET swap_history_id = NULL
|
||||
WHERE swap_history_id = $1`,
|
||||
[swapIdNum]
|
||||
);
|
||||
|
||||
// Delete the swap record
|
||||
await pool.query(
|
||||
'DELETE FROM workout_swaps WHERE id = $1',
|
||||
[swapIdNum]
|
||||
);
|
||||
|
||||
logger.info('Workout swap reverted', {
|
||||
userId: userIdNum,
|
||||
swapId: swapIdNum
|
||||
});
|
||||
|
||||
res.status(200).json({
|
||||
success: true,
|
||||
message: 'Swap reverted'
|
||||
});
|
||||
} catch (err) {
|
||||
logger.error('Error reverting swap', { error: err.message, stack: err.stack });
|
||||
res.status(500).json({ error: 'Database error' });
|
||||
}
|
||||
});
|
||||
|
||||
// GET /api/workouts/:programExerciseId/swaps - Get swap history
|
||||
router.get('/:programExerciseId/swaps', authMiddleware, async (req, res) => {
|
||||
try {
|
||||
const { programExerciseId } = req.params;
|
||||
const { limit = 10, offset = 0, fromDate } = req.query;
|
||||
const userId = req.user.id;
|
||||
|
||||
// Validation
|
||||
if (!programExerciseId) {
|
||||
return res.status(400).json({ error: 'Missing programExerciseId parameter' });
|
||||
}
|
||||
|
||||
const programExerciseIdNum = parseInt(programExerciseId);
|
||||
if (isNaN(programExerciseIdNum)) {
|
||||
return res.status(400).json({ error: 'Invalid programExerciseId format' });
|
||||
}
|
||||
|
||||
const limitNum = Math.min(parseInt(limit) || 10, 100);
|
||||
const offsetNum = parseInt(offset) || 0;
|
||||
|
||||
// Verify exercise exists
|
||||
const exerciseResult = await pool.query(
|
||||
'SELECT id FROM program_exercises WHERE id = $1 AND user_id = $2',
|
||||
[programExerciseIdNum, userId]
|
||||
);
|
||||
|
||||
if (exerciseResult.rows.length === 0) {
|
||||
return res.status(404).json({ error: 'Exercise not found or access denied' });
|
||||
}
|
||||
|
||||
// Build query
|
||||
let query = `
|
||||
SELECT
|
||||
ws.id,
|
||||
ws.swap_date as date,
|
||||
ws.created_at,
|
||||
fe.id as from_exercise_id,
|
||||
fe.name as from_exercise_name,
|
||||
fe.muscle_group as from_muscle_group,
|
||||
te.id as to_exercise_id,
|
||||
te.name as to_exercise_name,
|
||||
te.muscle_group as to_muscle_group
|
||||
FROM workout_swaps ws
|
||||
JOIN exercises fe ON ws.from_exercise_id = fe.id
|
||||
JOIN exercises te ON ws.to_exercise_id = te.id
|
||||
WHERE ws.program_exercise_id = $1 AND ws.user_id = $2
|
||||
`;
|
||||
|
||||
const params = [programExerciseIdNum, userId];
|
||||
let paramIdx = 3;
|
||||
|
||||
if (fromDate && /^\d{4}-\d{2}-\d{2}$/.test(fromDate)) {
|
||||
query += ` AND ws.swap_date >= $${paramIdx++}`;
|
||||
params.push(fromDate);
|
||||
}
|
||||
|
||||
query += ' ORDER BY ws.created_at DESC LIMIT $' + paramIdx + ' OFFSET $' + (paramIdx + 1);
|
||||
params.push(limitNum, offsetNum);
|
||||
|
||||
const result = await pool.query(query, params);
|
||||
|
||||
const swaps = result.rows.map(row => ({
|
||||
id: row.id,
|
||||
from_exercise: {
|
||||
id: row.from_exercise_id,
|
||||
name: row.from_exercise_name,
|
||||
muscle_group: row.from_muscle_group
|
||||
},
|
||||
to_exercise: {
|
||||
id: row.to_exercise_id,
|
||||
name: row.to_exercise_name,
|
||||
muscle_group: row.to_muscle_group
|
||||
},
|
||||
date: row.date,
|
||||
created_at: row.created_at
|
||||
}));
|
||||
|
||||
logger.debug('Swap history retrieved', {
|
||||
userId,
|
||||
programExerciseId: programExerciseIdNum,
|
||||
count: swaps.length
|
||||
});
|
||||
|
||||
res.status(200).json(swaps);
|
||||
} catch (err) {
|
||||
logger.error('Error fetching swaps', { error: err.message, stack: err.stack });
|
||||
res.status(500).json({ error: 'Database error' });
|
||||
}
|
||||
});
|
||||
|
||||
// GET /api/workouts/:date/available - Get available exercises for a date
|
||||
router.get('/:date/available', authMiddleware, async (req, res) => {
|
||||
try {
|
||||
const { date } = req.params;
|
||||
const { programDayId } = req.query;
|
||||
const userId = req.user.id;
|
||||
|
||||
// Validation
|
||||
if (!date || !/^\d{4}-\d{2}-\d{2}$/.test(date)) {
|
||||
return res.status(400).json({ error: 'Invalid date format. Use YYYY-MM-DD' });
|
||||
}
|
||||
|
||||
const userIdNum = parseInt(userId);
|
||||
|
||||
let query = `
|
||||
SELECT
|
||||
pe.id as program_exercise_id,
|
||||
pe.exercise_id,
|
||||
e.name,
|
||||
e.muscle_group,
|
||||
pe.sets,
|
||||
pe.reps_min,
|
||||
pe.reps_max,
|
||||
pd.program_day_id,
|
||||
(
|
||||
SELECT COUNT(*)
|
||||
FROM exercises e2
|
||||
WHERE e2.muscle_group = e.muscle_group
|
||||
AND e2.id != e.id
|
||||
) as alternatives
|
||||
FROM program_exercises pe
|
||||
JOIN exercises e ON pe.exercise_id = e.id
|
||||
JOIN program_days pd ON pe.program_day_id = pd.id
|
||||
JOIN programs p ON pd.program_id = p.id
|
||||
WHERE p.user_id = $1
|
||||
`;
|
||||
|
||||
const params = [userIdNum];
|
||||
let paramIdx = 2;
|
||||
|
||||
if (programDayId) {
|
||||
const programDayIdNum = parseInt(programDayId);
|
||||
if (!isNaN(programDayIdNum)) {
|
||||
query += ` AND pd.program_day_id = $${paramIdx++}`;
|
||||
params.push(programDayIdNum);
|
||||
}
|
||||
}
|
||||
|
||||
query += ' ORDER BY pd.day_of_week, pe.exercise_order';
|
||||
|
||||
const result = await pool.query(query, params);
|
||||
|
||||
const exercises = result.rows.map(row => ({
|
||||
id: row.exercise_id,
|
||||
programExerciseId: row.program_exercise_id,
|
||||
name: row.name,
|
||||
muscleGroup: row.muscle_group,
|
||||
sets: row.sets,
|
||||
reps_min: row.reps_min,
|
||||
reps_max: row.reps_max,
|
||||
alternatives: row.alternatives
|
||||
}));
|
||||
|
||||
logger.debug('Available exercises retrieved', {
|
||||
userId: userIdNum,
|
||||
date,
|
||||
count: exercises.length
|
||||
});
|
||||
|
||||
res.status(200).json({
|
||||
date,
|
||||
exercises
|
||||
});
|
||||
} catch (err) {
|
||||
logger.error('Error fetching available exercises', { error: err.message, stack: err.stack });
|
||||
res.status(500).json({ error: 'Database error' });
|
||||
}
|
||||
});
|
||||
|
||||
return router;
|
||||
}
|
||||
|
||||
module.exports = { createWorkoutRouter };
|
||||
@@ -0,0 +1,106 @@
|
||||
const logger = require('../utils/logger');
|
||||
|
||||
/**
|
||||
* Calculate recovery score based on last workout date
|
||||
* 100% if >72h ago
|
||||
* 50% if 48-72h ago
|
||||
* 20% if 24-48h ago
|
||||
* 0% if <24h ago
|
||||
*/
|
||||
function calculateRecoveryScore(lastWorkoutDate) {
|
||||
if (!lastWorkoutDate) {
|
||||
return 1.0; // 100% recovered if never trained
|
||||
}
|
||||
|
||||
const now = new Date();
|
||||
const lastWorkout = new Date(lastWorkoutDate);
|
||||
const hoursSinceWorkout = (now - lastWorkout) / (1000 * 60 * 60);
|
||||
|
||||
if (hoursSinceWorkout > 72) {
|
||||
return 1.0; // 100%
|
||||
} else if (hoursSinceWorkout > 48) {
|
||||
return 0.5; // 50%
|
||||
} else if (hoursSinceWorkout > 24) {
|
||||
return 0.2; // 20%
|
||||
} else {
|
||||
return 0.0; // 0%
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Update or create muscle group recovery record
|
||||
*/
|
||||
async function updateMuscleGroupRecovery(pool, userId, muscleGroup, intensity = 0.5) {
|
||||
try {
|
||||
const result = await pool.query(
|
||||
`INSERT INTO muscle_group_recovery (user_id, muscle_group, last_workout_date, intensity, exercises_count, created_at, updated_at)
|
||||
VALUES ($1, $2, CURRENT_TIMESTAMP, $3, 1, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP)
|
||||
ON CONFLICT (user_id, muscle_group)
|
||||
DO UPDATE SET
|
||||
last_workout_date = CURRENT_TIMESTAMP,
|
||||
intensity = $3,
|
||||
exercises_count = muscle_group_recovery.exercises_count + 1,
|
||||
updated_at = CURRENT_TIMESTAMP
|
||||
RETURNING *`,
|
||||
[userId, muscleGroup, intensity]
|
||||
);
|
||||
return result.rows[0];
|
||||
} catch (err) {
|
||||
logger.error('Error updating muscle group recovery', { error: err.message, userId, muscleGroup });
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get recovery scores for all muscle groups for a user
|
||||
*/
|
||||
async function getMuscleGroupRecovery(pool, userId) {
|
||||
try {
|
||||
const result = await pool.query(
|
||||
`SELECT
|
||||
id,
|
||||
user_id,
|
||||
muscle_group,
|
||||
last_workout_date,
|
||||
intensity,
|
||||
exercises_count,
|
||||
created_at,
|
||||
updated_at
|
||||
FROM muscle_group_recovery
|
||||
WHERE user_id = $1
|
||||
ORDER BY muscle_group`,
|
||||
[userId]
|
||||
);
|
||||
|
||||
return result.rows.map(row => ({
|
||||
...row,
|
||||
recovery_score: calculateRecoveryScore(row.last_workout_date),
|
||||
recovery_percentage: Math.round(calculateRecoveryScore(row.last_workout_date) * 100)
|
||||
}));
|
||||
} catch (err) {
|
||||
logger.error('Error getting muscle group recovery', { error: err.message, userId });
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the most recovered muscle groups (top N)
|
||||
*/
|
||||
async function getMostRecoveredGroups(pool, userId, limit = 5) {
|
||||
try {
|
||||
const recovery = await getMuscleGroupRecovery(pool, userId);
|
||||
return recovery
|
||||
.sort((a, b) => b.recovery_score - a.recovery_score)
|
||||
.slice(0, limit);
|
||||
} catch (err) {
|
||||
logger.error('Error getting most recovered groups', { error: err.message, userId });
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
calculateRecoveryScore,
|
||||
updateMuscleGroupRecovery,
|
||||
getMuscleGroupRecovery,
|
||||
getMostRecoveredGroups
|
||||
};
|
||||
@@ -0,0 +1,77 @@
|
||||
const request = require('supertest');
|
||||
const app = require('../src/index.js');
|
||||
const { Pool } = require('pg');
|
||||
|
||||
// Setup database connection for tests
|
||||
const pool = new Pool({
|
||||
host: process.env.DB_HOST || 'postgres',
|
||||
port: process.env.DB_PORT || 5432,
|
||||
user: process.env.DB_USER || 'postgres',
|
||||
password: process.env.DB_PASSWORD || 'homelab_postgres_2026',
|
||||
database: process.env.DB_NAME || 'gravl'
|
||||
});
|
||||
|
||||
describe('Phase 06 - Recovery Tracking & Swap System', () => {
|
||||
let authToken;
|
||||
let userId;
|
||||
|
||||
// Setup: Create test user
|
||||
before(async () => {
|
||||
const res = await request(app)
|
||||
.post('/api/auth/register')
|
||||
.send({
|
||||
email: `test-${Date.now()}@test.com`,
|
||||
password: 'testpass123'
|
||||
});
|
||||
|
||||
authToken = res.body.token;
|
||||
userId = res.body.user.id;
|
||||
});
|
||||
|
||||
describe('06-02: Muscle Group Recovery Tracking', () => {
|
||||
it('GET /api/recovery/muscle-groups - should return recovery status', async () => {
|
||||
const res = await request(app)
|
||||
.get('/api/recovery/muscle-groups')
|
||||
.set('Authorization', `Bearer ${authToken}`);
|
||||
|
||||
expect(res.status).toBe(200);
|
||||
expect(res.body).toHaveProperty('userId');
|
||||
expect(res.body).toHaveProperty('muscleGroups');
|
||||
expect(Array.isArray(res.body.muscleGroups)).toBe(true);
|
||||
});
|
||||
|
||||
it('GET /api/recovery/most-recovered - should return top recovered groups', async () => {
|
||||
const res = await request(app)
|
||||
.get('/api/recovery/most-recovered?limit=3')
|
||||
.set('Authorization', `Bearer ${authToken}`);
|
||||
|
||||
expect(res.status).toBe(200);
|
||||
expect(res.body).toHaveProperty('recovered');
|
||||
expect(res.body.limit).toBe(3);
|
||||
});
|
||||
});
|
||||
|
||||
describe('06-03: Smart Workout Recommendations', () => {
|
||||
it('GET /api/recommendations/smart-workout - should return recommendations', async () => {
|
||||
const res = await request(app)
|
||||
.get('/api/recommendations/smart-workout')
|
||||
.set('Authorization', `Bearer ${authToken}`);
|
||||
|
||||
expect(res.status).toBe(200);
|
||||
expect(res.body).toHaveProperty('recommendations');
|
||||
expect(Array.isArray(res.body.recommendations)).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe('06-01: Workout Swap System', () => {
|
||||
it('GET /api/workouts/available - should return available exercises', async () => {
|
||||
const res = await request(app)
|
||||
.get('/api/workouts/available')
|
||||
.set('Authorization', `Bearer ${authToken}`);
|
||||
|
||||
expect(res.status).toBe(200);
|
||||
expect(res.body).toHaveProperty('exercises');
|
||||
expect(Array.isArray(res.body.exercises)).toBe(true);
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user