From 1f54d08e96c6e3af8dcd1be1468024932b339574 Mon Sep 17 00:00:00 2001 From: Calum Towers Date: Thu, 9 Jan 2025 19:26:18 +0000 Subject: [PATCH] wip --- .../VatsimNet/ProcessVatsimNetWebhook.php | 3 +- .../Webhooks/MemberChangedAction.php | 28 ++++++++----------- 2 files changed, 13 insertions(+), 18 deletions(-) diff --git a/app/Http/Controllers/External/VatsimNet/ProcessVatsimNetWebhook.php b/app/Http/Controllers/External/VatsimNet/ProcessVatsimNetWebhook.php index a9743a7c5..63b1dd243 100644 --- a/app/Http/Controllers/External/VatsimNet/ProcessVatsimNetWebhook.php +++ b/app/Http/Controllers/External/VatsimNet/ProcessVatsimNetWebhook.php @@ -17,8 +17,7 @@ public function __invoke() 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(); + dispatch(new $class(request()->json('resource'), $action))->afterResponse(); } } diff --git a/app/Jobs/ExternalServices/VatsimNet/Webhooks/MemberChangedAction.php b/app/Jobs/ExternalServices/VatsimNet/Webhooks/MemberChangedAction.php index 557e5e85d..4cb6531fb 100644 --- a/app/Jobs/ExternalServices/VatsimNet/Webhooks/MemberChangedAction.php +++ b/app/Jobs/ExternalServices/VatsimNet/Webhooks/MemberChangedAction.php @@ -8,24 +8,24 @@ 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 int $memberId; - - protected array $data; + protected Account $account; + protected Collection $data; public function __construct(int $memberId, array $data) { - $this->memberId = $memberId; - $this->data = $data; + $this->account = Account::with('states', 'qualifications')->findOrFail($memberId); + $this->data = collect($data); } public function handle() { - foreach ($this->data['deltas'] as $delta) { + 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']), @@ -38,34 +38,30 @@ public function handle() private function processAccountChange(string $field, mixed $value): void { - Account::firstWhere('id', $this->memberId)->update([ + $this->account->update([ $field => $value, ]); } private function processAtcRatingChange(mixed $value): void { - Account::firstWhere('id', $this->memberId)->updateVatsimRatings(atcRating: $value); + $this->account->updateVatsimRatings(atcRating: $value); } private function processPilotRatingChange(mixed $value): void { - Account::firstWhere('id', $this->memberId)->updateVatsimRatings(pilotRating: $value); + $this->account->updateVatsimRatings(pilotRating: $value); } private function processStateChange(): void { - dump('processing state change'); - - $account = Account::with('states')->firstWhere('id', $this->memberId); - - $currentRegion = $account->primary_permanent_state->pivot->region; - $currentDivision = $account->primary_permanent_state->pivot->division; + $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'); - $account->updateDivision( + $this->account->updateDivision( division: is_null($divisionChange) ? $currentDivision : $divisionChange['after'], region: is_null($regionChange) ? $currentRegion : $regionChange['after'], );