option('dry-run'); $this->info('RUN Command user:cleanup'.($dryRun ? ' (dry-run)' : '')); \Log::channel('cleanup')->info('COMMAND [user:cleanup] started.'.($dryRun ? ' dry-run' : '')); $this->runStage( 'Harter Cut (Ablauf vor mind. '.$suspension->deleteDays().' Tagen)', $suspension->terminationCandidates(), $suspension, $dryRun, fn (User $user) => $suspension->terminate($user), ); $this->runStage( 'Pausieren (Ablauf vor mind. '.$suspension->suspendDays().' Tagen)', $suspension->suspensionCandidates(), $suspension, $dryRun, fn (User $user) => $suspension->suspend($user, 'user:cleanup'), ); \Log::channel('cleanup')->info('COMMAND [user:cleanup] finished.'); return self::SUCCESS; } /** * @param iterable $candidates * @param callable(User): (array|null) $action */ private function runStage(string $label, iterable $candidates, ConsultantSuspensionService $suspension, bool $dryRun, callable $action): void { $rows = []; $done = 0; $failed = 0; foreach ($candidates as $user) { $counts = $dryRun ? $suspension->preview($user) : null; if (! $dryRun) { try { $counts = $action($user); } catch (\Throwable $e) { \Log::channel('cleanup')->error('user:cleanup '.$label.' fehlgeschlagen fuer user_id '.$user->id.': '.$e->getMessage()); $counts = null; } $counts === null ? $failed++ : $done++; } $rows[] = [ $user->id, trim($user->account?->first_name.' '.$user->account?->last_name), $user->email, $user->payment_account ? Carbon::parse($user->payment_account)->format('d.m.Y') : '', $counts['team'] ?? '-', $counts['customers'] ?? '-', $counts['abos'] ?? '-', $counts['portal_customers'] ?? '-', $counts['parked'] ?? '-', ]; } $this->line(''); $this->info($label.': '.count($rows).' Berater'.($dryRun ? '' : " (erledigt {$done}, übersprungen {$failed})")); if ($rows !== []) { $this->table(['ID', 'Name', 'E-Mail', 'Ablauf', 'Team', 'Kunden', 'Kundenabos', 'Portal-Logins', 'beim Sponsor geparkt'], $rows); } } }