Files
eatvio-infra/app/Actions/Fortify/CreateNewUser.php
2023-12-06 15:08:34 +03:00

114 lines
4.6 KiB
PHP

<?php
namespace App\Actions\Fortify;
use App\Models\User;
use App\Models\UserDetailz;
use App\Models\CircumferenceMeasure;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\Rule;
use Laravel\Fortify\Contracts\CreatesNewUsers;
use Laravel\Jetstream\Jetstream;
use App\Rules\MinWords;
class CreateNewUser implements CreatesNewUsers
{
use PasswordValidationRules;
/**
* Validate and create a newly registered user.
*
* @param array<string, string> $input
*/
public function create(array $input): User
{
$data = [];
$data['name'] = $input['first_name'] . " " . $input['last_name'];
$data['email'] = $input['email'];
$data['password'] = $input['password'];
$data['password_confirmation'] = $input['password_confirmation'];
$data['terms'] = $input['terms'];
Validator::make($data, [
'name' => ['required', 'string', 'max:255'],
'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
'password' => $this->passwordRules(),
'terms' => Jetstream::hasTermsAndPrivacyPolicyFeature() ? ['accepted', 'required'] : '',
])->validate();
$detailz = [
'first_name' => $input['first_name'],
'last_name' => $input['last_name'],
'birthdate' => $input['birthdate'],
'gender' => $input['gender'],
'country' => $input['country'],
'city' => $input['city'],
'lang' => $input['lang'],
'weight' => $input['weight'],
'height' => $input['height'],
'chest_circumference' => $input['chest_circumference'] ?? 0,
'waist_circumference' => $input['waist_circumference'] ?? 0,
'hip_circumference' => $input['hip_circumference'] ?? 0,
'blood_pressure_up' => $input['blood_pressure_up'] ?? 0,
'blood_pressure_down' => $input['blood_pressure_down'] ?? 0,
'has_hypo' => $input['has_hypo'] ?? false,
'has_hyper' => $input['has_hyper'] ?? false,
'about_alergy' => $input['about_alergy'] ?? '',
'about_taking_pils' => $input['about_taking_pils'] ?? '',
'about_sport_hobbies' => $input['about_sport_hobbies'] ?? '',
'favorite_foods' => $input['favorite_foods'] ?? '',
'unfavorite_foods' => $input['unfavorite_foods'] ?? '',
'additional_info' => $input['additional_info'] ?? '',
];
Validator::make($detailz, [
'first_name' => ['required', 'string', 'max:255'],
'last_name' => ['required', 'string', 'max:255'],
'birthdate' => ['required', 'date'],
'gender' => ['required', Rule::in(['male', 'female'])],
'country' => ['required', 'string', 'max:255'],
'city' => ['required', 'string', 'max:255'],
'lang' => ['required', 'string', 'max:255'],
'weight' => ['required', 'integer'],
'height' => ['required', 'integer'],
'chest_circumference' => ['required', 'integer'],
'waist_circumference' => ['required', 'integer'],
'hip_circumference' => ['required', 'integer'],
'blood_pressure_up' => ['required', 'integer'],
'blood_pressure_down' => ['required', 'integer'],
'has_hypo' => ['required', Rule::in(['0', '1'])],
'has_hyper' => ['required', Rule::in(['0', '1'])],
'about_alergy' => ['sometimes', 'nullable', new MinWords(1)],
'about_taking_pils' => ['sometimes', 'nullable', new MinWords(1)],
'about_sport_hobbies' => ['sometimes', 'nullable', new MinWords(1)],
'favorite_foods' => ['sometimes', 'nullable', new MinWords(1)],
'unfavorite_foods' => ['sometimes', 'nullable', new MinWords(1)],
'additional_info' => ['sometimes', 'nullable', new MinWords(1)],
])->validate();
$newUser = User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => Hash::make($data['password']),
]);
$detailz['user_id'] = $newUser->id;
$detailz['is_mood_fillable'] = true;
$detailz['is_bedtime_fillable'] = true;
UserDetailz::create($detailz);
CircumferenceMeasure::create([
'user_id' => $newUser->id,
'chest_circumference' => $detailz['chest_circumference'],
'waist_circumference' => $detailz['waist_circumference'],
'hip_circumference' => $detailz['hip_circumference']
]);
return $newUser;
}
}