2023-12-15 15:53:55 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
|
|
|
|
|
use App\Notifications\Customer\DestroyCustomer;
|
|
|
|
|
use App\Notifications\Customer\DestroyedCustomer;
|
|
|
|
|
use App\Notifications\Customer\ResetPassword;
|
|
|
|
|
use App\Notifications\Customer\VerifyEmail;
|
2023-12-22 15:09:55 +01:00
|
|
|
use App\Traits\Models\ActivityLog;
|
2023-12-15 15:53:55 +00:00
|
|
|
use Illuminate\Contracts\Auth\MustVerifyEmail;
|
|
|
|
|
use Illuminate\Contracts\Database\Query\Builder;
|
|
|
|
|
use Illuminate\Database\Eloquent\Casts\Attribute;
|
|
|
|
|
use Illuminate\Database\Eloquent\Prunable;
|
|
|
|
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
|
|
|
|
use Illuminate\Foundation\Auth\User as Authenticatable;
|
|
|
|
|
use Illuminate\Notifications\Notifiable;
|
|
|
|
|
use Illuminate\Support\Facades\Cache;
|
|
|
|
|
use Illuminate\Support\Facades\Validator;
|
|
|
|
|
use Illuminate\Validation\Rule;
|
|
|
|
|
use Illuminate\Validation\ValidationException;
|
|
|
|
|
use LDC\LDCCRM\Enums\ApiError;
|
|
|
|
|
use LDC\LDCCRM\Exceptions\ApiErrorException;
|
|
|
|
|
use LDC\LDCCRM\Exceptions\CrmException;
|
|
|
|
|
use LDC\LDCCRM\Models\Customer as CrmCustomer;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @property string $email Email.
|
|
|
|
|
* @property string $hashcode Hashcode.
|
|
|
|
|
* @property string $remember_token Remember token.
|
2024-01-26 16:24:14 +01:00
|
|
|
* @property \LDC\LDCCRM\Models\Customer $api_data
|
2023-12-15 15:53:55 +00:00
|
|
|
* @property \Carbon\Carbon|null $email_verified_at
|
|
|
|
|
* @property \Carbon\Carbon|null $last_connection Date de derniere connexion, (pour les questions oblig CRM).
|
|
|
|
|
* @property-read \Carbon\Carbon $created_at Created date.
|
|
|
|
|
* @property-read \Carbon\Carbon $updated_at Updated date.
|
|
|
|
|
*/
|
|
|
|
|
class Customer extends Authenticatable implements MustVerifyEmail
|
|
|
|
|
{
|
2023-12-22 15:09:55 +01:00
|
|
|
use ActivityLog;
|
2023-12-15 15:53:55 +00:00
|
|
|
use Notifiable;
|
|
|
|
|
use Prunable;
|
|
|
|
|
use SoftDeletes;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The attributes that are mass assignable.
|
|
|
|
|
*
|
|
|
|
|
* @var array<int, string>
|
|
|
|
|
*/
|
|
|
|
|
protected $fillable = [
|
|
|
|
|
'email',
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The attributes that should be cast.
|
|
|
|
|
*
|
|
|
|
|
* @var array<string, string>
|
|
|
|
|
*/
|
|
|
|
|
protected $casts = [
|
|
|
|
|
'last_connection' => 'datetime',
|
|
|
|
|
'email_verified_at' => 'datetime',
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Perform any actions required after the model boots.
|
|
|
|
|
*
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
protected static function booted()
|
|
|
|
|
{
|
|
|
|
|
// * EVENT CLOSURES.
|
|
|
|
|
static::creating(function (self $model) {
|
2024-01-26 16:24:14 +01:00
|
|
|
self::assertEmailIsUnique($model->id, $model->email);
|
2023-12-15 15:53:55 +00:00
|
|
|
});
|
|
|
|
|
static::updating(function (self $model) {
|
2024-01-26 16:24:14 +01:00
|
|
|
self::assertEmailIsUnique($model->id, $model->email);
|
2023-12-15 15:53:55 +00:00
|
|
|
});
|
|
|
|
|
static::deleted(function (self $model) {
|
|
|
|
|
if ($model->forceDeleting) {
|
|
|
|
|
if ($model->existsInCrm()) {
|
|
|
|
|
$model->api_data->delete();
|
|
|
|
|
}
|
|
|
|
|
$model->notify(new DestroyedCustomer());
|
|
|
|
|
} else {
|
|
|
|
|
$model->notify(new DestroyCustomer());
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
static::restored(function () {
|
|
|
|
|
\session()->flash(
|
|
|
|
|
'warning',
|
|
|
|
|
\trans('Votre compte à été réactivé.')
|
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// * METHODS
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Supprimer les comptes inactivés depuis plus d'une semaine
|
|
|
|
|
* de manière permanente.
|
|
|
|
|
*
|
|
|
|
|
* ! Les comptes non activés depuis une semaine ou les comptes supprimés depuis une semaine.
|
|
|
|
|
*
|
2024-01-26 16:24:14 +01:00
|
|
|
* @return \Illuminate\Database\Eloquent\Builder
|
2023-12-15 15:53:55 +00:00
|
|
|
*/
|
2024-01-26 16:24:14 +01:00
|
|
|
public function prunable(): \Illuminate\Database\Eloquent\Builder
|
2023-12-15 15:53:55 +00:00
|
|
|
{
|
2024-01-26 16:24:14 +01:00
|
|
|
return self::query()->where(function (Builder $q) {
|
2023-12-15 15:53:55 +00:00
|
|
|
$q->where('updated_at', '<=', now()->subWeek())
|
|
|
|
|
->whereNull('email_verified_at');
|
|
|
|
|
})->orWhere(function (Builder $q) {
|
|
|
|
|
$q->where('deleted_at', '<=', now()->subWeek());
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Asserts using validation that the email is unique
|
|
|
|
|
*
|
|
|
|
|
* @param mixed $id
|
|
|
|
|
* @param mixed $email
|
|
|
|
|
* @return void
|
|
|
|
|
* @throws \Illuminate\Validation\ValidationException If user email as already used.
|
|
|
|
|
*/
|
|
|
|
|
private static function assertEmailIsUnique(mixed $id, mixed $email): void
|
|
|
|
|
{
|
|
|
|
|
self::assertFieldIsUnique($id, 'email', $email);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Asserts that the field is unique
|
|
|
|
|
*
|
|
|
|
|
* @param mixed $id
|
|
|
|
|
* @param string $field
|
|
|
|
|
* @param mixed $value
|
|
|
|
|
* @return void
|
|
|
|
|
* @throws \Illuminate\Validation\ValidationException If the field is not unique.
|
|
|
|
|
*/
|
|
|
|
|
private static function assertFieldIsUnique(mixed $id, string $field, mixed $value): void
|
|
|
|
|
{
|
|
|
|
|
Validator::make([$field => $value], [
|
|
|
|
|
$field => Rule::unique('customers', $field)->ignore($id),
|
|
|
|
|
], [
|
|
|
|
|
$field . '.unique' => trans('La valeur :value pour le champ :attribute est déjà utilisé', [
|
|
|
|
|
'attribute' => $field,
|
|
|
|
|
'value' => $value
|
|
|
|
|
])
|
|
|
|
|
])->validate();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// * --- AUTH ---
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Send the email verification notification.
|
|
|
|
|
*
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
public function sendEmailVerificationNotification()
|
|
|
|
|
{
|
|
|
|
|
$this->deleteIfDoesNotExistsInCrm();
|
|
|
|
|
$this->notify(new VerifyEmail());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Mark the given user's email as verified.
|
|
|
|
|
*
|
|
|
|
|
* @return boolean
|
|
|
|
|
* @throws \LDC\LDCCRM\Exceptions\CrmException If an error occurs on server side or on getting response.
|
|
|
|
|
* @throws \LDC\LDCCRM\Exceptions\ApiErrorException If any error or \LDC\LDCCRM\Enums\ApiError occurs.
|
|
|
|
|
* @phpcs:ignore Squiz.Commenting.FunctionCommentThrowTag.WrongNumber
|
|
|
|
|
*/
|
|
|
|
|
public function markEmailAsVerified()
|
|
|
|
|
{
|
|
|
|
|
$this->deleteIfDoesNotExistsInCrm();
|
|
|
|
|
try {
|
|
|
|
|
return $this->api_data->activate() && parent::markEmailAsVerified();
|
|
|
|
|
} catch (ApiErrorException $e) {
|
|
|
|
|
if ($e->getApiError()->equals(ApiError::customerAlreadyActive())) {
|
2024-01-26 16:24:14 +01:00
|
|
|
return parent::markEmailAsVerified();
|
2023-12-15 15:53:55 +00:00
|
|
|
}
|
|
|
|
|
throw $e;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Send the password reset notification.
|
|
|
|
|
*
|
|
|
|
|
* @param mixed $token
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
public function sendPasswordResetNotification(mixed $token)
|
|
|
|
|
{
|
|
|
|
|
$this->deleteIfDoesNotExistsInCrm();
|
|
|
|
|
/** @var string $token */
|
|
|
|
|
$this->notify(new ResetPassword($token));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Check if the customer exists in CRM.
|
|
|
|
|
*
|
|
|
|
|
* @return boolean
|
|
|
|
|
*/
|
|
|
|
|
public function existsInCrm(): bool
|
|
|
|
|
{
|
|
|
|
|
return $this->hashcode and CrmCustomer::find($this->hashcode);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Force delete and logout if is logged in case the customer
|
|
|
|
|
* does not exists in CRM.
|
|
|
|
|
*
|
|
|
|
|
* @return void
|
|
|
|
|
* @throws \Illuminate\Validation\ValidationException If the account is deleted because does not exists in CRM.
|
|
|
|
|
*/
|
|
|
|
|
private function deleteIfDoesNotExistsInCrm(): void
|
|
|
|
|
{
|
|
|
|
|
if ($this->existsInCrm()) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
/** @var static|null $loggedCustomer */
|
|
|
|
|
$loggedCustomer = \auth('frontend')->user();
|
2024-01-26 16:24:14 +01:00
|
|
|
$isLogged = \optional($loggedCustomer)->id === $this->id and !isset($this->id);
|
2023-12-15 15:53:55 +00:00
|
|
|
if ($isLogged) {
|
|
|
|
|
\auth('frontend')->logout();
|
|
|
|
|
}
|
|
|
|
|
$this->forceDelete();
|
|
|
|
|
throw ValidationException::withMessages([
|
|
|
|
|
'error' => trans('Le compte \'existe pas.')
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// * --- END AUTH ---
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get api_data attribute, the crm Data of a customer
|
|
|
|
|
*
|
|
|
|
|
* @return \Illuminate\Database\Eloquent\Casts\Attribute
|
|
|
|
|
*/
|
|
|
|
|
public function apiData(): Attribute
|
|
|
|
|
{
|
|
|
|
|
return Attribute::make(
|
|
|
|
|
get: fn (): CrmCustomer => self::cacheCrm($this->hashcode, "apiData", function () {
|
|
|
|
|
/** @var \LDC\LDCCRM\CrmApiV2 */
|
|
|
|
|
$crm = app('crmAPI');
|
|
|
|
|
$crmCustomer = $crm->getCustomer($this->hashcode);
|
|
|
|
|
if (!$crmCustomer) {
|
|
|
|
|
throw new CrmException('CRM get Customer returned null');
|
|
|
|
|
}
|
|
|
|
|
return $crmCustomer;
|
|
|
|
|
})
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Set the crm Data of a customer
|
|
|
|
|
*
|
|
|
|
|
* @param array $data
|
|
|
|
|
* @return boolean
|
|
|
|
|
*
|
|
|
|
|
* @throws \LDC\LDCCRM\Exceptions\CrmException If an error occurs on server side or on getting response.
|
|
|
|
|
* @throws \LDC\LDCCRM\Exceptions\ApiErrorException If any error or \LDC\LDCCRM\Enums\ApiError occurs.
|
|
|
|
|
* @phpcs:disable Squiz.Commenting.FunctionCommentThrowTag.WrongNumber
|
|
|
|
|
*/
|
|
|
|
|
public function updateApiData(array $data): bool
|
|
|
|
|
{
|
|
|
|
|
// phpcs:enable
|
2024-01-26 16:24:14 +01:00
|
|
|
/** @var \LDC\LDCCRM\Models\Customer|null $crmCustomer */
|
2023-12-15 15:53:55 +00:00
|
|
|
$crmCustomer = $this->api_data;
|
|
|
|
|
if (!$crmCustomer) {
|
|
|
|
|
throw new CrmException('CRM get Customer returned null');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$crmCustomer->fill($data);
|
|
|
|
|
$result = $crmCustomer->save();
|
|
|
|
|
self::clearCrmCached($crmCustomer->hashcode, 'apiData');
|
|
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// * CRM CACHE
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Clear cached apiData
|
|
|
|
|
*
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
public function clearApiDataCache(): void
|
|
|
|
|
{
|
|
|
|
|
/** @var \LDC\LDCCRM\Models\Customer */
|
|
|
|
|
$crmCustomer = $this->api_data;
|
|
|
|
|
self::clearCrmCached($crmCustomer->hashcode, 'apiData');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Put customer crm part in cache
|
|
|
|
|
*
|
|
|
|
|
* @template T
|
|
|
|
|
* @param string $uuid The customer uuid.
|
|
|
|
|
* @param string $part_name The part name for the cache path.
|
|
|
|
|
* @param \Closure(void):T $part The part to cache.
|
|
|
|
|
* @param integer $ttl Time to live in seconds for the cache part.
|
|
|
|
|
* @return T
|
|
|
|
|
* @phpcs:disable Squiz.Commenting.FunctionComment.IncorrectTypeHint
|
|
|
|
|
*/
|
|
|
|
|
public static function cacheCrm(string $uuid, string $part_name, \Closure $part, int $ttl = 60 * 60)
|
|
|
|
|
{
|
|
|
|
|
// phpcs:enable
|
2024-01-26 16:24:14 +01:00
|
|
|
// @phpstan-ignore-next-line
|
2023-12-15 15:53:55 +00:00
|
|
|
return Cache::remember("customers.{$uuid}.{$part_name}", $ttl, $part);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Clear customer crm cache part or totally
|
|
|
|
|
*
|
|
|
|
|
* @param string $uuid
|
|
|
|
|
* @param string $part_name
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
public static function clearCrmCached(string $uuid, string $part_name): void
|
|
|
|
|
{
|
|
|
|
|
$cache = "customers.{$uuid}";
|
|
|
|
|
if ($part_name) {
|
|
|
|
|
$cache .= ".{$part_name}";
|
|
|
|
|
}
|
|
|
|
|
Cache::forget($cache);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// * --- END CRM CACHE
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* This mutate Customer attributes to array
|
|
|
|
|
* including only non critical properties and crm values
|
|
|
|
|
* (One that can be send to front office without security breach)
|
|
|
|
|
*
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
|
|
|
|
public function toArray()
|
|
|
|
|
{
|
|
|
|
|
if ($this->hashcode) {
|
2024-01-26 16:24:14 +01:00
|
|
|
/** @var \LDC\LDCCRM\Models\Customer $crmCustomer */
|
|
|
|
|
$crmCustomer = $this->api_data;
|
2023-12-15 15:53:55 +00:00
|
|
|
return collect(array_merge(
|
|
|
|
|
parent::toArray(),
|
2024-01-26 16:24:14 +01:00
|
|
|
(array)$crmCustomer
|
2023-12-15 15:53:55 +00:00
|
|
|
))->intersectByKeys([
|
2024-01-26 16:24:14 +01:00
|
|
|
'email' => '',
|
|
|
|
|
'firstname' => '',
|
|
|
|
|
'lastname' => '',
|
|
|
|
|
'address' => '',
|
|
|
|
|
'address2' => '',
|
|
|
|
|
'postcode' => '',
|
2023-12-15 15:53:55 +00:00
|
|
|
'birth_date' => '',
|
2024-01-26 16:24:14 +01:00
|
|
|
'city' => '',
|
|
|
|
|
'country' => '',
|
|
|
|
|
'opt_in' => '',
|
|
|
|
|
'phone' => ''
|
2023-12-15 15:53:55 +00:00
|
|
|
])->toArray();
|
|
|
|
|
} else {
|
|
|
|
|
return parent::toArray();
|
2024-01-26 16:24:14 +01:00
|
|
|
} //end if
|
2023-12-15 15:53:55 +00:00
|
|
|
}
|
|
|
|
|
}
|