| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134 |
- <?php
- namespace App\Services;
- use App\Models\UnitUser;
- use App\Models\User;
- use Illuminate\Database\Eloquent\Collection;
- use Illuminate\Http\UploadedFile;
- use Illuminate\Support\Facades\Auth;
- use Illuminate\Support\Facades\Storage;
- class UserService
- {
- public function authUser(): ?User
- {
- return Auth::user()->load('units');
- }
- public function getAll(): Collection
- {
- return User::with(['state', 'units', 'userType'])->orderBy('name')->get();
- }
- public function getAllByUnit(): Collection
- {
- $unitId = request()->input('active_unit_id') ?? Auth::user()->units->first()?->id;
- if (!$unitId) {
- return User::with(['state', 'units', 'userType'])->whereNull('id')->get();
- }
- return User::with(['state', 'units', 'userType'])
- ->whereHas('units', fn($q) => $q->where('units.id', $unitId))
- ->orderBy('name')
- ->get();
- }
- public function findById(int $id): ?User
- {
- return User::with(['state', 'units', 'userType'])->find($id);
- }
- public function create(array $data): User
- {
- // Suporta unit_ids[] (múltiplas) ou unit_id (retrocompatibilidade)
- $unitIds = $data['unit_ids'] ?? null;
- if (empty($unitIds) && isset($data['unit_id'])) {
- $unitIds = array_filter([$data['unit_id']]);
- }
- if (empty($unitIds)) {
- $fallback = Auth::user()->units->first()?->id;
- $unitIds = $fallback ? [$fallback] : [];
- }
- unset($data['unit_ids'], $data['unit_id']);
- $data = $this->handleAvatar($data);
- $user = User::create($data);
- if (!empty($unitIds)) {
- $user->units()->sync($unitIds);
- }
- return $user->load('state', 'units');
- }
- public function update(int $id, array $data): ?User
- {
- $model = $this->findById($id);
- if (!$model) {
- return null;
- }
- // Suporta unit_ids[] (múltiplas) com fallback para unit_id (retrocompatibilidade)
- $hasUnitIds = array_key_exists('unit_ids', $data);
- $unitIds = $data['unit_ids'] ?? null;
- $hasUnitId = array_key_exists('unit_id', $data);
- $unitId = $data['unit_id'] ?? null;
- unset($data['unit_ids'], $data['unit_id']);
- $data = $this->handleAvatar($data, $model->avatar_url);
- $model->update($data);
- if ($hasUnitIds) {
- $model->units()->sync(array_filter($unitIds ?? []));
- } elseif ($hasUnitId) {
- $model->units()->sync($unitId ? [$unitId] : []);
- }
- return $model->fresh(['state', 'units']);
- }
- public function delete(int $id): bool
- {
- $model = $this->findById($id);
- if (!$model) {
- return false;
- }
- if ($model->avatar_url) {
- Storage::delete($model->avatar_url);
- }
- return $model->delete();
- }
- public function getUserTypes(): \Illuminate\Database\Eloquent\Collection
- {
- return \App\Models\UserType::orderBy('label')->get();
- }
- private function handleAvatar(array $data, ?string $oldAvatarPath = null): array
- {
- if (!isset($data['avatar'])) {
- return $data;
- }
- if ($data['avatar'] instanceof UploadedFile) {
- if ($oldAvatarPath) {
- Storage::delete($oldAvatarPath);
- }
- $data['avatar_url'] = $data['avatar']->store('users/avatars');
- } elseif (is_null($data['avatar'])) {
- if ($oldAvatarPath) {
- Storage::delete($oldAvatarPath);
- }
- $data['avatar_url'] = null;
- }
- unset($data['avatar']);
- return $data;
- }
- }
|