Skip to content
Open
Show file tree
Hide file tree
Changes from 3 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,
);
}
}
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