Skip to content

Commit 14548fb

Browse files
authored
Ability to GET each component's group nested under its relationships, when querying schedules or incidents via the API (#357)
1 parent 1198c1f commit 14548fb

4 files changed

Lines changed: 38 additions & 2 deletions

File tree

src/Http/Controllers/Api/IncidentController.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ class IncidentController extends Controller
2828
*/
2929
public const ALLOWED_INCLUDES = [
3030
'components',
31+
'components.group',
3132
'updates',
3233
'user',
3334
];

src/Http/Controllers/Api/ScheduleController.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class ScheduleController extends Controller
3434
public function index()
3535
{
3636
$schedules = QueryBuilder::for(Schedule::class)
37-
->allowedIncludes(['components', 'updates', 'user'])
37+
->allowedIncludes(['components', 'components.group', 'updates', 'user'])
3838
->allowedFilters(['name', AllowedFilter::custom('status', new ScheduleStatusFilter)])
3939
->allowedSorts(['name', 'id', 'scheduled_at', 'completed_at'])
4040
->simplePaginate(request('per_page', 15));
@@ -60,7 +60,7 @@ public function store(CreateScheduleRequestData $data, CreateSchedule $createSch
6060
public function show(Schedule $schedule)
6161
{
6262
$scheduleQuery = QueryBuilder::for(Schedule::class)
63-
->allowedIncludes(['components', 'updates', 'user'])
63+
->allowedIncludes(['components', 'components.group', 'updates', 'user'])
6464
->find($schedule->id);
6565

6666
return ScheduleResource::make($scheduleQuery)

tests/Feature/Api/IncidentTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
use Cachet\Enums\ComponentStatusEnum;
44
use Cachet\Enums\IncidentStatusEnum;
55
use Cachet\Models\Component;
6+
use Cachet\Models\ComponentGroup;
67
use Cachet\Models\Incident;
78
use Cachet\Models\IncidentTemplate;
89
use Laravel\Sanctum\Sanctum;
@@ -188,6 +189,22 @@
188189
]);
189190
});
190191

192+
it('can get an incident with components and their groups', function () {
193+
$group = ComponentGroup::factory()->create();
194+
$component = Component::factory()->for($group, 'group')->create();
195+
$incident = Incident::factory()->create();
196+
$incident->components()->attach($component, ['component_status' => ComponentStatusEnum::partial_outage->value]);
197+
198+
$response = getJson('/status/api/incidents/'.$incident->id.'?include=components.group');
199+
200+
$response->assertOk();
201+
202+
$included = collect($response->json('included'));
203+
204+
expect($included->firstWhere('id', (string) $component->id))->not->toBeNull()
205+
->and($included->firstWhere('attributes.name', $group->name))->not->toBeNull();
206+
});
207+
191208
it('cannot create an incident if not authenticated', function () {
192209
$response = postJson('/status/api/incidents', [
193210
'name' => 'New Incident Occurred',

tests/Feature/Api/ScheduleTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
<?php
22

3+
use Cachet\Enums\ComponentStatusEnum;
34
use Cachet\Enums\ScheduleStatusEnum;
45
use Cachet\Models\Component;
6+
use Cachet\Models\ComponentGroup;
57
use Cachet\Models\Schedule;
68
use Laravel\Sanctum\Sanctum;
79
use Workbench\App\User;
@@ -195,6 +197,22 @@
195197
]);
196198
});
197199

200+
it('can get a schedule with components and their groups', function () {
201+
$group = ComponentGroup::factory()->create();
202+
$component = Component::factory()->for($group, 'group')->create();
203+
$schedule = Schedule::factory()->create();
204+
$schedule->components()->attach($component, ['component_status' => ComponentStatusEnum::partial_outage->value]);
205+
206+
$response = getJson('/status/api/schedules/'.$schedule->id.'?include=components.group');
207+
208+
$response->assertOk();
209+
210+
$included = collect($response->json('included'));
211+
212+
expect($included->firstWhere('id', (string) $component->id))->not->toBeNull()
213+
->and($included->firstWhere('attributes.name', $group->name))->not->toBeNull();
214+
});
215+
198216
it('cannot create a schedule if not authenticated', function () {
199217
$response = postJson('/status/api/schedules', [
200218
'name' => 'New Scheduled Maintenance',

0 commit comments

Comments
 (0)