Add missing parts for delete customer (jobs) but deny removing customers (on Model)

This commit is contained in:
Jérémy Munsch 2023-12-01 16:55:25 +01:00
parent 95bb4e3b09
commit 5ba294b496
No known key found for this signature in database
GPG key ID: 18361AF3BFEC0FEC
3 changed files with 27 additions and 0 deletions

View file

@ -22,6 +22,7 @@ class Kernel extends ConsoleKernel
*/
protected function schedule(Schedule $schedule)
{
$schedule->job(DeleteOldCustomers::class)->dailyAt('22:50');
}
/**

View file

@ -0,0 +1,23 @@
<?php
namespace App\Jobs\DeleteOlds;
use App\Models\Customer;
use Illuminate\Support\Facades\Log;
/**
* Delete customers that are older than 36 months
*/
class DeleteOldCustomers
{
/**
* Called when the job will be executed.
*
* @return void
*/
public function handle()
{
Log::debug(__CLASS__ . ' Removing old customers');
Customer::query()->withTrashed()->where('last_connection', '<=', now()->subMonths(36))->forceDelete();
}
}

View file

@ -70,6 +70,9 @@ class Customer extends Authenticatable implements MustVerifyEmail
static::updating(function (self $model) {
static::assertEmailIsUnique($model->id, $model->email);
});
static::deleting(function () {
throw new \RuntimeException('This cannot be done à cause de MesBR');
});
static::deleted(function (self $model) {
if ($model->forceDeleting) {
if ($model->existsInCrm()) {