Build out the trainer role

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>
This commit is contained in:
2026-08-10 12:28:23 +03:00
co-authored by Claude Sonnet 5
parent ebd0578dc9
commit e8173ff90a
17 changed files with 543 additions and 5 deletions
@@ -20,6 +20,9 @@
@auth
<a href="{{ route('dashboard') }}" class="text-sm font-medium text-gray-700 dark:text-gray-300 hover:text-gray-900 dark:hover:text-white">{{ __('app.nav.dashboard') }}</a>
<a href="{{ route('programs.index') }}" class="text-sm font-medium text-gray-700 dark:text-gray-300 hover:text-gray-900 dark:hover:text-white">{{ __('app.nav.programs') }}</a>
@if(Auth::user()->is_trainer)
<a href="{{ route('trainer.clients') }}" class="text-sm font-medium text-gray-700 dark:text-gray-300 hover:text-gray-900 dark:hover:text-white">{{ __('app.nav.clients') }}</a>
@endif
@if(Auth::user()->is_admin)
<a href="{{ route('admin.dashboard') }}" class="text-sm font-medium text-indigo-700 dark:text-indigo-300 hover:text-indigo-900 dark:hover:text-indigo-200">{{ __('app.nav.admin') }}</a>
@endif
@@ -100,6 +103,9 @@
@auth
<x-responsive-nav-link :href="route('dashboard')">{{ __('app.nav.dashboard') }}</x-responsive-nav-link>
<x-responsive-nav-link :href="route('programs.index')">{{ __('app.nav.programs') }}</x-responsive-nav-link>
@if(Auth::user()->is_trainer)
<x-responsive-nav-link :href="route('trainer.clients')">{{ __('app.nav.clients') }}</x-responsive-nav-link>
@endif
@if(Auth::user()->is_admin)
<x-responsive-nav-link :href="route('admin.dashboard')">{{ __('app.nav.admin') }}</x-responsive-nav-link>
@endif
@@ -0,0 +1,37 @@
<x-app-layout>
<section class="bg-white dark:bg-gray-900 border-b border-gray-100 dark:border-gray-800">
<div class="mx-auto max-w-7xl px-4 sm:px-6 lg:px-8 py-8">
<a href="{{ route('trainer.clients') }}" class="text-sm text-gray-600 dark:text-gray-300 hover:text-gray-900 dark:hover:text-white">&larr; My Clients</a>
<h1 class="mt-2 text-2xl sm:text-3xl font-extrabold tracking-tight text-gray-900 dark:text-white">Assign a Workout to {{ $client->name }}</h1>
<p class="mt-2 text-gray-600 dark:text-gray-300">You'll be able to add exercises to it right after creating it.</p>
</div>
</section>
<section class="mx-auto max-w-2xl px-4 sm:px-6 lg:px-8 py-8">
@if ($errors->any())
<div class="mb-4 rounded-xl bg-red-50 text-red-700 px-4 py-3">
<ul class="list-disc ms-5 text-sm">
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif
<form method="POST" action="{{ route('trainer.workouts.store', $client) }}" class="space-y-5 bg-white dark:bg-gray-800 rounded-2xl p-6 ring-1 ring-gray-200 dark:ring-gray-700">
@csrf
<div>
<label class="block text-sm font-medium text-gray-700 dark:text-gray-300">Title</label>
<input type="text" name="title" required placeholder="e.g. Week 1 - Push Day" class="mt-1 w-full rounded-xl border-gray-300 dark:border-gray-700 bg-white/80 dark:bg-gray-900 text-gray-900 dark:text-gray-100 px-3 py-2">
</div>
<div>
<label class="block text-sm font-medium text-gray-700 dark:text-gray-300">Estimated minutes</label>
<input type="number" name="estimated_minutes" min="1" max="600" class="mt-1 w-32 rounded-xl border-gray-300 dark:border-gray-700 bg-white/80 dark:bg-gray-900 text-gray-900 dark:text-gray-100 px-3 py-2">
</div>
<div>
<label class="block text-sm font-medium text-gray-700 dark:text-gray-300">Notes</label>
<textarea name="notes" rows="3" class="mt-1 w-full rounded-xl border-gray-300 dark:border-gray-700 bg-white/80 dark:bg-gray-900 text-gray-900 dark:text-gray-100 px-3 py-2"></textarea>
</div>
<button type="submit" class="inline-flex items-center rounded-xl bg-gradient-to-r from-fuchsia-600 to-indigo-600 px-4 py-2 text-sm font-semibold text-white shadow hover:from-fuchsia-500 hover:to-indigo-500">Assign Workout</button>
</form>
</section>
</x-app-layout>
+70
View File
@@ -0,0 +1,70 @@
<x-app-layout>
<section class="bg-white dark:bg-gray-900 border-b border-gray-100 dark:border-gray-800">
<div class="mx-auto max-w-7xl px-4 sm:px-6 lg:px-8 py-8">
<h1 class="text-2xl sm:text-3xl font-extrabold tracking-tight text-gray-900 dark:text-white">My Clients</h1>
<p class="mt-2 text-gray-600 dark:text-gray-300">Add clients by the email they registered with, and assign them workouts.</p>
</div>
</section>
<section class="mx-auto max-w-7xl px-4 sm:px-6 lg:px-8 py-6">
@if(session('status'))
<div class="mb-4 rounded-xl bg-emerald-50 text-emerald-700 px-4 py-3">{{ session('status') }}</div>
@endif
@if ($errors->any())
<div class="mb-4 rounded-xl bg-red-50 text-red-700 px-4 py-3">
<ul class="list-disc ms-5 text-sm">
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif
<form method="POST" action="{{ route('trainer.clients.add') }}" class="mb-6 flex items-end gap-3 bg-white dark:bg-gray-800 rounded-2xl p-4 ring-1 ring-gray-200 dark:ring-gray-700">
@csrf
<div class="w-full sm:w-80">
<label class="block text-sm font-medium text-gray-700 dark:text-gray-300">Client's email</label>
<input type="email" name="email" required placeholder="client@example.com" class="mt-1 w-full rounded-xl border-gray-300 dark:border-gray-700 bg-white/80 dark:bg-gray-900 text-gray-900 dark:text-gray-100 px-3 py-2">
</div>
<button type="submit" class="inline-flex items-center rounded-xl bg-gradient-to-r from-fuchsia-600 to-indigo-600 px-4 py-2 text-sm font-semibold text-white shadow hover:from-fuchsia-500 hover:to-indigo-500">Add Client</button>
</form>
@if($clients->isEmpty())
<div class="rounded-2xl border border-dashed border-gray-300 dark:border-gray-700 p-8 text-center text-gray-600 dark:text-gray-300">No clients yet. Add one by email above.</div>
@else
<div class="grid grid-cols-1 lg:grid-cols-2 gap-6">
@foreach($clients as $client)
<div class="rounded-2xl ring-1 ring-gray-200 dark:ring-gray-700 bg-white dark:bg-gray-800 p-5">
<div class="flex items-center justify-between">
<div>
<h3 class="text-lg font-semibold text-gray-900 dark:text-white">{{ $client->name }}</h3>
<p class="text-sm text-gray-600 dark:text-gray-300">{{ $client->email }}</p>
</div>
<div class="flex items-center gap-3">
<a href="{{ route('trainer.workouts.create', $client) }}" class="text-sm font-semibold text-indigo-600 hover:text-indigo-500">Assign workout</a>
<form method="POST" action="{{ route('trainer.clients.remove', $client) }}" onsubmit="return confirm('Remove {{ $client->name }} from your clients?');">
@csrf
@method('DELETE')
<button type="submit" class="text-sm text-red-600 hover:text-red-500">Remove</button>
</form>
</div>
</div>
@if($client->workouts->isEmpty())
<p class="mt-4 text-sm text-gray-500">No workouts assigned yet.</p>
@else
<ul class="mt-4 space-y-2">
@foreach($client->workouts as $workout)
<li class="flex items-center justify-between rounded-xl ring-1 ring-gray-200 dark:ring-gray-700 bg-white dark:bg-gray-900 px-3 py-2 text-sm">
<span class="text-gray-900 dark:text-gray-100">{{ $workout->title }}</span>
<span class="text-gray-500">{{ $workout->created_at->format('M j, Y') }}</span>
</li>
@endforeach
</ul>
@endif
</div>
@endforeach
</div>
@endif
</section>
</x-app-layout>
+3
View File
@@ -23,6 +23,9 @@
<div class="rounded-2xl ring-1 ring-gray-200 dark:ring-gray-700 bg-white dark:bg-gray-800 p-5 flex flex-col">
<div class="flex-1">
<h3 class="text-lg font-semibold text-gray-900 dark:text-white">{{ $workout->title }}</h3>
@if($workout->assignedByTrainer)
<span class="mt-1 inline-flex items-center rounded-full bg-indigo-100 dark:bg-indigo-900/40 text-indigo-700 dark:text-indigo-300 px-2.5 py-0.5 text-xs font-medium">Assigned by {{ $workout->assignedByTrainer->name }}</span>
@endif
@if($workout->estimated_minutes)
<p class="mt-1 text-sm text-gray-600 dark:text-gray-300">~ {{ $workout->estimated_minutes }} min</p>
@endif
+3
View File
@@ -23,6 +23,9 @@
</form>
@endauth
</div>
@if($workout->assignedByTrainer)
<span class="mt-2 inline-flex items-center rounded-full bg-indigo-100 dark:bg-indigo-900/40 text-indigo-700 dark:text-indigo-300 px-2.5 py-0.5 text-xs font-medium">Assigned by {{ $workout->assignedByTrainer->name }}</span>
@endif
<p class="mt-1 text-gray-600 dark:text-gray-300">@if($workout->estimated_minutes) ~ {{ $workout->estimated_minutes }} min · @endif {{ $workout->items->count() }} exercises</p>
</div>
<div class="flex items-center gap-3">