Skip to content

Commit

Permalink
fix: server errors for OBS trying to apply
Browse files Browse the repository at this point in the history
Fixes #1012 in store function as well
  • Loading branch information
blt950 committed Sep 17, 2024
1 parent 90f4ef0 commit 3950aa2
Showing 1 changed file with 25 additions and 13 deletions.
38 changes: 25 additions & 13 deletions app/Http/Controllers/TrainingController.php
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ public function apply()
if (App::environment('production')) {
$res = $client->request('GET', 'https://api.vatsim.net/v2/members/' . $user->id . '/stats');
} else {
$res = $client->request('GET', 'https://api.vatsim.net/v2/members/1830299/stats');
$res = $client->request('GET', 'https://api.vatsim.net/v2/members/819096/stats');
}

// Process the data if we got a 200 OK
Expand Down Expand Up @@ -278,22 +278,34 @@ public function store(Request $request)
// Check if user fulfill rating hour requirement
$vatsimStats = [];
$client = new \GuzzleHttp\Client();
if (App::environment('production')) {
$res = $client->request('GET', 'https://api.vatsim.net/v2/members/' . \Auth::id() . '/stats');
} else {
$res = $client->request('GET', 'https://api.vatsim.net/v2/members/819096/stats');
}

if ($res->getStatusCode() == 200) {
$vatsimStats = json_decode($res->getBody(), true);

if (isset($vatsimStats[strtolower(\Auth::user()->rating_short)])) {
$vatsimHours = $vatsimStats[strtolower(\Auth::user()->rating_short)];
try {
if (App::environment('production')) {
$res = $client->request('GET', 'https://api.vatsim.net/v2/members/' . \Auth::id() . '/stats');
} else {
$res = $client->request('GET', 'https://api.vatsim.net/v2/members/819096/stats');
}

// Process the data if we got a 200 OK
if ($res->getStatusCode() == 200) {
$vatsimStats = json_decode($res->getBody(), true);

if (isset($vatsimStats[strtolower(\Auth::user()->rating_short)])) {
$vatsimHours = $vatsimStats[strtolower(\Auth::user()->rating_short)];
} else {
$vatsimHours = 0;
}
} else {
return redirect()->back()->withErrors('We were unable to load the application for you due to missing data from VATSIM. Please try again later.');
}
} catch (\GuzzleHttp\Exception\ClientException $e) {

// If the resource returns 404 and user is S1, it just means the user has no hours yet and can apply for training
if ($e->getResponse()->getStatusCode() == 404 && \Auth::user()->rating == VatsimRating::OBS->value) {
$vatsimHours = 0;
} else {
return redirect()->back()->withErrors('An error occurred while fetching data from VATSIM. Please try again later.');
}
} else {
return redirect()->back()->withErrors('We were unable to submit the application for you due to missing data from VATSIM. Please try again later.');
}

// Loop through the ratings applied for
Expand Down

0 comments on commit 3950aa2

Please sign in to comment.