ldc-boutique-cnc-sable/app/Http/Controllers/Fo/Customers/CustomerController.php
Jérémy Munsch b8a0f16efa
fix(Crm): update customer input handling in UpdateCustomer action
Refactor input validation and data preparation for customer updates.
2026-10-27 18:57:46 +01:00

73 lines
2.1 KiB
PHP

<?php
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\Queries\Crm\GetCrmCustomerQuery;
use App\Queries\Crm\GetCrmCustomerQueryInput;
use Domain\Models\ValueObjects\CrmCustomer;
use Illuminate\Contracts\View\View;
use Illuminate\Http\RedirectResponse;
use Illuminate\Validation\ValidationException;
class CustomerController extends Controller
{
public function __construct(
private readonly Executor $executor,
) {
}
public function dashboard(): View
{
$this->authorize('manageOwnProfile', Customer::class);
return view('fo.customer.dashboard');
}
public function editInfos(GetCrmCustomerQuery $getCrmCustomerQuery): View
{
$this->authorize('manageOwnProfile', Customer::class);
$output = $this->executor->execute(
$getCrmCustomerQuery,
new GetCrmCustomerQueryInput()
);
abort_unless(
$output->customer instanceof CrmCustomer,
503,
trans('An error occurred, please try again later.')
);
return view('fo.customer.edit.infos', [
'crmCustomer' => $output->customer,
]);
}
public function editInfosSubmit(UpdateInfosRequest $request, UpdateCustomer $updateCustomer): RedirectResponse
{
$this->authorize('manageOwnProfile', Customer::class);
$output = $this->executor->execute(
$updateCustomer,
$request->toUpdateCustomerInput()
);
return match (true) {
$output->result instanceof CrmCustomer => back()
->with('success', trans('Your information has been saved.')),
$output->result instanceof ValidationException => back()
->withInput()
->withErrors($output->result->getMessage()),
default => back()
->withInput()
->withErrors(trans('An error occurred, please try again later.')),
};
}
}