diff --git a/src/Actions/Update/CreateUpdate.php b/src/Actions/Update/CreateUpdate.php index 75f8ecd7..b3b5fd55 100644 --- a/src/Actions/Update/CreateUpdate.php +++ b/src/Actions/Update/CreateUpdate.php @@ -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; @@ -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, + ]); + }); + } } diff --git a/tests/Unit/Actions/Update/CreateUpdateTest.php b/tests/Unit/Actions/Update/CreateUpdateTest.php index 2b334273..f6580c4e 100644 --- a/tests/Unit/Actions/Update/CreateUpdateTest.php +++ b/tests/Unit/Actions/Update/CreateUpdateTest.php @@ -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();