From b8a0f16efa163da46cb0a1e52c54bc87a3521594 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Munsch?= Date: Tue, 27 Oct 2026 18:57:46 +0100 Subject: [PATCH] fix(Crm): update customer input handling in UpdateCustomer action Refactor input validation and data preparation for customer updates. --- app/Actions/Crm/UpdateCustomer.php | 31 ++-- .../Fo/Customers/CustomerController.php | 18 ++- .../Fo/Customers/CustomerControllerTest.php | 149 ++++++++++++++++++ 3 files changed, 179 insertions(+), 19 deletions(-) diff --git a/app/Actions/Crm/UpdateCustomer.php b/app/Actions/Crm/UpdateCustomer.php index 84ac869b..1bd26f5c 100644 --- a/app/Actions/Crm/UpdateCustomer.php +++ b/app/Actions/Crm/UpdateCustomer.php @@ -24,23 +24,32 @@ final class UpdateCustomer implements Action #[\Override] public function __invoke(ActionInput $input): UpdateCustomerOutput { - Assert::isInstanceOf($input, UnsubscribeFromNewsletterInput::class); + Assert::isInstanceOf($input, UpdateCustomerInput::class); $customer = $this->authService->getCustomerAsMandatory(); try { + /** @var array{firstname:string,lastname:string,birth_date:string,phone?:string,address?:string,postcode:string,city:string,opt_in?:bool} $data */ + $data = [ + 'firstname' => $input->firstname, + 'lastname' => $input->lastname, + 'birth_date' => $input->birthDate, + 'postcode' => $input->postcode, + 'city' => $input->city, + 'opt_in' => $input->optIn, + ]; + + if ($input->phone !== null) { + $data['phone'] = $input->phone; + } + + if ($input->address !== null) { + $data['address'] = $input->address; + } + $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, - ] + $data ); return new UpdateCustomerOutput($result); diff --git a/app/Http/Controllers/Fo/Customers/CustomerController.php b/app/Http/Controllers/Fo/Customers/CustomerController.php index 934d218b..69d3df33 100644 --- a/app/Http/Controllers/Fo/Customers/CustomerController.php +++ b/app/Http/Controllers/Fo/Customers/CustomerController.php @@ -34,35 +34,37 @@ class CustomerController extends Controller { $this->authorize('manageOwnProfile', Customer::class); - $crmCustomer = $this->executor->execute( + $output = $this->executor->execute( $getCrmCustomerQuery, new GetCrmCustomerQueryInput() ); abort_unless( - $crmCustomer->customer instanceof CrmCustomer, + $output->customer instanceof CrmCustomer, 503, trans('An error occurred, please try again later.') ); - return view('fo.customer.edit.infos', compact('crmCustomer')); + return view('fo.customer.edit.infos', [ + 'crmCustomer' => $output->customer, + ]); } public function editInfosSubmit(UpdateInfosRequest $request, UpdateCustomer $updateCustomer): RedirectResponse { $this->authorize('manageOwnProfile', Customer::class); - $result = $this->executor->execute( + $output = $this->executor->execute( $updateCustomer, $request->toUpdateCustomerInput() ); - return match ($result) { - $result instanceof CrmCustomer => back() + return match (true) { + $output->result instanceof CrmCustomer => back() ->with('success', trans('Your information has been saved.')), - $result instanceof ValidationException => back() + $output->result instanceof ValidationException => back() ->withInput() - ->withErrors($result->getMessage()), + ->withErrors($output->result->getMessage()), default => back() ->withInput() ->withErrors(trans('An error occurred, please try again later.')), diff --git a/tests/Feature/App/Http/Controllers/Fo/Customers/CustomerControllerTest.php b/tests/Feature/App/Http/Controllers/Fo/Customers/CustomerControllerTest.php index 08b6f02c..accc6d50 100644 --- a/tests/Feature/App/Http/Controllers/Fo/Customers/CustomerControllerTest.php +++ b/tests/Feature/App/Http/Controllers/Fo/Customers/CustomerControllerTest.php @@ -3,9 +3,16 @@ declare(strict_types=1); use App\Models\Customer; +use App\Models\Employee; use App\Models\Page; use App\Services\CrmService; +use Carbon\CarbonImmutable; use Database\Seeders\ContentSeeder; +use Domain\Enums\Country; +use Domain\Models\ValueObjects\Code; +use Domain\Models\ValueObjects\CrmCustomer; +use Domain\Models\ValueObjects\Email; +use Domain\Models\ValueObjects\Matricule; use Illuminate\Support\Facades\App; use Illuminate\Support\Facades\Cache; use Illuminate\Support\Facades\View; @@ -87,4 +94,146 @@ describe('CustomerController', function () { // THEN $response->assertStatus(302); }); + + it('renders customer edit infos page with crm customer data', function () { + /** @var \Tests\TestCase $this */ + // GIVEN + $this->seed(ContentSeeder::class); + View::share('PageList', Page::getCached() + ->mapWithKeys(fn ($page) => [ + $page->slug->value() => $page, + ])); + Cache::flush(); + + $hashcode = new CrmHashcode(Uuid::uuid4()); + $customer = Customer::forceCreate([ + 'id' => Uuid::uuid7()->toString(), + 'hashcode' => $hashcode->value, + 'email' => new Email('jean.dupont@example.com'), + ]); + + Employee::forceCreate([ + 'id' => 8882, + 'customer_id' => $customer->id, + 'matricule' => new Matricule('8888002'), + 'nom' => 'Dupont', + 'prenom' => 'Jean', + 'code' => new Code('ABCDEF13'), + 'etablissement' => 'ETAB', + 'site' => 'SITE', + 'service' => '101', + 'created_at' => CarbonImmutable::now(), + 'updated_at' => CarbonImmutable::now(), + ]); + + $crmCustomer = new CrmCustomer( + hashcode: $hashcode->value, + email: 'jean.dupont@example.com', + password: 'pwd', + firstname: 'Jean', + lastname: 'Dupont', + birthDate: '1985-05-15', + phone: '0123456789', + address: '10 rue de la Paix', + address2: null, + city: 'Paris', + postcode: '75002', + country: Country::france, + optIn: true, + status: true, + lastConnection: null, + ); + + $crmMock = \Mockery::mock(CrmService::class)->makePartial(); + $crmMock->shouldReceive('getCustomerFromHashCode')->andReturn($crmCustomer); + App::instance(CrmService::class, $crmMock); + + $this->actingAs($customer, 'frontend'); + + // WHEN + $response = $this->get('/compte/profil'); + + // THEN + $response->assertOk(); + $response->assertViewIs('fo.customer.edit.infos'); + $response->assertViewHas('crmCustomer', $crmCustomer); + $response->assertSee('value="Dupont"', false); + $response->assertSee('value="Jean"', false); + }); + + it('successfully updates customer infos and redirects back', function () { + /** @var \Tests\TestCase $this */ + // GIVEN + $this->seed(ContentSeeder::class); + View::share('PageList', Page::getCached() + ->mapWithKeys(fn ($page) => [ + $page->slug->value() => $page, + ])); + Cache::flush(); + + $hashcode = new CrmHashcode(Uuid::uuid4()); + $customer = Customer::forceCreate([ + 'id' => Uuid::uuid7()->toString(), + 'hashcode' => $hashcode->value, + 'email' => new Email('jean.dupont@example.com'), + ]); + + Employee::forceCreate([ + 'id' => 8883, + 'customer_id' => $customer->id, + 'matricule' => new Matricule('8888003'), + 'nom' => 'Dupont', + 'prenom' => 'Jean', + 'code' => new Code('ABCDEF14'), + 'etablissement' => 'ETAB', + 'site' => 'SITE', + 'service' => '101', + 'created_at' => CarbonImmutable::now(), + 'updated_at' => CarbonImmutable::now(), + ]); + + $updatedCrmCustomer = new CrmCustomer( + hashcode: $hashcode->value, + email: 'jean.dupont@example.com', + password: 'pwd', + firstname: 'Jean-Pierre', + lastname: 'Durand', + birthDate: '1985-05-15', + phone: '0123456789', + address: '20 avenue Foch', + address2: null, + city: 'Lyon', + postcode: '69001', + country: Country::france, + optIn: true, + status: true, + lastConnection: null, + ); + + $crmMock = \Mockery::mock(CrmService::class); + $crmMock->shouldReceive('getCustomerFromHashCode') + ->andReturn($updatedCrmCustomer); + $crmMock->shouldReceive('updateCustomerInfos') + ->once() + ->andReturn($updatedCrmCustomer); + App::instance(CrmService::class, $crmMock); + + $this->actingAs($customer, 'frontend'); + + // WHEN + $response = $this->post('/compte/profil', [ + 'firstname' => 'Jean-Pierre', + 'lastname' => 'Durand', + 'birth_date' => '1985-05-15', + 'phone' => '0123456789', + 'address' => '20 avenue Foch', + 'city' => 'Lyon', + 'postcode' => '69001', + 'opt_in' => true, + ]); + + // THEN + $response->assertRedirect(); + $response->assertSessionHas('success', trans('Your information has been saved.')); + }); });