From 6642b49aae0b3f6fd86826c60057624e8dfa063e Mon Sep 17 00:00:00 2001 From: stevan Date: Mon, 20 Oct 2025 18:36:23 +0200 Subject: [PATCH] Priority keep approved status when detele duplicate events --- app/Console/Commands/CleanDuplicateEvents.php | 23 +++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/app/Console/Commands/CleanDuplicateEvents.php b/app/Console/Commands/CleanDuplicateEvents.php index 7051de22f..7f2123be3 100644 --- a/app/Console/Commands/CleanDuplicateEvents.php +++ b/app/Console/Commands/CleanDuplicateEvents.php @@ -46,7 +46,8 @@ public function handle(): int e.title, e.start_date, e.end_date, - e.created_at + e.created_at, + e.status FROM events e WHERE YEAR(e.created_at) = ? AND e.title <> '' @@ -100,18 +101,32 @@ public function handle(): int $rows = []; foreach ($groups as $g) { - $first = $byId[$g[0]] ?? null; + $events = array_map(fn($id) => $byId[$id] ?? null, $g); + + usort($events, function ($a, $b) { + $priority = ['APPROVED' => 1, 'PENDING' => 2, 'REJECTED' => 3]; + $aScore = $priority[$a->status] ?? 4; + $bScore = $priority[$b->status] ?? 4; + return $aScore <=> $bScore; + }); + + $keep = $events[0]->id ?? null; + $deleteIds = array_filter(array_column(array_slice($events, 1), 'id')); + + $first = $events[0] ?? null; $rows[] = [ implode(', ', $g), count($g) . ' items', $first?->title ?? '—', $first?->start_date ?? '—', $first?->end_date ?? '—', + 'Keep ID: ' . ($keep ?? '—'), ]; - $toDelete = array_merge($toDelete, array_slice($g, 1)); + + $toDelete = array_merge($toDelete, $deleteIds); } - $this->table(['Event IDs (Group)', 'Count', 'Title', 'Start date', 'End date'], $rows); + $this->table(['Event IDs (Group)', 'Count', 'Title', 'Start date', 'End date', 'Keep'], $rows); if (!$shouldDelete) { $this->newLine();