ldc-boutique-cnc-sable/app/Http/Controllers/Fo/Customers/CustomerController.php
2026-10-27 09:58:12 +01:00

73 lines
2.1 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Http\Controllers\Fo\Customers;
use App\Http\Controllers\Controller;
use App\Http\Requests\Fo\Customer\UpdateInfosRequest;
use App\Models\Customer;
use App\Services\CrmService;
use Domain\Models\ValueObjects\CrmCustomer;
use Illuminate\Contracts\View\View;
use Illuminate\Http\RedirectResponse;
use Infrastructure\Adapters\Services\AppCustomerAuthService;
class CustomerController extends Controller
{
public function __construct(
private readonly AppCustomerAuthService $authService,
private readonly CrmService $crmService,
) {
}
public function dashboard(): View
{
$this->authorize('manageOwnProfile', Customer::class);
return view('fo.customer.dashboard');
}
public function editInfos(): View
{
$this->authorize('manageOwnProfile', Customer::class);
$customer = $this->authService->getCustomerAsMandatory();
$crmCustomer = $this->crmService->getCustomerFromHashCode($customer->hashcode);
abort_unless(
$crmCustomer instanceof CrmCustomer,
503,
trans('An error occurred, please try again later.')
);
return view('fo.customer.edit.infos', compact('customer', 'crmCustomer'));
}
public function editInfosSubmit(UpdateInfosRequest $request): RedirectResponse
{
$this->authorize('manageOwnProfile', Customer::class);
$customer = $this->authService->getCustomerAsMandatory();
try {
$result = $this->crmService->updateCustomerInfos(
$customer,
$request->toCrmData()
);
if ($result instanceof CrmCustomer) {
return back()->with('success', trans('Your information has been saved.'));
}
return back()
->withInput()
->withErrors($result->getMessage());
} catch (\Throwable $e) {
report($e);
return back()
->withInput()
->withErrors(trans('An error occurred, please try again later.'));
}
}
}