Skip to content

Commit

Permalink
Merge pull request #332 from ECFMP/fixing-event-filters
Browse files Browse the repository at this point in the history
fix: event participation filters crashing api
  • Loading branch information
AndyTWF authored Dec 10, 2022
2 parents f0f481a + 1aaa917 commit 7415122
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 18 deletions.
28 changes: 18 additions & 10 deletions app/Http/Resources/FlowMeasureResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use App\Helpers\ApiDateTimeFormatter;
use App\Helpers\FlowMeasureFilterApiFormatter;
use App\Models\Event;
use Illuminate\Http\Resources\Json\JsonResource;

class FlowMeasureResource extends JsonResource
Expand All @@ -26,8 +27,8 @@ public function toArray($request): array
'measure' => [
'type' => $this->type,
'value' => $this->isMandatoryRoute()
? $this->mandatory_route
: $this->value,
? $this->mandatory_route
: $this->value,
],
'filters' => $this->formatFilters($this->filters),
'notified_flight_information_regions' => $this->notifiedFlightInformationRegions->pluck('id')->toArray(),
Expand All @@ -46,18 +47,25 @@ private function formatFilters(array $filters): array
);
}

private function formatSingleFilter(string $type, $value): array|int|string
private function formatSingleFilter(string $type, $value): array |int|string
{
return match ($type) {
'ADES', 'ADEP' => FlowMeasureFilterApiFormatter::formatAirportList($value),
'level_above', 'level_below' => (int)$value,
'level' => array_map(fn ($level) => (int)$level, $value),
'member_event', 'member_not_event' => [
'event_id' => (int)$value['event_id'],
'event_api' => $value['event_api'],
'event_vatcan' => $value['event_vatcan'],
],
'level_above', 'level_below' => (int) $value,
'level' => array_map(fn ($level) => (int) $level, $value),
'member_event', 'member_not_event' => $this->formatEventMembershipFilters($value),
default => $value
};
}

private function formatEventMembershipFilters($value): array
{
$event = Event::withTrashed()->where('id', $value)->first();

return [
'event_id' => $event->id,
'event_api' => null,
'event_vatcan' => $event->vatcan_code,
];
}
}
4 changes: 2 additions & 2 deletions database/factories/FlowMeasureFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ public function withMemberEvent(Event $event): static
'type' => 'member_event',
'value' => [
'event_id' => (string) $event->id,
'event_api' => 'testapicode',
'event_api' => null,
'event_vatcan' => 'testvatcancode',
]
]
Expand All @@ -197,7 +197,7 @@ public function withMemberNotEvent(Event $event): static
'type' => 'member_not_event',
'value' => [
'event_id' => (string) $event->id,
'event_api' => 'testapicode',
'event_api' => null,
'event_vatcan' => 'testvatcancode',
]
]
Expand Down
16 changes: 10 additions & 6 deletions tests/Api/FlowMeasureTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,9 @@ public function testItReturnsAFlowMeasureWithALevelFilter()

public function testItReturnsAFlowMeasureWithAMemberEventFilter()
{
$event = Event::factory()->create();
$event = Event::factory()
->withVatcanCode()
->create();
$flowMeasure = FlowMeasure::factory()
->withMemberEvent($event)
->create();
Expand Down Expand Up @@ -326,8 +328,8 @@ public function testItReturnsAFlowMeasureWithAMemberEventFilter()
'type' => 'member_event',
'value' => [
'event_id' => $event->id,
'event_api' => 'testapicode',
'event_vatcan' => 'testvatcancode',
'event_api' => null,
'event_vatcan' => $event->vatcan_code,
],
],
],
Expand All @@ -338,7 +340,9 @@ public function testItReturnsAFlowMeasureWithAMemberEventFilter()

public function testItReturnsAFlowMeasureWithAMemberNotEventFilter()
{
$event = Event::factory()->create();
$event = Event::factory()
->withVatcanCode()
->create();
$flowMeasure = FlowMeasure::factory()
->withMemberNotEvent($event)
->create();
Expand Down Expand Up @@ -369,8 +373,8 @@ public function testItReturnsAFlowMeasureWithAMemberNotEventFilter()
'type' => 'member_not_event',
'value' => [
'event_id' => $event->id,
'event_api' => 'testapicode',
'event_vatcan' => 'testvatcancode',
'event_api' => null,
'event_vatcan' => $event->vatcan_code,
],
],
],
Expand Down

0 comments on commit 7415122

Please sign in to comment.