feat: history and calculate needs for user
This commit is contained in:
@@ -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'],
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -2,10 +2,6 @@
|
||||
|
||||
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 Illuminate\Foundation\Http\FormRequest;
|
||||
use Illuminate\Validation\Rule;
|
||||
@@ -56,11 +52,6 @@ class RegisterRequest extends FormRequest
|
||||
'termsAccepted' => ['required', 'accepted'],
|
||||
'avatar' => ['sometimes', 'nullable', 'image', 'max:2048'],
|
||||
'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;
|
||||
}
|
||||
}
|
||||
@@ -2,10 +2,6 @@
|
||||
|
||||
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 Illuminate\Foundation\Http\FormRequest;
|
||||
use Illuminate\Validation\Rule;
|
||||
@@ -22,27 +18,24 @@ class UpdateUserRequest extends FormRequest
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'name' => [
|
||||
'sometimes',
|
||||
'string',
|
||||
'min:3',
|
||||
'max:32',
|
||||
'regex:/^[a-zA-Z0-9_.-]+$/',
|
||||
Rule::unique('users', 'name')->ignore($this->user()),
|
||||
],
|
||||
'bio' => ['sometimes', 'nullable', 'string'],
|
||||
'height' => ['sometimes', 'nullable', 'integer'],
|
||||
'locale' => ['sometimes', 'string', Rule::in(config('app.supported_locales', ['fr', 'en']))],
|
||||
'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
|
||||
{
|
||||
return $this->moderationChecks(
|
||||
[$this->input('bio')],
|
||||
[$this->input('name'), $this->input('bio')],
|
||||
[$this->file('avatar')],
|
||||
);
|
||||
}
|
||||
@@ -51,10 +44,6 @@ class UpdateUserRequest extends FormRequest
|
||||
{
|
||||
return [
|
||||
'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'),
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user