feat: fix seeder
This commit is contained in:
@@ -7,14 +7,14 @@ use App\Enums\FollowStatus;
|
||||
use App\Enums\IngredientUnit;
|
||||
use App\Enums\MealPostType;
|
||||
use App\Enums\MealPostVisibility;
|
||||
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\Enums\WorkoutSource;
|
||||
use App\Models\User;
|
||||
use App\Models\WeightEntry;
|
||||
use App\Models\WorkoutSessions;
|
||||
use Illuminate\Database\Console\Seeds\WithoutModelEvents;
|
||||
use Illuminate\Database\Seeder;
|
||||
use Illuminate\Support\Collection;
|
||||
@@ -112,19 +112,90 @@ class DemoContentSeeder extends Seeder
|
||||
'bio' => 'Des repas adaptés aux longues sorties et à la récupération.',
|
||||
],
|
||||
])->map(
|
||||
fn (array $profile): User => User::factory()->create($profile)
|
||||
fn (array $profile): User => $this->createUser($profile)
|
||||
);
|
||||
|
||||
User::factory()->withoutNutritionOnboarding()->create([
|
||||
$this->createUser([
|
||||
'name' => 'Nouveau profil',
|
||||
'email' => 'onboarding@example.com',
|
||||
'locale' => 'fr',
|
||||
'terms_accepted_at' => now(),
|
||||
]);
|
||||
], withNutritionOnboarding: false);
|
||||
|
||||
return $users;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $profile
|
||||
*/
|
||||
private function createUser(array $profile, bool $withNutritionOnboarding = true): User
|
||||
{
|
||||
$onboardingAttributes = $withNutritionOnboarding
|
||||
? [
|
||||
'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(),
|
||||
]
|
||||
: [
|
||||
'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,
|
||||
];
|
||||
|
||||
$user = User::query()->create([
|
||||
'locale' => 'en',
|
||||
'role' => UserRole::USER,
|
||||
'email_verified_at' => now(),
|
||||
'password' => 'password',
|
||||
'avatar_url' => null,
|
||||
...$onboardingAttributes,
|
||||
...$profile,
|
||||
]);
|
||||
|
||||
if ($withNutritionOnboarding) {
|
||||
$this->createNutritionOnboarding($user);
|
||||
}
|
||||
|
||||
return $user;
|
||||
}
|
||||
|
||||
private function createNutritionOnboarding(User $user): void
|
||||
{
|
||||
$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(),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Collection<int, User> $users
|
||||
*/
|
||||
@@ -239,7 +310,7 @@ class DemoContentSeeder extends Seeder
|
||||
$demoUser->weightEntries()->delete();
|
||||
|
||||
foreach (range(11, 0) as $weeksAgo) {
|
||||
WeightEntry::factory()->for($demoUser)->create([
|
||||
$demoUser->weightEntries()->create([
|
||||
'weight_kg' => round(74.8 - ((11 - $weeksAgo) * 0.28), 2),
|
||||
'measured_at' => now()->startOfDay()->subWeeks($weeksAgo),
|
||||
]);
|
||||
@@ -275,7 +346,7 @@ class DemoContentSeeder extends Seeder
|
||||
throw new LogicException('Unable to build the demo workout collection.');
|
||||
}
|
||||
|
||||
WorkoutSessions::factory()->for($demoUser)->create([
|
||||
$demoUser->workouts()->create([
|
||||
'external_id' => $source === WorkoutSource::STRAVA ? "demo-strava-{$index}" : null,
|
||||
'source' => $source,
|
||||
'type' => $workout['type'],
|
||||
|
||||
@@ -20,6 +20,9 @@ it('creates a complete local demonstration dataset', function () {
|
||||
expect(User::query()->count())->toBe(10)
|
||||
->and($demoUser->hasCompletedNutritionOnboarding())->toBeTrue()
|
||||
->and($onboardingUser->hasCompletedNutritionOnboarding())->toBeFalse()
|
||||
->and(User::query()->whereNotNull('onboarding_completed_at')->count())->toBe(9)
|
||||
->and($demoUser->nutritionPlans()->count())->toBe(1)
|
||||
->and($onboardingUser->nutritionPlans()->count())->toBe(0)
|
||||
->and($demoUser->weightEntries()->count())->toBe(12)
|
||||
->and($demoUser->workouts()->count())->toBe(12)
|
||||
->and($demoUser->following()->count())->toBe(8)
|
||||
|
||||
Reference in New Issue
Block a user