diff --git a/app/Console/Commands/UpdateAchievementsForSanta.php b/app/Console/Commands/UpdateAchievementsForSanta.php new file mode 100644 index 00000000..014a3375 --- /dev/null +++ b/app/Console/Commands/UpdateAchievementsForSanta.php @@ -0,0 +1,75 @@ +info('Starting the achievement update process for Secret Santa participants.'); + + $participants = $this->getEligibleParticipants(); + + $this->rewardParticipants($participants); + + $this->info('Achievement update process completed.'); + } + + /** + * Retrieve participants eligible for the Secret Santa achievement. + * + * @return \Illuminate\Support\Collection + */ + private function getEligibleParticipants(): Collection + { + return SecretSantaParticipant::with('user') + ->where('status', 'done') + ->get() + ->whenEmpty(function () { + $this->info('No eligible participants found.'); + + return collect(); + }); + } + + /** + * Reward the eligible participants with the Secret Santa achievement. + * + * @param \Illuminate\Support\Collection $participants + */ + private function rewardParticipants(Collection $participants): void + { + $participants->each(function (SecretSantaParticipant $participant) { + $user = $participant->user; + + if ($user === null) { + $this->warn("Participant ID {$participant->id} does not have an associated user."); + } + + $user->reward(SecretSanta::class); + $this->info("Awarded Secret Santa achievement to user: {$user->id}"); + }); + } +}