File manager - Edit - /var/www/html/homologBancodetalentos/app/Http/Controllers/Web/TalentBank/NotificationApiController.php
Back
<?php namespace App\Http\Controllers\Web\TalentBank; use App\Http\Controllers\Controller; use App\Http\Services\TalentBank\NotificationService; use App\Models\TalentBank\Candidate; use App\Models\TalentBank\Company; use Illuminate\Http\JsonResponse; use Illuminate\Http\Request; class NotificationApiController extends Controller { public function __construct(private readonly NotificationService $notificationService) {} public function unreadCount(Request $request): JsonResponse { $recipient = $this->resolveRecipient($request); $count = $recipient instanceof Candidate ? $this->notificationService->unreadCountForCandidate($recipient) : $this->notificationService->unreadCountForCompany($recipient); return response()->json(['unread' => $count]); } public function recent(Request $request): JsonResponse { $recipient = $this->resolveRecipient($request); $limit = min(20, max(1, (int) $request->query('limit', 8))); $items = $recipient instanceof Candidate ? $this->notificationService->recentForCandidate($recipient, $limit) : $this->notificationService->recentForCompany($recipient, $limit); return response()->json([ 'items' => $items->map(fn ($n) => $this->formatItem($n, $recipient))->values(), ]); } public function markAsRead(Request $request, int $id): JsonResponse { $recipient = $this->resolveRecipient($request); if ($recipient instanceof Candidate) { $this->notificationService->markCandidateNotificationAsRead($id, $recipient); } else { $this->notificationService->markCompanyNotificationAsRead($id, $recipient); } return response()->json(['ok' => true]); } public function markAllAsRead(Request $request): JsonResponse { $recipient = $this->resolveRecipient($request); if ($recipient instanceof Candidate) { $this->notificationService->markAllCandidateNotificationsAsRead($recipient); } else { $this->notificationService->markAllCompanyNotificationsAsRead($recipient); } return response()->json(['ok' => true]); } private function resolveRecipient(Request $request): Candidate|Company { $user = $request->input('talentBankUser') ?? $request->talentBankUser ?? null; abort_if(! $user instanceof Candidate && ! $user instanceof Company, 403); return $user; } /** * @param \App\Models\TalentBank\CandidateNotification|\App\Models\TalentBank\CompanyNotification $notification */ private function formatItem($notification, Candidate|Company $recipient): array { $actionUrl = $notification->action_url; if (! $actionUrl && $notification->jobListing) { $actionUrl = $recipient instanceof Company ? route('talent-bank.company.jobs.applicants', $notification->jobListing) : route('vagas.show', $notification->jobListing); } return [ 'id' => $notification->id, 'type' => $notification->type, 'title' => $notification->title, 'body' => $notification->body, 'read' => (bool) $notification->read, 'action_url' => $actionUrl, 'created_at' => $notification->created_at?->toIso8601String(), 'created_human' => $notification->created_at?->diffForHumans(), ]; } }
| ver. 1.4 |
Github
|
.
| PHP 8.3.28 | Generation time: 0.02 |
proxy
|
phpinfo
|
Settings