File manager - Edit - /var/www/html/pcadigital/api/app/Http/Services/NotificationService.php
Back
<?php namespace App\Http\Services; use App\Classes\BaseServiceResponse; use App\Enums\NotificationTypesEnum; use App\Enums\OccurrenceStatusEnum; use App\Models\Comment; use App\Models\Historic; use App\Models\Notification; use App\Models\Occurrence; use App\Repositories\NotificationRepository; use Carbon\Carbon; use Illuminate\Http\JsonResponse; use Illuminate\Support\Facades\Log; class NotificationService extends BaseService { private AuthService $authService; private NotificationRepository $notificationRepository; public function __construct() { $this->authService = new AuthService(); $this->notificationRepository = new NotificationRepository(); } public function createWhenHasNewOccurrence(Historic $historic, string $token): void { $response = $this->authService->getUsersByApp($token); $data = json_decode($response->getContent(), true); if ($historic->status == OccurrenceStatusEnum::OPEN) { $this->notifyUsers($historic, $data, NotificationTypesEnum::OCCURRENCE); } if ($historic->status == OccurrenceStatusEnum::OMBUDSMAN || $historic->status == OccurrenceStatusEnum::WITHOUT_ORGAN) { $this->notifyOmbudsman($historic, $data, NotificationTypesEnum::OCCURRENCE); } } public function createWhenHasNewHistoric(Historic $historic, string $token): void { $response = $this->authService->getUsersByApp($token, 'organization_id='.$historic->organ_id); $data = json_decode($response->getContent(), true); if ( $historic->status == OccurrenceStatusEnum::OMBUDSMAN || $historic->status == OccurrenceStatusEnum::WITHOUT_ORGAN || $historic->status == OccurrenceStatusEnum::DISAPPROVED ) { $this->notifyOmbudsman($historic, $data, NotificationTypesEnum::HISTORIC); } else { $this->notifyUsers($historic, $data, NotificationTypesEnum::HISTORIC); } if ($historic->status != OccurrenceStatusEnum::DISAPPROVED->value && $historic->final_commeny == null) { $occurrence = $historic->occurrence; $this->notifyOccurrenceUser($occurrence, NotificationTypesEnum::HISTORIC, $historic); } } public function createWhenHasNewComment(Occurrence $occurrence) { $this->notifyOccurrenceUser($occurrence, NotificationTypesEnum::COMMENT); } private function notifyUsers(Historic $historic, array $data, NotificationTypesEnum $type) { $users = $data['data']['users'] ?? null; if (!$users || !is_array($users)) { Log::warning('Notificação não enviada: chave "data.users" ausente ou inválida.', [ 'data' => $data ]); return; } foreach ($users as $user) { $notification = new Notification(); $notification->auth_uuid = $user['id']; $notification->type = $type->value; $notification->data = json_encode([ 'message' => 'Nova ocorrência criada', 'occurrence_id' => $historic->occurrence->id, 'occurrence_status' => $historic->status ]); $notification->save(); } } private function notifyOmbudsman(Historic $historic, array $data, NotificationTypesEnum $type): void { foreach ($data['data']['users'] as $user) { if (isset($user['department']) && is_array($user['department'])) { foreach ($user['department'] as $department) { if (isset($department['name'])) { $equalIgnoreCase = strcasecmp(OccurrenceStatusEnum::OMBUDSMAN->value, $department['name']) === 0; if ($equalIgnoreCase) { $notification = new Notification(); $notification->auth_uuid = $user['id']; $notification->type = $type->value; $notification->data = json_encode([ 'message' => 'Ocorrencia criada sem departamento definido', 'occurrence_id' => $historic->occurrence->id, 'occurrence_status' => $historic->status ]); $notification->save(); } } } } elseif (isset($user['department']['name'])) { $equalIgnoreCase = strcasecmp(OccurrenceStatusEnum::OMBUDSMAN->value, $user['department']['name']) === 0; if ($equalIgnoreCase) { $notification = new Notification(); $notification->auth_uuid = $user['id']; $notification->type = $type->value; $notification->data = json_encode([ 'message' => 'Ocorrencia criada sem departamento definido', 'occurrence_id' => $historic->occurrence->id, 'occurrence_status' => $historic->status ]); $notification->save(); } } } } public function getAllUnreadByUser(string $userId): BaseServiceResponse { $notifications = $this->notificationRepository->getUnreadByUser($userId); return $this->success( ['notifications' => $notifications] ); } public function setAsRead(Notification $notification): BaseServiceResponse { $notification->read_at = Carbon::now(); $notification->save(); return $this->success( null, JsonResponse::HTTP_ACCEPTED ); } private function notifyOccurrenceUser(Occurrence $occurrence, NotificationTypesEnum $type, Historic $historic = null) { if($type == NotificationTypesEnum::COMMENT) { $message = 'Um novo comentario foi adicionado a ocorrencia'; } if($type == NotificationTypesEnum::HISTORIC ) { $message = 'Status da ocorrencia foi alterado'; } $notification = new Notification(); $notification->auth_uuid = $occurrence->created_by; $notification->type = $type->value; $notification->data = json_encode([ 'message' => $message, 'occurrence_id' => $occurrence->id, 'occurrence_status' => $occurrence->status, ]); $notification->save(); } }
| ver. 1.4 |
Github
|
.
| PHP 8.3.28 | Generation time: 0.02 |
proxy
|
phpinfo
|
Settings