Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TECH-232: Add support for VATSIM.net Webhooks #3948

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace App\Http\Controllers\External\VatsimNet;

use App\Http\Controllers\BaseController;

class ProcessVatsimNetWebhook extends BaseController
{
public function __invoke()
{
if (request()->header('Authorization') !== config('services.vatsim-net.webhook.key')) {
return response()->json([
'status' => 'forbidden',
], 403);
}

foreach (request()->json('actions') as $action) {
$class = config("services.vatsim-net.webhook.jobs.{$action['action']}");
if ($class && class_exists($class)) {
dispatch(new $class(request()->json('resource'), $action))->afterResponse();
}
}

return response()->json([
'status' => 'ok',
]);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

namespace App\Jobs\ExternalServices\VatsimNet\Webhooks;

use App\Models\Mship\Account;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Collection;

class MemberChangedAction implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

protected Account $account;

protected Collection $data;

public function __construct(int $memberId, array $data)
{
$this->account = Account::with('states', 'qualifications')->findOrFail($memberId);
$this->data = collect($data);
}

public function handle()
{
foreach ($this->data->get('deltas') as $delta) {
match ($delta['field']) {
'id', 'name_first', 'name_last', 'email', 'reg_date' => $this->processAccountChange($delta['field'], $delta['after']),
'rating' => $this->processAtcRatingChange($delta['after']),
'pilotrating' => $this->processPilotRatingChange($delta['after']),
'division_id', 'region_id' => $this->processStateChange(),
default => null
};
}
}

private function processAccountChange(string $field, mixed $value): void
{
$this->account->update([
$field => $value,
]);
}

private function processAtcRatingChange(mixed $value): void
{
$this->account->updateVatsimRatings(atcRating: $value);
}

private function processPilotRatingChange(mixed $value): void
{
$this->account->updateVatsimRatings(pilotRating: $value);
}

private function processStateChange(): void
{
$currentRegion = $this->account->primary_permanent_state->pivot->region;
$currentDivision = $this->account->primary_permanent_state->pivot->division;

$regionChange = collect($this->data['deltas'])->firstWhere('field', 'region_id');
$divisionChange = collect($this->data['deltas'])->firstWhere('field', 'division_id');

$this->account->updateDivision(
division: is_null($divisionChange) ? $currentDivision : $divisionChange['after'],
region: is_null($regionChange) ? $currentRegion : $regionChange['after'],
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace App\Jobs\ExternalServices\VatsimNet\Webhooks;

use App\Models\Mship\Account;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Arr;

class MemberCreatedAction implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

protected int $memberId;

protected array $data;

public function __construct(int $memberId, array $data)
{
$this->memberId = $memberId;
$this->data = $data;
}

public function handle()
{
$account = Account::updateOrCreate(['id' => $this->getField('id')], [
'name_first' => $this->getField('name_first'),
'name_last' => $this->getField('name_last'),
'email' => $this->getField('email'),
'joined_at' => $this->getField('reg_date'),
]);
$account->updateVatsimRatings($this->getField('rating'), $this->getField('pilotrating'));
$account->updateDivision($this->getField('division_id'), $this->getField('region_id'));
$account->save();
}

private function getField(string $field)
{
return Arr::get(collect($this->data['deltas'])->firstWhere('field', $field), 'after');
}
}
2 changes: 1 addition & 1 deletion app/Models/Mship/Concerns/HasQualifications.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public function removeQualification(Qualification $qualification)
* @param int|null $atcRating The VATSIM ATC rating
* @param int|null $pilotRating The VATSIM pilot rating
*/
public function updateVatsimRatings(?int $atcRating, ?int $pilotRating)
public function updateVatsimRatings(?int $atcRating = null, ?int $pilotRating = null)
{
if ($atcRating === 0) {
$this->addNetworkBan('Network ban discovered via Cert login.');
Expand Down
7 changes: 7 additions & 0 deletions config/services.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
<?php

use App\Jobs\ExternalServices\VatsimNet\Webhooks\MemberChangedAction;
use App\Jobs\ExternalServices\VatsimNet\Webhooks\MemberCreatedAction;

return [

/*
Expand Down Expand Up @@ -78,6 +81,10 @@
'vatsim-net' => [
'webhook' => [
'key' => env('VATSIM_NET_WEBHOOK_KEY'),
'jobs' => [
'member_created_action' => MemberCreatedAction::class,
'member_changed_action' => MemberChangedAction::class,
],
],
'api' => [
'base' => env('VATSIM_API_BASE', 'https://api.vatsim.net/api/'),
Expand Down
16 changes: 3 additions & 13 deletions routes/web-external.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

use App\Http\Controllers\External\VatsimNet\ProcessVatsimNetWebhook;

Route::group([
'prefix' => 'external',
'as' => 'external.',
Expand All @@ -9,19 +11,7 @@
'prefix' => 'vatsim-net',
'as' => 'vatsim-net.',
], function () {

Route::post('webhook', function () {
Log::info(print_r([
'Authorization' => request()->header('Authorization'),
'User-Agent' => request()->header('User-Agent'),
'Body' => request()->all(),
], true));

return response()->json([
'status' => 'ok',
]);
})->name('webhook');

Route::post('webhook', ProcessVatsimNetWebhook::class)->name('webhook');
});

});
Loading