Adds UpdateCustomer action, input, and output classes to handle customer information updates. Integrates with existing services and updates the CustomerController to utilize the new action.
95 lines
3.1 KiB
PHP
95 lines
3.1 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Http\Requests\Fo\Customer;
|
|
|
|
use App\Actions\Crm\UpdateCustomerInput;
|
|
use Illuminate\Foundation\Http\FormRequest;
|
|
use Illuminate\Support\Facades\Date;
|
|
use Infrastructure\Adapters\Services\AppCustomerAuthService;
|
|
|
|
class UpdateInfosRequest extends FormRequest
|
|
{
|
|
public function __construct(
|
|
private readonly AppCustomerAuthService $authService
|
|
) {
|
|
}
|
|
|
|
public function authorize(): bool
|
|
{
|
|
return $this->authService->check();
|
|
}
|
|
|
|
#[\Override]
|
|
protected function prepareForValidation()
|
|
{
|
|
$this->merge([
|
|
'opt_in' => $this->boolean('opt_in'),
|
|
]);
|
|
}
|
|
|
|
public function toUpdateCustomerInput(): UpdateCustomerInput
|
|
{
|
|
return new UpdateCustomerInput(
|
|
firstname: $this->string('firstname')->value(),
|
|
lastname: $this->string('lastname')->value(),
|
|
birthDate: $this->string('birth_date')->value(),
|
|
phone: $this->string('phone')->value(),
|
|
city: $this->string('city')->value(),
|
|
postcode: ! empty($this->string('postcode')) ?
|
|
$this->string('postcode')->value() : null,
|
|
address: ! empty($this->string('address')) ?
|
|
$this->string('address')->value() : null,
|
|
optIn: $this->boolean('opt_in'),
|
|
);
|
|
}
|
|
|
|
/** @return array<string,mixed> */
|
|
public function rules(): array
|
|
{
|
|
$majorityDate = Date::now()->subYears(18)->format('Y-m-d');
|
|
|
|
return [
|
|
'firstname' => ['required', 'string', 'max:255'],
|
|
'lastname' => ['required', 'string', 'max:255'],
|
|
'birth_date' => [
|
|
'required',
|
|
'date',
|
|
'date_format:Y-m-d',
|
|
'before_or_equal:' . $majorityDate,
|
|
],
|
|
'address' => ['nullable', 'string', 'max:255'],
|
|
'phone' => ['nullable', 'phone:FR'],
|
|
'city' => ['required', 'string', 'max:255'],
|
|
'postcode' => ['required', 'postal_code:FR'],
|
|
'opt_in' => ['boolean'],
|
|
];
|
|
}
|
|
|
|
/** @return array<string,string> */
|
|
#[\Override]
|
|
public function messages(): array
|
|
{
|
|
return [
|
|
'birth_date.before_or_equal' => trans('validation.custom.birth_date.before_or_equal'),
|
|
'birth_date.date' => trans('validation.custom.birth_date.date'),
|
|
'birth_date.date_format' => trans('validation.custom.birth_date.date_format'),
|
|
];
|
|
}
|
|
|
|
/** @return array<string,string> */
|
|
#[\Override]
|
|
public function attributes(): array
|
|
{
|
|
return [
|
|
'firstname' => trans('validation.attributes.first_name'),
|
|
'lastname' => trans('validation.attributes.last_name'),
|
|
'birth_date' => trans('validation.attributes.date_of_birth'),
|
|
'address' => trans('validation.attributes.address'),
|
|
'phone' => trans('validation.attributes.phone'),
|
|
'city' => trans('validation.attributes.city'),
|
|
'postcode' => trans('validation.attributes.postal_code'),
|
|
];
|
|
}
|
|
}
|