Skip to content
Open
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
36 changes: 33 additions & 3 deletions src/Actions/Incident/CreateIncident.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Cachet\Actions\Incident;

use Cachet\Data\Requests\Incident\CreateIncidentRequestData;
use Cachet\Enums\ComponentStatusEnum;
use Cachet\Models\Component;
use Cachet\Models\Incident;
use Cachet\Models\IncidentTemplate;
Expand All @@ -26,17 +27,25 @@ public function handle(CreateIncidentRequestData $data): Incident

// @todo Dispatch notification that incident was created.

return Incident::create(array_merge(
$incident = Incident::create(array_merge(
['guid' => Str::uuid()],
$data->toArray()
));

if (!empty($data->components)) {
$this->associateComponents($incident, $data->components);
}

return $incident;
}

/**
* Render the incident template with the given data.
*/
private function parseTemplate(IncidentTemplate $template, CreateIncidentRequestData $data): string
{
$firstComponent = !empty($data->components) ? $data->components[0] : null;

$vars = array_merge($data->templateVars, [
'incident' => [
'name' => $data->name,
Expand All @@ -46,11 +55,32 @@ private function parseTemplate(IncidentTemplate $template, CreateIncidentRequest
'notify' => $data->notifications ?? false,
'stickied' => $data->stickied ?? false,
'occurred_at' => $data->occurredAt ?? Carbon::now(),
'component' => $data->componentId ? Component::find($data->componentId) : null,
'component_status' => $data->componentStatus ?? null,
'component' => $firstComponent ? Component::find($firstComponent['component_id']) : null,
'component_status' => $firstComponent ? ComponentStatusEnum::from($firstComponent['component_status']) : null,
'components' => collect($data->components)->map(function ($comp) {
return [
'component' => Component::find($comp['component_id']),
'status' => ComponentStatusEnum::from($comp['component_status']),
];
})->toArray(),
],
]);

return $template->render($vars);
}

/**
* Associate components with the incident.
*/
private function associateComponents(Incident $incident, array $components): void
{
foreach ($components as $componentData) {
$componentId = $componentData['component_id'];
$componentStatus = ComponentStatusEnum::from($componentData['component_status']);

$incident->components()->attach($componentId, ['component_status' => $componentStatus->value]);

Component::query()->find($componentId)?->update(['status' => $componentStatus->value]);
}
}
}
15 changes: 5 additions & 10 deletions src/Data/Requests/Incident/CreateIncidentRequestData.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@
use Cachet\Data\BaseData;
use Cachet\Enums\ComponentStatusEnum;
use Cachet\Enums\IncidentStatusEnum;
use Cachet\Models\Component;
use Illuminate\Validation\Rule;
use Spatie\LaravelData\Attributes\Validation\Enum;
use Spatie\LaravelData\Attributes\Validation\Exists;
use Spatie\LaravelData\Attributes\Validation\Max;
use Spatie\LaravelData\Attributes\Validation\RequiredWithout;
use Spatie\LaravelData\Support\Validation\ValidationContext;
Expand All @@ -29,10 +27,7 @@ public function __construct(
public readonly bool $notifications = false,
public readonly ?string $occurredAt = null,
public readonly array $templateVars = [],
#[Exists(Component::class, 'id')]
public readonly ?int $componentId = null,
#[Enum(ComponentStatusEnum::class)]
public readonly ?ComponentStatusEnum $componentStatus = null,
public readonly array $components = [],
) {}

public static function rules(ValidationContext $context): array
Expand All @@ -47,8 +42,9 @@ public static function rules(ValidationContext $context): array
'notifications' => ['boolean'],
'occurred_at' => ['nullable', 'string'],
'template_vars' => ['array'],
'component_id' => [Rule::exists('components', 'id')],
'component_status' => ['nullable', Rule::enum(ComponentStatusEnum::class), 'required_with:component_id'],
'components' => ['array'],
'components.*.component_id' => [Rule::exists('components', 'id')],
'components.*.component_status' => ['nullable', Rule::enum(ComponentStatusEnum::class), 'required_with:component_id'],
];
}

Expand All @@ -64,8 +60,7 @@ public function withMessage(string $message): self
notifications: $this->notifications,
occurredAt: $this->occurredAt,
templateVars: $this->templateVars,
componentId: $this->componentId,
componentStatus: $this->componentStatus,
components: $this->components,
);
}
}
1 change: 0 additions & 1 deletion src/Http/Resources/Incident.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ public function toAttributes(Request $request): array
'guid' => $this->guid,
'name' => $this->name,
'message' => $this->message,
'component_id' => $this->component_id,
'visible' => $this->visible,
'stickied' => $this->stickied,
'notifications' => $this->notifications,
Expand Down
4 changes: 4 additions & 0 deletions src/Models/Incident.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,10 @@ protected static function boot()
self::creating(function (Incident $model) {
$model->guid = Str::uuid();
});

static::deleting(function ($incident) {
$incident->components()->detach();
});
}

/**
Expand Down
32 changes: 32 additions & 0 deletions tests/Feature/Api/IncidentTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

use Cachet\Enums\IncidentStatusEnum;
use Cachet\Models\Component;
use Cachet\Models\Incident;
use Cachet\Models\IncidentTemplate;
use Laravel\Sanctum\Sanctum;
Expand Down Expand Up @@ -256,6 +257,37 @@
]);
});

it('can create an incident with attached components', function () {
Sanctum::actingAs(User::factory()->create(), ['incidents.manage']);

$component1 = Component::factory()->create();
$component2 = Component::factory()->create();

$response = postJson('/status/api/incidents?include=components', [
'name' => 'New Incident Occurred',
'message' => 'Something went wrong.',
'status' => 2,
'components' => [
['component_id' => $component1->id, 'component_status' => 2],
['component_id' => $component2->id, 'component_status' => 3],
],
]);

$response->assertCreated();
$response->assertJson([
'data' => [
'relationships' => [
'components' => [
'data' => [
['type' => 'components', 'id' => $component1->id],
['type' => 'components', 'id' => $component2->id],
]
]
],
],
]);
});

it('cannot create an incident with bad data', function (array $payload) {
Sanctum::actingAs(User::factory()->create(), ['incidents.manage']);

Expand Down
Loading