Initial commit
This commit is contained in:
47
app/Console/Commands/CleanupPresenceData.php
Normal file
47
app/Console/Commands/CleanupPresenceData.php
Normal file
@@ -0,0 +1,47 @@
|
||||
<?php
|
||||
|
||||
// 6. Console Command for Cleanup
|
||||
|
||||
namespace App\Console\Commands;
|
||||
|
||||
use Illuminate\Console\Command;
|
||||
use Spatie\Activitylog\Models\Activity;
|
||||
|
||||
class CleanupPresenceData extends Command
|
||||
{
|
||||
protected $signature = 'presence:cleanup';
|
||||
protected $description;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
$this->description = 'Clean up old presence activity logs, keeping last ' .
|
||||
timebank_config('presence_settings.keep_last_presence_updates') . ' per profile';
|
||||
}
|
||||
|
||||
public function handle()
|
||||
{
|
||||
// Get all presence activities grouped by causer (profile)
|
||||
$presenceActivities = Activity::where('log_name', 'presence_update')
|
||||
->whereNotNull('causer_id')
|
||||
->whereNotNull('causer_type')
|
||||
->orderBy('created_at', 'desc')
|
||||
->get()
|
||||
->groupBy(function ($activity) {
|
||||
return $activity->causer_type . '_' . $activity->causer_id;
|
||||
});
|
||||
|
||||
$totalDeleted = 0;
|
||||
|
||||
foreach ($presenceActivities as $profileKey => $activities) {
|
||||
// Keep only the latest records for each profile as defined in config
|
||||
if ($activities->count() > timebank_config('presence_settings.keep_last_presence_updates')) {
|
||||
$toDelete = $activities->skip(4)->pluck('id');
|
||||
$deleted = Activity::whereIn('id', $toDelete)->delete();
|
||||
$totalDeleted += $deleted;
|
||||
}
|
||||
}
|
||||
|
||||
$this->info("Deleted {$totalDeleted} old presence records, keeping last " . timebank_config('presence_settings.keep_last_presence_updates') . " per profile.");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user