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
+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',
};
}
}