Merge branch 'job-clean-database' into 'develop'

feat: Added a daily job to clean contacts, customers and logs older than 3 years

See merge request ldc-web/projets/mesbr!7
This commit is contained in:
Vincent BRIDIER 2025-04-23 13:02:45 +00:00
commit 9fac9bb8b4
2 changed files with 94 additions and 52 deletions

View file

@ -6,6 +6,7 @@ use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
use App\Site;
use App\Campagne;
use App\Jobs\CleanOldDatasJob;
use App\User;
use App\Notification;
use App\Mail\AlertBr;
@ -22,8 +23,7 @@ class Kernel extends ConsoleKernel
*
* @var array
*/
protected $commands = [
];
protected $commands = [];
/**
* Define the application's command schedule.
@ -33,6 +33,9 @@ class Kernel extends ConsoleKernel
*/
protected function schedule(Schedule $schedule)
{
// Clean datas older than 3 years every day at midnight.
$schedule->job(CleanOldDatasJob::class)->daily();
// ---- Commande d'alerte appelé tt les jours de la semaine a 7h00
$schedule->call(function () {
$sites = Site::all();
@ -40,7 +43,7 @@ class Kernel extends ConsoleKernel
$couponDiffusionGlobal = array();
$sendGlobal = false;
foreach ($sites as $site) {
// ---- Pour chaque site
// ---- Pour chaque site
$campagnes = Campagne::where(['camp_siteId' => $site->site_id])->get();
foreach ($campagnes as $campagne) {
$sendMail = false;
@ -54,16 +57,16 @@ class Kernel extends ConsoleKernel
$coupon->coupon_left = $this->getFullQuantity($coupon) - $this->getUsedQuantity($coupon);
if ($coupon->coupon_left < $coupon->coupon_seuil_alert) {
array_push($couponSeuil, $coupon);
array_push($couponSeuilGlobal, [$site->site_nom , $coupon]);
array_push($couponSeuilGlobal, [$site->site_nom, $coupon]);
$sendGlobal = true;
$couponAlert = true;
}
// --- Check MaxDateDiffusion
if ($coupon->coupon_maxdatediffusion != null && strtotime($coupon->coupon_maxdatediffusion) < (time() + (30 * 24 * 60 * 60))) {
// Fin de diffusion dans moins de 30jours
// Fin de diffusion dans moins de 30jours
array_push($couponDiffusion, $coupon);
array_push($couponDiffusionGlobal, [$site->site_nom , $coupon]);
array_push($couponDiffusionGlobal, [$site->site_nom, $coupon]);
$sendGlobal = true;
$couponAlert = true;
}
@ -80,14 +83,14 @@ class Kernel extends ConsoleKernel
}
$sendMail = true;
}
}//end if
}//end foreach
} //end if
} //end foreach
if ($sendMail) {
Mail::to($users)->send(new AlertBr($couponSeuil, $couponDiffusion, $site->site_nom));
}
}//end foreach
}//end foreach
} //end foreach
} //end foreach
if ($sendGlobal) {
$superadmins = array();
$admins = User::where(['roles' => 'superadmin'])->get();
@ -100,48 +103,6 @@ class Kernel extends ConsoleKernel
Mail::to($superadmins)->send(new AlertBrAdmin($couponSeuilGlobal, $couponDiffusionGlobal));
}
})->weekdays()->at('7:00');
// ---- Sauvegarde des stats tous les jours a 23:45
// $schedule->call(function () {
// $campagnes = Campagne::all();
// foreach ($campagnes as $campagne) {
// $last_stat = Statistique::where(['stat_campId' => $campagne->camp_id])->orderBy('created_at', 'DESC')->first();
// if(isset($last_stat->created_at)){
// $dateStart = $last_stat->created_at;
// } else {
// $dateStart = Carbon::now()->setTime(0,0,0)->subMonth()->format('Y-m-d H:i:s');
// }
// $coupons = $campagne->coupons()->get();
// foreach ($coupons as $coupon) {
// if ($campagne->camp_type == 0 || $campagne->camp_type == 2) { //Calcul des BR consommé pour les murs
// $counts = Mur::selectRaw('count(*) as count, DATE_FORMAT(mur_createdate, "%Y-%m-%d") as date')
// ->where(['mur_couponId' => $coupon->coupon_id, 'mur_campid' => $campagne->camp_id])
// ->whereBetween('mur_createdate', [$dateStart, Carbon::now()->setTime(23,59,59)->format('Y-m-d H:i:s')])
// ->groupBy(DB::raw("DATE_FORMAT(mur_createdate, '%Y-%m-%d')"))
// ->pluck('count', 'date');
// foreach($counts as $date => $count){
// $stat = new Statistique();
// $stat->stat_campId = $campagne->camp_id;
// $stat->stat_couponId = $coupon->coupon_id;
// $stat->stat_couponPrinted = $count;
// $stat->created_at = $date;
// $stat->save();
// }
// }
// else { //Calcul des BR consommé pour les paniers
// $count = 0;
// $paniers = Panier::where('panier_campId', $campagne->camp_id)->where(function($query) use ($dateStart){
// $query->whereBetween('panier_createdate', [$dateStart, Carbon::now()->setTime(23,59,59)->format('Y-m-d H:i:s')])
// ->orWhereBetween('panier_updatedate', [$dateStart, Carbon::now()->setTime(23,59,59)->format('Y-m-d H:i:s')]);
// })->get();
// foreach ($paniers as $panier) {
// $panierContentCount = $panier->panierContents()->where(['panier_content_couponId' => $coupon->coupon_id])->sum('panier_content_printed');
// $count = $count + $panierContentCount;
// }
// }
// }
// }
// })->everyFifteenMinutes();
}
/**

View file

@ -0,0 +1,81 @@
<?php
namespace App\Jobs;
use App\Contact;
use App\Log as LogModel;
use App\Models\Customers;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
class CleanOldDatasJob implements ShouldQueue
{
use Dispatchable;
use InteractsWithQueue;
use Queueable;
use SerializesModels;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct()
{
// Construct method.
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
try {
DB::transaction(function () {
// Delete old contacts.
$deletedContact = Contact::query()
->where('contact_updatedate', '<', now()->subYears(3))
->delete();
Log::info(\sprintf(
'CleanOldDatasJob : %s contacts deleted',
$deletedContact
));
// Delete old customers.
$deletedCustomers = Customers::query()
->where('updated_at', '<', now()->subYears(3))
->delete();
Log::info(\sprintf(
'CleanOldDatasJob : %s customers deleted',
$deletedCustomers
));
// Delete old logs.
$deletedLogs = LogModel::query()
->where('created_at', '<', now()->subYears(3))
->delete();
Log::info(\sprintf(
'CleanOldDatasJob : %s logs deleted',
$deletedLogs
));
});
} catch (\Exception $e) {
Log::error(
sprintf(
"CleanOldDatasJob failed - Code : %s - %s",
$e->getCode(),
$e->getMessage(),
)
);
\report($e);
} //end try
}
}