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
+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>