new Email($data['email']), ]); // * Temp hashcode to satisfy constraint $customer->hashcode = new CrmHashcode(Uuid::uuid4()); $customer->saveOrFail(); $employee = $this->employeeRepository ->findByMatriculeAndCode( new Matricule($requestMatricule), new Code($requestCode) ); if (! $employee) { throw new ModelNotFoundException('Employee not found'); } $employee->customer()->associate($customer); $employee->saveOrFail(); $phone = Arr::get($data, 'phone'); $address = Arr::get($data, 'address'); $crmCustomer = new CrmCustomer( hashcode: null, email: Arr::string($data, 'email'), password: Arr::string($data, 'password'), firstname: Arr::string($data, 'firstname'), lastname: Arr::string($data, 'lastname'), birthDate: $data['birth_date']->format('Y-m-d'), phone: is_string($phone) ? $phone : null, address: is_string($address) ? $address : null, address2: null, city: Arr::string($data, 'city'), postcode: Arr::string($data, 'postal_code'), country: Country::france, optIn: Arr::boolean($data, '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 = new CrmHashcode(Uuid::uuid4()); $customer->saveOrFail(); $customer->sendEmailVerificationNotification(); $this->authService->logIn($customer); } catch (CrmCustomerExistsException $e) { 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'), ]); } }); } }