feat(crm): implement customer update functionality

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.
This commit is contained in:
Jérémy Munsch 2026-09-30 09:47:10 +02:00
parent e89856966d
commit 5037154108
No known key found for this signature in database
GPG key ID: 2AAECDA6B7330F91
9 changed files with 204 additions and 41 deletions

View file

@ -0,0 +1,53 @@
<?php
declare(strict_types=1);
namespace App\Actions\Crm;
use App\Contracts\Action;
use App\Contracts\ActionInput;
use App\Services\CrmService;
use Infrastructure\Adapters\Services\AppCustomerAuthService;
use Webmozart\Assert\Assert;
/**
* @implements \App\Contracts\Action<\App\Actions\Crm\UpdateCustomerInput, \App\Actions\Crm\UpdateCustomerOutput>
*/
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);
}
}
}

View file

@ -0,0 +1,22 @@
<?php
declare(strict_types=1);
namespace App\Actions\Crm;
use App\Contracts\ActionInput;
final readonly class UpdateCustomerInput implements ActionInput
{
public function __construct(
public string $firstname,
public string $lastname,
public string $birthDate,
public ?string $phone,
public ?string $address,
public string $postcode,
public string $city,
public bool $optIn,
) {
}
}

View file

@ -0,0 +1,16 @@
<?php
declare(strict_types=1);
namespace App\Actions\Crm;
use Domain\Models\ValueObjects\CrmCustomer;
use Illuminate\Validation\ValidationException;
final readonly class UpdateCustomerOutput
{
public function __construct(
public CrmCustomer | ValidationException | false $result
) {
}
}

View file

@ -4,20 +4,22 @@ declare(strict_types=1);
namespace App\Http\Controllers\Fo\Customers;
use App\Actions\Crm\UpdateCustomer;
use App\Contracts\Executor;
use App\Http\Controllers\Controller;
use App\Http\Requests\Fo\Customer\UpdateInfosRequest;
use App\Models\Customer;
use App\Services\CrmService;
use App\Queries\Crm\GetCrmCustomerQuery;
use App\Queries\Crm\GetCrmCustomerQueryInput;
use Domain\Models\ValueObjects\CrmCustomer;
use Illuminate\Contracts\View\View;
use Illuminate\Http\RedirectResponse;
use Infrastructure\Adapters\Services\AppCustomerAuthService;
use Illuminate\Validation\ValidationException;
class CustomerController extends Controller
{
public function __construct(
private readonly AppCustomerAuthService $authService,
private readonly CrmService $crmService,
private readonly Executor $executor,
) {
}
@ -28,46 +30,42 @@ class CustomerController extends Controller
return view('fo.customer.dashboard');
}
public function editInfos(): View
public function editInfos(GetCrmCustomerQuery $getCrmCustomerQuery): View
{
$this->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.')),
};
}
}

View file

@ -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<string,mixed> */

View file

@ -0,0 +1,39 @@
<?php
declare(strict_types=1);
namespace App\Queries\Crm;
use App\Contracts\Query;
use App\Contracts\QueryInput;
use App\Services\CrmService;
use Infrastructure\Adapters\Services\AppCustomerAuthService;
use Webmozart\Assert\Assert;
/**
* @implements \App\Contracts\Query<\App\Queries\Crm\GetCrmCustomerQueryInput, \App\Queries\Crm\GetCrmCustomerQueryOutput>
*/
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);
}
}

View file

@ -0,0 +1,16 @@
<?php
declare(strict_types=1);
namespace App\Queries\Crm;
use App\Contracts\QueryInput;
use Shared\ValueObjects\CrmHashcode;
final readonly class GetCrmCustomerQueryInput implements QueryInput
{
public function __construct(
public ?CrmHashcode $fromHashcodeOrAuthenticatedUserHashcode = null,
) {
}
}

View file

@ -0,0 +1,19 @@
<?php
declare(strict_types=1);
namespace App\Queries\Crm;
use App\Exceptions\CrmServiceException as ExceptionsCrmServiceException;
use Domain\Models\ValueObjects\CrmCustomer;
final readonly class GetCrmCustomerQueryOutput
{
public bool $success;
public function __construct(
public null | CrmCustomer | ExceptionsCrmServiceException $customer,
) {
$this->success = $this->customer !== null && $this->customer instanceof CrmCustomer;
}
}

View file

@ -54,7 +54,7 @@
{{-- Email (lecture seule) --}}
<div class="mb-3">
<div class="form-floating">
<input class="form-control" id="email" type="email" value="{{ $customer->email->value }}" aria-describedby="email-hint"
<input class="form-control" id="email" type="email" value="{{ $crmCustomer->email }}" aria-describedby="email-hint"
placeholder="{{ __('Email Address') }}" autocomplete="email" disabled>
<label for="email">{{ __('Email Address') }}</label>
</div>