feat: history and calculate needs for user
CI / 🧪 Tests Laravel (push) Failing after 2m18s
CI / 🐳 Build & Push Images (push) Has been skipped

This commit is contained in:
2026-08-14 12:45:37 +02:00
parent ea5a5737ec
commit 69372a49ed
51 changed files with 2226 additions and 305 deletions
@@ -0,0 +1,79 @@
<?php
namespace App\Actions;
use App\Enums\NutritionPlanSource;
use App\Enums\WeeklyReviewStatus;
use App\Models\NutritionPlan;
use App\Models\WeeklyReview;
use Illuminate\Support\Facades\DB;
class AcceptWeeklyReviewAdjustment
{
public function execute(WeeklyReview $weeklyReview): NutritionPlan
{
return DB::transaction(function () use ($weeklyReview): NutritionPlan {
$lockedReview = WeeklyReview::query()->lockForUpdate()->findOrFail($weeklyReview->getKey());
abort_if($lockedReview->accepted_at !== null, 409, 'WEEKLY_ADJUSTMENT_ALREADY_ACCEPTED');
abort_unless(
$lockedReview->status === WeeklyReviewStatus::ADJUSTMENT_RECOMMENDED
&& $lockedReview->recommended_calorie_adjustment !== 0,
409,
'WEEKLY_ADJUSTMENT_UNAVAILABLE',
);
$user = $lockedReview->user()->lockForUpdate()->firstOrFail();
$currentPlan = $user->nutritionPlans()->where('is_active', true)->latest('effective_from')->firstOrFail();
abort_unless(
$lockedReview->nutrition_plan_id === $currentPlan->getKey(),
409,
'WEEKLY_ADJUSTMENT_STALE',
);
$now = now();
$targetCalories = max(1200, $currentPlan->calories + $lockedReview->recommended_calorie_adjustment);
$proteins = (int) round($currentPlan->proteins);
$fats = (int) round($currentPlan->fats);
$carbs = max(0, (int) round(($targetCalories - ($proteins * 4) - ($fats * 9)) / 4));
$calories = ($proteins * 4) + ($carbs * 4) + ($fats * 9);
abort_if(
$calories === $currentPlan->calories,
409,
'WEEKLY_ADJUSTMENT_UNAVAILABLE',
);
$currentPlan->update([
'is_active' => false,
'effective_until' => $now,
]);
$nextPlan = $user->nutritionPlans()->create([
'source' => NutritionPlanSource::WEEKLY_ADJUSTMENT,
'formula_version' => $currentPlan->formula_version,
'calories' => $calories,
'proteins' => $proteins,
'carbs' => $carbs,
'fats' => $fats,
'resting_metabolism' => $currentPlan->resting_metabolism,
'maintenance_calories' => $currentPlan->maintenance_calories,
'calorie_adjustment_percent' => ($calories - $currentPlan->maintenance_calories) / max(1, $currentPlan->maintenance_calories),
'calculation_details' => [
...$currentPlan->calculation_details,
'weeklyReviewId' => $lockedReview->getKey(),
'weeklyAdjustmentCalories' => $lockedReview->recommended_calorie_adjustment,
],
'effective_from' => $now,
]);
$lockedReview->update([
'accepted_at' => $now,
'accepted_nutrition_plan_id' => $nextPlan->getKey(),
]);
return $nextPlan;
}, attempts: 3);
}
}
@@ -0,0 +1,28 @@
<?php
namespace App\Actions;
use App\Enums\NutritionPlanSource;
use App\Models\User;
class CompleteNutritionOnboarding
{
public function __construct(private RecalculateNutritionPlan $recalculateNutritionPlan) {}
/**
* @param array<string, mixed> $data
*/
public function execute(User $user, array $data): User
{
if ($user->hasCompletedNutritionOnboarding()) {
return $user->loadMissing(['activeNutritionPlan', 'latestWeightEntry']);
}
return $this->recalculateNutritionPlan->execute(
user: $user,
data: $data,
source: NutritionPlanSource::ONBOARDING,
completeOnboarding: true,
);
}
}
+87
View File
@@ -0,0 +1,87 @@
<?php
namespace App\Actions;
use App\Enums\NutritionPlanSource;
use App\Enums\PacePreference;
use App\Enums\PhysicalActivityLevel;
use App\Enums\UserSex;
use App\Enums\WeightGoal;
use App\Models\User;
use App\Services\NutritionPlanCalculator;
use Carbon\CarbonImmutable;
use Illuminate\Support\Facades\DB;
class RecalculateNutritionPlan
{
public function __construct(private NutritionPlanCalculator $calculator) {}
/**
* @param array<string, mixed> $data
*/
public function execute(
User $user,
array $data,
NutritionPlanSource $source,
bool $completeOnboarding = false,
): User {
return DB::transaction(function () use ($user, $data, $source, $completeOnboarding): User {
$lockedUser = User::query()->lockForUpdate()->findOrFail($user->getKey());
$now = now();
if ($completeOnboarding && $lockedUser->hasCompletedNutritionOnboarding()) {
return $lockedUser->load(['activeNutritionPlan', 'latestWeightEntry']);
}
$lockedUser->update([
'height' => $data['heightCm'],
'target_weight' => $data['targetWeightKg'] ?? null,
'date_of_birth' => $data['dateOfBirth'],
'sex' => $data['sex'],
'physical_activity_level' => $data['physicalActivityLevel'],
'weight_goal' => $data['weightGoal'],
'pace_preference' => $data['pacePreference'],
...($completeOnboarding ? [
'onboarding_completed_at' => $now,
'nutrition_estimate_accepted_at' => $now,
] : []),
]);
$weightKg = (float) $data['weightKg'];
$latestWeight = $lockedUser->weightEntries()->latest('measured_at')->first();
if ($latestWeight === null || abs($latestWeight->weight_kg - $weightKg) >= 0.01) {
$lockedUser->weightEntries()->create([
'weight_kg' => $weightKg,
'measured_at' => $now,
]);
}
$lockedUser->nutritionPlans()
->where('is_active', true)
->update([
'is_active' => false,
'effective_until' => $now,
]);
$calculation = $this->calculator->calculate(
weightKg: $weightKg,
heightCm: (int) $data['heightCm'],
dateOfBirth: CarbonImmutable::parse($data['dateOfBirth']),
sex: UserSex::from($data['sex']),
activityLevel: PhysicalActivityLevel::from($data['physicalActivityLevel']),
weightGoal: WeightGoal::from($data['weightGoal']),
pacePreference: PacePreference::from($data['pacePreference']),
);
$lockedUser->nutritionPlans()->create([
...$calculation,
'source' => $source,
'is_active' => true,
'effective_from' => $now,
]);
return $lockedUser->fresh()->load(['activeNutritionPlan', 'latestWeightEntry']);
}, attempts: 3);
}
}
+10
View File
@@ -0,0 +1,10 @@
<?php
namespace App\Enums;
enum NutritionPlanSource: string
{
case ONBOARDING = 'onboarding';
case PROFILE_RECALCULATION = 'profile_recalculation';
case WEEKLY_ADJUSTMENT = 'weekly_adjustment';
}
+10
View File
@@ -0,0 +1,10 @@
<?php
namespace App\Enums;
enum WeeklyReviewStatus: string
{
case INSUFFICIENT_DATA = 'insufficient_data';
case ON_TRACK = 'on_track';
case ADJUSTMENT_RECOMMENDED = 'adjustment_recommended';
}
@@ -2,12 +2,7 @@
namespace App\Filament\Resources\Users\Schemas; namespace App\Filament\Resources\Users\Schemas;
use App\Enums\PacePreference;
use App\Enums\PhysicalActivityLevel;
use App\Enums\UserRole; use App\Enums\UserRole;
use App\Enums\UserSex;
use App\Enums\WeightGoal;
use Filament\Forms\Components\DatePicker;
use Filament\Forms\Components\DateTimePicker; use Filament\Forms\Components\DateTimePicker;
use Filament\Forms\Components\FileUpload; use Filament\Forms\Components\FileUpload;
use Filament\Forms\Components\Select; use Filament\Forms\Components\Select;
@@ -65,82 +60,11 @@ class UserForm
->label(__('admin.users.fields.email_verified_at')), ->label(__('admin.users.fields.email_verified_at')),
]), ]),
Section::make(__('admin.users.sections.profile')) Section::make(__('admin.users.sections.profile'))
->columns(2)
->schema([ ->schema([
Textarea::make('bio') Textarea::make('bio')
->label(__('admin.users.fields.bio')) ->label(__('admin.users.fields.bio'))
->rows(4) ->rows(4)
->columnSpanFull(), ->columnSpanFull(),
TextInput::make('height')
->label(__('admin.users.fields.height'))
->integer()
->minValue(0)
->maxValue(300)
->suffix('cm'),
DatePicker::make('date_of_birth')
->label(__('admin.users.fields.date_of_birth'))
->minDate('1900-01-01')
->maxDate(now()->subDay()),
Select::make('sex')
->label(__('admin.users.fields.sex'))
->options(UserSex::class)
->default(UserSex::UNKNOWN->value)
->required(),
]),
Section::make(__('admin.users.sections.nutrition_goals'))
->columns(4)
->schema([
TextInput::make('daily_calorie_goal')
->label(__('admin.users.fields.daily_calorie_goal'))
->integer()
->minValue(0)
->maxValue(100000)
->suffix('kcal')
->default(2000)
->required(),
TextInput::make('daily_protein_goal')
->label(__('admin.users.fields.daily_protein_goal'))
->numeric()
->minValue(0)
->maxValue(10000)
->suffix('g')
->default(120)
->required(),
TextInput::make('daily_carbs_goal')
->label(__('admin.users.fields.daily_carbs_goal'))
->numeric()
->minValue(0)
->maxValue(10000)
->suffix('g')
->default(250)
->required(),
TextInput::make('daily_fats_goal')
->label(__('admin.users.fields.daily_fats_goal'))
->numeric()
->minValue(0)
->maxValue(10000)
->suffix('g')
->default(70)
->required(),
]),
Section::make(__('admin.users.sections.activity'))
->columns(3)
->schema([
Select::make('physical_activity_level')
->label(__('admin.users.fields.physical_activity_level'))
->options(PhysicalActivityLevel::class)
->default(PhysicalActivityLevel::SEDENTARY->value)
->required(),
Select::make('weight_goal')
->label(__('admin.users.fields.weight_goal'))
->options(WeightGoal::class)
->default(WeightGoal::LOSE_WEIGHT->value)
->required(),
Select::make('pace_preference')
->label(__('admin.users.fields.pace_preference'))
->options(PacePreference::class)
->default(PacePreference::SLOW->value)
->required(),
]), ]),
]); ]);
} }
@@ -67,6 +67,16 @@ class UserInfolist
->numeric(decimalPlaces: 0) ->numeric(decimalPlaces: 0)
->suffix(' cm') ->suffix(' cm')
->placeholder(__('admin.users.placeholders.empty')), ->placeholder(__('admin.users.placeholders.empty')),
TextEntry::make('latestWeightEntry.weight_kg')
->label(__('admin.users.fields.current_weight'))
->numeric(maxDecimalPlaces: 2)
->suffix(' kg')
->placeholder(__('admin.users.placeholders.empty')),
TextEntry::make('target_weight')
->label(__('admin.users.fields.target_weight'))
->numeric(maxDecimalPlaces: 2)
->suffix(' kg')
->placeholder(__('admin.users.placeholders.empty')),
TextEntry::make('date_of_birth') TextEntry::make('date_of_birth')
->label(__('admin.users.fields.date_of_birth')) ->label(__('admin.users.fields.date_of_birth'))
->date() ->date()
@@ -75,25 +85,29 @@ class UserInfolist
->label(__('admin.users.fields.sex')) ->label(__('admin.users.fields.sex'))
->badge(), ->badge(),
]), ]),
Section::make(__('admin.users.sections.nutrition_goals')) Section::make(__('admin.users.sections.nutrition_plan'))
->columns(4) ->columns(4)
->schema([ ->schema([
TextEntry::make('daily_calorie_goal') TextEntry::make('activeNutritionPlan.calories')
->label(__('admin.users.fields.daily_calorie_goal')) ->label(__('admin.users.fields.calorie_goal'))
->numeric(decimalPlaces: 0) ->numeric(decimalPlaces: 0)
->suffix(' kcal'), ->suffix(' kcal')
TextEntry::make('daily_protein_goal') ->placeholder(__('admin.users.placeholders.empty')),
->label(__('admin.users.fields.daily_protein_goal')) TextEntry::make('activeNutritionPlan.proteins')
->label(__('admin.users.fields.protein_goal'))
->numeric(maxDecimalPlaces: 2) ->numeric(maxDecimalPlaces: 2)
->suffix(' g'), ->suffix(' g')
TextEntry::make('daily_carbs_goal') ->placeholder(__('admin.users.placeholders.empty')),
->label(__('admin.users.fields.daily_carbs_goal')) TextEntry::make('activeNutritionPlan.carbs')
->label(__('admin.users.fields.carbs_goal'))
->numeric(maxDecimalPlaces: 2) ->numeric(maxDecimalPlaces: 2)
->suffix(' g'), ->suffix(' g')
TextEntry::make('daily_fats_goal') ->placeholder(__('admin.users.placeholders.empty')),
->label(__('admin.users.fields.daily_fats_goal')) TextEntry::make('activeNutritionPlan.fats')
->label(__('admin.users.fields.fats_goal'))
->numeric(maxDecimalPlaces: 2) ->numeric(maxDecimalPlaces: 2)
->suffix(' g'), ->suffix(' g')
->placeholder(__('admin.users.placeholders.empty')),
]), ]),
Section::make(__('admin.users.sections.activity')) Section::make(__('admin.users.sections.activity'))
->columns(3) ->columns(3)
@@ -106,7 +120,12 @@ class UserInfolist
->badge(), ->badge(),
TextEntry::make('pace_preference') TextEntry::make('pace_preference')
->label(__('admin.users.fields.pace_preference')) ->label(__('admin.users.fields.pace_preference'))
->badge(), ->badge()
->placeholder(__('admin.users.placeholders.empty')),
TextEntry::make('onboarding_completed_at')
->label(__('admin.users.fields.onboarding_completed_at'))
->dateTime()
->placeholder(__('admin.users.placeholders.empty')),
]), ]),
Section::make(__('admin.users.sections.metadata')) Section::make(__('admin.users.sections.metadata'))
->columns(2) ->columns(2)
@@ -95,29 +95,25 @@ class UsersTable
->badge() ->badge()
->sortable() ->sortable()
->toggleable(isToggledHiddenByDefault: true), ->toggleable(isToggledHiddenByDefault: true),
TextColumn::make('daily_calorie_goal') TextColumn::make('activeNutritionPlan.calories')
->label(__('admin.users.fields.daily_calorie_goal')) ->label(__('admin.users.fields.calorie_goal'))
->numeric(decimalPlaces: 0) ->numeric(decimalPlaces: 0)
->suffix(' kcal') ->suffix(' kcal')
->sortable()
->toggleable(isToggledHiddenByDefault: true), ->toggleable(isToggledHiddenByDefault: true),
TextColumn::make('daily_protein_goal') TextColumn::make('activeNutritionPlan.proteins')
->label(__('admin.users.fields.daily_protein_goal')) ->label(__('admin.users.fields.protein_goal'))
->numeric(maxDecimalPlaces: 2) ->numeric(maxDecimalPlaces: 2)
->suffix(' g') ->suffix(' g')
->sortable()
->toggleable(isToggledHiddenByDefault: true), ->toggleable(isToggledHiddenByDefault: true),
TextColumn::make('daily_carbs_goal') TextColumn::make('activeNutritionPlan.carbs')
->label(__('admin.users.fields.daily_carbs_goal')) ->label(__('admin.users.fields.carbs_goal'))
->numeric(maxDecimalPlaces: 2) ->numeric(maxDecimalPlaces: 2)
->suffix(' g') ->suffix(' g')
->sortable()
->toggleable(isToggledHiddenByDefault: true), ->toggleable(isToggledHiddenByDefault: true),
TextColumn::make('daily_fats_goal') TextColumn::make('activeNutritionPlan.fats')
->label(__('admin.users.fields.daily_fats_goal')) ->label(__('admin.users.fields.fats_goal'))
->numeric(maxDecimalPlaces: 2) ->numeric(maxDecimalPlaces: 2)
->suffix(' g') ->suffix(' g')
->sortable()
->toggleable(isToggledHiddenByDefault: true), ->toggleable(isToggledHiddenByDefault: true),
TextColumn::make('bio') TextColumn::make('bio')
->label(__('admin.users.fields.bio')) ->label(__('admin.users.fields.bio'))
+21 -45
View File
@@ -43,7 +43,7 @@ class AuthController extends Controller
$this->startWebSession($request, $user); $this->startWebSession($request, $user);
return response()->json([ return response()->json([
'message' => __('api.auth.registered_unverified'), 'code' => 'REGISTERED_UNVERIFIED',
'user' => new UserResource($user), 'user' => new UserResource($user),
], 201); ], 201);
} }
@@ -73,7 +73,7 @@ class AuthController extends Controller
} }
return response()->json([ return response()->json([
'message' => __('api.auth.registered_unverified'), 'code' => 'REGISTERED_UNVERIFIED',
'token' => $this->createMobileToken($user, $request->validated('device_name')), 'token' => $this->createMobileToken($user, $request->validated('device_name')),
'user' => new UserResource($user), 'user' => new UserResource($user),
], 201); ], 201);
@@ -101,7 +101,7 @@ class AuthController extends Controller
if (! hash_equals($hash, sha1($user->getEmailForVerification()))) { if (! hash_equals($hash, sha1($user->getEmailForVerification()))) {
if ($request->expectsJson()) { if ($request->expectsJson()) {
return response()->json([ return response()->json([
'message' => __('api.auth.invalid_verification_link'), 'code' => 'INVALID_VERIFICATION_LINK',
], 403); ], 403);
} }
@@ -118,9 +118,9 @@ class AuthController extends Controller
if ($request->expectsJson()) { if ($request->expectsJson()) {
return response()->json([ return response()->json([
'message' => $alreadyVerified 'code' => $alreadyVerified
? __('api.auth.already_verified') ? 'EMAIL_ALREADY_VERIFIED'
: __('api.auth.verified'), : 'EMAIL_VERIFIED',
'user' => new UserResource($user->fresh()), 'user' => new UserResource($user->fresh()),
]); ]);
} }
@@ -141,7 +141,7 @@ class AuthController extends Controller
} }
return response()->json([ return response()->json([
'message' => __('api.auth.verification_sent_if_unverified'), 'code' => 'VERIFICATION_EMAIL_SENT_IF_UNVERIFIED',
]); ]);
} }
@@ -152,12 +152,12 @@ class AuthController extends Controller
if ($status === Password::RESET_THROTTLED) { if ($status === Password::RESET_THROTTLED) {
return response()->json([ return response()->json([
'message' => __($status), 'code' => 'PASSWORD_RESET_THROTTLED',
], 429); ], 429);
} }
return response()->json([ return response()->json([
'message' => __('api.auth.password_reset_link_sent'), 'code' => 'PASSWORD_RESET_LINK_SENT',
]); ]);
} }
@@ -167,12 +167,12 @@ class AuthController extends Controller
if ($status !== Password::PASSWORD_RESET) { if ($status !== Password::PASSWORD_RESET) {
return response()->json([ return response()->json([
'message' => __($status), 'code' => 'PASSWORD_RESET_FAILED',
], 422); ], 422);
} }
return response()->json([ return response()->json([
'message' => __('api.auth.password_reset'), 'code' => 'PASSWORD_RESET',
]); ]);
} }
@@ -228,7 +228,7 @@ class AuthController extends Controller
} }
return response()->json([ return response()->json([
'message' => __('api.auth.logout'), 'code' => 'LOGGED_OUT',
]); ]);
} }
@@ -237,7 +237,7 @@ class AuthController extends Controller
$user = auth()->user(); $user = auth()->user();
if (! $user) { if (! $user) {
return response()->json(['message' => __('api.auth.unauthenticated')], 401); return response()->json(['code' => 'UNAUTHENTICATED'], 401);
} }
return new UserResource($user); return new UserResource($user);
@@ -247,10 +247,9 @@ class AuthController extends Controller
{ {
$user = $request->user(); $user = $request->user();
abort_if($user->isSuspended(), 403, __('api.auth.suspended')); abort_if($user->isSuspended(), 403, 'ACCOUNT_SUSPENDED');
$data = $request->validated(); $data = $request->validated();
$nutritionGoals = $data['nutritionGoals'] ?? null;
if ($request->hasFile('avatar')) { if ($request->hasFile('avatar')) {
if ($user->avatar_url) { if ($user->avatar_url) {
@@ -263,13 +262,6 @@ class AuthController extends Controller
$userAttributes = $this->userAttributesFromRequestData($data); $userAttributes = $this->userAttributesFromRequestData($data);
if (is_array($nutritionGoals)) {
$userAttributes['daily_calorie_goal'] = $nutritionGoals['calories'];
$userAttributes['daily_protein_goal'] = $nutritionGoals['proteins'];
$userAttributes['daily_carbs_goal'] = $nutritionGoals['carbs'];
$userAttributes['daily_fats_goal'] = $nutritionGoals['fats'];
}
if (! empty($userAttributes)) { if (! empty($userAttributes)) {
$user->update($userAttributes); $user->update($userAttributes);
} }
@@ -281,7 +273,7 @@ class AuthController extends Controller
{ {
$user = $request->user(); $user = $request->user();
abort_if($user->isSuspended(), 403, __('api.auth.suspended')); abort_if($user->isSuspended(), 403, 'ACCOUNT_SUSPENDED');
$currentAccessToken = $user->currentAccessToken(); $currentAccessToken = $user->currentAccessToken();
@@ -299,7 +291,7 @@ class AuthController extends Controller
} }
return response()->json([ return response()->json([
'message' => __('api.auth.password_updated'), 'code' => 'PASSWORD_UPDATED',
]); ]);
} }
@@ -350,7 +342,7 @@ class AuthController extends Controller
} }
return response()->json([ return response()->json([
'message' => __('api.auth.deleted'), 'code' => 'ACCOUNT_DELETED',
]); ]);
} }
@@ -382,7 +374,7 @@ class AuthController extends Controller
} }
return response()->json([ return response()->json([
'message' => __('api.auth.verification_email_failed'), 'code' => 'VERIFICATION_EMAIL_FAILED',
], 503); ], 503);
} }
@@ -398,13 +390,13 @@ class AuthController extends Controller
if (! $user || ! Hash::check((string) $data['password'], $user->password)) { if (! $user || ! Hash::check((string) $data['password'], $user->password)) {
return response()->json([ return response()->json([
'message' => __('api.auth.invalid_credentials'), 'code' => 'INVALID_CREDENTIALS',
], 401); ], 401);
} }
if ($user->isSuspended()) { if ($user->isSuspended()) {
return response()->json([ return response()->json([
'message' => __('api.auth.suspended'), 'code' => 'ACCOUNT_SUSPENDED',
], 403); ], 403);
} }
@@ -414,7 +406,7 @@ class AuthController extends Controller
if (! $user->hasVerifiedEmail()) { if (! $user->hasVerifiedEmail()) {
return response()->json([ return response()->json([
'message' => __('api.auth.email_not_verified'), 'code' => 'EMAIL_NOT_VERIFIED',
], 403); ], 403);
} }
@@ -480,25 +472,9 @@ class AuthController extends Controller
'name', 'name',
'avatar_url', 'avatar_url',
'locale', 'locale',
'height',
'bio', 'bio',
'sex',
'date_of_birth',
]); ]);
$attributeMap = [
'physicalActivityLevel' => 'physical_activity_level',
'weightGoal' => 'weight_goal',
'pacePreference' => 'pace_preference',
'dateOfBirth' => 'date_of_birth',
];
foreach ($attributeMap as $requestKey => $attribute) {
if (array_key_exists($requestKey, $data)) {
$attributes[$attribute] = $data[$requestKey];
}
}
return $attributes; return $attributes;
} }
} }
@@ -0,0 +1,19 @@
<?php
namespace App\Http\Controllers;
use App\Actions\CompleteNutritionOnboarding;
use App\Http\Requests\CompleteNutritionOnboardingRequest;
use App\Http\Resources\UserResource;
class NutritionOnboardingController extends Controller
{
public function __invoke(
CompleteNutritionOnboardingRequest $request,
CompleteNutritionOnboarding $completeNutritionOnboarding,
): UserResource {
return new UserResource(
$completeNutritionOnboarding->execute($request->user(), $request->validated())
);
}
}
@@ -0,0 +1,24 @@
<?php
namespace App\Http\Controllers;
use App\Http\Resources\NutritionPlanResource;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\AnonymousResourceCollection;
class NutritionPlanController extends Controller
{
public function index(Request $request): AnonymousResourceCollection
{
return NutritionPlanResource::collection(
$request->user()->nutritionPlans()->latest('effective_from')->limit(50)->get()
);
}
public function current(Request $request): NutritionPlanResource
{
$plan = $request->user()->nutritionPlans()->where('is_active', true)->latest('effective_from')->firstOrFail();
return new NutritionPlanResource($plan);
}
}
@@ -0,0 +1,22 @@
<?php
namespace App\Http\Controllers;
use App\Actions\RecalculateNutritionPlan;
use App\Enums\NutritionPlanSource;
use App\Http\Requests\UpdateNutritionProfileRequest;
use App\Http\Resources\UserResource;
class NutritionProfileController extends Controller
{
public function __invoke(
UpdateNutritionProfileRequest $request,
RecalculateNutritionPlan $recalculateNutritionPlan,
): UserResource {
return new UserResource($recalculateNutritionPlan->execute(
user: $request->user(),
data: $request->validated(),
source: NutritionPlanSource::PROFILE_RECALCULATION,
));
}
}
@@ -0,0 +1,39 @@
<?php
namespace App\Http\Controllers;
use App\Actions\AcceptWeeklyReviewAdjustment;
use App\Http\Resources\NutritionPlanResource;
use App\Http\Resources\WeeklyReviewResource;
use App\Models\WeeklyReview;
use App\Services\WeeklyReviewService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\AnonymousResourceCollection;
class WeeklyReviewController extends Controller
{
public function index(Request $request): AnonymousResourceCollection
{
return WeeklyReviewResource::collection(
$request->user()->weeklyReviews()->latest('period_start')->limit(52)->get()
);
}
public function current(Request $request, WeeklyReviewService $weeklyReviewService): JsonResponse
{
return (new WeeklyReviewResource($weeklyReviewService->generate($request->user())))
->response()
->setStatusCode(200);
}
public function accept(
Request $request,
WeeklyReview $weeklyReview,
AcceptWeeklyReviewAdjustment $acceptWeeklyReviewAdjustment,
): NutritionPlanResource {
abort_unless($weeklyReview->user_id === $request->user()->getKey(), 404);
return new NutritionPlanResource($acceptWeeklyReviewAdjustment->execute($weeklyReview));
}
}
@@ -0,0 +1,55 @@
<?php
namespace App\Http\Controllers;
use App\Http\Requests\ListWeightEntriesRequest;
use App\Http\Requests\StoreWeightEntryRequest;
use App\Http\Resources\WeightEntryResource;
use App\Models\WeightEntry;
use Carbon\CarbonImmutable;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\AnonymousResourceCollection;
use Illuminate\Http\Response;
class WeightEntryController extends Controller
{
/**
* Display a listing of the resource.
*/
public function index(ListWeightEntriesRequest $request): AnonymousResourceCollection
{
$from = now()->subDays($request->integer('days', 365))->startOfDay();
$entries = $request->user()
->weightEntries()
->where('measured_at', '>=', $from)
->latest('measured_at')
->get();
return WeightEntryResource::collection($entries);
}
/**
* Store a newly created resource in storage.
*/
public function store(StoreWeightEntryRequest $request): WeightEntryResource
{
$entry = $request->user()->weightEntries()->create([
'weight_kg' => $request->validated('weightKg'),
'measured_at' => CarbonImmutable::parse($request->validated('measuredAt') ?? now()),
]);
return new WeightEntryResource($entry);
}
/**
* Remove the specified resource from storage.
*/
public function destroy(Request $request, WeightEntry $weightEntry): Response
{
abort_unless($weightEntry->user_id === $request->user()->getKey(), 404);
$weightEntry->delete();
return response()->noContent();
}
}
@@ -0,0 +1,26 @@
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;
class EnsureNutritionOnboardingCompleted
{
/**
* Handle an incoming request.
*
* @param \Closure(\Illuminate\Http\Request): (\Symfony\Component\HttpFoundation\Response) $next
*/
public function handle(Request $request, Closure $next): Response
{
if (! $request->user()?->hasCompletedNutritionOnboarding()) {
return response()->json([
'code' => 'ONBOARDING_REQUIRED',
], 409);
}
return $next($request);
}
}
@@ -0,0 +1,82 @@
<?php
namespace App\Http\Requests;
use App\Enums\PacePreference;
use App\Enums\PhysicalActivityLevel;
use App\Enums\UserSex;
use App\Enums\WeightGoal;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;
use Illuminate\Validation\Validator;
class CompleteNutritionOnboardingRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*/
public function authorize(): bool
{
return $this->user() !== null;
}
/**
* Get the validation rules that apply to the request.
*
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
*/
public function rules(): array
{
return [
'dateOfBirth' => [
'required',
'date_format:Y-m-d',
'before_or_equal:'.now()->subYears(18)->toDateString(),
'after_or_equal:'.now()->subYears(100)->toDateString(),
],
'sex' => ['required', Rule::in([UserSex::MAN->value, UserSex::WOMAN->value])],
'heightCm' => ['required', 'integer', 'min:120', 'max:230'],
'weightKg' => ['required', 'numeric', 'min:35', 'max:300'],
'targetWeightKg' => ['nullable', 'numeric', 'min:35', 'max:300'],
'physicalActivityLevel' => ['required', Rule::enum(PhysicalActivityLevel::class)],
'weightGoal' => ['required', Rule::enum(WeightGoal::class)],
'pacePreference' => ['required', Rule::enum(PacePreference::class)],
'nutritionEstimateAccepted' => ['required', 'accepted'],
];
}
public function after(): array
{
return [
function (Validator $validator): void {
$weight = $this->float('weightKg');
$targetWeight = $this->input('targetWeightKg');
$goal = $this->string('weightGoal')->toString();
if ($targetWeight === null || $targetWeight === '') {
return;
}
$targetWeight = (float) $targetWeight;
if ($goal === WeightGoal::LOSE_WEIGHT->value && $targetWeight >= $weight) {
$validator->errors()->add('targetWeightKg', 'NUTRITION_TARGET_WEIGHT_MUST_BE_LOWER');
}
if ($goal === WeightGoal::GAIN_WEIGHT->value && $targetWeight <= $weight) {
$validator->errors()->add('targetWeightKg', 'NUTRITION_TARGET_WEIGHT_MUST_BE_HIGHER');
}
},
];
}
public function messages(): array
{
return [
'dateOfBirth.before_or_equal' => 'NUTRITION_ADULTS_ONLY',
'dateOfBirth.after_or_equal' => 'NUTRITION_BIRTH_DATE_OUT_OF_RANGE',
'sex.in' => 'NUTRITION_METABOLIC_SEX_REQUIRED',
'nutritionEstimateAccepted.accepted' => 'NUTRITION_ESTIMATE_ACCEPTANCE_REQUIRED',
];
}
}
@@ -0,0 +1,28 @@
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class ListWeightEntriesRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*/
public function authorize(): bool
{
return $this->user()?->hasCompletedNutritionOnboarding() === true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
*/
public function rules(): array
{
return [
'days' => ['sometimes', 'integer', 'min:7', 'max:3650'],
];
}
}
-9
View File
@@ -2,10 +2,6 @@
namespace App\Http\Requests; namespace App\Http\Requests;
use App\Enums\PacePreference;
use App\Enums\PhysicalActivityLevel;
use App\Enums\UserSex;
use App\Enums\WeightGoal;
use App\Http\Requests\Concerns\ModeratesUserContent; use App\Http\Requests\Concerns\ModeratesUserContent;
use Illuminate\Foundation\Http\FormRequest; use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule; use Illuminate\Validation\Rule;
@@ -56,11 +52,6 @@ class RegisterRequest extends FormRequest
'termsAccepted' => ['required', 'accepted'], 'termsAccepted' => ['required', 'accepted'],
'avatar' => ['sometimes', 'nullable', 'image', 'max:2048'], 'avatar' => ['sometimes', 'nullable', 'image', 'max:2048'],
'bio' => ['nullable', 'string'], 'bio' => ['nullable', 'string'],
'physicalActivityLevel' => ['sometimes', Rule::enum(PhysicalActivityLevel::class)],
'weightGoal' => ['sometimes', Rule::enum(WeightGoal::class)],
'pacePreference' => ['sometimes', Rule::enum(PacePreference::class)],
'sex' => ['sometimes', Rule::enum(UserSex::class)],
'dateOfBirth' => ['sometimes', 'nullable', 'date_format:Y-m-d', 'before:today', 'after:1900-01-01'],
]; ];
} }
@@ -0,0 +1,29 @@
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class StoreWeightEntryRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*/
public function authorize(): bool
{
return $this->user()?->hasCompletedNutritionOnboarding() === true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
*/
public function rules(): array
{
return [
'weightKg' => ['required', 'numeric', 'min:35', 'max:300'],
'measuredAt' => ['sometimes', 'nullable', 'date', 'before_or_equal:now'],
];
}
}
@@ -0,0 +1,14 @@
<?php
namespace App\Http\Requests;
class UpdateNutritionProfileRequest extends CompleteNutritionOnboardingRequest
{
public function rules(): array
{
$rules = parent::rules();
unset($rules['nutritionEstimateAccepted']);
return $rules;
}
}
+9 -20
View File
@@ -2,10 +2,6 @@
namespace App\Http\Requests; namespace App\Http\Requests;
use App\Enums\PacePreference;
use App\Enums\PhysicalActivityLevel;
use App\Enums\UserSex;
use App\Enums\WeightGoal;
use App\Http\Requests\Concerns\ModeratesUserContent; use App\Http\Requests\Concerns\ModeratesUserContent;
use Illuminate\Foundation\Http\FormRequest; use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule; use Illuminate\Validation\Rule;
@@ -22,27 +18,24 @@ class UpdateUserRequest extends FormRequest
public function rules(): array public function rules(): array
{ {
return [ return [
'name' => [
'sometimes',
'string',
'min:3',
'max:32',
'regex:/^[a-zA-Z0-9_.-]+$/',
Rule::unique('users', 'name')->ignore($this->user()),
],
'bio' => ['sometimes', 'nullable', 'string'], 'bio' => ['sometimes', 'nullable', 'string'],
'height' => ['sometimes', 'nullable', 'integer'],
'locale' => ['sometimes', 'string', Rule::in(config('app.supported_locales', ['fr', 'en']))], 'locale' => ['sometimes', 'string', Rule::in(config('app.supported_locales', ['fr', 'en']))],
'avatar' => ['sometimes', 'nullable', 'image', 'max:2048'], 'avatar' => ['sometimes', 'nullable', 'image', 'max:2048'],
'nutritionGoals' => ['sometimes', 'array'],
'nutritionGoals.calories' => ['required_with:nutritionGoals', 'integer', 'min:0', 'max:100000'],
'nutritionGoals.proteins' => ['required_with:nutritionGoals', 'numeric', 'min:0', 'max:10000'],
'nutritionGoals.carbs' => ['required_with:nutritionGoals', 'numeric', 'min:0', 'max:10000'],
'nutritionGoals.fats' => ['required_with:nutritionGoals', 'numeric', 'min:0', 'max:10000'],
'physicalActivityLevel' => ['sometimes', Rule::enum(PhysicalActivityLevel::class)],
'weightGoal' => ['sometimes', Rule::enum(WeightGoal::class)],
'pacePreference' => ['sometimes', Rule::enum(PacePreference::class)],
'sex' => ['sometimes', Rule::enum(UserSex::class)],
'dateOfBirth' => ['sometimes', 'nullable', 'date_format:Y-m-d', 'before:today', 'after:1900-01-01'],
]; ];
} }
public function after(): array public function after(): array
{ {
return $this->moderationChecks( return $this->moderationChecks(
[$this->input('bio')], [$this->input('name'), $this->input('bio')],
[$this->file('avatar')], [$this->file('avatar')],
); );
} }
@@ -51,10 +44,6 @@ class UpdateUserRequest extends FormRequest
{ {
return [ return [
'avatar.max' => __('api.validation.image_max_2mb'), 'avatar.max' => __('api.validation.image_max_2mb'),
'nutritionGoals.calories.integer' => __('api.validation.calories_integer'),
'nutritionGoals.*.required_with' => __('api.validation.nutrition_goals_required'),
'nutritionGoals.*.min' => __('api.validation.nutrition_goals_positive'),
'nutritionGoals.*.max' => __('api.validation.nutrition_goals_max'),
]; ];
} }
@@ -0,0 +1,36 @@
<?php
namespace App\Http\Resources;
use App\Models\NutritionPlan;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
/** @mixin NutritionPlan */
class NutritionPlanResource extends JsonResource
{
/**
* Transform the resource into an array.
*
* @return array<string, mixed>
*/
public function toArray(Request $request): array
{
return [
'id' => $this->id,
'source' => $this->source,
'formulaVersion' => $this->formula_version,
'calories' => $this->calories,
'proteins' => $this->proteins,
'carbs' => $this->carbs,
'fats' => $this->fats,
'restingMetabolism' => $this->resting_metabolism,
'maintenanceCalories' => $this->maintenance_calories,
'calorieAdjustmentPercent' => $this->calorie_adjustment_percent,
'calculationDetails' => $this->calculation_details,
'effectiveFrom' => $this->effective_from?->toISOString(),
'effectiveUntil' => $this->effective_until?->toISOString(),
'isActive' => $this->is_active,
];
}
}
+22 -10
View File
@@ -15,14 +15,19 @@ class UserResource extends JsonResource
*/ */
public function toArray(Request $request): array public function toArray(Request $request): array
{ {
$this->resource->loadMissing(['activeNutritionPlan', 'latestWeightEntry']);
$nutritionPlan = $this->activeNutritionPlan;
return [ return [
'id' => $this->id, // à voir si suppression par la suite 'id' => $this->id,
'name' => $this->name, 'name' => $this->name,
'email' => $this->email, 'email' => $this->email,
'locale' => $this->locale, 'locale' => $this->locale,
'emailVerified' => $this->hasVerifiedEmail(), 'emailVerified' => $this->hasVerifiedEmail(),
'emailVerifiedAt' => $this->email_verified_at?->toISOString(), 'emailVerifiedAt' => $this->email_verified_at?->toISOString(),
'height' => $this->height, 'height' => $this->height,
'currentWeight' => $this->latestWeightEntry?->weight_kg,
'targetWeight' => $this->target_weight,
'avatarUrl' => $this->avatar_url ? asset(Storage::url($this->avatar_url)) : null, 'avatarUrl' => $this->avatar_url ? asset(Storage::url($this->avatar_url)) : null,
'bio' => $this->bio, 'bio' => $this->bio,
'role' => $this->role, 'role' => $this->role,
@@ -38,20 +43,27 @@ class UserResource extends JsonResource
'hasActiveSubscription' => $this->hasActiveSubscription(), 'hasActiveSubscription' => $this->hasActiveSubscription(),
'suspendedAt' => $this->suspended_at?->toISOString(), 'suspendedAt' => $this->suspended_at?->toISOString(),
'physicalActivityLevel' => $this->physical_activity_level, 'physicalActivityLevel' => $this->physical_activity_level,
'physicalActivityLevelLabel' => $this->physical_activity_level?->getLabel(),
'weightGoal' => $this->weight_goal, 'weightGoal' => $this->weight_goal,
'weightGoalLabel' => $this->weight_goal?->getLabel(),
'pacePreference' => $this->pace_preference, 'pacePreference' => $this->pace_preference,
'pacePreferenceLabel' => $this->pace_preference?->getLabel(),
'sex' => $this->sex, 'sex' => $this->sex,
'sexLabel' => $this->sex?->getLabel(),
'dateOfBirth' => $this->date_of_birth?->toDateString(), 'dateOfBirth' => $this->date_of_birth?->toDateString(),
'nutritionGoals' => [ 'onboardingStatus' => $this->hasCompletedNutritionOnboarding() ? 'completed' : 'required',
'calories' => (int) $this->daily_calorie_goal, 'missingOnboardingFields' => $this->hasCompletedNutritionOnboarding() ? [] : [
'proteins' => (float) $this->daily_protein_goal, 'dateOfBirth',
'carbs' => (float) $this->daily_carbs_goal, 'sex',
'fats' => (float) $this->daily_fats_goal, 'heightCm',
'weightKg',
'physicalActivityLevel',
'weightGoal',
'pacePreference',
], ],
'nutritionPlan' => NutritionPlanResource::make($nutritionPlan),
'nutritionGoals' => $nutritionPlan ? [
'calories' => $nutritionPlan->calories,
'proteins' => $nutritionPlan->proteins,
'carbs' => $nutritionPlan->carbs,
'fats' => $nutritionPlan->fats,
] : null,
]; ];
} }
} }
@@ -0,0 +1,42 @@
<?php
namespace App\Http\Resources;
use App\Models\WeeklyReview;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
/** @mixin WeeklyReview */
class WeeklyReviewResource extends JsonResource
{
/**
* Transform the resource into an array.
*
* @return array<string, mixed>
*/
public function toArray(Request $request): array
{
return [
'id' => $this->id,
'periodStart' => $this->period_start?->toDateString(),
'periodEnd' => $this->period_end?->toDateString(),
'averages' => [
'calories' => $this->average_calories,
'proteins' => $this->average_proteins,
'carbs' => $this->average_carbs,
'fats' => $this->average_fats,
],
'loggedDaysCount' => $this->logged_days_count,
'weightEntriesCount' => $this->weight_entries_count,
'weightTrendKg' => $this->weight_trend_kg,
'targetWeightTrendKg' => $this->target_weight_trend_kg,
'status' => $this->status,
'recommendedCalorieAdjustment' => $this->recommended_calorie_adjustment,
'messageCode' => $this->message_code,
'metrics' => $this->metrics,
'generatedAt' => $this->generated_at?->toISOString(),
'acceptedAt' => $this->accepted_at?->toISOString(),
'acceptedNutritionPlanId' => $this->accepted_nutrition_plan_id,
];
}
}
@@ -0,0 +1,27 @@
<?php
namespace App\Http\Resources;
use App\Models\WeightEntry;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
/** @mixin WeightEntry */
class WeightEntryResource extends JsonResource
{
/**
* Transform the resource into an array.
*
* @return array<string, mixed>
*/
public function toArray(Request $request): array
{
return [
'id' => $this->id,
'weightKg' => $this->weight_kg,
'source' => $this->source,
'measuredAt' => $this->measured_at?->toISOString(),
'createdAt' => $this->created_at?->toISOString(),
];
}
}
+65
View File
@@ -0,0 +1,65 @@
<?php
namespace App\Models;
use App\Enums\NutritionPlanSource;
use Illuminate\Database\Eloquent\Concerns\HasUlids;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
class NutritionPlan extends Model
{
/** @use HasFactory<\Database\Factories\NutritionPlanFactory> */
use HasFactory, HasUlids;
protected $fillable = [
'user_id',
'source',
'formula_version',
'calories',
'proteins',
'carbs',
'fats',
'resting_metabolism',
'maintenance_calories',
'calorie_adjustment_percent',
'calculation_details',
'is_active',
'effective_from',
'effective_until',
];
protected $attributes = [
'is_active' => true,
];
protected function casts(): array
{
return [
'source' => NutritionPlanSource::class,
'calories' => 'integer',
'proteins' => 'float',
'carbs' => 'float',
'fats' => 'float',
'resting_metabolism' => 'integer',
'maintenance_calories' => 'integer',
'calorie_adjustment_percent' => 'float',
'calculation_details' => 'array',
'is_active' => 'boolean',
'effective_from' => 'immutable_datetime',
'effective_until' => 'immutable_datetime',
];
}
public function user(): BelongsTo
{
return $this->belongsTo(User::class);
}
public function weeklyReviews(): HasMany
{
return $this->hasMany(WeeklyReview::class);
}
}
+39 -8
View File
@@ -51,6 +51,7 @@ class User extends Authenticatable implements FilamentUser, HasAvatar, HasLocale
'password', 'password',
'avatar_url', 'avatar_url',
'height', 'height',
'target_weight',
'bio', 'bio',
'account_verified_at', 'account_verified_at',
'certification_purchased_at', 'certification_purchased_at',
@@ -58,15 +59,13 @@ class User extends Authenticatable implements FilamentUser, HasAvatar, HasLocale
'subscription_store', 'subscription_store',
'subscription_expires_at', 'subscription_expires_at',
'subscription_is_trial', 'subscription_is_trial',
'daily_calorie_goal',
'daily_protein_goal',
'daily_carbs_goal',
'daily_fats_goal',
'physical_activity_level', 'physical_activity_level',
'weight_goal', 'weight_goal',
'pace_preference', 'pace_preference',
'sex', 'sex',
'date_of_birth', 'date_of_birth',
'onboarding_completed_at',
'nutrition_estimate_accepted_at',
'terms_accepted_at', 'terms_accepted_at',
'social_notifications_enabled', 'social_notifications_enabled',
'engagement_reminders_enabled', 'engagement_reminders_enabled',
@@ -97,16 +96,16 @@ class User extends Authenticatable implements FilamentUser, HasAvatar, HasLocale
'account_verified_at' => 'datetime', 'account_verified_at' => 'datetime',
'certification_purchased_at' => 'datetime', 'certification_purchased_at' => 'datetime',
'password' => 'hashed', 'password' => 'hashed',
'daily_calorie_goal' => 'integer', 'height' => 'integer',
'daily_protein_goal' => 'float', 'target_weight' => 'float',
'daily_carbs_goal' => 'float',
'daily_fats_goal' => 'float',
'physical_activity_level' => PhysicalActivityLevel::class, 'physical_activity_level' => PhysicalActivityLevel::class,
'weight_goal' => WeightGoal::class, 'weight_goal' => WeightGoal::class,
'pace_preference' => PacePreference::class, 'pace_preference' => PacePreference::class,
'sex' => UserSex::class, 'sex' => UserSex::class,
'role' => UserRole::class, 'role' => UserRole::class,
'date_of_birth' => 'immutable_date', 'date_of_birth' => 'immutable_date',
'onboarding_completed_at' => 'datetime',
'nutrition_estimate_accepted_at' => 'datetime',
'terms_accepted_at' => 'datetime', 'terms_accepted_at' => 'datetime',
'social_notifications_enabled' => 'boolean', 'social_notifications_enabled' => 'boolean',
'engagement_reminders_enabled' => 'boolean', 'engagement_reminders_enabled' => 'boolean',
@@ -146,6 +145,38 @@ class User extends Authenticatable implements FilamentUser, HasAvatar, HasLocale
return $this->hasMany(WorkoutSessions::class); return $this->hasMany(WorkoutSessions::class);
} }
public function nutritionPlans(): HasMany
{
return $this->hasMany(NutritionPlan::class);
}
public function activeNutritionPlan(): HasOne
{
return $this->hasOne(NutritionPlan::class)
->where('is_active', true)
->latestOfMany('effective_from');
}
public function weightEntries(): HasMany
{
return $this->hasMany(WeightEntry::class);
}
public function latestWeightEntry(): HasOne
{
return $this->hasOne(WeightEntry::class)->latestOfMany('measured_at');
}
public function weeklyReviews(): HasMany
{
return $this->hasMany(WeeklyReview::class);
}
public function hasCompletedNutritionOnboarding(): bool
{
return $this->onboarding_completed_at !== null;
}
public function stravaConnection(): HasOne public function stravaConnection(): HasOne
{ {
return $this->hasOne(StravaConnection::class); return $this->hasOne(StravaConnection::class);
+73
View File
@@ -0,0 +1,73 @@
<?php
namespace App\Models;
use App\Enums\WeeklyReviewStatus;
use Illuminate\Database\Eloquent\Concerns\HasUlids;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
class WeeklyReview extends Model
{
/** @use HasFactory<\Database\Factories\WeeklyReviewFactory> */
use HasFactory, HasUlids;
protected $fillable = [
'user_id',
'nutrition_plan_id',
'period_start',
'period_end',
'average_calories',
'average_proteins',
'average_carbs',
'average_fats',
'logged_days_count',
'weight_entries_count',
'weight_trend_kg',
'target_weight_trend_kg',
'status',
'recommended_calorie_adjustment',
'message_code',
'metrics',
'generated_at',
'accepted_at',
'accepted_nutrition_plan_id',
];
protected function casts(): array
{
return [
'period_start' => 'immutable_date',
'period_end' => 'immutable_date',
'average_calories' => 'float',
'average_proteins' => 'float',
'average_carbs' => 'float',
'average_fats' => 'float',
'logged_days_count' => 'integer',
'weight_entries_count' => 'integer',
'weight_trend_kg' => 'float',
'target_weight_trend_kg' => 'float',
'status' => WeeklyReviewStatus::class,
'recommended_calorie_adjustment' => 'integer',
'metrics' => 'array',
'generated_at' => 'immutable_datetime',
'accepted_at' => 'immutable_datetime',
];
}
public function user(): BelongsTo
{
return $this->belongsTo(User::class);
}
public function nutritionPlan(): BelongsTo
{
return $this->belongsTo(NutritionPlan::class);
}
public function acceptedNutritionPlan(): BelongsTo
{
return $this->belongsTo(NutritionPlan::class, 'accepted_nutrition_plan_id');
}
}
+38
View File
@@ -0,0 +1,38 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Concerns\HasUlids;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
class WeightEntry extends Model
{
/** @use HasFactory<\Database\Factories\WeightEntryFactory> */
use HasFactory, HasUlids;
protected $fillable = [
'user_id',
'weight_kg',
'source',
'measured_at',
];
protected $attributes = [
'source' => 'manual',
];
protected function casts(): array
{
return [
'weight_kg' => 'float',
'measured_at' => 'immutable_datetime',
];
}
public function user(): BelongsTo
{
return $this->belongsTo(User::class);
}
}
+128
View File
@@ -0,0 +1,128 @@
<?php
namespace App\Services;
use App\Enums\PacePreference;
use App\Enums\PhysicalActivityLevel;
use App\Enums\UserSex;
use App\Enums\WeightGoal;
use Carbon\CarbonInterface;
use InvalidArgumentException;
class NutritionPlanCalculator
{
public const FORMULA_VERSION = 'mifflin_st_jeor_v1';
/**
* @return array{
* formula_version: string,
* calories: int,
* proteins: float,
* carbs: float,
* fats: float,
* resting_metabolism: int,
* maintenance_calories: int,
* calorie_adjustment_percent: float,
* calculation_details: array<string, float|int|string>
* }
*/
public function calculate(
float $weightKg,
int $heightCm,
CarbonInterface $dateOfBirth,
UserSex $sex,
PhysicalActivityLevel $activityLevel,
WeightGoal $weightGoal,
PacePreference $pacePreference,
): array {
$sexCoefficient = match ($sex) {
UserSex::MAN => 5,
UserSex::WOMAN => -161,
default => throw new InvalidArgumentException('A metabolic calculation reference is required.'),
};
$age = (int) $dateOfBirth->diffInYears(now());
$restingMetabolism = (int) round(
(10 * $weightKg) + (6.25 * $heightCm) - (5 * $age) + $sexCoefficient
);
$activityFactor = $this->activityFactor($activityLevel);
$maintenanceCalories = (int) (round(($restingMetabolism * $activityFactor) / 10) * 10);
$adjustmentPercent = $this->calorieAdjustmentPercent($weightGoal, $pacePreference);
$unboundedCalories = (int) (round(($maintenanceCalories * (1 + $adjustmentPercent)) / 10) * 10);
$minimumCalories = max(1200, (int) (ceil(($restingMetabolism * 0.8) / 10) * 10));
$calorieTarget = max($minimumCalories, $unboundedCalories);
$heightMeters = $heightCm / 100;
$proteinReferenceWeight = min($weightKg, 25 * ($heightMeters ** 2));
$proteinFactor = $this->proteinFactor($activityLevel, $weightGoal);
$proteins = max(1, (int) round($proteinReferenceWeight * $proteinFactor));
$fats = max(1, (int) round(($calorieTarget * 0.25) / 9));
$carbs = max(0, (int) round(($calorieTarget - ($proteins * 4) - ($fats * 9)) / 4));
$macroCalories = ($proteins * 4) + ($carbs * 4) + ($fats * 9);
return [
'formula_version' => self::FORMULA_VERSION,
'calories' => $macroCalories,
'proteins' => $proteins,
'carbs' => $carbs,
'fats' => $fats,
'resting_metabolism' => $restingMetabolism,
'maintenance_calories' => $maintenanceCalories,
'calorie_adjustment_percent' => $adjustmentPercent,
'calculation_details' => [
'activityFactor' => $activityFactor,
'ageAtCalculation' => $age,
'heightCm' => $heightCm,
'inputWeightKg' => round($weightKg, 2),
'minimumCalories' => $minimumCalories,
'pacePreference' => $pacePreference->value,
'proteinFactor' => $proteinFactor,
'proteinReferenceWeightKg' => round($proteinReferenceWeight, 2),
'sexReference' => $sex->value,
'weightGoal' => $weightGoal->value,
],
];
}
private function activityFactor(PhysicalActivityLevel $activityLevel): float
{
return match ($activityLevel) {
PhysicalActivityLevel::SEDENTARY => 1.2,
PhysicalActivityLevel::LIGHTLY_ACTIVE => 1.375,
PhysicalActivityLevel::MODERATELY_ACTIVE => 1.55,
PhysicalActivityLevel::VERY_ACTIVE => 1.725,
PhysicalActivityLevel::ATHLETE => 1.9,
};
}
private function calorieAdjustmentPercent(WeightGoal $weightGoal, PacePreference $pacePreference): float
{
if ($weightGoal === WeightGoal::MAINTAIN_WEIGHT) {
return 0;
}
$magnitude = match ($pacePreference) {
PacePreference::SLOW => $weightGoal === WeightGoal::LOSE_WEIGHT ? 0.10 : 0.05,
PacePreference::NORMAL => $weightGoal === WeightGoal::LOSE_WEIGHT ? 0.15 : 0.10,
PacePreference::FAST => $weightGoal === WeightGoal::LOSE_WEIGHT ? 0.20 : 0.15,
};
return $weightGoal === WeightGoal::LOSE_WEIGHT ? -$magnitude : $magnitude;
}
private function proteinFactor(PhysicalActivityLevel $activityLevel, WeightGoal $weightGoal): float
{
$factor = match ($activityLevel) {
PhysicalActivityLevel::SEDENTARY => 1.2,
PhysicalActivityLevel::LIGHTLY_ACTIVE => 1.4,
PhysicalActivityLevel::MODERATELY_ACTIVE => 1.6,
PhysicalActivityLevel::VERY_ACTIVE, PhysicalActivityLevel::ATHLETE => 1.8,
};
$factor += match ($weightGoal) {
WeightGoal::LOSE_WEIGHT => 0.2,
WeightGoal::GAIN_WEIGHT => 0.1,
WeightGoal::MAINTAIN_WEIGHT => 0,
};
return min($factor, 2.0);
}
}
+227
View File
@@ -0,0 +1,227 @@
<?php
namespace App\Services;
use App\Enums\PacePreference;
use App\Enums\WeeklyReviewStatus;
use App\Enums\WeightGoal;
use App\Models\MealPosts;
use App\Models\User;
use App\Models\WeeklyReview;
use Carbon\CarbonImmutable;
use Carbon\CarbonInterface;
use Illuminate\Support\Collection;
class WeeklyReviewService
{
/**
* Generate or refresh a review for the last completed week.
*/
public function generate(User $user, ?CarbonImmutable $periodStart = null): WeeklyReview
{
$periodStart ??= CarbonImmutable::now()->subWeek()->startOfWeek(CarbonInterface::MONDAY);
$periodStart = $periodStart->startOfDay();
$periodEnd = $periodStart->endOfWeek(CarbonInterface::SUNDAY);
$existingReview = $user->weeklyReviews()->whereDate('period_start', $periodStart)->first();
if ($existingReview?->accepted_at !== null) {
return $existingReview;
}
$currentPlan = $user->nutritionPlans()->where('is_active', true)->latest('effective_from')->firstOrFail();
$nutritionPlan = $user->nutritionPlans()
->where('effective_from', '<=', $periodEnd)
->latest('effective_from')
->first()
?? $currentPlan;
$meals = MealPosts::query()
->where('user_id', $user->getKey())
->whereBetween('eaten_at', [$periodStart, $periodEnd])
->get(['calories', 'proteins', 'carbs', 'fats', 'eaten_at']);
$loggedDaysCount = $meals->pluck('eaten_at')
->map(fn (CarbonInterface $date): string => $date->toDateString())
->unique()
->count();
$averages = $this->nutritionAverages($meals, $loggedDaysCount);
$weights = $user->weightEntries()
->whereBetween('measured_at', [$periodStart->subWeek(), $periodEnd])
->oldest('measured_at')
->get();
$trend = $this->weightTrend($weights);
$latestWeight = $weights->last()?->weight_kg ?? $user->latestWeightEntry()->value('weight_kg');
$targetTrend = $latestWeight === null ? null : $this->targetWeightTrend(
(float) $latestWeight,
$user->weight_goal,
$user->pace_preference,
);
[$status, $adjustment, $messageCode] = $this->recommendation(
loggedDaysCount: $loggedDaysCount,
weightCount: $weights->count(),
trend: $trend,
targetTrend: $targetTrend,
averageCalories: $averages['calories'],
planCalories: $nutritionPlan->calories,
weightGoal: $user->weight_goal,
);
if ($nutritionPlan->getKey() !== $currentPlan->getKey()) {
$status = WeeklyReviewStatus::INSUFFICIENT_DATA;
$adjustment = 0;
$messageCode = 'WEEKLY_REVIEW_PLAN_CHANGED';
}
return $user->weeklyReviews()->updateOrCreate(
['period_start' => $periodStart->toDateString()],
[
'nutrition_plan_id' => $nutritionPlan->getKey(),
'period_end' => $periodEnd->toDateString(),
'average_calories' => $averages['calories'],
'average_proteins' => $averages['proteins'],
'average_carbs' => $averages['carbs'],
'average_fats' => $averages['fats'],
'logged_days_count' => $loggedDaysCount,
'weight_entries_count' => $weights->count(),
'weight_trend_kg' => $trend,
'target_weight_trend_kg' => $targetTrend,
'status' => $status,
'recommended_calorie_adjustment' => $adjustment,
'message_code' => $messageCode,
'metrics' => [
'analysisStart' => $periodStart->subWeek()->toDateString(),
'analysisEnd' => $periodEnd->toDateString(),
'firstWeightKg' => $weights->first()?->weight_kg,
'lastWeightKg' => $weights->last()?->weight_kg,
'nutritionPlanCalories' => $nutritionPlan->calories,
],
'generated_at' => now(),
],
);
}
/**
* @param Collection<int, MealPosts> $meals
* @return array{calories: float, proteins: float, carbs: float, fats: float}
*/
private function nutritionAverages(Collection $meals, int $loggedDaysCount): array
{
$divisor = max(1, $loggedDaysCount);
return [
'calories' => round((float) $meals->sum('calories') / $divisor, 2),
'proteins' => round((float) $meals->sum('proteins') / $divisor, 2),
'carbs' => round((float) $meals->sum('carbs') / $divisor, 2),
'fats' => round((float) $meals->sum('fats') / $divisor, 2),
];
}
private function weightTrend(Collection $weights): ?float
{
$first = $weights->first();
$last = $weights->last();
if ($weights->count() < 4 || $first === null || $last === null) {
return null;
}
$days = max(1, $first->measured_at->diffInDays($last->measured_at));
if ($days < 7) {
return null;
}
return round((($last->weight_kg - $first->weight_kg) / $days) * 7, 2);
}
private function targetWeightTrend(
float $weightKg,
WeightGoal $weightGoal,
PacePreference $pacePreference,
): float {
$percentage = match ($weightGoal) {
WeightGoal::LOSE_WEIGHT => match ($pacePreference) {
PacePreference::SLOW => -0.0025,
PacePreference::NORMAL => -0.005,
PacePreference::FAST => -0.0075,
},
WeightGoal::GAIN_WEIGHT => match ($pacePreference) {
PacePreference::SLOW => 0.00125,
PacePreference::NORMAL => 0.0025,
PacePreference::FAST => 0.00375,
},
WeightGoal::MAINTAIN_WEIGHT => 0,
};
return round($weightKg * $percentage, 2);
}
/**
* @return array{WeeklyReviewStatus, int, string}
*/
private function recommendation(
int $loggedDaysCount,
int $weightCount,
?float $trend,
?float $targetTrend,
float $averageCalories,
int $planCalories,
WeightGoal $weightGoal,
): array {
if ($loggedDaysCount < 5 || $weightCount < 4 || $trend === null || $targetTrend === null) {
return [
WeeklyReviewStatus::INSUFFICIENT_DATA,
0,
'WEEKLY_REVIEW_INSUFFICIENT_TRACKING',
];
}
$calorieDifferenceRatio = abs($averageCalories - $planCalories) / max(1, $planCalories);
if ($calorieDifferenceRatio > 0.15) {
return [
WeeklyReviewStatus::INSUFFICIENT_DATA,
0,
'WEEKLY_REVIEW_INSUFFICIENT_ADHERENCE',
];
}
$tolerance = $weightGoal === WeightGoal::MAINTAIN_WEIGHT
? 0.2
: max(0.1, abs($targetTrend) * 0.35);
$difference = $trend - $targetTrend;
if (abs($difference) <= $tolerance) {
return [
WeeklyReviewStatus::ON_TRACK,
0,
'WEEKLY_REVIEW_ON_TRACK',
];
}
$adjustment = match ($weightGoal) {
WeightGoal::LOSE_WEIGHT => $difference > 0 ? -100 : 100,
WeightGoal::GAIN_WEIGHT => $difference < 0 ? 100 : -100,
WeightGoal::MAINTAIN_WEIGHT => $trend > 0 ? -100 : 100,
};
return [
WeeklyReviewStatus::ADJUSTMENT_RECOMMENDED,
$adjustment,
$this->adjustmentMessage($weightGoal, $adjustment),
];
}
private function adjustmentMessage(WeightGoal $weightGoal, int $adjustment): string
{
return match ($weightGoal) {
WeightGoal::LOSE_WEIGHT => $adjustment < 0
? 'WEEKLY_REVIEW_LOSS_SLOWER'
: 'WEEKLY_REVIEW_LOSS_FASTER',
WeightGoal::GAIN_WEIGHT => $adjustment > 0
? 'WEEKLY_REVIEW_GAIN_SLOWER'
: 'WEEKLY_REVIEW_GAIN_FASTER',
WeightGoal::MAINTAIN_WEIGHT => $adjustment > 0
? 'WEEKLY_REVIEW_MAINTENANCE_DOWN'
: 'WEEKLY_REVIEW_MAINTENANCE_UP',
};
}
}
+84 -2
View File
@@ -1,11 +1,16 @@
<?php <?php
use Illuminate\Auth\Access\AuthorizationException;
use Illuminate\Auth\AuthenticationException; use Illuminate\Auth\AuthenticationException;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Foundation\Application; use Illuminate\Foundation\Application;
use Illuminate\Foundation\Configuration\Exceptions; use Illuminate\Foundation\Configuration\Exceptions;
use Illuminate\Foundation\Configuration\Middleware; use Illuminate\Foundation\Configuration\Middleware;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use Illuminate\Routing\Exceptions\InvalidSignatureException; use Illuminate\Routing\Exceptions\InvalidSignatureException;
use Illuminate\Support\Str;
use Illuminate\Validation\ValidationException;
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
return Application::configure(basePath: dirname(__DIR__)) return Application::configure(basePath: dirname(__DIR__))
->withRouting( ->withRouting(
@@ -20,6 +25,7 @@ return Application::configure(basePath: dirname(__DIR__))
$middleware->alias([ $middleware->alias([
'verified' => \App\Http\Middleware\EnsureEmailIsVerified::class, 'verified' => \App\Http\Middleware\EnsureEmailIsVerified::class,
'not_suspended' => \App\Http\Middleware\EnsureAccountIsNotSuspended::class, 'not_suspended' => \App\Http\Middleware\EnsureAccountIsNotSuspended::class,
'onboarded' => \App\Http\Middleware\EnsureNutritionOnboardingCompleted::class,
]); ]);
}) })
->withExceptions(function (Exceptions $exceptions): void { ->withExceptions(function (Exceptions $exceptions): void {
@@ -29,7 +35,7 @@ return Application::configure(basePath: dirname(__DIR__))
} }
return response()->json([ return response()->json([
'message' => __('api.auth.unauthenticated'), 'code' => 'UNAUTHENTICATED',
], 401); ], 401);
}); });
@@ -39,7 +45,83 @@ return Application::configure(basePath: dirname(__DIR__))
} }
return response()->json([ return response()->json([
'message' => __('api.auth.invalid_verification_link'), 'code' => 'INVALID_VERIFICATION_LINK',
], 403); ], 403);
}); });
$exceptions->render(function (ValidationException $exception, Request $request) {
if (! $request->expectsJson()) {
return null;
}
$failedRules = $exception->validator->failed();
$errors = collect($exception->errors())->mapWithKeys(
function (array $messages, string $attribute) use ($failedRules): array {
$explicitCodes = collect($messages)
->filter(fn (string $message): bool => preg_match('/^[A-Z][A-Z0-9_]+$/', $message) === 1)
->values();
if ($explicitCodes->isNotEmpty()) {
return [$attribute => $explicitCodes->all()];
}
$codes = collect(array_keys($failedRules[$attribute] ?? []))
->map(fn (string $rule): string => 'VALIDATION_'.Str::upper(Str::snake(class_basename($rule))))
->values()
->all();
return [$attribute => $codes !== [] ? $codes : ['VALIDATION_INVALID']];
}
);
return response()->json([
'code' => 'VALIDATION_ERROR',
'errors' => $errors,
], $exception->status);
});
$exceptions->render(function (AuthorizationException $exception, Request $request) {
if (! $request->expectsJson()) {
return null;
}
return response()->json(['code' => 'FORBIDDEN'], 403);
});
$exceptions->render(function (ModelNotFoundException $exception, Request $request) {
if (! $request->expectsJson()) {
return null;
}
return response()->json(['code' => 'NOT_FOUND'], 404);
});
$exceptions->render(function (HttpExceptionInterface $exception, Request $request) {
if (! $request->expectsJson()) {
return null;
}
$message = $exception->getMessage();
$code = preg_match('/^[A-Z][A-Z0-9_]+$/', $message) === 1
? $message
: match ($exception->getStatusCode()) {
401 => 'UNAUTHENTICATED',
403 => 'FORBIDDEN',
404 => 'NOT_FOUND',
409 => 'CONFLICT',
422 => 'VALIDATION_ERROR',
429 => 'TOO_MANY_REQUESTS',
default => $exception->getStatusCode() >= 500 ? 'SERVER_ERROR' : 'REQUEST_FAILED',
};
return response()->json(['code' => $code], $exception->getStatusCode());
});
$exceptions->render(function (Throwable $exception, Request $request) {
if (! $request->expectsJson()) {
return null;
}
return response()->json(['code' => 'SERVER_ERROR'], 500);
});
})->create(); })->create();
@@ -0,0 +1,41 @@
<?php
namespace Database\Factories;
use App\Enums\NutritionPlanSource;
use App\Models\User;
use Illuminate\Database\Eloquent\Factories\Factory;
/**
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\NutritionPlan>
*/
class NutritionPlanFactory extends Factory
{
/**
* Define the model's default state.
*
* @return array<string, mixed>
*/
public function definition(): array
{
return [
'user_id' => User::factory(),
'source' => NutritionPlanSource::ONBOARDING,
'formula_version' => 'mifflin_st_jeor_v1',
'calories' => 2200,
'proteins' => 112,
'carbs' => 286,
'fats' => 61,
'resting_metabolism' => 1650,
'maintenance_calories' => 2200,
'calorie_adjustment_percent' => 0,
'calculation_details' => [
'activityFactor' => 1.55,
'inputWeightKg' => 70,
],
'is_active' => true,
'effective_from' => now(),
'effective_until' => null,
];
}
}
+76 -4
View File
@@ -2,7 +2,13 @@
namespace Database\Factories; namespace Database\Factories;
use App\Enums\NutritionPlanSource;
use App\Enums\PacePreference;
use App\Enums\PhysicalActivityLevel;
use App\Enums\UserRole; use App\Enums\UserRole;
use App\Enums\UserSex;
use App\Enums\WeightGoal;
use App\Models\User;
use Illuminate\Database\Eloquent\Factories\Factory; use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Facades\Hash; use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Str; use Illuminate\Support\Str;
@@ -32,14 +38,50 @@ class UserFactory extends Factory
'email_verified_at' => now(), 'email_verified_at' => now(),
'password' => static::$password ??= Hash::make('password'), 'password' => static::$password ??= Hash::make('password'),
'avatar_url' => null, 'avatar_url' => null,
'daily_calorie_goal' => 2000, 'height' => 175,
'daily_protein_goal' => 120, 'target_weight' => 70,
'daily_carbs_goal' => 250, 'date_of_birth' => '1990-01-01',
'daily_fats_goal' => 70, 'sex' => UserSex::MAN,
'physical_activity_level' => PhysicalActivityLevel::MODERATELY_ACTIVE,
'weight_goal' => WeightGoal::MAINTAIN_WEIGHT,
'pace_preference' => PacePreference::NORMAL,
'onboarding_completed_at' => now(),
'nutrition_estimate_accepted_at' => now(),
'remember_token' => Str::random(10), 'remember_token' => Str::random(10),
]; ];
} }
public function configure(): static
{
return $this->afterCreating(function (User $user): void {
if (! $user->hasCompletedNutritionOnboarding()) {
return;
}
$user->weightEntries()->create([
'weight_kg' => 70,
'measured_at' => now(),
]);
$user->nutritionPlans()->create([
'source' => NutritionPlanSource::ONBOARDING,
'formula_version' => 'mifflin_st_jeor_v1',
'calories' => 2200,
'proteins' => 112,
'carbs' => 286,
'fats' => 61,
'resting_metabolism' => 1650,
'maintenance_calories' => 2200,
'calorie_adjustment_percent' => 0,
'calculation_details' => [
'activityFactor' => 1.55,
'inputWeightKg' => 70,
],
'effective_from' => now(),
]);
});
}
/** /**
* Indicate that the model's email address should be unverified. * Indicate that the model's email address should be unverified.
*/ */
@@ -49,4 +91,34 @@ class UserFactory extends Factory
'email_verified_at' => null, 'email_verified_at' => null,
]); ]);
} }
public function onboarded(): static
{
return $this->state(fn (array $attributes): array => [
'height' => 175,
'target_weight' => 70,
'date_of_birth' => '1990-01-01',
'sex' => UserSex::MAN,
'physical_activity_level' => PhysicalActivityLevel::MODERATELY_ACTIVE,
'weight_goal' => WeightGoal::MAINTAIN_WEIGHT,
'pace_preference' => PacePreference::NORMAL,
'onboarding_completed_at' => now(),
'nutrition_estimate_accepted_at' => now(),
]);
}
public function withoutNutritionOnboarding(): static
{
return $this->state(fn (array $attributes): array => [
'height' => null,
'target_weight' => null,
'date_of_birth' => null,
'sex' => null,
'physical_activity_level' => null,
'weight_goal' => null,
'pace_preference' => null,
'onboarding_completed_at' => null,
'nutrition_estimate_accepted_at' => null,
]);
}
} }
@@ -0,0 +1,43 @@
<?php
namespace Database\Factories;
use App\Enums\WeeklyReviewStatus;
use App\Models\User;
use Illuminate\Database\Eloquent\Factories\Factory;
/**
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\WeeklyReview>
*/
class WeeklyReviewFactory extends Factory
{
/**
* Define the model's default state.
*
* @return array<string, mixed>
*/
public function definition(): array
{
return [
'user_id' => User::factory(),
'nutrition_plan_id' => null,
'period_start' => now()->startOfWeek(),
'period_end' => now()->endOfWeek(),
'average_calories' => 2100,
'average_proteins' => 110,
'average_carbs' => 260,
'average_fats' => 70,
'logged_days_count' => 7,
'weight_entries_count' => 4,
'weight_trend_kg' => -0.3,
'target_weight_trend_kg' => -0.35,
'status' => WeeklyReviewStatus::ON_TRACK,
'recommended_calorie_adjustment' => 0,
'message_code' => 'WEEKLY_REVIEW_ON_TRACK',
'metrics' => [],
'generated_at' => now(),
'accepted_at' => null,
'accepted_nutrition_plan_id' => null,
];
}
}
+27
View File
@@ -0,0 +1,27 @@
<?php
namespace Database\Factories;
use App\Models\User;
use Illuminate\Database\Eloquent\Factories\Factory;
/**
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\WeightEntry>
*/
class WeightEntryFactory extends Factory
{
/**
* Define the model's default state.
*
* @return array<string, mixed>
*/
public function definition(): array
{
return [
'user_id' => User::factory(),
'weight_kg' => fake()->randomFloat(2, 45, 120),
'source' => 'manual',
'measured_at' => now(),
];
}
}
@@ -19,20 +19,17 @@ return new class extends Migration
$table->string('password'); $table->string('password');
$table->string('role')->default('user'); $table->string('role')->default('user');
$table->text('bio')->nullable(); $table->text('bio')->nullable();
$table->integer('height')->nullable(); $table->unsignedSmallInteger('height')->nullable();
$table->decimal('target_weight', 5, 2)->nullable();
$table->date('date_of_birth')->nullable(); $table->date('date_of_birth')->nullable();
$table->string('sex')->default('unknown'); $table->string('sex')->nullable();
$table->string('physical_activity_level')->default('sedentary'); $table->string('physical_activity_level')->nullable();
$table->string('weight_goal')->default('lose_weight'); $table->string('weight_goal')->nullable();
$table->string('pace_preference')->default('slow'); $table->string('pace_preference')->nullable();
$table->string('locale', 8)->default('en')->after('email'); $table->string('locale', 8)->default('en')->after('email');
$table->string('avatar_url', 2048)->nullable(); $table->string('avatar_url', 2048)->nullable();
$table->timestampTz('onboarding_completed_at')->nullable()->index();
// Goals $table->timestampTz('nutrition_estimate_accepted_at')->nullable();
$table->unsignedInteger('daily_calorie_goal')->default(2000)->after('avatar_url');
$table->decimal('daily_protein_goal', 8, 2)->default(120)->after('daily_calorie_goal');
$table->decimal('daily_carbs_goal', 8, 2)->default(250)->after('daily_protein_goal');
$table->decimal('daily_fats_goal', 8, 2)->default(70)->after('daily_carbs_goal');
// Moderation // Moderation
$table->timestamp('suspended_at')->nullable()->after('deleted_at')->index(); $table->timestamp('suspended_at')->nullable()->after('deleted_at')->index();
$table->ulid('suspended_by')->nullable()->after('suspended_at')->index(); $table->ulid('suspended_by')->nullable()->after('suspended_at')->index();
@@ -0,0 +1,44 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('nutrition_plans', function (Blueprint $table) {
$table->ulid('id')->primary();
$table->foreignUlid('user_id')->constrained('users')->cascadeOnDelete();
$table->string('source', 32);
$table->string('formula_version', 64);
$table->unsignedInteger('calories');
$table->decimal('proteins', 8, 2);
$table->decimal('carbs', 8, 2);
$table->decimal('fats', 8, 2);
$table->unsignedInteger('resting_metabolism');
$table->unsignedInteger('maintenance_calories');
$table->decimal('calorie_adjustment_percent', 6, 4)->default(0);
$table->json('calculation_details');
$table->boolean('is_active')->default(true);
$table->timestampTz('effective_from');
$table->timestampTz('effective_until')->nullable();
$table->timestampsTz();
$table->index(['user_id', 'is_active']);
$table->index(['user_id', 'effective_from']);
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('nutrition_plans');
}
};
@@ -0,0 +1,33 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('weight_entries', function (Blueprint $table) {
$table->ulid('id')->primary();
$table->foreignUlid('user_id')->constrained('users')->cascadeOnDelete();
$table->decimal('weight_kg', 5, 2);
$table->string('source', 32)->default('manual');
$table->timestampTz('measured_at');
$table->timestampsTz();
$table->index(['user_id', 'measured_at']);
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('weight_entries');
}
};
@@ -0,0 +1,49 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('weekly_reviews', function (Blueprint $table) {
$table->ulid('id')->primary();
$table->foreignUlid('user_id')->constrained('users')->cascadeOnDelete();
$table->foreignUlid('nutrition_plan_id')->nullable()->constrained('nutrition_plans')->nullOnDelete();
$table->date('period_start');
$table->date('period_end');
$table->decimal('average_calories', 8, 2)->default(0);
$table->decimal('average_proteins', 8, 2)->default(0);
$table->decimal('average_carbs', 8, 2)->default(0);
$table->decimal('average_fats', 8, 2)->default(0);
$table->unsignedTinyInteger('logged_days_count')->default(0);
$table->unsignedTinyInteger('weight_entries_count')->default(0);
$table->decimal('weight_trend_kg', 5, 2)->nullable();
$table->decimal('target_weight_trend_kg', 5, 2)->nullable();
$table->string('status', 32);
$table->smallInteger('recommended_calorie_adjustment')->default(0);
$table->string('message_code', 80);
$table->json('metrics');
$table->timestampTz('generated_at');
$table->timestampTz('accepted_at')->nullable();
$table->foreignUlid('accepted_nutrition_plan_id')->nullable()->constrained('nutrition_plans')->nullOnDelete();
$table->timestampsTz();
$table->unique(['user_id', 'period_start']);
$table->index(['user_id', 'period_end']);
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('weekly_reviews');
}
};
+8 -5
View File
@@ -16,7 +16,7 @@ return [
'sections' => [ 'sections' => [
'account' => 'Account', 'account' => 'Account',
'profile' => 'Profile', 'profile' => 'Profile',
'nutrition_goals' => 'Nutrition goals', 'nutrition_plan' => 'Active nutrition plan',
'activity' => 'Activity', 'activity' => 'Activity',
'metadata' => 'Metadata', 'metadata' => 'Metadata',
], ],
@@ -36,17 +36,20 @@ return [
'password' => 'Password', 'password' => 'Password',
'bio' => 'Bio', 'bio' => 'Bio',
'height' => 'Height', 'height' => 'Height',
'current_weight' => 'Current weight',
'target_weight' => 'Target weight',
'date_of_birth' => 'Date of birth', 'date_of_birth' => 'Date of birth',
'sex' => 'Sex', 'sex' => 'Sex',
'daily_calorie_goal' => 'Calories', 'calorie_goal' => 'Calories',
'daily_protein_goal' => 'Protein', 'protein_goal' => 'Protein',
'daily_carbs_goal' => 'Carbs', 'carbs_goal' => 'Carbs',
'daily_fats_goal' => 'Fats', 'fats_goal' => 'Fats',
'physical_activity_level' => 'Activity level', 'physical_activity_level' => 'Activity level',
'physical_activity_level_short' => 'Activity', 'physical_activity_level_short' => 'Activity',
'weight_goal' => 'Weight goal', 'weight_goal' => 'Weight goal',
'weight_goal_short' => 'Weight goal', 'weight_goal_short' => 'Weight goal',
'pace_preference' => 'Pace', 'pace_preference' => 'Pace',
'onboarding_completed_at' => 'Nutrition profile completed at',
'created_at' => 'Created at', 'created_at' => 'Created at',
'updated_at' => 'Updated at', 'updated_at' => 'Updated at',
], ],
+26 -3
View File
@@ -125,6 +125,32 @@ return [
'blocked' => 'User blocked.', 'blocked' => 'User blocked.',
'cannot_block_self' => 'You cannot block yourself.', 'cannot_block_self' => 'You cannot block yourself.',
], ],
'nutrition' => [
'onboarding_required' => 'Complete your nutrition profile before continuing.',
'weekly_adjustment_unavailable' => 'No weekly adjustment is available.',
'weekly_adjustment_already_accepted' => 'This adjustment has already been applied.',
'weekly_adjustment_stale' => 'Your plan changed after this review. Generate a new review before adapting it.',
'validation' => [
'adults_only' => 'Automatic calculation is available to adults only.',
'birth_date_range' => 'This birth date cannot be used for the calculation.',
'sex_reference' => 'Choose the female or male calculation reference.',
'estimate_acceptance' => 'You must confirm that you understand this is an estimate.',
'target_lower' => 'The target weight must be lower than the current weight.',
'target_higher' => 'The target weight must be higher than the current weight.',
],
'weekly_review' => [
'insufficient_tracking' => 'Log at least 5 days of meals and 4 weigh-ins across two weeks to receive a recommendation.',
'insufficient_adherence' => 'Food tracking is still too far from the plan to make a reliable adjustment.',
'on_track' => 'Your weight trend matches your goal. The plan remains unchanged.',
'plan_changed' => 'Your plan changed after this period. Keep tracking with the new plan before adapting it.',
'loss_slower' => 'Weight loss is slower than expected. A small calorie decrease is proposed.',
'loss_faster' => 'Weight loss is faster than expected. A small calorie increase is proposed.',
'gain_slower' => 'Weight gain is slower than expected. A small calorie increase is proposed.',
'gain_faster' => 'Weight gain is faster than expected. A small calorie decrease is proposed.',
'maintenance_down' => 'Weight is trending down. A small calorie increase is proposed.',
'maintenance_up' => 'Weight is trending up. A small calorie decrease is proposed.',
],
],
'validation' => [ 'validation' => [
'image_required' => 'Add an image to analyze.', 'image_required' => 'Add an image to analyze.',
'image_file' => 'The file must be an image.', 'image_file' => 'The file must be an image.',
@@ -134,9 +160,6 @@ return [
'visibility_valid' => 'Choose a valid visibility.', 'visibility_valid' => 'Choose a valid visibility.',
'month_valid' => 'Choose a valid month.', 'month_valid' => 'Choose a valid month.',
'calories_integer' => 'Calories must be an integer.', 'calories_integer' => 'Calories must be an integer.',
'nutrition_goals_required' => 'Fill in all nutrition goals.',
'nutrition_goals_positive' => 'Nutrition goals must be positive.',
'nutrition_goals_max' => 'This nutrition goal value is too high.',
'report_reason_required' => 'Choose a report reason.', 'report_reason_required' => 'Choose a report reason.',
'report_reason_valid' => 'Choose a valid report reason.', 'report_reason_valid' => 'Choose a valid report reason.',
'report_details_max' => 'The report details may not be greater than 2000 characters.', 'report_details_max' => 'The report details may not be greater than 2000 characters.',
+8 -5
View File
@@ -16,7 +16,7 @@ return [
'sections' => [ 'sections' => [
'account' => 'Compte', 'account' => 'Compte',
'profile' => 'Profil', 'profile' => 'Profil',
'nutrition_goals' => 'Objectifs nutritionnels', 'nutrition_plan' => 'Plan nutritionnel actif',
'activity' => 'Activité', 'activity' => 'Activité',
'metadata' => 'Métadonnées', 'metadata' => 'Métadonnées',
], ],
@@ -36,17 +36,20 @@ return [
'password' => 'Mot de passe', 'password' => 'Mot de passe',
'bio' => 'Bio', 'bio' => 'Bio',
'height' => 'Taille', 'height' => 'Taille',
'current_weight' => 'Poids actuel',
'target_weight' => 'Poids cible',
'date_of_birth' => 'Date de naissance', 'date_of_birth' => 'Date de naissance',
'sex' => 'Sexe', 'sex' => 'Sexe',
'daily_calorie_goal' => 'Calories', 'calorie_goal' => 'Calories',
'daily_protein_goal' => 'Protéines', 'protein_goal' => 'Protéines',
'daily_carbs_goal' => 'Glucides', 'carbs_goal' => 'Glucides',
'daily_fats_goal' => 'Lipides', 'fats_goal' => 'Lipides',
'physical_activity_level' => 'Niveau dactivité', 'physical_activity_level' => 'Niveau dactivité',
'physical_activity_level_short' => 'Activité', 'physical_activity_level_short' => 'Activité',
'weight_goal' => 'Objectif de poids', 'weight_goal' => 'Objectif de poids',
'weight_goal_short' => 'Objectif poids', 'weight_goal_short' => 'Objectif poids',
'pace_preference' => 'Rythme', 'pace_preference' => 'Rythme',
'onboarding_completed_at' => 'Profil nutritionnel complété le',
'created_at' => 'Créé le', 'created_at' => 'Créé le',
'updated_at' => 'Mis à jour le', 'updated_at' => 'Mis à jour le',
], ],
+26 -3
View File
@@ -125,6 +125,32 @@ return [
'blocked' => 'Utilisateur bloqué.', 'blocked' => 'Utilisateur bloqué.',
'cannot_block_self' => 'Vous ne pouvez pas vous bloquer vous-même.', 'cannot_block_self' => 'Vous ne pouvez pas vous bloquer vous-même.',
], ],
'nutrition' => [
'onboarding_required' => 'Termine ton profil nutritionnel avant de continuer.',
'weekly_adjustment_unavailable' => 'Aucun ajustement hebdomadaire nest disponible.',
'weekly_adjustment_already_accepted' => 'Cet ajustement a déjà été appliqué.',
'weekly_adjustment_stale' => 'Ton plan a changé depuis ce bilan. Génère un nouveau bilan avant de ladapter.',
'validation' => [
'adults_only' => 'Le calcul automatique est réservé aux personnes majeures.',
'birth_date_range' => 'La date de naissance saisie ne peut pas être utilisée pour ce calcul.',
'sex_reference' => 'Choisis la référence de calcul femme ou homme.',
'estimate_acceptance' => 'Tu dois confirmer avoir compris quil sagit dune estimation.',
'target_lower' => 'Le poids cible doit être inférieur au poids actuel.',
'target_higher' => 'Le poids cible doit être supérieur au poids actuel.',
],
'weekly_review' => [
'insufficient_tracking' => 'Ajoute au moins 5 journées de repas et 4 pesées réparties sur deux semaines pour obtenir une recommandation.',
'insufficient_adherence' => 'Le suivi alimentaire est encore trop éloigné du plan pour proposer un ajustement fiable.',
'on_track' => 'Ta tendance de poids est cohérente avec ton objectif. Le plan reste inchangé.',
'plan_changed' => 'Ton plan a changé depuis cette période. Continue le suivi avec le nouveau plan avant de ladapter.',
'loss_slower' => 'La perte est plus lente que prévu. Une baisse légère du budget est proposée.',
'loss_faster' => 'La perte est plus rapide que prévu. Une hausse légère du budget est proposée.',
'gain_slower' => 'La prise est plus lente que prévu. Une hausse légère du budget est proposée.',
'gain_faster' => 'La prise est plus rapide que prévu. Une baisse légère du budget est proposée.',
'maintenance_down' => 'Le poids tend à baisser. Une hausse légère du budget est proposée.',
'maintenance_up' => 'Le poids tend à monter. Une baisse légère du budget est proposée.',
],
],
'validation' => [ 'validation' => [
'image_required' => 'Ajoute une image à analyser.', 'image_required' => 'Ajoute une image à analyser.',
'image_file' => 'Le fichier doit être une image.', 'image_file' => 'Le fichier doit être une image.',
@@ -134,9 +160,6 @@ return [
'visibility_valid' => 'Choisis une visibilité valide.', 'visibility_valid' => 'Choisis une visibilité valide.',
'month_valid' => 'Choisis un mois valide.', 'month_valid' => 'Choisis un mois valide.',
'calories_integer' => 'Les calories doivent être un nombre entier.', 'calories_integer' => 'Les calories doivent être un nombre entier.',
'nutrition_goals_required' => 'Renseigne tous les objectifs nutritionnels.',
'nutrition_goals_positive' => 'Les objectifs nutritionnels doivent être positifs.',
'nutrition_goals_max' => 'La valeur de cet objectif nutritionnel est trop élevée.',
'report_reason_required' => 'Choisis une raison de signalement.', 'report_reason_required' => 'Choisis une raison de signalement.',
'report_reason_valid' => 'Choisis une raison de signalement valide.', 'report_reason_valid' => 'Choisis une raison de signalement valide.',
'report_details_max' => 'Le détail du signalement ne doit pas dépasser 2000 caractères.', 'report_details_max' => 'Le détail du signalement ne doit pas dépasser 2000 caractères.',
+35 -4
View File
@@ -9,6 +9,9 @@ use App\Http\Controllers\MealImageAnalysisController;
use App\Http\Controllers\MealPostController; use App\Http\Controllers\MealPostController;
use App\Http\Controllers\NotificationController; use App\Http\Controllers\NotificationController;
use App\Http\Controllers\NotificationPreferenceController; use App\Http\Controllers\NotificationPreferenceController;
use App\Http\Controllers\NutritionOnboardingController;
use App\Http\Controllers\NutritionPlanController;
use App\Http\Controllers\NutritionProfileController;
use App\Http\Controllers\PostReviewsController; use App\Http\Controllers\PostReviewsController;
use App\Http\Controllers\PublicUserProfileController; use App\Http\Controllers\PublicUserProfileController;
use App\Http\Controllers\ReportController; use App\Http\Controllers\ReportController;
@@ -16,6 +19,8 @@ use App\Http\Controllers\RevenueCatController;
use App\Http\Controllers\SearchController; use App\Http\Controllers\SearchController;
use App\Http\Controllers\StravaController; use App\Http\Controllers\StravaController;
use App\Http\Controllers\UserBlockController; use App\Http\Controllers\UserBlockController;
use App\Http\Controllers\WeeklyReviewController;
use App\Http\Controllers\WeightEntryController;
use App\Http\Controllers\WorkoutSessionController; use App\Http\Controllers\WorkoutSessionController;
use Illuminate\Support\Facades\Route; use Illuminate\Support\Facades\Route;
@@ -59,6 +64,32 @@ Route::prefix('legal-documents')->group(function (): void {
}); });
Route::middleware(['auth:sanctum', 'verified', 'not_suspended'])->group(function (): void { Route::middleware(['auth:sanctum', 'verified', 'not_suspended'])->group(function (): void {
Route::post('nutrition-onboarding', NutritionOnboardingController::class)
->middleware('throttle:account-action')
->name('nutrition-onboarding.complete');
});
Route::middleware(['auth:sanctum', 'verified', 'not_suspended', 'onboarded'])->group(function (): void {
Route::patch('nutrition-profile', NutritionProfileController::class)
->middleware('throttle:account-action')
->name('nutrition-profile.update');
Route::get('nutrition-plans', [NutritionPlanController::class, 'index'])->name('nutrition-plans.index');
Route::get('nutrition-plans/current', [NutritionPlanController::class, 'current'])->name('nutrition-plans.current');
Route::get('weight-entries', [WeightEntryController::class, 'index'])->name('weight-entries.index');
Route::post('weight-entries', [WeightEntryController::class, 'store'])
->middleware('throttle:content-write')
->name('weight-entries.store');
Route::delete('weight-entries/{weightEntry}', [WeightEntryController::class, 'destroy'])
->middleware('throttle:content-write')
->name('weight-entries.destroy');
Route::get('weekly-reviews', [WeeklyReviewController::class, 'index'])->name('weekly-reviews.index');
Route::get('weekly-reviews/current', [WeeklyReviewController::class, 'current'])->name('weekly-reviews.current');
Route::post('weekly-reviews/{weeklyReview}/accept', [WeeklyReviewController::class, 'accept'])
->middleware('throttle:account-action')
->name('weekly-reviews.accept');
});
Route::middleware(['auth:sanctum', 'verified', 'not_suspended', 'onboarded'])->group(function (): void {
Route::post('device-tokens', [DeviceTokenController::class, 'store'])->middleware('throttle:device-token'); Route::post('device-tokens', [DeviceTokenController::class, 'store'])->middleware('throttle:device-token');
Route::delete('device-tokens', [DeviceTokenController::class, 'destroy'])->middleware('throttle:device-token'); Route::delete('device-tokens', [DeviceTokenController::class, 'destroy'])->middleware('throttle:device-token');
Route::get('notifications', [NotificationController::class, 'index'])->name('notifications.index'); Route::get('notifications', [NotificationController::class, 'index'])->name('notifications.index');
@@ -74,13 +105,13 @@ Route::post('webhooks/revenuecat', [RevenueCatController::class, 'webhook'])
->name('revenuecat.webhook'); ->name('revenuecat.webhook');
// Billing // Billing
Route::middleware(['auth:sanctum', 'verified', 'not_suspended'])->prefix('billing')->group(function (): void { Route::middleware(['auth:sanctum', 'verified', 'not_suspended', 'onboarded'])->prefix('billing')->group(function (): void {
Route::post('sync', [RevenueCatController::class, 'sync']) Route::post('sync', [RevenueCatController::class, 'sync'])
->middleware('throttle:account-action') ->middleware('throttle:account-action')
->name('billing.sync'); ->name('billing.sync');
}); });
Route::middleware(['auth:sanctum', 'verified', 'not_suspended']) Route::middleware(['auth:sanctum', 'verified', 'not_suspended', 'onboarded'])
->prefix('certification') ->prefix('certification')
->group(function (): void { ->group(function (): void {
Route::get('/', [CertificationController::class, 'show'])->name('certification.show'); Route::get('/', [CertificationController::class, 'show'])->name('certification.show');
@@ -90,7 +121,7 @@ Route::middleware(['auth:sanctum', 'verified', 'not_suspended'])
}); });
// Workouts // Workouts
Route::middleware(['auth:sanctum', 'verified', 'not_suspended'])->group(function (): void { Route::middleware(['auth:sanctum', 'verified', 'not_suspended', 'onboarded'])->group(function (): void {
Route::get('workouts/calendar', [WorkoutSessionController::class, 'calendar'])->name('workouts.calendar'); Route::get('workouts/calendar', [WorkoutSessionController::class, 'calendar'])->name('workouts.calendar');
Route::get('workouts', [WorkoutSessionController::class, 'index'])->name('workouts.index'); Route::get('workouts', [WorkoutSessionController::class, 'index'])->name('workouts.index');
Route::post('workouts', [WorkoutSessionController::class, 'store'])->middleware('throttle:content-write')->name('workouts.store'); Route::post('workouts', [WorkoutSessionController::class, 'store'])->middleware('throttle:content-write')->name('workouts.store');
@@ -102,7 +133,7 @@ Route::middleware(['auth:sanctum', 'verified', 'not_suspended'])->group(function
}); });
// Meals // Meals
Route::middleware(['auth:sanctum', 'verified', 'not_suspended'])->group(function (): void { Route::middleware(['auth:sanctum', 'verified', 'not_suspended', 'onboarded'])->group(function (): void {
Route::get('search', SearchController::class)->name('search'); Route::get('search', SearchController::class)->name('search');
Route::get('users/{user}', [PublicUserProfileController::class, 'show'])->name('users.show'); Route::get('users/{user}', [PublicUserProfileController::class, 'show'])->name('users.show');
Route::get('me/blocked-users', [UserBlockController::class, 'index'])->name('users.blocks.index'); Route::get('me/blocked-users', [UserBlockController::class, 'index'])->name('users.blocks.index');
+16 -32
View File
@@ -1,9 +1,5 @@
<?php <?php
use App\Enums\PacePreference;
use App\Enums\PhysicalActivityLevel;
use App\Enums\UserSex;
use App\Enums\WeightGoal;
use App\Mail\VerifyAccount; use App\Mail\VerifyAccount;
use App\Models\User; use App\Models\User;
use Illuminate\Auth\Notifications\VerifyEmail; use Illuminate\Auth\Notifications\VerifyEmail;
@@ -22,11 +18,6 @@ it('sends a verification email when a user registers', function () {
'email' => 'leon@example.com', 'email' => 'leon@example.com',
'password' => 'Motsdfdepasse123*', 'password' => 'Motsdfdepasse123*',
'locale' => 'fr-FR', 'locale' => 'fr-FR',
'physicalActivityLevel' => PhysicalActivityLevel::LIGHTLY_ACTIVE->value,
'weightGoal' => WeightGoal::MAINTAIN_WEIGHT->value,
'pacePreference' => PacePreference::NORMAL->value,
'sex' => UserSex::WOMAN->value,
'dateOfBirth' => '2003-04-24',
'termsAccepted' => true, 'termsAccepted' => true,
]) ])
->assertCreated() ->assertCreated()
@@ -35,11 +26,8 @@ it('sends a verification email when a user registers', function () {
->assertJsonPath('user.emailVerified', false) ->assertJsonPath('user.emailVerified', false)
->assertJsonPath('user.analysisAccessLevel', 'free') ->assertJsonPath('user.analysisAccessLevel', 'free')
->assertJsonPath('user.canAnalyzeMeals', false) ->assertJsonPath('user.canAnalyzeMeals', false)
->assertJsonPath('user.physicalActivityLevel', PhysicalActivityLevel::LIGHTLY_ACTIVE->value) ->assertJsonPath('user.onboardingStatus', 'required')
->assertJsonPath('user.weightGoal', WeightGoal::MAINTAIN_WEIGHT->value) ->assertJsonPath('user.nutritionGoals', null)
->assertJsonPath('user.pacePreference', PacePreference::NORMAL->value)
->assertJsonPath('user.sex', UserSex::WOMAN->value)
->assertJsonPath('user.dateOfBirth', '2003-04-24')
->assertCookieMissing('token'); ->assertCookieMissing('token');
$user = User::where('email', 'leon@example.com')->firstOrFail(); $user = User::where('email', 'leon@example.com')->firstOrFail();
@@ -51,13 +39,13 @@ it('sends a verification email when a user registers', function () {
$this->assertDatabaseHas('users', [ $this->assertDatabaseHas('users', [
'id' => $user->id, 'id' => $user->id,
'locale' => 'fr', 'locale' => 'fr',
'physical_activity_level' => PhysicalActivityLevel::LIGHTLY_ACTIVE->value, 'physical_activity_level' => null,
'weight_goal' => WeightGoal::MAINTAIN_WEIGHT->value, 'weight_goal' => null,
'pace_preference' => PacePreference::NORMAL->value, 'pace_preference' => null,
'sex' => UserSex::WOMAN->value, 'sex' => null,
]); ]);
expect($user->date_of_birth?->toDateString())->toBe('2003-04-24'); expect($user->date_of_birth)->toBeNull();
Notification::assertSentTo($user, VerifyEmail::class); Notification::assertSentTo($user, VerifyEmail::class);
}); });
@@ -75,7 +63,7 @@ it('returns a temporary error when the verification email cannot be sent during
'termsAccepted' => true, 'termsAccepted' => true,
]) ])
->assertServiceUnavailable() ->assertServiceUnavailable()
->assertJsonPath('message', __('api.auth.verification_email_failed')); ->assertJsonPath('code', 'VERIFICATION_EMAIL_FAILED');
$this->assertDatabaseMissing('users', [ $this->assertDatabaseMissing('users', [
'email' => 'leon@example.com', 'email' => 'leon@example.com',
@@ -91,16 +79,12 @@ it('registers mobile users with a bearer token', function () {
'password' => 'Motsdfdepasse123*', 'password' => 'Motsdfdepasse123*',
'deviceName' => 'Bowli Android', 'deviceName' => 'Bowli Android',
'locale' => 'fr-FR', 'locale' => 'fr-FR',
'physicalActivityLevel' => PhysicalActivityLevel::LIGHTLY_ACTIVE->value,
'weightGoal' => WeightGoal::MAINTAIN_WEIGHT->value,
'pacePreference' => PacePreference::NORMAL->value,
'sex' => UserSex::MAN->value,
'dateOfBirth' => '2003-04-24',
'termsAccepted' => true, 'termsAccepted' => true,
]) ])
->assertCreated() ->assertCreated()
->assertJsonPath('user.email', 'mobile.leon@example.com') ->assertJsonPath('user.email', 'mobile.leon@example.com')
->assertJsonPath('user.emailVerified', false) ->assertJsonPath('user.emailVerified', false)
->assertJsonPath('user.onboardingStatus', 'required')
->assertJsonStructure(['token']) ->assertJsonStructure(['token'])
->assertCookieMissing('token'); ->assertCookieMissing('token');
@@ -183,7 +167,7 @@ it('generates verification links with a relative signature', function () {
->withServerVariables(['HTTP_HOST' => 'different-host.test']) ->withServerVariables(['HTTP_HOST' => 'different-host.test'])
->getJson($requestUrl) ->getJson($requestUrl)
->assertOk() ->assertOk()
->assertJsonPath('message', __('api.auth.verified')); ->assertJsonPath('code', 'EMAIL_VERIFIED');
expect($user->fresh()->hasVerifiedEmail())->toBeTrue(); expect($user->fresh()->hasVerifiedEmail())->toBeTrue();
}); });
@@ -197,7 +181,7 @@ it('verifies a user from a signed email link', function () {
$this->getJson($url) $this->getJson($url)
->assertOk() ->assertOk()
->assertJsonPath('message', __('api.auth.verified')) ->assertJsonPath('code', 'EMAIL_VERIFIED')
->assertJsonPath('user.emailVerified', true); ->assertJsonPath('user.emailVerified', true);
expect($user->fresh()->hasVerifiedEmail())->toBeTrue(); expect($user->fresh()->hasVerifiedEmail())->toBeTrue();
@@ -225,7 +209,7 @@ it('blocks login for unverified users', function () {
'password' => 'password', 'password' => 'password',
]) ])
->assertForbidden() ->assertForbidden()
->assertJsonPath('message', __('api.auth.email_not_verified')) ->assertJsonPath('code', 'EMAIL_NOT_VERIFIED')
->assertCookieMissing('token'); ->assertCookieMissing('token');
$this->assertDatabaseMissing('personal_access_tokens', [ $this->assertDatabaseMissing('personal_access_tokens', [
@@ -288,7 +272,7 @@ it('logs out mobile users by deleting the current bearer token', function () {
->withToken($token) ->withToken($token)
->postJson('/api/auth/logout') ->postJson('/api/auth/logout')
->assertOk() ->assertOk()
->assertJsonPath('message', __('api.auth.logout')); ->assertJsonPath('code', 'LOGGED_OUT');
$this->assertDatabaseMissing('personal_access_tokens', [ $this->assertDatabaseMissing('personal_access_tokens', [
'name' => 'Bowli iOS', 'name' => 'Bowli iOS',
@@ -305,7 +289,7 @@ it('resends a verification email for an unverified user without authentication',
'email' => $user->email, 'email' => $user->email,
]) ])
->assertOk() ->assertOk()
->assertJsonPath('message', __('api.auth.verification_sent_if_unverified')); ->assertJsonPath('code', 'VERIFICATION_EMAIL_SENT_IF_UNVERIFIED');
Notification::assertSentTo($user, VerifyEmail::class); Notification::assertSentTo($user, VerifyEmail::class);
}); });
@@ -319,13 +303,13 @@ it('does not reveal whether an email can receive a verification email', function
'email' => $verifiedUser->email, 'email' => $verifiedUser->email,
]) ])
->assertOk() ->assertOk()
->assertJsonPath('message', __('api.auth.verification_sent_if_unverified')); ->assertJsonPath('code', 'VERIFICATION_EMAIL_SENT_IF_UNVERIFIED');
$this->postJson('/api/auth/email/verification-notification', [ $this->postJson('/api/auth/email/verification-notification', [
'email' => 'missing@example.com', 'email' => 'missing@example.com',
]) ])
->assertOk() ->assertOk()
->assertJsonPath('message', __('api.auth.verification_sent_if_unverified')); ->assertJsonPath('code', 'VERIFICATION_EMAIL_SENT_IF_UNVERIFIED');
Notification::assertNothingSent(); Notification::assertNothingSent();
}); });
+93
View File
@@ -0,0 +1,93 @@
<?php
use App\Enums\PacePreference;
use App\Enums\PhysicalActivityLevel;
use App\Enums\UserSex;
use App\Enums\WeightGoal;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Laravel\Sanctum\Sanctum;
uses(RefreshDatabase::class);
function onboardingPayload(array $overrides = []): array
{
return [
'dateOfBirth' => '1992-04-18',
'sex' => UserSex::WOMAN->value,
'heightCm' => 168,
'weightKg' => 72.4,
'targetWeightKg' => 66,
'physicalActivityLevel' => PhysicalActivityLevel::MODERATELY_ACTIVE->value,
'weightGoal' => WeightGoal::LOSE_WEIGHT->value,
'pacePreference' => PacePreference::NORMAL->value,
'nutritionEstimateAccepted' => true,
...$overrides,
];
}
it('returns an explicit onboarding state and blocks application endpoints', function () {
$user = User::factory()->withoutNutritionOnboarding()->create();
Sanctum::actingAs($user);
$this->getJson('/api/auth/me')
->assertOk()
->assertJsonPath('data.onboardingStatus', 'required')
->assertJsonPath('data.nutritionGoals', null)
->assertJsonPath('data.currentWeight', null);
$this->getJson('/api/meal-posts/stats?period=day&date=2026-08-14')
->assertStatus(409)
->assertJsonPath('code', 'ONBOARDING_REQUIRED')
->assertJsonMissingPath('message');
});
it('completes onboarding atomically and returns the calculated plan', function () {
$user = User::factory()->withoutNutritionOnboarding()->create();
Sanctum::actingAs($user);
$response = $this->postJson('/api/nutrition-onboarding', onboardingPayload());
$response
->assertOk()
->assertJsonPath('data.onboardingStatus', 'completed')
->assertJsonPath('data.height', 168)
->assertJsonPath('data.currentWeight', 72.4)
->assertJsonPath('data.targetWeight', 66)
->assertJsonPath('data.nutritionPlan.formulaVersion', 'mifflin_st_jeor_v1')
->assertJsonPath('data.nutritionPlan.source', 'onboarding');
expect($response->json('data.nutritionGoals.calories'))->toBeGreaterThan(1200);
$this->assertDatabaseCount('weight_entries', 1);
$this->assertDatabaseCount('nutrition_plans', 1);
expect($user->fresh()->onboarding_completed_at)->not->toBeNull();
});
it('is idempotent when the completion response is retried', function () {
$user = User::factory()->withoutNutritionOnboarding()->create();
Sanctum::actingAs($user);
$this->postJson('/api/nutrition-onboarding', onboardingPayload())->assertOk();
$this->postJson('/api/nutrition-onboarding', onboardingPayload())->assertOk();
$this->assertDatabaseCount('weight_entries', 1);
$this->assertDatabaseCount('nutrition_plans', 1);
});
it('validates adult age, formula reference and target direction', function () {
Sanctum::actingAs(User::factory()->withoutNutritionOnboarding()->create());
$this->postJson('/api/nutrition-onboarding', onboardingPayload([
'dateOfBirth' => now()->subYears(17)->toDateString(),
'sex' => UserSex::OTHER->value,
'targetWeightKg' => 80,
]))
->assertUnprocessable()
->assertJsonValidationErrors(['dateOfBirth', 'sex', 'targetWeightKg'])
->assertJsonPath('code', 'VALIDATION_ERROR')
->assertJsonPath('errors.dateOfBirth.0', 'NUTRITION_ADULTS_ONLY')
->assertJsonPath('errors.sex.0', 'NUTRITION_METABOLIC_SEX_REQUIRED')
->assertJsonPath('errors.targetWeightKg.0', 'NUTRITION_TARGET_WEIGHT_MUST_BE_LOWER')
->assertJsonMissingPath('message');
});
+41 -42
View File
@@ -11,11 +11,12 @@ use Laravel\Sanctum\Sanctum;
uses(RefreshDatabase::class); uses(RefreshDatabase::class);
it('returns nutrition goals with the authenticated user', function () { it('returns nutrition goals with the authenticated user', function () {
$user = User::factory()->create([ $user = User::factory()->create();
'daily_calorie_goal' => 2400, $user->nutritionPlans()->where('is_active', true)->update([
'daily_protein_goal' => 140, 'calories' => 2400,
'daily_carbs_goal' => 280, 'proteins' => 140,
'daily_fats_goal' => 80, 'carbs' => 280,
'fats' => 80,
]); ]);
Sanctum::actingAs($user); Sanctum::actingAs($user);
@@ -29,52 +30,48 @@ it('returns nutrition goals with the authenticated user', function () {
->assertJsonPath('data.nutritionGoals.fats', 80); ->assertJsonPath('data.nutritionGoals.fats', 80);
}); });
it('updates nutrition goals for the authenticated user', function () { it('recalculates nutrition goals from the complete profile', function () {
$user = User::factory()->create(); $user = User::factory()->create();
Sanctum::actingAs($user); Sanctum::actingAs($user);
$response = $this->patchJson('/api/auth/me', [ $response = $this->patchJson('/api/nutrition-profile', [
'nutritionGoals' => [ 'dateOfBirth' => '1991-06-12',
'calories' => 2200, 'sex' => UserSex::MAN->value,
'proteins' => 135.5, 'heightCm' => 182,
'carbs' => 260, 'weightKg' => 84,
'fats' => 75.25, 'targetWeightKg' => 78,
], 'physicalActivityLevel' => PhysicalActivityLevel::VERY_ACTIVE->value,
'weightGoal' => WeightGoal::LOSE_WEIGHT->value,
'pacePreference' => PacePreference::NORMAL->value,
]); ]);
$response $response
->assertOk() ->assertOk()
->assertJsonPath('data.nutritionGoals.calories', 2200) ->assertJsonPath('data.nutritionPlan.source', 'profile_recalculation')
->assertJsonPath('data.nutritionGoals.proteins', 135.5) ->assertJsonPath('data.currentWeight', 84)
->assertJsonPath('data.nutritionGoals.carbs', 260) ->assertJsonPath('data.targetWeight', 78);
->assertJsonPath('data.nutritionGoals.fats', 75.25);
$this->assertDatabaseHas('users', [ expect($response->json('data.nutritionGoals.calories'))->toBeGreaterThan(1200);
'id' => $user->id, $this->assertDatabaseCount('nutrition_plans', 2);
'daily_calorie_goal' => 2200, $this->assertDatabaseCount('weight_entries', 2);
'daily_protein_goal' => 135.5, $this->assertDatabaseHas('nutrition_plans', ['is_active' => false]);
'daily_carbs_goal' => 260,
'daily_fats_goal' => 75.25,
]);
}); });
it('validates nutrition goals', function () { it('does not allow direct nutrition goal overrides through the account endpoint', function () {
Sanctum::actingAs(User::factory()->create()); $user = User::factory()->create();
Sanctum::actingAs($user);
$this->patchJson('/api/auth/me', [ $this->patchJson('/api/auth/me', [
'nutritionGoals' => [ 'nutritionGoals' => [
'calories' => -1, 'calories' => 9000,
'proteins' => 'abc', 'proteins' => 1,
'carbs' => 1,
'fats' => 1,
], ],
]) ])
->assertUnprocessable() ->assertOk()
->assertJsonValidationErrors([ ->assertJsonPath('data.nutritionGoals.calories', 2200);
'nutritionGoals.calories',
'nutritionGoals.proteins',
'nutritionGoals.carbs',
'nutritionGoals.fats',
]);
}); });
it('returns profile preferences with the authenticated user', function () { it('returns profile preferences with the authenticated user', function () {
@@ -98,25 +95,25 @@ it('updates profile preferences for the authenticated user', function () {
Sanctum::actingAs($user); Sanctum::actingAs($user);
$this->patchJson('/api/auth/me', [ $this->patchJson('/api/nutrition-profile', [
'physicalActivityLevel' => PhysicalActivityLevel::MODERATELY_ACTIVE->value, 'physicalActivityLevel' => PhysicalActivityLevel::MODERATELY_ACTIVE->value,
'weightGoal' => WeightGoal::MAINTAIN_WEIGHT->value, 'weightGoal' => WeightGoal::MAINTAIN_WEIGHT->value,
'pacePreference' => PacePreference::NORMAL->value, 'pacePreference' => PacePreference::NORMAL->value,
'sex' => UserSex::MAN->value, 'sex' => UserSex::MAN->value,
'dateOfBirth' => '2003-04-24', 'dateOfBirth' => '2003-04-24',
'locale' => 'fr-FR', 'heightCm' => 180,
'weightKg' => 75,
'targetWeightKg' => null,
]) ])
->assertOk() ->assertOk()
->assertJsonPath('data.physicalActivityLevel', PhysicalActivityLevel::MODERATELY_ACTIVE->value) ->assertJsonPath('data.physicalActivityLevel', PhysicalActivityLevel::MODERATELY_ACTIVE->value)
->assertJsonPath('data.weightGoal', WeightGoal::MAINTAIN_WEIGHT->value) ->assertJsonPath('data.weightGoal', WeightGoal::MAINTAIN_WEIGHT->value)
->assertJsonPath('data.pacePreference', PacePreference::NORMAL->value) ->assertJsonPath('data.pacePreference', PacePreference::NORMAL->value)
->assertJsonPath('data.sex', UserSex::MAN->value) ->assertJsonPath('data.sex', UserSex::MAN->value)
->assertJsonPath('data.locale', 'fr')
->assertJsonPath('data.dateOfBirth', '2003-04-24'); ->assertJsonPath('data.dateOfBirth', '2003-04-24');
$this->assertDatabaseHas('users', [ $this->assertDatabaseHas('users', [
'id' => $user->id, 'id' => $user->id,
'locale' => 'fr',
'physical_activity_level' => PhysicalActivityLevel::MODERATELY_ACTIVE->value, 'physical_activity_level' => PhysicalActivityLevel::MODERATELY_ACTIVE->value,
'weight_goal' => WeightGoal::MAINTAIN_WEIGHT->value, 'weight_goal' => WeightGoal::MAINTAIN_WEIGHT->value,
'pace_preference' => PacePreference::NORMAL->value, 'pace_preference' => PacePreference::NORMAL->value,
@@ -129,13 +126,14 @@ it('updates profile preferences for the authenticated user', function () {
it('validates profile preferences', function () { it('validates profile preferences', function () {
Sanctum::actingAs(User::factory()->create()); Sanctum::actingAs(User::factory()->create());
$this->patchJson('/api/auth/me', [ $this->patchJson('/api/nutrition-profile', [
'physicalActivityLevel' => 'daily', 'physicalActivityLevel' => 'daily',
'weightGoal' => 'bulk', 'weightGoal' => 'bulk',
'pacePreference' => 'urgent', 'pacePreference' => 'urgent',
'sex' => 'x', 'sex' => 'x',
'locale' => 'es',
'dateOfBirth' => 'tomorrow', 'dateOfBirth' => 'tomorrow',
'heightCm' => 20,
'weightKg' => 5,
]) ])
->assertUnprocessable() ->assertUnprocessable()
->assertJsonValidationErrors([ ->assertJsonValidationErrors([
@@ -143,7 +141,8 @@ it('validates profile preferences', function () {
'weightGoal', 'weightGoal',
'pacePreference', 'pacePreference',
'sex', 'sex',
'locale',
'dateOfBirth', 'dateOfBirth',
'heightCm',
'weightKg',
]); ]);
}); });
+132
View File
@@ -0,0 +1,132 @@
<?php
use App\Enums\PacePreference;
use App\Enums\WeeklyReviewStatus;
use App\Enums\WeightGoal;
use App\Models\MealPosts;
use App\Models\NutritionPlan;
use App\Models\User;
use Carbon\CarbonImmutable;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Laravel\Sanctum\Sanctum;
uses(RefreshDatabase::class);
beforeEach(function () {
CarbonImmutable::setTestNow('2026-08-17 10:00:00');
});
afterEach(function () {
CarbonImmutable::setTestNow();
});
it('returns an insufficient data review without changing the plan', function () {
$user = User::factory()->create();
Sanctum::actingAs($user);
$this->getJson('/api/weekly-reviews/current')
->assertOk()
->assertJsonPath('data.periodStart', '2026-08-10')
->assertJsonPath('data.periodEnd', '2026-08-16')
->assertJsonPath('data.status', WeeklyReviewStatus::INSUFFICIENT_DATA->value)
->assertJsonPath('data.recommendedCalorieAdjustment', 0);
$this->assertDatabaseCount('nutrition_plans', 1);
});
it('proposes and applies a small confirmed adjustment', function () {
$user = User::factory()->create([
'weight_goal' => WeightGoal::LOSE_WEIGHT,
'pace_preference' => PacePreference::NORMAL,
]);
$user->weightEntries()->delete();
foreach ([
['2026-08-03 07:00:00', 70.0],
['2026-08-07 07:00:00', 69.98],
['2026-08-12 07:00:00', 69.95],
['2026-08-16 07:00:00', 69.90],
] as [$measuredAt, $weightKg]) {
$user->weightEntries()->create([
'weight_kg' => $weightKg,
'measured_at' => $measuredAt,
]);
}
foreach (range(10, 14) as $day) {
MealPosts::factory()->for($user, 'user')->create([
'calories' => 2200,
'proteins' => 112,
'carbs' => 286,
'fats' => 61,
'eaten_at' => "2026-08-{$day} 12:00:00",
]);
}
Sanctum::actingAs($user);
$reviewResponse = $this->getJson('/api/weekly-reviews/current')
->assertOk()
->assertJsonPath('data.status', WeeklyReviewStatus::ADJUSTMENT_RECOMMENDED->value)
->assertJsonPath('data.recommendedCalorieAdjustment', -100);
$reviewId = $reviewResponse->json('data.id');
$this->postJson("/api/weekly-reviews/{$reviewId}/accept")
->assertCreated()
->assertJsonPath('data.source', 'weekly_adjustment');
$this->assertDatabaseCount('nutrition_plans', 2);
$this->assertDatabaseHas('weekly_reviews', [
'id' => $reviewId,
'recommended_calorie_adjustment' => -100,
]);
expect($user->weeklyReviews()->findOrFail($reviewId)->accepted_at)->not->toBeNull();
});
it('does not allow accepting another users review', function () {
$user = User::factory()->create();
$otherUser = User::factory()->create();
$review = $otherUser->weeklyReviews()->create([
'period_start' => '2026-08-10',
'period_end' => '2026-08-16',
'status' => WeeklyReviewStatus::ADJUSTMENT_RECOMMENDED,
'recommended_calorie_adjustment' => -100,
'message_code' => 'WEEKLY_REVIEW_ADJUSTMENT',
'metrics' => [],
'generated_at' => now(),
]);
Sanctum::actingAs($user);
$this->postJson("/api/weekly-reviews/{$review->id}/accept")->assertNotFound();
});
it('rejects an adjustment generated for a plan that is no longer active', function () {
$user = User::factory()->create();
$previousPlan = $user->nutritionPlans()->where('is_active', true)->firstOrFail();
$review = $user->weeklyReviews()->create([
'nutrition_plan_id' => $previousPlan->getKey(),
'period_start' => '2026-08-10',
'period_end' => '2026-08-16',
'status' => WeeklyReviewStatus::ADJUSTMENT_RECOMMENDED,
'recommended_calorie_adjustment' => -100,
'message_code' => 'WEEKLY_REVIEW_ADJUSTMENT',
'metrics' => [],
'generated_at' => now(),
]);
$previousPlan->update([
'is_active' => false,
'effective_until' => now(),
]);
NutritionPlan::factory()->for($user)->create([
'is_active' => true,
'effective_from' => now()->addMinute(),
]);
Sanctum::actingAs($user);
$this->postJson("/api/weekly-reviews/{$review->id}/accept")
->assertStatus(409);
expect($review->fresh()->accepted_at)->toBeNull();
});
+46
View File
@@ -0,0 +1,46 @@
<?php
use App\Models\User;
use App\Models\WeightEntry;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Laravel\Sanctum\Sanctum;
uses(RefreshDatabase::class);
it('stores and lists the authenticated user weight history', function () {
$user = User::factory()->create();
Sanctum::actingAs($user);
$this->postJson('/api/weight-entries', [
'weightKg' => 69.35,
'measuredAt' => '2026-08-10T07:30:00Z',
])
->assertCreated()
->assertJsonPath('data.weightKg', 69.35);
$this->getJson('/api/weight-entries?days=365')
->assertOk()
->assertJsonPath('data.0.weightKg', 70)
->assertJsonPath('data.1.weightKg', 69.35);
});
it('does not allow deleting another users weight entry', function () {
$user = User::factory()->create();
$otherUser = User::factory()->create();
$entry = WeightEntry::factory()->for($otherUser)->create();
Sanctum::actingAs($user);
$this->deleteJson("/api/weight-entries/{$entry->id}")->assertNotFound();
$this->assertModelExists($entry);
});
it('validates weight measurements', function () {
Sanctum::actingAs(User::factory()->create());
$this->postJson('/api/weight-entries', [
'weightKg' => 10,
'measuredAt' => now()->addDay()->toISOString(),
])
->assertUnprocessable()
->assertJsonValidationErrors(['weightKg', 'measuredAt']);
});
@@ -0,0 +1,70 @@
<?php
use App\Enums\PacePreference;
use App\Enums\PhysicalActivityLevel;
use App\Enums\UserSex;
use App\Enums\WeightGoal;
use App\Services\NutritionPlanCalculator;
use Carbon\CarbonImmutable;
beforeEach(function () {
CarbonImmutable::setTestNow('2026-08-14 10:00:00');
});
afterEach(function () {
CarbonImmutable::setTestNow();
});
it('calculates a reproducible maintenance plan', function () {
$plan = (new NutritionPlanCalculator)->calculate(
weightKg: 80,
heightCm: 180,
dateOfBirth: CarbonImmutable::parse('1996-08-14'),
sex: UserSex::MAN,
activityLevel: PhysicalActivityLevel::MODERATELY_ACTIVE,
weightGoal: WeightGoal::MAINTAIN_WEIGHT,
pacePreference: PacePreference::NORMAL,
);
expect($plan)
->formula_version->toBe('mifflin_st_jeor_v1')
->resting_metabolism->toBe(1780)
->maintenance_calories->toBe(2760)
->calorie_adjustment_percent->toBe(0.0)
->proteins->toBe(128)
->fats->toBe(77)
->carbs->toBe(389)
->calories->toBe(2761)
->and(($plan['proteins'] * 4) + ($plan['carbs'] * 4) + ($plan['fats'] * 9))
->toBe($plan['calories']);
});
it('applies bounded loss and gain adjustments', function (WeightGoal $goal, PacePreference $pace, float $expectedAdjustment) {
$plan = (new NutritionPlanCalculator)->calculate(
weightKg: 60,
heightCm: 165,
dateOfBirth: CarbonImmutable::parse('1990-01-01'),
sex: UserSex::WOMAN,
activityLevel: PhysicalActivityLevel::LIGHTLY_ACTIVE,
weightGoal: $goal,
pacePreference: $pace,
);
expect($plan['calorie_adjustment_percent'])->toBe($expectedAdjustment)
->and($plan['calories'])->toBeGreaterThanOrEqual(1200);
})->with([
'normal loss' => [WeightGoal::LOSE_WEIGHT, PacePreference::NORMAL, -0.15],
'fast gain' => [WeightGoal::GAIN_WEIGHT, PacePreference::FAST, 0.15],
]);
it('rejects a non binary metabolic calculation reference', function () {
(new NutritionPlanCalculator)->calculate(
weightKg: 70,
heightCm: 170,
dateOfBirth: CarbonImmutable::parse('1990-01-01'),
sex: UserSex::UNKNOWN,
activityLevel: PhysicalActivityLevel::SEDENTARY,
weightGoal: WeightGoal::MAINTAIN_WEIGHT,
pacePreference: PacePreference::SLOW,
);
})->throws(InvalidArgumentException::class);