countAutomaticGrants(); $userIds = DB::table('company_user') ->whereIn('granted_via', ['auto', 'auto-upgrade']) ->when($this->option('user'), fn ($query, $userId) => $query->where('user_id', (int) $userId)) ->distinct() ->pluck('user_id'); if ($userIds->isEmpty()) { $this->info('Keine automatisch vergebenen Firmenrechte gefunden — nichts zu tun.'); return self::SUCCESS; } $bar = $this->output->createProgressBar($userIds->count()); foreach ($userIds->chunk(200) as $chunk) { User::query()->whereIn('id', $chunk->all())->get()->each(function (User $user) use ($contactAccess, $bar): void { $contactAccess->syncMembershipsForEmail($user); $bar->advance(); }); } $bar->finish(); $this->newLine(); $after = $this->countAutomaticGrants(); $this->info("Abgeglichen: {$userIds->count()} User. Automatische Rechte vorher {$before}, nachher {$after}."); return self::SUCCESS; } private function countAutomaticGrants(): int { return DB::table('company_user') ->where('role', 'responsible') ->whereIn('granted_via', ['auto', 'auto-upgrade']) ->count(); } }