From 14e33574fcccfc04d1f1a69c4abe9110491b5133 Mon Sep 17 00:00:00 2001 From: "B.Fatih KOZ" Date: Thu, 23 Sep 2021 22:04:24 +0300 Subject: [PATCH] Default PirepState logic and location change (#1317) * Update PirepService.php Moved `$default_state` logic below `PirepFiled` event, so admins can have some flexibility to define their own default states by listening that event. Any ideas ? Hopefully closes #1312 * Update PirepService.php Added the missing `$pirep->refresh();` --- app/Services/PirepService.php | 31 ++++++++++++++++++------------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/app/Services/PirepService.php b/app/Services/PirepService.php index ecc21505..8f6aed67 100644 --- a/app/Services/PirepService.php +++ b/app/Services/PirepService.php @@ -460,19 +460,6 @@ class PirepService extends Service */ public function submit(Pirep $pirep) { - // Figure out what default state should be. Look at the default - // behavior from the rank that the pilot is assigned to - $default_state = PirepState::PENDING; - if ($pirep->source === PirepSource::ACARS) { - if ($pirep->user->rank->auto_approve_acars) { - $default_state = PirepState::ACCEPTED; - } - } else { - if ($pirep->user->rank->auto_approve_manual) { - $default_state = PirepState::ACCEPTED; - } - } - // Check if there is a simbrief_id, change it to be set to the PIREP // at the end of the flight when it's been submitted finally. // Prefile, Save (as draft) and File already have this but the Submit button @@ -489,6 +476,24 @@ class PirepService extends Service Log::info('New PIREP filed', [$pirep]); event(new PirepFiled($pirep)); + $pirep->refresh(); + + // Figure out what pirep state should be, if nothing provided yet. + if ($pirep->state != PirepState::ACCEPTED || $pirep->state != PirepState::REJECTED) { + $default_state = PirepState::PENDING; + } else { + $default_state = $pirep->state; + } + + // If pirep is still at PENDING state decide the default behavior by looking at rank settings + if ($pirep->state === PirepState::PENDING) { + if ($pirep->source === PirepSource::ACARS && $pirep->user->rank->auto_approve_acars) { + $default_state = PirepState::ACCEPTED; + } elseif ($pirep->source === PirepSource::MANUAL && $pirep->user->rank->auto_approve_manual) { + $default_state = PirepState::ACCEPTED; + } + } + // only update the pilot last state if they are accepted if ($default_state === PirepState::ACCEPTED) { $pirep = $this->accept($pirep);