Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app-modules/squads/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"extra": {
"laravel": {
"providers": [
"He4rt\\Squads\\Providers\\SquadsServiceProvider"
"He4rt\\Squads\\SquadsServiceProvider"
]
}
}
Expand Down
35 changes: 35 additions & 0 deletions app-modules/squads/database/factories/SquadFactory.php
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);

Copy link
Copy Markdown
Contributor

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

$table->timestampsTz();

$table->unique(['tenant_id', 'slug']);
$table->index(['tenant_id', 'status']);
});
}

public function down(): void
{
Schema::dropIfExists('squads');
}
};
26 changes: 26 additions & 0 deletions app-modules/squads/src/Actions/ActivateSquad.php
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

View workflow job for this annotation

GitHub Actions / Perform Rector Check / Run

ThrowIfAndThrowUnlessExceptionsToUseClassStringRector / ThrowIfRector --- Original +++ New @@ -13,9 +13,7 @@ { public function handle(User $actor, Squad $squad): Squad { - if (!$actor->isAdmin()) { - throw new AuthorizationException(); - } + throw_unless($actor->isAdmin(), AuthorizationException::class); $squad->update([ 'status' => SquadStatus::Active,
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();
}
}
26 changes: 26 additions & 0 deletions app-modules/squads/src/Actions/ArchiveSquad.php
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

View workflow job for this annotation

GitHub Actions / Perform Rector Check / Run

ThrowIfAndThrowUnlessExceptionsToUseClassStringRector / ThrowIfRector --- Original +++ New @@ -13,9 +13,7 @@ { public function handle(User $actor, Squad $squad): Squad { - if (!$actor->isAdmin()) { - throw new AuthorizationException(); - } + throw_unless($actor->isAdmin(), AuthorizationException::class); $squad->update([ 'status' => SquadStatus::Archived,
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();
}
}
50 changes: 50 additions & 0 deletions app-modules/squads/src/Actions/CreateSquad.php
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

View workflow job for this annotation

GitHub Actions / Perform Rector Check / Run

ThrowIfAndThrowUnlessExceptionsToUseClassStringRector / ThrowIfRector --- Original +++ New @@ -15,9 +15,7 @@ { public function handle(User $actor, Tenant $tenant, string $name, ?string $objective = null): Squad { - if (!$actor->isAdmin()) { - throw new AuthorizationException(); - } + throw_unless($actor->isAdmin(), AuthorizationException::class); /** @var Squad $squad */ $squad = Squad::query()->create([
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;
}
}
26 changes: 26 additions & 0 deletions app-modules/squads/src/Actions/DeactivateSquad.php
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

View workflow job for this annotation

GitHub Actions / Perform Rector Check / Run

ThrowIfAndThrowUnlessExceptionsToUseClassStringRector / ThrowIfRector --- Original +++ New @@ -13,9 +13,7 @@ { public function handle(User $actor, Squad $squad): Squad { - if (!$actor->isAdmin()) { - throw new AuthorizationException(); - } + throw_unless($actor->isAdmin(), AuthorizationException::class); $squad->update([ 'status' => SquadStatus::Inactive,
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();
}
}
13 changes: 13 additions & 0 deletions app-modules/squads/src/Enums/SquadStatus.php
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';
}
61 changes: 61 additions & 0 deletions app-modules/squads/src/Models/Squad.php
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,
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace He4rt\Squads\Providers;
namespace He4rt\Squads;

use Illuminate\Support\ServiceProvider;

Expand Down
Loading
Loading