'resource'->>'legislation' = 'true' OR e.payload->'resource'->>'document_type' = 'legislation' THEN 'legislation' WHEN e.payload->'resource'->>'editorial_content' IS NOT NULL THEN 'article' ELSE 'document' END"; /** @param array $data */ private function screen(Request $request, string $component, array $data): Response { $response = $request->expectsJson() ? response()->json($data) : Inertia::render($component, $data)->toResponse($request); $response->headers->set('Cache-Control', 'private, no-store'); return $response; } public function index(Request $request, PortalEditorialWorkflow $workflow): Response { abort_unless($workflow->enabled(), 503); $actor = $request->user(); $access = app(DocumentManagementAccess::class); $query = $workflow->queue($actor); $filters = array_filter($request->validate(['state' => 'nullable|in:draft,submitted,in_review,approved,published,returned,rejected,archived', 'kind' => 'nullable|in:article,legislation,document']), fn ($value) => $value !== null && $value !== ''); if (isset($filters['state'])) { $query->where('e.state', $filters['state']); } if (isset($filters['kind'])) { $query->whereRaw(self::KIND.' = ?', [$filters['kind']]); } $items = $query->orderByDesc('e.updated_at')->paginate(25, ['r.id', DB::raw("e.payload->'resource'->>'title' AS title"), 'r.document_type', 'r.published', 'e.id as edition_id', 'e.revision_no', 'e.state', 'e.legal_required', 'u.name as responsible'])->withQueryString(); $opportunities = app(ContentManagement::class)->access($actor)['opportunities'] ?? null; $data = ['items' => $items, 'filters' => $filters, 'notices' => $workflow->notices($actor), 'resourceAccess' => $access->full($actor) || $access->editorial($actor), 'opportunitiesUrl' => $opportunities['index'] ?? null, 'opportunityPending' => $workflow->opportunityPending($actor)]; return $this->screen($request, 'management/editorial-workflow', $data); } public function show(Request $request, int $id, PortalEditorialWorkflow $workflow): Response { $workflow->authorize($request->user(), $id); $resource = DB::table('portal_resources')->where('id', $id)->first(); abort_unless($resource !== null, 404); $selected = $request->validate(['edition' => 'nullable|uuid'])['edition'] ?? null; $latest = $workflow->latest($id); $e = $selected ? DB::table('portal_editorial_editions')->where('resource_id', $id)->where('id', $selected)->first() : $latest; abort_if($selected && ! $e, 404); $payload = $e ? $workflow->payload($e) : null; $independent = ! $e || ! $workflow->requiresIndependent($e) || ! in_array($request->user()->id, $payload['contributors'], true); $data = ['resource' => ['id' => $id, 'title' => $resource->title, 'published' => (bool) $resource->published, 'public_edition_no' => $resource->published ? DB::table('portal_editorial_editions')->where('resource_id', $id)->where('state', 'published')->max('revision_no') : null], 'edition' => $e ? [...(array) $e, 'payload' => $payload, 'fresh' => $e->state === 'published' || $workflow->fresh($e)] : null, 'current' => $e && $e->id === $latest?->id, 'independentRequired' => $e ? $workflow->requiresIndependent($e) : false, 'canApprove' => $independent && app(DocumentManagementAccess::class)->full($request->user()), 'manager' => app(DocumentManagementAccess::class)->full($request->user()), 'reviewers' => User::whereIn('email', config('portal.managers', []))->whereNotNull('email_verified_at')->get(['id', 'name']), 'legalReviewer' => in_array($request->user()->id, config('editorial_workflow.legal_reviewers', []), true), 'history' => DB::table('audit_events')->where('source_system', 'document-access-v1')->where('action', 'like', 'editorial_%')->where('after_data->resource_id', $id)->orderByDesc('id')->get(['id', 'action', 'actor_user_id', 'after_data', 'occurred_at']), 'editions' => DB::table('portal_editorial_editions')->where('resource_id', $id)->orderByDesc('revision_no')->get(['id', 'revision_no', 'state', 'content_hash', 'author_id', 'created_at'])]; return $this->screen($request, 'management/editorial-edition', $data); } public function transition(Request $request, int $id, string $edition, PortalEditorialWorkflow $workflow): Response { $result = $workflow->transition($request->user(), $id, $edition, $request->all()); return $request->expectsJson() ? response()->json(['edition' => $result])->header('Cache-Control', 'private, no-store') : back()->with('success', 'Operação editorial registada.'); } public function file(Request $request, int $id, string $edition, string $document, PortalEditorialWorkflow $workflow): Response { $workflow->authorize($request->user(), $id); $e = DB::table('portal_editorial_editions')->where('id', $edition)->where('resource_id', $id)->first(); abort_unless($e !== null, 404); $file = null; foreach ($workflow->payload($e)['files'] as $entry) { if ($entry['id'] === $document) { $file = $entry; break; } } abort_unless($file !== null, 404); abort_unless($workflow->canReadFile($request->user(), $document), 403); $record = DB::table('documents')->where('id', $document)->first(); abort_unless($record && hash_equals(trim($file['sha256']), trim($record->sha256)) && (int) $file['version_no'] === (int) $record->version_no, 409, 'O anexo mudou; esta edição conserva a referência anterior.'); return $this->binary($record); } private function binary(\stdClass $record): Response { $response = app(VerifiedDocumentBinary::class)->respond($record); $response->headers->set('Cache-Control', 'private, no-store'); $response->headers->set('X-Robots-Tag', 'noindex, nofollow'); return $response; } public function markRead(Request $request, int $notice, PortalEditorialWorkflow $workflow): Response { abort_unless($workflow->enabled(), 503); $n = DB::table('portal_editorial_notifications')->where('id', $notice)->where('user_id', $request->user()->id)->first(); abort_unless($n !== null, 404); abort_unless($workflow->visibleNotice($request->user(), $n), 404); DB::table('portal_editorial_notifications')->where('id', $notice)->where('user_id', $request->user()->id)->whereNull('read_at')->update(['read_at' => now()]); return $request->expectsJson() ? response()->json(['read' => true]) : back(); } }