Big changes added events for decisions

This commit is contained in:
Simon Pocrnjič
2025-10-22 23:20:04 +02:00
parent 872b76b012
commit 67ebe4b225
36 changed files with 2240 additions and 189 deletions
+130 -3
View File
@@ -3,10 +3,12 @@
namespace App\Http\Controllers;
use App\Models\Action;
use App\Models\ArchiveSetting;
use App\Models\Decision;
use App\Models\EmailTemplate;
use App\Models\Segment;
use Illuminate\Http\Request;
use Illuminate\Validation\ValidationException;
use Inertia\Inertia;
class WorkflowController extends Controller
@@ -15,9 +17,11 @@ public function index(Request $request)
{
return Inertia::render('Settings/Workflow/Index', [
'actions' => Action::query()->with(['decisions', 'segment'])->withCount('activities')->orderBy('id')->get(),
'decisions' => Decision::query()->with('actions')->withCount('activities')->orderBy('id')->get(),
'decisions' => Decision::query()->with(['actions', 'events'])->withCount('activities')->orderBy('id')->get(),
'segments' => Segment::query()->get(),
'email_templates' => EmailTemplate::query()->where('active', true)->get(['id', 'name', 'entity_types']),
'events' => \App\Models\Event::query()->orderBy('name')->get(['id', 'name', 'key', 'description', 'active']),
'archive_settings' => ArchiveSetting::query()->where('enabled', true)->orderBy('id')->get(['id', 'name']),
]);
}
@@ -87,11 +91,43 @@ public function storeDecision(Request $request)
'actions' => 'nullable|array',
'actions.*.id' => 'required_with:actions.*|integer|exists:actions,id',
'actions.*.name' => 'required_with:actions.*|string|max:50',
'events' => 'nullable|array',
'events.*.id' => 'required_with:events.*|integer|exists:events,id',
'events.*.active' => 'sometimes|boolean',
'events.*.run_order' => 'nullable|integer',
'events.*.config' => 'nullable|array',
]);
$actionIds = collect($attributes['actions'] ?? [])->pluck('id')->toArray();
$eventsPayload = collect($attributes['events'] ?? []);
\DB::transaction(function () use ($attributes, $actionIds) {
// Extra server-side validation for event-specific config keys
$validationErrors = [];
foreach ($eventsPayload as $i => $ev) {
$idEv = isset($ev['id']) ? (int) $ev['id'] : null;
if (! $idEv) {
continue;
}
$eventModel = \App\Models\Event::find($idEv);
$key = $eventModel?->key ?? ($ev['key'] ?? null);
if ($key === 'add_segment') {
$seg = $ev['config']['segment_id'] ?? null;
if (empty($seg) || ! Segment::where('id', $seg)->exists()) {
$validationErrors["events.$i.config.segment_id"] = 'Please select a valid segment for the add_segment event.';
}
} elseif ($key === 'archive_contract') {
$as = $ev['config']['archive_setting_id'] ?? null;
if (empty($as) || ! ArchiveSetting::where('id', $as)->exists()) {
$validationErrors["events.$i.config.archive_setting_id"] = 'Please select a valid archive setting for the archive_contract event.';
}
}
}
if (! empty($validationErrors)) {
throw ValidationException::withMessages($validationErrors);
}
\DB::transaction(function () use ($attributes, $actionIds, $eventsPayload) {
/** @var \App\Models\Decision $row */
$row = Decision::create([
'name' => $attributes['name'],
@@ -103,6 +139,32 @@ public function storeDecision(Request $request)
if (! empty($actionIds)) {
$row->actions()->sync($actionIds);
}
// Attach decision events with pivot attributes
if ($eventsPayload->isNotEmpty()) {
$sync = [];
foreach ($eventsPayload as $ev) {
$id = (int) ($ev['id'] ?? 0);
if ($id <= 0) {
continue;
}
$cfg = $ev['config'] ?? null;
if (is_array($cfg)) {
$cfg = json_encode($cfg);
} elseif (is_string($cfg)) {
$trim = trim($cfg);
$cfg = $trim === '' ? null : $cfg;
} else {
$cfg = null;
}
$sync[$id] = [
'active' => (bool) ($ev['active'] ?? true),
'run_order' => isset($ev['run_order']) ? (int) $ev['run_order'] : null,
'config' => $cfg,
];
}
$row->events()->sync($sync);
}
});
return to_route('settings.workflow')->with('success', 'Decision created successfully!');
@@ -120,11 +182,43 @@ public function updateDecision(int $id, Request $request)
'actions' => 'nullable|array',
'actions.*.id' => 'required_with:actions.*|integer|exists:actions,id',
'actions.*.name' => 'required_with:actions.*|string|max:50',
'events' => 'nullable|array',
'events.*.id' => 'required_with:events.*|integer|exists:events,id',
'events.*.active' => 'sometimes|boolean',
'events.*.run_order' => 'nullable|integer',
'events.*.config' => 'nullable|array',
]);
$actionIds = collect($attributes['actions'] ?? [])->pluck('id')->toArray();
$eventsPayload = collect($attributes['events'] ?? []);
\DB::transaction(function () use ($attributes, $actionIds, $row) {
// Extra server-side validation for event-specific config keys
$validationErrors = [];
foreach ($eventsPayload as $i => $ev) {
$idEv = isset($ev['id']) ? (int) $ev['id'] : null;
if (! $idEv) {
continue;
}
$eventModel = \App\Models\Event::find($idEv);
$key = $eventModel?->key ?? ($ev['key'] ?? null);
if ($key === 'add_segment') {
$seg = $ev['config']['segment_id'] ?? null;
if (empty($seg) || ! Segment::where('id', $seg)->exists()) {
$validationErrors["events.$i.config.segment_id"] = 'Please select a valid segment for the add_segment event.';
}
} elseif ($key === 'archive_contract') {
$as = $ev['config']['archive_setting_id'] ?? null;
if (empty($as) || ! ArchiveSetting::where('id', $as)->exists()) {
$validationErrors["events.$i.config.archive_setting_id"] = 'Please select a valid archive setting for the archive_contract event.';
}
}
}
if (! empty($validationErrors)) {
throw ValidationException::withMessages($validationErrors);
}
\DB::transaction(function () use ($attributes, $actionIds, $eventsPayload, $row) {
$row->update([
'name' => $attributes['name'],
'color_tag' => $attributes['color_tag'] ?? null,
@@ -132,6 +226,39 @@ public function updateDecision(int $id, Request $request)
'email_template_id' => $attributes['email_template_id'] ?? null,
]);
$row->actions()->sync($actionIds);
// Sync decision events with pivot attributes
if ($eventsPayload->isNotEmpty()) {
$sync = [];
foreach ($eventsPayload as $ev) {
$id = (int) ($ev['id'] ?? 0);
if ($id <= 0) {
continue;
}
$cfg = $ev['config'] ?? null;
// ensure string JSON stored; accept already-JSON strings
if (is_array($cfg)) {
$cfg = json_encode($cfg);
} elseif (is_string($cfg)) {
$trim = trim($cfg);
// If not valid JSON, keep raw string (handler side can parse/ignore)
$cfg = $trim === '' ? null : $cfg;
} else {
$cfg = null;
}
$sync[$id] = [
'active' => (bool) ($ev['active'] ?? true),
'run_order' => isset($ev['run_order']) ? (int) $ev['run_order'] : null,
'config' => $cfg,
];
}
$row->events()->sync($sync);
} else {
// If empty provided explicitly, detach all to reflect UI intent
if (array_key_exists('events', $attributes)) {
$row->events()->detach();
}
}
});
return to_route('settings.workflow')->with('success', 'Decision updated successfully!');