-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2eb48f2
commit 18cff1b
Showing
6 changed files
with
157 additions
and
14 deletions.
There are no files selected for viewing
28 changes: 28 additions & 0 deletions
28
app/Http/Controllers/External/VatsimNet/ProcessVatsimNetWebhook.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { | ||
if (class_exists($class = config("services.vatsim-net.webhook.jobs.{$action['action']}"))) { | ||
dispatch(new $class(request()->json('resource'), $action)); | ||
// ->afterResponse(); | ||
} | ||
} | ||
|
||
return response()->json([ | ||
'status' => 'ok', | ||
]); | ||
} | ||
} |
74 changes: 74 additions & 0 deletions
74
app/Jobs/ExternalServices/VatsimNet/Webhooks/MemberChangedAction.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
<?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; | ||
|
||
class MemberChangedAction 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() | ||
{ | ||
foreach ($this->data['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($delta['after']), | ||
default => null | ||
}; | ||
} | ||
} | ||
|
||
private function processAccountChange(string $field, mixed $value): void | ||
{ | ||
Account::firstWhere('id', $this->memberId)->update([ | ||
$field => $value, | ||
]); | ||
} | ||
|
||
private function processAtcRatingChange(mixed $value): void | ||
{ | ||
Account::firstWhere('id', $this->memberId)->updateVatsimRatings(atcRating: $value); | ||
} | ||
|
||
private function processPilotRatingChange(mixed $value): void | ||
{ | ||
Account::firstWhere('id', $this->memberId)->updateVatsimRatings(pilotRating: $value); | ||
} | ||
|
||
private function processStateChange(mixed $value): void | ||
{ | ||
// if both a division and region is changed in the deltas | ||
// this will run twice, which is not ideal | ||
|
||
$account = Account::with('states')->firstWhere('id', $this->memberId); | ||
|
||
$currentRegion = $account->primary_permanent_state->pivot->region; | ||
$currentDivision = $account->primary_permanent_state->pivot->division; | ||
|
||
$regionChange = collect($this->data['deltas'])->firstWhere('field', 'region_id'); | ||
$divisionChange = collect($this->data['deltas'])->firstWhere('field', 'division_id'); | ||
|
||
$account->updateDivision( | ||
division: is_null($divisionChange) ? $currentDivision : $divisionChange['after'], | ||
region: is_null($regionChange) ? $currentRegion : $regionChange['after'], | ||
); | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
app/Jobs/ExternalServices/VatsimNet/Webhooks/MemberCreatedAction.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters