Skip to content

Commit c3a585c

Browse files
jbrooksukclaude
andauthored
Set linked components to operational when incident is fixed (#333)
Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 1e78e18 commit c3a585c

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

src/Actions/Update/CreateUpdate.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
use Cachet\Data\Requests\IncidentUpdate\CreateIncidentUpdateRequestData;
66
use Cachet\Data\Requests\ScheduleUpdate\CreateScheduleUpdateRequestData;
7+
use Cachet\Enums\ComponentStatusEnum;
8+
use Cachet\Enums\IncidentStatusEnum;
79
use Cachet\Models\Incident;
810
use Cachet\Models\Schedule;
911
use Cachet\Models\Update;
@@ -19,8 +21,24 @@ public function handle(Incident|Schedule $resource, CreateIncidentUpdateRequestD
1921

2022
$resource->updates()->save($update);
2123

24+
if ($resource instanceof Incident && $data->status === IncidentStatusEnum::fixed) {
25+
$this->updateComponentsToOperational($resource);
26+
}
27+
2228
// @todo Dispatch notification that incident was updated.
2329

2430
return $update;
2531
}
32+
33+
/**
34+
* Set all linked components back to operational when an incident is fixed.
35+
*/
36+
private function updateComponentsToOperational(Incident $incident): void
37+
{
38+
$incident->components()->each(function ($component) use ($incident) {
39+
$incident->components()->updateExistingPivot($component->id, [
40+
'component_status' => ComponentStatusEnum::operational,
41+
]);
42+
});
43+
}
2644
}

tests/Unit/Actions/Update/CreateUpdateTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,30 @@
3939
->latestStatus->toEqual(IncidentStatusEnum::identified);
4040
});
4141

42+
it('sets linked component status to operational when incident update status is fixed', function () {
43+
$incident = Incident::factory()->create([
44+
'status' => IncidentStatusEnum::investigating,
45+
]);
46+
47+
$component = \Cachet\Models\Component::factory()->create([
48+
'status' => \Cachet\Enums\ComponentStatusEnum::operational,
49+
]);
50+
51+
$incident->components()->attach($component->id, [
52+
'component_status' => \Cachet\Enums\ComponentStatusEnum::major_outage,
53+
]);
54+
55+
$data = CreateIncidentUpdateRequestData::from([
56+
'message' => 'This issue has been fixed.',
57+
'status' => IncidentStatusEnum::fixed,
58+
]);
59+
60+
app(CreateUpdate::class)->handle($incident, $data);
61+
62+
expect($incident->components()->first()->pivot->component_status)
63+
->toEqual(\Cachet\Enums\ComponentStatusEnum::operational);
64+
});
65+
4266
it('can create a schedule update', function () {
4367
$schedule = Schedule::factory()->create();
4468

0 commit comments

Comments
 (0)