Prechádzať zdrojové kódy

✨ feat(user): atualizar model, resource, service e trait de upload de imagem

Fase: dev | Origin: melhoria-interna
Gustavo Zanatta 1 týždeň pred
rodič
commit
1628738323

+ 7 - 0
app/Http/Controllers/Controller.php

@@ -3,6 +3,7 @@
 namespace App\Http\Controllers;
 
 use Illuminate\Http\JsonResponse;
+use Illuminate\Http\Request;
 
 abstract class Controller
 {
@@ -15,4 +16,10 @@ abstract class Controller
     {
         return response()->json(data: ['message' => $message], status: $code);
     }
+
+    protected function getCookieName(Request $request): string
+    {
+        $appOrigin = $request->header('X-App-Origin', 'default');
+        return "{$appOrigin}_refresh_token";
+    }
 }

+ 16 - 7
app/Http/Resources/UserResource.php

@@ -17,13 +17,22 @@ class UserResource extends JsonResource
     public function toArray(Request $request): array
     {
         return [
-            'id' => $this->id,
-            'name' => $this->name,
-            'email' => $this->email,
-            'language' => $this->language,
-            'type' => $this->type,
-            'created_at' => Carbon::parse($this->created_at)->format('Y-m-d H:i:s'),
-            'updated_at' => Carbon::parse($this->updated_at)->format('Y-m-d H:i:s'),
+            'id'             => $this->id,
+            'name'           => $this->name,
+            'email'          => $this->email,
+            'cpf'            => $this->cpf,
+            'registration'   => $this->registration,
+            'language'       => $this->language,
+            'type'           => $this->type,
+            'status'         => $this->status,
+            'admission_date' => $this->admission_date?->format('d/m/Y'),
+            'expiry_date'    => $this->expiry_date?->format('d/m/Y'),
+            'position_id'    => $this->position_id,
+            'position'       => $this->whenLoaded('position', fn() => ['id' => $this->position->id, 'name' => $this->position->name]),
+            'sector_id'      => $this->sector_id,
+            'sector'         => $this->whenLoaded('sector', fn() => ['id' => $this->sector->id, 'name' => $this->sector->name]),
+            'created_at'     => Carbon::parse($this->created_at)->format('Y-m-d H:i:s'),
+            'updated_at'     => Carbon::parse($this->updated_at)->format('Y-m-d H:i:s'),
         ];
     }
 

+ 40 - 4
app/Models/User.php

@@ -3,13 +3,16 @@
 namespace App\Models;
 
 use App\Enums\LanguageEnum;
+use App\Enums\UserStatusEnum;
 use App\Enums\UserTypeEnum;
 use App\Traits\HasPermissions;
 use Illuminate\Database\Eloquent\Factories\HasFactory;
+use Illuminate\Database\Eloquent\Relations\BelongsTo;
+use Illuminate\Database\Eloquent\Relations\BelongsToMany;
+use Illuminate\Database\Eloquent\Relations\HasMany;
 use Illuminate\Foundation\Auth\User as Authenticatable;
 use Illuminate\Notifications\Notifiable;
 use Laravel\Sanctum\HasApiTokens;
-use Illuminate\Database\Eloquent\Relations\BelongsToMany;
 use Carbon\Carbon;
 
 /**
@@ -65,9 +68,12 @@ class User extends Authenticatable
     {
         return [
             "email_verified_at" => "datetime",
-            "password" => "hashed",
-            "type" => UserTypeEnum::class,
-            "language" => LanguageEnum::class,
+            "password"          => "hashed",
+            "type"              => UserTypeEnum::class,
+            "language"          => LanguageEnum::class,
+            "status"            => UserStatusEnum::class,
+            "admission_date"    => "date",
+            "expiry_date"       => "date",
         ];
     }
 
@@ -120,6 +126,36 @@ class User extends Authenticatable
             ->delete();
     }
 
+    public function position(): BelongsTo
+    {
+        return $this->belongsTo(Position::class);
+    }
+
+    public function sector(): BelongsTo
+    {
+        return $this->belongsTo(Sector::class);
+    }
+
+    public function dependents(): HasMany
+    {
+        return $this->hasMany(UserDependent::class, 'responsible_user_id');
+    }
+
+    public function appointments(): HasMany
+    {
+        return $this->hasMany(Appointment::class);
+    }
+
+    public function notificationSends(): HasMany
+    {
+        return $this->hasMany(NotificationSend::class);
+    }
+
+    public function storeItemInterests(): HasMany
+    {
+        return $this->hasMany(StoreItemInterest::class);
+    }
+
     /**
      * @return BelongsToMany
      */

+ 1 - 1
app/Services/UserService.php

@@ -17,7 +17,7 @@ class UserService
 
     public function getAll(): Collection
     {
-        return User::orderBy("created_at", "desc")->get();
+        return User::with(['position', 'sector'])->orderBy("created_at", "desc")->get();
     }
 
     public function findById(int $id): ?User

+ 0 - 3
app/Traits/UploadsBase64Image.php

@@ -39,13 +39,10 @@ trait UploadsBase64Image
         // Generate a unique filename
         $filename = Str::random(20) . '.' . $type;
 
-        // Define the full path where the file will be stored
         $filePath = $folder . '/' . $filename;
 
-        // Save the file
         Storage::put($filePath, $image);
 
-        // Return the URL of the uploaded image
         return Storage::url($filePath);
     }
 }