Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/Actions/Update/CreateUpdate.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

use Cachet\Data\Requests\IncidentUpdate\CreateIncidentUpdateRequestData;
use Cachet\Data\Requests\ScheduleUpdate\CreateScheduleUpdateRequestData;
use Cachet\Enums\ComponentStatusEnum;
use Cachet\Enums\IncidentStatusEnum;
use Cachet\Models\Incident;
use Cachet\Models\Schedule;
use Cachet\Models\Update;
Expand All @@ -19,8 +21,24 @@ public function handle(Incident|Schedule $resource, CreateIncidentUpdateRequestD

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

if ($resource instanceof Incident && $data->status === IncidentStatusEnum::fixed) {
$this->updateComponentsToOperational($resource);
}

// @todo Dispatch notification that incident was updated.

return $update;
}

/**
* Set all linked components back to operational when an incident is fixed.
*/
private function updateComponentsToOperational(Incident $incident): void
{
$incident->components()->each(function ($component) use ($incident) {
$incident->components()->updateExistingPivot($component->id, [
'component_status' => ComponentStatusEnum::operational,
]);
});
}
}
24 changes: 24 additions & 0 deletions tests/Unit/Actions/Update/CreateUpdateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,30 @@
->latestStatus->toEqual(IncidentStatusEnum::identified);
});

it('sets linked component status to operational when incident update status is fixed', function () {
$incident = Incident::factory()->create([
'status' => IncidentStatusEnum::investigating,
]);

$component = \Cachet\Models\Component::factory()->create([
'status' => \Cachet\Enums\ComponentStatusEnum::operational,
]);

$incident->components()->attach($component->id, [
'component_status' => \Cachet\Enums\ComponentStatusEnum::major_outage,
]);

$data = CreateIncidentUpdateRequestData::from([
'message' => 'This issue has been fixed.',
'status' => IncidentStatusEnum::fixed,
]);

app(CreateUpdate::class)->handle($incident, $data);

expect($incident->components()->first()->pivot->component_status)
->toEqual(\Cachet\Enums\ComponentStatusEnum::operational);
});

it('can create a schedule update', function () {
$schedule = Schedule::factory()->create();

Expand Down