NotificationService.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <?php
  2. namespace App\Services;
  3. use App\Enums\NotificationRecipientEnum;
  4. use App\Enums\UserStatusEnum;
  5. use App\Enums\UserTypeEnum;
  6. use App\Models\Notification;
  7. use App\Models\NotificationSend;
  8. use App\Models\User;
  9. use Illuminate\Contracts\Pagination\LengthAwarePaginator;
  10. use Illuminate\Database\Eloquent\Collection;
  11. use Illuminate\Http\UploadedFile;
  12. use Illuminate\Support\Facades\Auth;
  13. use Illuminate\Support\Facades\DB;
  14. class NotificationService
  15. {
  16. public function __construct(protected MediaService $mediaService) {}
  17. public function getAll(): Collection
  18. {
  19. return Notification::with('createdBy')->orderBy('created_at', 'desc')->get();
  20. }
  21. public function getAllPaginated(int $perPage = 12): LengthAwarePaginator
  22. {
  23. return Notification::with(['createdBy', 'media'])
  24. ->withCount([
  25. 'sends',
  26. 'sends as seen_count' => fn($q) => $q->where('read', true),
  27. ])
  28. ->orderBy('created_at', 'desc')
  29. ->paginate($perPage);
  30. }
  31. public function findById(int $id): ?Notification
  32. {
  33. return Notification::with(['createdBy', 'sends', 'media'])->find($id);
  34. }
  35. public function create(array $data): Notification
  36. {
  37. return DB::transaction(function () use ($data) {
  38. $image = null;
  39. if (isset($data['image']) && $data['image'] instanceof UploadedFile) {
  40. $image = $data['image'];
  41. }
  42. unset($data['image']);
  43. $data['created_by'] = Auth::id();
  44. $data['type'] = $data['type'] ?? 'manual';
  45. $notification = Notification::create($data);
  46. if ($image) {
  47. $this->mediaService->upload($image, 'notification', $notification->id, 'imagem');
  48. }
  49. $this->dispatchSends($notification);
  50. return $notification->load('media');
  51. });
  52. }
  53. public function getUnreadByUser(int $userId): Collection
  54. {
  55. return NotificationSend::with(['notification', 'notification.media'])
  56. ->where('user_id', $userId)
  57. ->where('read', false)
  58. ->orderBy('created_at', 'desc')
  59. ->get();
  60. }
  61. public function getByUser(int $userId): Collection
  62. {
  63. return NotificationSend::with([
  64. 'notification',
  65. 'notification.media',
  66. ])
  67. ->where('user_id', $userId)
  68. ->orderBy('created_at', 'desc')
  69. ->get();
  70. }
  71. public function markAsRead(int $sendId, int $userId): bool
  72. {
  73. $send = NotificationSend::where('id', $sendId)
  74. ->where('user_id', $userId)
  75. ->first();
  76. if (!$send) {
  77. return false;
  78. }
  79. $send->update(['read' => true, 'read_at' => now()]);
  80. return true;
  81. }
  82. public function createAutoForUser(array $data, int $userId): Notification
  83. {
  84. $data['created_by'] = Auth::id();
  85. $data['type'] = 'automatica';
  86. $notification = Notification::create($data);
  87. NotificationSend::create([
  88. 'notification_id' => $notification->id,
  89. 'user_id' => $userId,
  90. 'read' => false,
  91. ]);
  92. return $notification;
  93. }
  94. public function delete(int $id): bool
  95. {
  96. $model = Notification::find($id);
  97. if (!$model) {
  98. return false;
  99. }
  100. return $model->delete();
  101. }
  102. private function dispatchSends(Notification $notification): void
  103. {
  104. $query = User::query();
  105. match ($notification->recipient) {
  106. NotificationRecipientEnum::ASSOCIADO => $query->where('type', UserTypeEnum::ASSOCIADO->value),
  107. NotificationRecipientEnum::PARCEIRO => $query->where('type', UserTypeEnum::PARCEIRO->value)
  108. ->where('status', '!=', UserStatusEnum::INACTIVE->value),
  109. default => null,
  110. };
  111. if ($notification->recipient_position_id) {
  112. $query->where('position_id', $notification->recipient_position_id);
  113. }
  114. if ($notification->recipient_sector_id) {
  115. $query->where('sector_id', $notification->recipient_sector_id);
  116. }
  117. $users = $query->pluck('id');
  118. $sends = $users->map(fn($userId) => [
  119. 'notification_id' => $notification->id,
  120. 'user_id' => $userId,
  121. 'read' => false,
  122. 'created_at' => now(),
  123. 'updated_at' => now(),
  124. ])->toArray();
  125. NotificationSend::insert($sends);
  126. }
  127. }