feat: history and calculate needs for user
This commit is contained in:
@@ -11,11 +11,12 @@ use Laravel\Sanctum\Sanctum;
|
||||
uses(RefreshDatabase::class);
|
||||
|
||||
it('returns nutrition goals with the authenticated user', function () {
|
||||
$user = User::factory()->create([
|
||||
'daily_calorie_goal' => 2400,
|
||||
'daily_protein_goal' => 140,
|
||||
'daily_carbs_goal' => 280,
|
||||
'daily_fats_goal' => 80,
|
||||
$user = User::factory()->create();
|
||||
$user->nutritionPlans()->where('is_active', true)->update([
|
||||
'calories' => 2400,
|
||||
'proteins' => 140,
|
||||
'carbs' => 280,
|
||||
'fats' => 80,
|
||||
]);
|
||||
|
||||
Sanctum::actingAs($user);
|
||||
@@ -29,52 +30,48 @@ it('returns nutrition goals with the authenticated user', function () {
|
||||
->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();
|
||||
|
||||
Sanctum::actingAs($user);
|
||||
|
||||
$response = $this->patchJson('/api/auth/me', [
|
||||
'nutritionGoals' => [
|
||||
'calories' => 2200,
|
||||
'proteins' => 135.5,
|
||||
'carbs' => 260,
|
||||
'fats' => 75.25,
|
||||
],
|
||||
$response = $this->patchJson('/api/nutrition-profile', [
|
||||
'dateOfBirth' => '1991-06-12',
|
||||
'sex' => UserSex::MAN->value,
|
||||
'heightCm' => 182,
|
||||
'weightKg' => 84,
|
||||
'targetWeightKg' => 78,
|
||||
'physicalActivityLevel' => PhysicalActivityLevel::VERY_ACTIVE->value,
|
||||
'weightGoal' => WeightGoal::LOSE_WEIGHT->value,
|
||||
'pacePreference' => PacePreference::NORMAL->value,
|
||||
]);
|
||||
|
||||
$response
|
||||
->assertOk()
|
||||
->assertJsonPath('data.nutritionGoals.calories', 2200)
|
||||
->assertJsonPath('data.nutritionGoals.proteins', 135.5)
|
||||
->assertJsonPath('data.nutritionGoals.carbs', 260)
|
||||
->assertJsonPath('data.nutritionGoals.fats', 75.25);
|
||||
->assertJsonPath('data.nutritionPlan.source', 'profile_recalculation')
|
||||
->assertJsonPath('data.currentWeight', 84)
|
||||
->assertJsonPath('data.targetWeight', 78);
|
||||
|
||||
$this->assertDatabaseHas('users', [
|
||||
'id' => $user->id,
|
||||
'daily_calorie_goal' => 2200,
|
||||
'daily_protein_goal' => 135.5,
|
||||
'daily_carbs_goal' => 260,
|
||||
'daily_fats_goal' => 75.25,
|
||||
]);
|
||||
expect($response->json('data.nutritionGoals.calories'))->toBeGreaterThan(1200);
|
||||
$this->assertDatabaseCount('nutrition_plans', 2);
|
||||
$this->assertDatabaseCount('weight_entries', 2);
|
||||
$this->assertDatabaseHas('nutrition_plans', ['is_active' => false]);
|
||||
});
|
||||
|
||||
it('validates nutrition goals', function () {
|
||||
Sanctum::actingAs(User::factory()->create());
|
||||
it('does not allow direct nutrition goal overrides through the account endpoint', function () {
|
||||
$user = User::factory()->create();
|
||||
Sanctum::actingAs($user);
|
||||
|
||||
$this->patchJson('/api/auth/me', [
|
||||
'nutritionGoals' => [
|
||||
'calories' => -1,
|
||||
'proteins' => 'abc',
|
||||
'calories' => 9000,
|
||||
'proteins' => 1,
|
||||
'carbs' => 1,
|
||||
'fats' => 1,
|
||||
],
|
||||
])
|
||||
->assertUnprocessable()
|
||||
->assertJsonValidationErrors([
|
||||
'nutritionGoals.calories',
|
||||
'nutritionGoals.proteins',
|
||||
'nutritionGoals.carbs',
|
||||
'nutritionGoals.fats',
|
||||
]);
|
||||
->assertOk()
|
||||
->assertJsonPath('data.nutritionGoals.calories', 2200);
|
||||
});
|
||||
|
||||
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);
|
||||
|
||||
$this->patchJson('/api/auth/me', [
|
||||
$this->patchJson('/api/nutrition-profile', [
|
||||
'physicalActivityLevel' => PhysicalActivityLevel::MODERATELY_ACTIVE->value,
|
||||
'weightGoal' => WeightGoal::MAINTAIN_WEIGHT->value,
|
||||
'pacePreference' => PacePreference::NORMAL->value,
|
||||
'sex' => UserSex::MAN->value,
|
||||
'dateOfBirth' => '2003-04-24',
|
||||
'locale' => 'fr-FR',
|
||||
'heightCm' => 180,
|
||||
'weightKg' => 75,
|
||||
'targetWeightKg' => null,
|
||||
])
|
||||
->assertOk()
|
||||
->assertJsonPath('data.physicalActivityLevel', PhysicalActivityLevel::MODERATELY_ACTIVE->value)
|
||||
->assertJsonPath('data.weightGoal', WeightGoal::MAINTAIN_WEIGHT->value)
|
||||
->assertJsonPath('data.pacePreference', PacePreference::NORMAL->value)
|
||||
->assertJsonPath('data.sex', UserSex::MAN->value)
|
||||
->assertJsonPath('data.locale', 'fr')
|
||||
->assertJsonPath('data.dateOfBirth', '2003-04-24');
|
||||
|
||||
$this->assertDatabaseHas('users', [
|
||||
'id' => $user->id,
|
||||
'locale' => 'fr',
|
||||
'physical_activity_level' => PhysicalActivityLevel::MODERATELY_ACTIVE->value,
|
||||
'weight_goal' => WeightGoal::MAINTAIN_WEIGHT->value,
|
||||
'pace_preference' => PacePreference::NORMAL->value,
|
||||
@@ -129,13 +126,14 @@ it('updates profile preferences for the authenticated user', function () {
|
||||
it('validates profile preferences', function () {
|
||||
Sanctum::actingAs(User::factory()->create());
|
||||
|
||||
$this->patchJson('/api/auth/me', [
|
||||
$this->patchJson('/api/nutrition-profile', [
|
||||
'physicalActivityLevel' => 'daily',
|
||||
'weightGoal' => 'bulk',
|
||||
'pacePreference' => 'urgent',
|
||||
'sex' => 'x',
|
||||
'locale' => 'es',
|
||||
'dateOfBirth' => 'tomorrow',
|
||||
'heightCm' => 20,
|
||||
'weightKg' => 5,
|
||||
])
|
||||
->assertUnprocessable()
|
||||
->assertJsonValidationErrors([
|
||||
@@ -143,7 +141,8 @@ it('validates profile preferences', function () {
|
||||
'weightGoal',
|
||||
'pacePreference',
|
||||
'sex',
|
||||
'locale',
|
||||
'dateOfBirth',
|
||||
'heightCm',
|
||||
'weightKg',
|
||||
]);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user