| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129 |
- <?php
- namespace App\Services;
- use App\Enums\NotificationRecipientEnum;
- use App\Enums\UserTypeEnum;
- use App\Models\Notification;
- use App\Models\NotificationSend;
- use App\Models\User;
- use Illuminate\Contracts\Pagination\LengthAwarePaginator;
- use Illuminate\Database\Eloquent\Collection;
- use Illuminate\Http\UploadedFile;
- use Illuminate\Support\Facades\Auth;
- use Illuminate\Support\Facades\DB;
- class NotificationService
- {
- public function __construct(protected MediaService $mediaService) {}
- public function getAll(): Collection
- {
- return Notification::with('createdBy')->orderBy('created_at', 'desc')->get();
- }
- public function getAllPaginated(int $perPage = 12): LengthAwarePaginator
- {
- return Notification::with(['createdBy', 'media'])
- ->withCount([
- 'sends',
- 'sends as seen_count' => fn($q) => $q->where('read', true),
- ])
- ->orderBy('created_at', 'desc')
- ->paginate($perPage);
- }
- public function findById(int $id): ?Notification
- {
- return Notification::with(['createdBy', 'sends', 'media'])->find($id);
- }
- public function create(array $data): Notification
- {
- return DB::transaction(function () use ($data) {
- $image = null;
- if (isset($data['image']) && $data['image'] instanceof UploadedFile) {
- $image = $data['image'];
- }
- unset($data['image']);
- $data['created_by'] = Auth::id();
- $data['type'] = $data['type'] ?? 'manual';
- $notification = Notification::create($data);
- if ($image) {
- $this->mediaService->upload($image, 'notification', $notification->id, 'imagem');
- }
- $this->dispatchSends($notification);
- return $notification->load('media');
- });
- }
- public function getUnreadByUser(int $userId): Collection
- {
- return NotificationSend::with('notification')
- ->where('user_id', $userId)
- ->where('read', false)
- ->orderBy('created_at', 'desc')
- ->get();
- }
- public function markAsRead(int $sendId, int $userId): bool
- {
- $send = NotificationSend::where('id', $sendId)
- ->where('user_id', $userId)
- ->first();
- if (!$send) {
- return false;
- }
- $send->update(['read' => true, 'read_at' => now()]);
- return true;
- }
- public function delete(int $id): bool
- {
- $model = Notification::find($id);
- if (!$model) {
- return false;
- }
- return $model->delete();
- }
- private function dispatchSends(Notification $notification): void
- {
- $query = User::query();
- match ($notification->recipient) {
- NotificationRecipientEnum::ASSOCIADO => $query->where('type', UserTypeEnum::ASSOCIADO->value),
- NotificationRecipientEnum::PARCEIRO => $query->where('type', UserTypeEnum::PARCEIRO->value),
- default => null,
- };
- if ($notification->recipient_position_id) {
- $query->where('position_id', $notification->recipient_position_id);
- }
- if ($notification->recipient_sector_id) {
- $query->where('sector_id', $notification->recipient_sector_id);
- }
- $users = $query->pluck('id');
- $sends = $users->map(fn($userId) => [
- 'notification_id' => $notification->id,
- 'user_id' => $userId,
- 'read' => false,
- 'created_at' => now(),
- 'updated_at' => now(),
- ])->toArray();
- NotificationSend::insert($sends);
- }
- }
|