From e46ebfc63aba235f03de52963193c63e6e884318 Mon Sep 17 00:00:00 2001 From: Miguel N Date: Tue, 16 Nov 2021 17:00:01 +0000 Subject: [PATCH] Decrease vacancy slots by one when firing approval event --- app/Console/Commands/CountVotes.php | 3 +-- app/Listeners/PromoteUser.php | 1 + app/Vacancy.php | 15 +++++++++++++++ 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/app/Console/Commands/CountVotes.php b/app/Console/Commands/CountVotes.php index c15ce68..af0c549 100755 --- a/app/Console/Commands/CountVotes.php +++ b/app/Console/Commands/CountVotes.php @@ -108,8 +108,7 @@ class CountVotes extends Command if ($pollResult) { $this->info('✓ Dispatched promotion event for applicant '.$application->user->name); if (! $this->option('dryrun')) { - $application->response->vacancy->vacancyCount -= 1; - $application->response->vacancy->save(); + $application->response->vacancy->decrease(); event(new ApplicationApprovedEvent(Application::find($application->id))); } else { diff --git a/app/Listeners/PromoteUser.php b/app/Listeners/PromoteUser.php index a3ca047..4b046a5 100755 --- a/app/Listeners/PromoteUser.php +++ b/app/Listeners/PromoteUser.php @@ -47,6 +47,7 @@ class PromoteUser public function handle(ApplicationApprovedEvent $event) { $event->application->setStatus('APPROVED'); + $event->application->response->vacancy->decrease(); $staffProfile = StaffProfile::create([ 'userID' => $event->application->user->id, diff --git a/app/Vacancy.php b/app/Vacancy.php index 0829a4e..0223f06 100755 --- a/app/Vacancy.php +++ b/app/Vacancy.php @@ -89,6 +89,21 @@ class Vacancy extends Model Log::warning('Vacancies: Vacancy '.$this->id.' ('.$this->vacancyName.') closed by '.Auth::user()->name); } + public function decrease() + { + if ($this->vacancyCount !== 0) + { + $this->update([ + 'vacancyCount' => $this->vacancyCount - 1 + ]); + + Log::info('Vacancies: Decreased vacancy slots by one.', [ + 'vacancyId' => $this->id, + 'vacancyName' => $this->vacancyName + ]); + } + } + /** * Check if the Modal is attached to the $checkingTeam Model. *