authorizeAbility($request, 'workflows.view'); $owner = $this->ownerRef($request); $organization = $this->organization($request); $workflows = Workflow::owned($owner) ->where('organization_id', $organization->id) ->withCount('steps') ->orderBy('name') ->get(); return view('qms.workflows.index', compact('workflows', 'organization')); } public function create(Request $request): View { $this->authorizeAbility($request, 'workflows.manage'); $owner = $this->ownerRef($request); $organization = $this->organization($request); return view('qms.workflows.create', [ 'organization' => $organization, 'branches' => Branch::owned($owner)->where('organization_id', $organization->id)->orderBy('name')->get(), 'queues' => ServiceQueue::owned($owner)->where('organization_id', $organization->id)->orderBy('name')->get(), 'templates' => config('qms.industry_templates'), 'workflowTemplates' => config('qms.workflow_templates'), ]); } public function store(Request $request): RedirectResponse { $this->authorizeAbility($request, 'workflows.manage'); $owner = $this->ownerRef($request); $organization = $this->organization($request); $validated = $request->validate([ 'name' => ['required', 'string', 'max:255'], 'description' => ['nullable', 'string'], 'branch_id' => ['nullable', 'exists:queue_branches,id'], 'industry_template' => ['nullable', 'string'], 'steps' => ['required', 'array', 'min:1'], 'steps.*.name' => ['required', 'string', 'max:255'], 'steps.*.service_queue_id' => ['nullable', 'exists:queue_service_queues,id'], ]); $workflow = Workflow::create([ 'owner_ref' => $owner, 'organization_id' => $organization->id, 'name' => $validated['name'], 'description' => $validated['description'] ?? null, 'branch_id' => $validated['branch_id'] ?? null, 'industry_template' => $validated['industry_template'] ?? null, 'is_active' => true, ]); foreach ($validated['steps'] as $i => $step) { WorkflowStep::create([ 'workflow_id' => $workflow->id, 'name' => $step['name'], 'service_queue_id' => $step['service_queue_id'] ?? null, 'sort_order' => $i + 1, ]); } AuditLogger::record($owner, 'workflow.created', $organization->id, $owner, Workflow::class, $workflow->id); return redirect()->route('qms.workflows.index')->with('success', 'Workflow created.'); } public function show(Request $request, Workflow $workflow): View { $this->authorizeAbility($request, 'workflows.view'); $this->authorizeOwner($request, $workflow); $workflow->load(['steps.serviceQueue', 'branch']); return view('qms.workflows.show', compact('workflow')); } public function edit(Request $request, Workflow $workflow): View { $this->authorizeAbility($request, 'workflows.manage'); $this->authorizeOwner($request, $workflow); $owner = $this->ownerRef($request); return view('qms.workflows.edit', [ 'workflow' => $workflow->load('steps'), 'branches' => Branch::owned($owner)->where('organization_id', $workflow->organization_id)->orderBy('name')->get(), 'queues' => ServiceQueue::owned($owner)->where('organization_id', $workflow->organization_id)->orderBy('name')->get(), ]); } public function update(Request $request, Workflow $workflow): RedirectResponse { $this->authorizeAbility($request, 'workflows.manage'); $this->authorizeOwner($request, $workflow); $validated = $request->validate([ 'name' => ['required', 'string', 'max:255'], 'description' => ['nullable', 'string'], 'branch_id' => ['nullable', 'exists:queue_branches,id'], 'is_active' => ['boolean'], 'steps' => ['required', 'array', 'min:1'], 'steps.*.name' => ['required', 'string', 'max:255'], 'steps.*.service_queue_id' => ['nullable', 'exists:queue_service_queues,id'], ]); $workflow->update([ 'name' => $validated['name'], 'description' => $validated['description'] ?? null, 'branch_id' => $validated['branch_id'] ?? null, 'is_active' => $request->boolean('is_active', $workflow->is_active), ]); $workflow->steps()->delete(); foreach ($validated['steps'] as $i => $step) { WorkflowStep::create([ 'workflow_id' => $workflow->id, 'name' => $step['name'], 'service_queue_id' => $step['service_queue_id'] ?? null, 'sort_order' => $i + 1, ]); } AuditLogger::record($this->ownerRef($request), 'workflow.updated', $workflow->organization_id, $this->ownerRef($request), Workflow::class, $workflow->id); return redirect()->route('qms.workflows.show', $workflow)->with('success', 'Workflow updated.'); } public function destroy(Request $request, Workflow $workflow): RedirectResponse { $this->authorizeAbility($request, 'workflows.manage'); $this->authorizeOwner($request, $workflow); $workflow->delete(); return redirect()->route('qms.workflows.index')->with('success', 'Workflow removed.'); } }