diff --git a/app/Actions/Crm/UpdateCustomer.php b/app/Actions/Crm/UpdateCustomer.php new file mode 100644 index 00000000..84ac869b --- /dev/null +++ b/app/Actions/Crm/UpdateCustomer.php @@ -0,0 +1,53 @@ + + */ +final class UpdateCustomer implements Action +{ + public function __construct( + private readonly AppCustomerAuthService $authService, + private readonly CrmService $crmService + ) { + } + + #[\Override] + public function __invoke(ActionInput $input): UpdateCustomerOutput + { + Assert::isInstanceOf($input, UnsubscribeFromNewsletterInput::class); + + $customer = $this->authService->getCustomerAsMandatory(); + + try { + $result = $this->crmService->updateCustomerInfos( + $customer, + [ + 'firstname' => $input->firstname, + 'lastname' => $input->lastname, + 'birth_date' => $input->birthDate, + 'phone' => $input->phone, + 'address' => $input->address, + 'postcode' => $input->postcode, + 'city' => $input->city, + 'opt_in' => $input->optIn, + ] + ); + + return new UpdateCustomerOutput($result); + } catch (\Throwable $e) { + report($e); + + return new UpdateCustomerOutput(false); + } + } +} diff --git a/app/Actions/Crm/UpdateCustomerInput.php b/app/Actions/Crm/UpdateCustomerInput.php new file mode 100644 index 00000000..51cf202e --- /dev/null +++ b/app/Actions/Crm/UpdateCustomerInput.php @@ -0,0 +1,22 @@ +authorize('manageOwnProfile', Customer::class); - $customer = $this->authService->getCustomerAsMandatory(); - $crmCustomer = $this->crmService->getCustomerFromHashCode($customer->hashcode); + $crmCustomer = $this->executor->execute( + $getCrmCustomerQuery, + new GetCrmCustomerQueryInput() + ); abort_unless( - $crmCustomer instanceof CrmCustomer, + $crmCustomer->customer instanceof CrmCustomer, 503, trans('An error occurred, please try again later.') ); - return view('fo.customer.edit.infos', compact('customer', 'crmCustomer')); + return view('fo.customer.edit.infos', compact('crmCustomer')); } - public function editInfosSubmit(UpdateInfosRequest $request): RedirectResponse + public function editInfosSubmit(UpdateInfosRequest $request, UpdateCustomer $updateCustomer): RedirectResponse { $this->authorize('manageOwnProfile', Customer::class); - $customer = $this->authService->getCustomerAsMandatory(); - try { - $result = $this->crmService->updateCustomerInfos( - $customer, - $request->toCrmData() - ); + $result = $this->executor->execute( + $updateCustomer, + $request->toUpdateCustomerInput() + ); - if ($result instanceof CrmCustomer) { - return back()->with('success', trans('Your information has been saved.')); - } - - return back() + return match ($result) { + $result instanceof CrmCustomer => back() + ->with('success', trans('Your information has been saved.')), + $result instanceof ValidationException => back() ->withInput() - ->withErrors($result->getMessage()); - } catch (\Throwable $e) { - report($e); - - return back() + ->withErrors($result->getMessage()), + default => back() ->withInput() - ->withErrors(trans('An error occurred, please try again later.')); - } + ->withErrors(trans('An error occurred, please try again later.')), + }; } } diff --git a/app/Http/Requests/Fo/Customer/UpdateInfosRequest.php b/app/Http/Requests/Fo/Customer/UpdateInfosRequest.php index 5c585f5c..84c98e6c 100644 --- a/app/Http/Requests/Fo/Customer/UpdateInfosRequest.php +++ b/app/Http/Requests/Fo/Customer/UpdateInfosRequest.php @@ -4,6 +4,7 @@ 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; @@ -28,21 +29,20 @@ class UpdateInfosRequest extends FormRequest ]); } - /** @return array{firstname:string,lastname:string,birth_date:string,phone?:string,address?:string,postcode:string,city:string,opt_in?:bool} */ - public function toCrmData(): array + public function toUpdateCustomerInput(): UpdateCustomerInput { - return [ - 'firstname' => $this->string('firstname')->value(), - 'lastname' => $this->string('lastname')->value(), - 'birth_date' => $this->string('birth_date')->value(), - 'phone' => $this->string('phone')->value(), - 'city' => $this->string('city')->value(), - 'postcode' => ! empty($this->string('postcode')) ? + 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')) ? + address: ! empty($this->string('address')) ? $this->string('address')->value() : null, - 'opt_in' => $this->boolean('opt_in'), - ]; + optIn: $this->boolean('opt_in'), + ); } /** @return array */ diff --git a/app/Queries/Crm/GetCrmCustomerQuery.php b/app/Queries/Crm/GetCrmCustomerQuery.php new file mode 100644 index 00000000..13ce06ec --- /dev/null +++ b/app/Queries/Crm/GetCrmCustomerQuery.php @@ -0,0 +1,39 @@ + + */ +final readonly class GetCrmCustomerQuery implements Query +{ + public function __construct( + private CrmService $crmService, + private AppCustomerAuthService $authService, + ) { + } + + public function __invoke(QueryInput $input): GetCrmCustomerQueryOutput + { + Assert::isAOf( + $input, + GetCrmCustomerQueryInput::class, + ); + + $hashcode = $input->fromHashcodeOrAuthenticatedUserHashcode ?? $this->authService->getCustomerAsMandatory()->hashcode; + + $crmCustomer = $this->crmService->getCustomerFromHashCode( + $hashcode + ); + + return new GetCrmCustomerQueryOutput($crmCustomer); + } +} diff --git a/app/Queries/Crm/GetCrmCustomerQueryInput.php b/app/Queries/Crm/GetCrmCustomerQueryInput.php new file mode 100644 index 00000000..9e0d26de --- /dev/null +++ b/app/Queries/Crm/GetCrmCustomerQueryInput.php @@ -0,0 +1,16 @@ +success = $this->customer !== null && $this->customer instanceof CrmCustomer; + } +} diff --git a/resources/views/fo/customer/edit/infos.blade.php b/resources/views/fo/customer/edit/infos.blade.php index aaf80f07..11d5d535 100644 --- a/resources/views/fo/customer/edit/infos.blade.php +++ b/resources/views/fo/customer/edit/infos.blade.php @@ -54,7 +54,7 @@ {{-- Email (lecture seule) --}}
-