113 lines
3.5 KiB
PHP
113 lines
3.5 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Actions\Customers;
|
|
|
|
use App\Contracts\Action;
|
|
use App\Contracts\ActionInput;
|
|
use App\Contracts\Repositories\EmployeeRepository;
|
|
use App\Exceptions\Crm\CrmCustomerExistsException;
|
|
use App\Exceptions\CrmServiceException;
|
|
use App\Models\Customer;
|
|
use App\Services\CrmService;
|
|
use Domain\Enums\Country;
|
|
use Domain\Models\ValueObjects\CrmCustomer;
|
|
use Illuminate\Database\Eloquent\ModelNotFoundException;
|
|
use Illuminate\Validation\ValidationException;
|
|
use Infrastructure\Adapters\Services\AppCustomerAuthService;
|
|
use Ramsey\Uuid\Uuid;
|
|
use Shared\ValueObjects\CrmHashcode;
|
|
use Webmozart\Assert\Assert;
|
|
|
|
/**
|
|
* @implements \App\Contracts\Action<\App\Actions\Customers\RegisterCustomerInput, \App\Models\Customer>
|
|
*/
|
|
final class RegisterCustomer implements Action
|
|
{
|
|
public function __construct(
|
|
private readonly AppCustomerAuthService $authService,
|
|
private readonly CrmService $crmService,
|
|
private readonly EmployeeRepository $employeeRepository,
|
|
) {
|
|
}
|
|
|
|
/**
|
|
* @param \App\Actions\Customers\RegisterCustomerInput $input
|
|
*
|
|
* @throws ValidationException
|
|
* @throws \UnexpectedValueException
|
|
* @throws ModelNotFoundException
|
|
*/
|
|
#[\Override]
|
|
public function __invoke(ActionInput $input): Customer
|
|
{
|
|
Assert::isInstanceOf($input, RegisterCustomerInput::class);
|
|
|
|
// * Persist and associate Customer + Employee
|
|
$customer = new Customer([
|
|
'email' => $input->email,
|
|
]);
|
|
// * Temp hashcode to satisfy constraint
|
|
$customer->hashcode = new CrmHashcode(Uuid::uuid4());
|
|
$customer->saveOrFail();
|
|
|
|
$employee = $this->employeeRepository->findByMatriculeAndCode(
|
|
$input->matricule,
|
|
$input->code
|
|
);
|
|
|
|
if (! $employee) {
|
|
throw new ModelNotFoundException('Employee not found');
|
|
}
|
|
|
|
$employee->customer()->associate($customer);
|
|
$employee->saveOrFail();
|
|
|
|
$crmCustomer = new CrmCustomer(
|
|
hashcode: null,
|
|
email: $input->email->value,
|
|
password: $input->password,
|
|
firstname: $input->firstname,
|
|
lastname: $input->lastname,
|
|
birthDate: $input->birthDate->format('Y-m-d'),
|
|
phone: $input->phone?->value,
|
|
address: $input->address,
|
|
address2: null,
|
|
city: $input->city,
|
|
postcode: $input->postcode,
|
|
country: Country::france,
|
|
optIn: $input->optIn,
|
|
status: false,
|
|
lastConnection: null,
|
|
);
|
|
|
|
try {
|
|
$result = $this->crmService->persist($crmCustomer);
|
|
|
|
if (! $result->hashcode) {
|
|
throw new \UnexpectedValueException(
|
|
'CRM did not return a hashcode for the created customer'
|
|
);
|
|
}
|
|
|
|
$customer->hashcode = CrmHashcode::fromString($result->hashcode);
|
|
$customer->saveOrFail();
|
|
|
|
$customer->sendEmailVerificationNotification();
|
|
|
|
$this->authService->logIn($customer);
|
|
|
|
return $customer;
|
|
} catch (CrmCustomerExistsException) {
|
|
throw ValidationException::withMessages([
|
|
'error' => trans('An account is already associated with this email address'),
|
|
]);
|
|
} catch (CrmServiceException $e) {
|
|
report($e);
|
|
throw ValidationException::withMessages([
|
|
'error' => trans('An error has occurred. Please try again later'),
|
|
]);
|
|
}
|
|
}
|
|
}
|