Skip to content

Commit

Permalink
fix: soft delete member if not found in API (#3483)
Browse files Browse the repository at this point in the history
  • Loading branch information
AxonC authored Feb 23, 2024
1 parent 0623302 commit 73f57a1
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
21 changes: 20 additions & 1 deletion app/Jobs/UpdateMember.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Http\Client\RequestException;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Http;
Expand Down Expand Up @@ -69,7 +70,11 @@ public function handle()
try {
$response = Http::withHeaders([
'Authorization' => $token,
])->get($url)->json();
])->get($url);

$response->throwIfStatus(404);

$response = $response->json();

/**
* For non-division members fields pertaining to personal information
Expand All @@ -92,6 +97,20 @@ public function handle()
'pilottime' => (string) 0,
'cid' => $response['id'],
];
} catch (RequestException $e) {
if ($e->response->status() === 404) {
Log::info("Member {$this->accountID} not found in VATSIM API. Deleting.");
$member->delete();

return;
}

Bugsnag::notifyException($e, function ($report) {
$report->setSeverity('error');
$report->setMetaData([
'accountID' => $this->accountID,
]);
});
} catch (\Exception $e) {
Bugsnag::notifyException($e, function ($report) {
$report->setSeverity('error');
Expand Down
20 changes: 20 additions & 0 deletions tests/Unit/Mship/UpdateMemberJobTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,24 @@ public function test_updates_non_home_members()
'cert_checked_at' => Carbon::now()->toDateTimeString(),
]);
}

public function test_should_delete_member_when_api_returns_404()
{
$existingMember = Account::factory()->create();
$existingMember->cert_checked_at = Carbon::now()->subDay();
$existingMember->addState(State::findByCode('DIVISION'));
$existingMember->addQualification(Qualification::code('S2')->first());
$existingMember->save();

$url = config('vatsim-api.base')."members/{$existingMember->id}";
Http::fake([
$url => Http::response('', 404),
]);

UpdateMember::dispatchSync($existingMember->id);

$this->assertSoftDeleted('mship_account', [
'id' => $existingMember->id,
]);
}
}

0 comments on commit 73f57a1

Please sign in to comment.