is('management/*', 'api/v1/existing-business/review*'); $query = app(BusinessRepository::class)->query(); if ($management) { Gate::authorize('reviewQueue', ExistingBusiness::class); $query->where('owner_user_id', '<>', $request->user()->id)->whereIn('status', [ 'submitted', 'triage', 'in_review', 'needs_information', 'approval_l1', 'approval_l2', 'approved', ]); } else { $query->where('owner_user_id', $request->user()->id); } $filters = $request->validate(['country' => ['nullable', app(CountryCatalogue::class)->rule()], 'origin' => ['nullable', Rule::in([...array_column(app(CountryCatalogue::class)->all(), 'code'), 'unknown'])], 'page' => ['nullable', 'integer', 'min:1']]); $query->orderByDesc('updated_at'); if (! empty($filters['country']) || ! empty($filters['origin'])) { // The private payload is encrypted: filter only the already-authorized collection. $matches = $query->get()->filter(function ($business) use ($filters) { $payload = $business->payload; $origin = $payload['declared_origin_country_code'] ?? null; $validOrigin = in_array($origin, array_column(app(CountryCatalogue::class)->all(), 'code'), true) ? $origin : 'unknown'; return (empty($filters['country']) || ($payload['country'] ?? null) === $filters['country']) && (empty($filters['origin']) || $validOrigin === $filters['origin']); })->values(); $page = (int) ($filters['page'] ?? 1); $records = new LengthAwarePaginator($matches->forPage($page, 20)->values(), $matches->count(), 20, $page, ['path' => $request->url(), 'query' => $request->query()]); } else { $records = $query->paginate(20)->withQueryString(); } $props = ['records' => $records->through(fn ($row) => $this->record($row)), 'filters' => $filters, 'countries' => app(CountryCatalogue::class)->all(), 'management' => $management, 'modalities' => ExistingBusinessDrafts::MODALITIES]; return $request->expectsJson() ? response()->json($props) : Inertia::render('investment/businesses', $props); } public function show(Request $request, string $id): Response|JsonResponse { $business = app(BusinessRepository::class)->find($id); Gate::authorize('view', $business); app(CompatibilityShadow::class)->schedule($id); $props = ['countries' => app(CountryCatalogue::class)->all(), 'sectors' => Sector::where('is_active', true)->orderBy('name')->get(['id', 'name']), 'formSchema' => BusinessFormSchema::fields(), 'record' => $this->record($business), 'canEdit' => $request->user()->can('update', $business), 'owner' => $request->user()->can('owns', $business), 'canReview' => $request->user()->can('review', $business), 'canTriage' => $request->user()->can('triage', $business), 'canApproveLevelOne' => $request->user()->can('approveLevelOne', $business), 'canApproveLevelTwo' => $request->user()->can('approveLevelTwo', $business), 'canPublish' => $request->user()->can('publish', $business), 'modalities' => ExistingBusinessDrafts::MODALITIES, 'objectKinds' => ExistingBusinessObjects::KINDS, 'verificationChecks' => ExistingBusinessVerification::CHECKS, 'verifications' => app(ExistingBusinessVerification::class)->findings($business), 'objects' => ExistingBusinessObject::where('business_id', $id)->orderBy('created_at')->get()->map(fn ($object) => [ ...$object->only(['id', 'kind', 'verification_status']), 'payload' => $object->payload]), 'history' => DB::table('existing_business_revisions')->where('business_id', $id)->orderByDesc('version_no')->limit(50) ->get(['version_no', 'action', 'created_at', 'reason'])->map(fn ($row) => ['version_no' => $row->version_no, 'action' => $row->action, 'created_at' => $row->created_at, 'note' => Crypt::decryptString($row->reason)])]; return $request->expectsJson() ? response()->json($props) : Inertia::render('investment/business', $props); } public function save(Request $request, ExistingBusinessDrafts $drafts, ?string $id = null): JsonResponse { $business = $drafts->save($request->user(), $request->all(), $id); return response()->json($this->record($business), $id ? 200 : 201); } public function transition(Request $request, ExistingBusinessIntake $intake, string $id): JsonResponse { $data = $request->validate(['version_no' => ['required', 'integer', 'min:1'], 'action' => ['required', 'string'], 'note' => ['required', 'string'], 'review_consent' => ['nullable', 'boolean'], 'confirmation_code' => ['nullable', 'string', 'max:100']]); $business = $intake->transition($request->user(), $id, $data['version_no'], $data['action'], $data['note'], (bool) ($data['review_consent'] ?? false), $data['confirmation_code'] ?? null); return response()->json($this->record($business)); } public function verify(Request $request, ExistingBusinessVerification $verification, string $id): JsonResponse { return response()->json($this->record($verification->save($request->user(), $id, $request->all()))); } public function saveObject(Request $request, ExistingBusinessObjects $objects, string $id, ?string $object = null): JsonResponse { $saved = $objects->save($request->user(), $id, $request->all(), $object); return response()->json(['id' => $saved->id, 'verification_status' => 'pending', 'version_no' => app(BusinessRepository::class)->find($id)->version_no], $object ? 200 : 201); } /** @return array */ private function record(ExistingBusiness $business): array { return [...$business->only([ 'id', 'code', 'modality', 'status', 'classification', 'version_no', 'sector_id', 'public_title', 'public_slug', 'activity_code', 'stage', 'objective', 'currency', 'amount_min', 'amount_max', 'available_percentage', 'funds_use', 'submitted_at', 'published_at', 'expires_at', 'next_action', 'next_action_due_at', 'assigned_to', ]), 'payload' => $business->payload, 'territory_paths' => app(InvestmentGeography::class)->displayPaths($business->payload)]; } }