is_trainer was captured at signup and stored, but nothing in the app used it - no client roster, no way to assign anything, a dead flag that misled users who checked it during registration. Adds a trainer/client link (a trainer adds a client by the email they registered with) and lets a trainer create workouts directly on a client's account (assigned_by_trainer_id on workouts). Deliberately scoped for privacy: a trainer only ever sees/manages the workouts they authored and assigned - not a client's other private data (their own workouts, weight log, health notes, session/completion history). That boundary needs a consent or notification system to relax safely, which doesn't exist yet, so it's left for a follow-up rather than building it half-safe. WorkoutController's authorization now distinguishes "manage" (owner or assigning trainer - view, add/edit exercises) from "own" (owner only - delete), so a trainer can build out an assigned workout's exercises after creating it, but can't delete a client's workout out from under them. Found a real bug via live testing before committing: the client roster page eager-loaded `assignedWorkouts` (keyed by assigned_by_trainer_id) on each client record, which is the trainer's own perspective, not the client's - so it silently showed "no workouts assigned" even after assigning one. Fixed to eager-load `workouts` filtered by assigned_by_trainer_id instead, with a regression test. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
65 lines
1.6 KiB
PHP
65 lines
1.6 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
|
|
|
class Workout extends Model
|
|
{
|
|
use HasFactory;
|
|
|
|
protected $fillable = [
|
|
'user_id',
|
|
'title',
|
|
'notes',
|
|
'estimated_minutes',
|
|
'program_id',
|
|
'week_number',
|
|
'day_number',
|
|
'assigned_by_trainer_id',
|
|
];
|
|
|
|
protected $casts = [
|
|
'estimated_minutes' => 'integer',
|
|
'week_number' => 'integer',
|
|
'day_number' => 'integer',
|
|
];
|
|
|
|
public function user(): BelongsTo
|
|
{
|
|
return $this->belongsTo(User::class);
|
|
}
|
|
|
|
public function program(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Program::class);
|
|
}
|
|
|
|
public function assignedByTrainer(): BelongsTo
|
|
{
|
|
return $this->belongsTo(User::class, 'assigned_by_trainer_id');
|
|
}
|
|
|
|
public function items(): HasMany
|
|
{
|
|
return $this->hasMany(WorkoutExercise::class)->orderBy('position');
|
|
}
|
|
|
|
public function exercises(): BelongsToMany
|
|
{
|
|
return $this->belongsToMany(Exercise::class, 'workout_exercises')
|
|
->withPivot(['id', 'position', 'sets', 'reps', 'weight', 'rest_seconds', 'notes'])
|
|
->withTimestamps()
|
|
->orderBy('workout_exercises.position');
|
|
}
|
|
|
|
public function favoritedBy()
|
|
{
|
|
return $this->belongsToMany(User::class, 'workout_favorites')->withTimestamps();
|
|
}
|
|
}
|