-
Notifications
You must be signed in to change notification settings - Fork 37
feat(squads): fundar Squad com ciclo de vida #382
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: feat/squads
Are you sure you want to change the base?
Changes from all commits
b937858
60d7250
07f9851
b55750a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace He4rt\Squads\Database\Factories; | ||
|
|
||
| use He4rt\Identity\Tenant\Models\Tenant; | ||
| use He4rt\Squads\Enums\SquadStatus; | ||
| use He4rt\Squads\Models\Squad; | ||
| use Illuminate\Database\Eloquent\Factories\Factory; | ||
| use Illuminate\Support\Str; | ||
|
|
||
| /** | ||
| * @extends Factory<Squad> | ||
| */ | ||
| final class SquadFactory extends Factory | ||
| { | ||
| protected $model = Squad::class; | ||
|
|
||
| /** | ||
| * @return array<string, mixed> | ||
| */ | ||
| public function definition(): array | ||
| { | ||
| $name = fake()->unique()->words(2, asText: true); | ||
|
|
||
| return [ | ||
| 'tenant_id' => Tenant::factory(), | ||
| 'name' => $name, | ||
| 'slug' => Str::slug($name), | ||
| 'objective' => fake()->sentence(), | ||
| 'status' => SquadStatus::Draft, | ||
| ]; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| use He4rt\Squads\Enums\SquadStatus; | ||
| use Illuminate\Database\Migrations\Migration; | ||
| use Illuminate\Database\Schema\Blueprint; | ||
| use Illuminate\Support\Facades\Schema; | ||
|
|
||
| return new class extends Migration | ||
| { | ||
| public function up(): void | ||
| { | ||
| Schema::create('squads', static function (Blueprint $table): void { | ||
| $table->uuid('id')->primary(); | ||
| $table->foreignUuid('tenant_id')->constrained('tenants')->cascadeOnDelete(); | ||
| $table->string('name'); | ||
| $table->string('slug'); | ||
| $table->text('objective')->nullable(); | ||
| $table->string('status', 30)->default(SquadStatus::Draft->value); | ||
| $table->timestampsTz(); | ||
|
|
||
| $table->unique(['tenant_id', 'slug']); | ||
| $table->index(['tenant_id', 'status']); | ||
| }); | ||
| } | ||
|
|
||
| public function down(): void | ||
| { | ||
| Schema::dropIfExists('squads'); | ||
| } | ||
| }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace He4rt\Squads\Actions; | ||
|
|
||
| use He4rt\Identity\User\Models\User; | ||
| use He4rt\Squads\Enums\SquadStatus; | ||
| use He4rt\Squads\Models\Squad; | ||
| use Illuminate\Auth\Access\AuthorizationException; | ||
|
|
||
| final class ActivateSquad | ||
| { | ||
|
Check failure on line 13 in app-modules/squads/src/Actions/ActivateSquad.php
|
||
| public function handle(User $actor, Squad $squad): Squad | ||
| { | ||
| if (!$actor->isAdmin()) { | ||
| throw new AuthorizationException(); | ||
| } | ||
|
|
||
| $squad->update([ | ||
| 'status' => SquadStatus::Active, | ||
| ]); | ||
|
Comment on lines
+5
to
+22
|
||
|
|
||
| return $squad->refresh(); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace He4rt\Squads\Actions; | ||
|
|
||
| use He4rt\Identity\User\Models\User; | ||
| use He4rt\Squads\Enums\SquadStatus; | ||
| use He4rt\Squads\Models\Squad; | ||
| use Illuminate\Auth\Access\AuthorizationException; | ||
|
|
||
| final class ArchiveSquad | ||
| { | ||
|
Check failure on line 13 in app-modules/squads/src/Actions/ArchiveSquad.php
|
||
| public function handle(User $actor, Squad $squad): Squad | ||
| { | ||
| if (!$actor->isAdmin()) { | ||
| throw new AuthorizationException(); | ||
| } | ||
|
|
||
| $squad->update([ | ||
| 'status' => SquadStatus::Archived, | ||
| ]); | ||
|
Comment on lines
+5
to
+22
|
||
|
|
||
| return $squad->refresh(); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace He4rt\Squads\Actions; | ||
|
|
||
| use He4rt\Identity\Tenant\Models\Tenant; | ||
| use He4rt\Identity\User\Models\User; | ||
| use He4rt\Squads\Enums\SquadStatus; | ||
| use He4rt\Squads\Models\Squad; | ||
| use Illuminate\Auth\Access\AuthorizationException; | ||
| use Illuminate\Support\Str; | ||
|
|
||
| final class CreateSquad | ||
| { | ||
|
Check failure on line 15 in app-modules/squads/src/Actions/CreateSquad.php
|
||
| public function handle(User $actor, Tenant $tenant, string $name, ?string $objective = null): Squad | ||
| { | ||
| if (!$actor->isAdmin()) { | ||
| throw new AuthorizationException(); | ||
| } | ||
|
|
||
| /** @var Squad $squad */ | ||
| $squad = Squad::query()->create([ | ||
| 'tenant_id' => $tenant->id, | ||
| 'name' => $name, | ||
| 'slug' => $this->uniqueSlug($tenant, $name), | ||
| 'objective' => $objective, | ||
| 'status' => SquadStatus::Draft, | ||
| ]); | ||
|
|
||
| return $squad; | ||
| } | ||
|
|
||
| private function uniqueSlug(Tenant $tenant, string $name): string | ||
| { | ||
| $baseSlug = Str::slug($name); | ||
| $slug = $baseSlug; | ||
| $suffix = 2; | ||
|
|
||
| while (Squad::query() | ||
| ->whereBelongsTo($tenant) | ||
| ->where('slug', $slug) | ||
| ->exists()) { | ||
| $slug = sprintf('%s-%d', $baseSlug, $suffix); | ||
| $suffix++; | ||
| } | ||
|
|
||
| return $slug; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace He4rt\Squads\Actions; | ||
|
|
||
| use He4rt\Identity\User\Models\User; | ||
| use He4rt\Squads\Enums\SquadStatus; | ||
| use He4rt\Squads\Models\Squad; | ||
| use Illuminate\Auth\Access\AuthorizationException; | ||
|
|
||
| final class DeactivateSquad | ||
| { | ||
|
Check failure on line 13 in app-modules/squads/src/Actions/DeactivateSquad.php
|
||
| public function handle(User $actor, Squad $squad): Squad | ||
| { | ||
| if (!$actor->isAdmin()) { | ||
| throw new AuthorizationException(); | ||
| } | ||
|
|
||
| $squad->update([ | ||
| 'status' => SquadStatus::Inactive, | ||
| ]); | ||
|
Comment on lines
+5
to
+22
|
||
|
|
||
| return $squad->refresh(); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace He4rt\Squads\Enums; | ||
|
|
||
| enum SquadStatus: string | ||
| { | ||
| case Draft = 'draft'; | ||
| case Active = 'active'; | ||
| case Inactive = 'inactive'; | ||
| case Archived = 'archived'; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace He4rt\Squads\Models; | ||
|
|
||
| use Carbon\CarbonInterface; | ||
| use He4rt\Identity\Tenant\Models\Tenant; | ||
| use He4rt\Squads\Database\Factories\SquadFactory; | ||
| use He4rt\Squads\Enums\SquadStatus; | ||
| use Illuminate\Database\Eloquent\Attributes\Table; | ||
| use Illuminate\Database\Eloquent\Attributes\UseFactory; | ||
| use Illuminate\Database\Eloquent\Concerns\HasUuids; | ||
| use Illuminate\Database\Eloquent\Factories\HasFactory; | ||
| use Illuminate\Database\Eloquent\Model; | ||
| use Illuminate\Database\Eloquent\Relations\BelongsTo; | ||
|
|
||
| /** | ||
| * @property string $id | ||
| * @property string $tenant_id | ||
| * @property string $name | ||
| * @property string $slug | ||
| * @property string|null $objective | ||
| * @property SquadStatus $status | ||
| * @property CarbonInterface|null $created_at | ||
| * @property CarbonInterface|null $updated_at | ||
| */ | ||
| #[UseFactory(SquadFactory::class)] | ||
| #[Table(name: 'squads')] | ||
| final class Squad extends Model | ||
| { | ||
| /** @use HasFactory<SquadFactory> */ | ||
| use HasFactory; | ||
| use HasUuids; | ||
|
|
||
| protected $fillable = [ | ||
| 'tenant_id', | ||
| 'name', | ||
| 'slug', | ||
| 'objective', | ||
| 'status', | ||
| ]; | ||
|
|
||
| /** | ||
| * @return BelongsTo<Tenant, $this> | ||
| */ | ||
| public function tenant(): BelongsTo | ||
| { | ||
| return $this->belongsTo(Tenant::class); | ||
| } | ||
|
|
||
| /** | ||
| * @return array<string, string> | ||
| */ | ||
| protected function casts(): array | ||
| { | ||
| return [ | ||
| 'status' => SquadStatus::class, | ||
| ]; | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not a good code practice to reference enum class with a value in a table column even when it will store enum values