|
@@ -7,31 +7,58 @@ use App\Enums\UserTypeEnum;
|
|
|
use App\Models\Notification;
|
|
use App\Models\Notification;
|
|
|
use App\Models\NotificationSend;
|
|
use App\Models\NotificationSend;
|
|
|
use App\Models\User;
|
|
use App\Models\User;
|
|
|
|
|
+use Illuminate\Contracts\Pagination\LengthAwarePaginator;
|
|
|
use Illuminate\Database\Eloquent\Collection;
|
|
use Illuminate\Database\Eloquent\Collection;
|
|
|
|
|
+use Illuminate\Http\UploadedFile;
|
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Illuminate\Support\Facades\Auth;
|
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
|
|
|
|
class NotificationService
|
|
class NotificationService
|
|
|
{
|
|
{
|
|
|
|
|
+ public function __construct(protected MediaService $mediaService) {}
|
|
|
|
|
+
|
|
|
public function getAll(): Collection
|
|
public function getAll(): Collection
|
|
|
{
|
|
{
|
|
|
return Notification::with('createdBy')->orderBy('created_at', 'desc')->get();
|
|
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
|
|
public function findById(int $id): ?Notification
|
|
|
{
|
|
{
|
|
|
- return Notification::with(['createdBy', 'sends'])->find($id);
|
|
|
|
|
|
|
+ return Notification::with(['createdBy', 'sends', 'media'])->find($id);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
public function create(array $data): Notification
|
|
public function create(array $data): Notification
|
|
|
{
|
|
{
|
|
|
return DB::transaction(function () use ($data) {
|
|
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['created_by'] = Auth::id();
|
|
|
|
|
+ $data['type'] = $data['type'] ?? 'manual';
|
|
|
|
|
+
|
|
|
$notification = Notification::create($data);
|
|
$notification = Notification::create($data);
|
|
|
|
|
|
|
|
|
|
+ if ($image) {
|
|
|
|
|
+ $this->mediaService->upload($image, 'notification', $notification->id, 'imagem');
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
$this->dispatchSends($notification);
|
|
$this->dispatchSends($notification);
|
|
|
|
|
|
|
|
- return $notification;
|
|
|
|
|
|
|
+ return $notification->load('media');
|
|
|
});
|
|
});
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -79,12 +106,12 @@ class NotificationService
|
|
|
default => null,
|
|
default => null,
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
- if ($notification->recipient_position) {
|
|
|
|
|
- $query->whereHas('position', fn($q) => $q->where('name', $notification->recipient_position));
|
|
|
|
|
|
|
+ if ($notification->recipient_position_id) {
|
|
|
|
|
+ $query->where('position_id', $notification->recipient_position_id);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- if ($notification->recipient_sector) {
|
|
|
|
|
- $query->whereHas('sector', fn($q) => $q->where('name', $notification->recipient_sector));
|
|
|
|
|
|
|
+ if ($notification->recipient_sector_id) {
|
|
|
|
|
+ $query->where('sector_id', $notification->recipient_sector_id);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
$users = $query->pluck('id');
|
|
$users = $query->pluck('id');
|