feat: history and calculate needs for user
This commit is contained in:
@@ -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,
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -2,7 +2,13 @@
|
||||
|
||||
namespace Database\Factories;
|
||||
|
||||
use App\Enums\NutritionPlanSource;
|
||||
use App\Enums\PacePreference;
|
||||
use App\Enums\PhysicalActivityLevel;
|
||||
use App\Enums\UserRole;
|
||||
use App\Enums\UserSex;
|
||||
use App\Enums\WeightGoal;
|
||||
use App\Models\User;
|
||||
use Illuminate\Database\Eloquent\Factories\Factory;
|
||||
use Illuminate\Support\Facades\Hash;
|
||||
use Illuminate\Support\Str;
|
||||
@@ -32,14 +38,50 @@ class UserFactory extends Factory
|
||||
'email_verified_at' => now(),
|
||||
'password' => static::$password ??= Hash::make('password'),
|
||||
'avatar_url' => null,
|
||||
'daily_calorie_goal' => 2000,
|
||||
'daily_protein_goal' => 120,
|
||||
'daily_carbs_goal' => 250,
|
||||
'daily_fats_goal' => 70,
|
||||
'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(),
|
||||
'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.
|
||||
*/
|
||||
@@ -49,4 +91,34 @@ class UserFactory extends Factory
|
||||
'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,
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -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('role')->default('user');
|
||||
$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->string('sex')->default('unknown');
|
||||
$table->string('physical_activity_level')->default('sedentary');
|
||||
$table->string('weight_goal')->default('lose_weight');
|
||||
$table->string('pace_preference')->default('slow');
|
||||
$table->string('sex')->nullable();
|
||||
$table->string('physical_activity_level')->nullable();
|
||||
$table->string('weight_goal')->nullable();
|
||||
$table->string('pace_preference')->nullable();
|
||||
$table->string('locale', 8)->default('en')->after('email');
|
||||
$table->string('avatar_url', 2048)->nullable();
|
||||
|
||||
// Goals
|
||||
$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');
|
||||
$table->timestampTz('onboarding_completed_at')->nullable()->index();
|
||||
$table->timestampTz('nutrition_estimate_accepted_at')->nullable();
|
||||
// Moderation
|
||||
$table->timestamp('suspended_at')->nullable()->after('deleted_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');
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user