Compare commits

...
Sign in to create a new pull request.

2 commits

Author SHA1 Message Date
nicolas.demangeat
f90abdfbf6 Merge branch 'master' into 'issue-45'
# Conflicts:
#   app/Http/Requests/Fo/Customers/CreateAccountRequest.php
2025-08-19 09:21:34 +00:00
Nicolas Demangeat
24d381555d fix: 🐛 add more verification to check if the consumer is in the local database 2025-08-19 11:19:23 +02:00
4 changed files with 40 additions and 10 deletions

View file

@ -143,7 +143,13 @@ class CrmUserProvider extends EloquentUserProvider implements UserProviderContra
]); ]);
$model->hashcode = $crmCustomer->hashcode; $model->hashcode = $crmCustomer->hashcode;
$model->email_verified_at = $crmCustomer->status ? now() : null; $model->email_verified_at = $crmCustomer->status ? now() : null;
$model->saveOrFail(); // Check if model is in local databse.
$exists = $model::query()->where('email', $model->email)->exists();
if (!$exists) {
$model->saveOrFail();
} else {
throw new \Exception('Le modèle existe déjà dans la base locale.');
}
return $model; return $model;
} }

View file

@ -18,6 +18,7 @@ use LDC\LDCCRM\Enums\ApiError;
use LDC\LDCCRM\Exceptions\ApiErrorException; use LDC\LDCCRM\Exceptions\ApiErrorException;
use LDC\LDCCRM\Exceptions\ValidationException as CrmValidationException; use LDC\LDCCRM\Exceptions\ValidationException as CrmValidationException;
use LDC\LDCCRM\Models\Customer as CrmCustomer; use LDC\LDCCRM\Models\Customer as CrmCustomer;
use Illuminate\Database\QueryException;
class RegisterController extends Controller class RegisterController extends Controller
{ {
@ -81,6 +82,7 @@ class RegisterController extends Controller
* @return \App\Models\Customer * @return \App\Models\Customer
* @throws IlluminateValidationException If there are validation errors. * @throws IlluminateValidationException If there are validation errors.
* @throws \RuntimeException On unhandled api Error. * @throws \RuntimeException On unhandled api Error.
* @throws IlluminateValidationException If there are already an email.
*/ */
protected function create(array $data): Customer protected function create(array $data): Customer
{ {
@ -138,7 +140,16 @@ class RegisterController extends Controller
throw IlluminateValidationException::withMessages([ throw IlluminateValidationException::withMessages([
'error' => $e->getMessage() 'error' => $e->getMessage()
]); ]);
} //end try } catch (QueryException $e) {
if ($e->getCode() === '23000') {
throw IlluminateValidationException::withMessages([
'error' => trans('Un compte existe déjà avec cette adresse.')
]);
}
report($e);
Log::error(__CLASS__ . '::create reached end of possibilities.');
throw new \RuntimeException(__CLASS__ . '::create reached end of possibilities.');
}//end try
} }
/** /**

View file

@ -6,6 +6,7 @@ use App\Models\Settings;
use App\Repositories\Fo\CrmRepository; use App\Repositories\Fo\CrmRepository;
use App\Rules\IsAdult; use App\Rules\IsAdult;
use Illuminate\Foundation\Http\FormRequest; use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Validator;
use Illuminate\Support\Arr; use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Auth;
use Illuminate\Validation\Rule; use Illuminate\Validation\Rule;
@ -99,13 +100,6 @@ class CreateAccountRequest extends FormRequest
'required', 'required',
'email:rfc,dns,spoof', 'email:rfc,dns,spoof',
'max:255', 'max:255',
// * Does email already exists in CRM ?
// phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundBeforeLastUsed
function (string $attribute, $value, \Closure $fail) {
if (CrmCustomer::findWithEmail(\strval($value))) {
$fail('Un compte existe déjà avec cette adresse.');
}
},
'unique:customers,email', 'unique:customers,email',
], ],
'password' => [ 'password' => [
@ -119,6 +113,25 @@ class CreateAccountRequest extends FormRequest
]; ];
} }
/**
* Get the "after" validation callables for the request.
*
* @return array
*/
public function after(): array
{
return [
function (Validator $validator) {
if (CrmCustomer::findWithEmail((string)$this->input('email'))) {
$validator->errors()->add(
'email',
'Un compte existe déjà avec cette adresse dans le CRM.'
);
}
},
];
}
/** /**
* Get custom attributes for validator errors. * Get custom attributes for validator errors.
* *

View file

@ -264,7 +264,7 @@ class Customer extends Authenticatable implements MustVerifyEmail
} }
$this->forceDelete(); $this->forceDelete();
throw ValidationException::withMessages([ throw ValidationException::withMessages([
'error' => trans('Le compte \'existe pas.') 'error' => trans('Le compte n\'existe pas.')
]); ]);
} }