-
Notifications
You must be signed in to change notification settings - Fork 41
feat(squads): PromoteToSubCaptain / demote (#358) #501
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
Open
guisaliba
wants to merge
1
commit into
feat/squads
Choose a base branch
from
story/358-promote-to-sub-captain
base: feat/squads
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+396
−0
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace He4rt\Squads\Actions; | ||
|
|
||
| use He4rt\Identity\User\Models\User; | ||
| use He4rt\Squads\Enums\MembershipAction; | ||
| use He4rt\Squads\Enums\SquadRole; | ||
| use He4rt\Squads\Exceptions\NotAnActiveSquadMember; | ||
| use He4rt\Squads\Models\Squad; | ||
| use He4rt\Squads\Models\SquadMember; | ||
| use He4rt\Squads\Policies\SquadPolicy; | ||
| use Illuminate\Support\Facades\DB; | ||
|
|
||
| final readonly class PromoteToSubCaptain | ||
| { | ||
| public function __construct( | ||
| private SquadPolicy $squadPolicy, | ||
| private RecordMembershipEvent $recordMembershipEvent, | ||
| ) {} | ||
|
|
||
| public function handle(User $actor, Squad $squad, User $subject, ?string $reason = null): SquadMember | ||
| { | ||
| $this->squadPolicy->authorize($actor, $squad); | ||
|
|
||
| return $this->transition($squad, $subject, $actor, SquadRole::SubCaptain, $reason); | ||
| } | ||
|
|
||
| public function demote(User $actor, Squad $squad, User $subject, ?string $reason = null): SquadMember | ||
| { | ||
| $this->squadPolicy->authorize($actor, $squad); | ||
|
|
||
| return $this->transition($squad, $subject, $actor, SquadRole::Member, $reason); | ||
| } | ||
|
|
||
| private function transition( | ||
| Squad $squad, | ||
| User $subject, | ||
| User $actor, | ||
| SquadRole $targetRole, | ||
| ?string $reason, | ||
| ): SquadMember { | ||
| return DB::transaction(function () use ($squad, $subject, $actor, $targetRole, $reason): SquadMember { | ||
| $member = SquadMember::query() | ||
| ->where('squad_id', $squad->id) | ||
| ->where('user_id', $subject->id) | ||
| ->whereNot('role', SquadRole::ExMember) | ||
| ->lockForUpdate() | ||
| ->first(); | ||
|
|
||
| throw_if($member === null, NotAnActiveSquadMember::for($squad, $subject)); | ||
|
|
||
| $fromRole = $member->role; | ||
|
|
||
| if ($fromRole === $targetRole) { | ||
| return $member; | ||
| } | ||
|
|
||
| $member->update([ | ||
| 'role' => $targetRole, | ||
| ]); | ||
|
|
||
| $this->recordMembershipEvent->handle( | ||
| squad: $squad, | ||
| subject: $subject, | ||
| action: $this->actionFor($fromRole, $targetRole), | ||
| fromRole: $fromRole, | ||
| toRole: $targetRole, | ||
| actor: $actor, | ||
| reason: $reason, | ||
| ); | ||
|
|
||
| return $member->refresh(); | ||
| }); | ||
| } | ||
|
|
||
| private function actionFor(SquadRole $fromRole, SquadRole $targetRole): MembershipAction | ||
| { | ||
| return $fromRole === SquadRole::Member && $targetRole === SquadRole::SubCaptain | ||
| ? MembershipAction::Promote | ||
| : MembershipAction::Demote; | ||
| } | ||
| } | ||
312 changes: 312 additions & 0 deletions
312
app-modules/squads/tests/Feature/PromoteToSubCaptainTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,312 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| use He4rt\Identity\User\Models\User; | ||
| use He4rt\Squads\Actions\PromoteToSubCaptain; | ||
| use He4rt\Squads\Enums\MembershipAction; | ||
| use He4rt\Squads\Enums\SquadRole; | ||
| use He4rt\Squads\Exceptions\NotAnActiveSquadMember; | ||
| use He4rt\Squads\Models\Squad; | ||
| use He4rt\Squads\Models\SquadMember; | ||
| use He4rt\Squads\Models\SquadMembershipEvent; | ||
| use Illuminate\Auth\Access\AuthorizationException; | ||
|
|
||
| beforeEach(function (): void { | ||
| config(['he4rt.admins' => 'guisaliba']); | ||
|
|
||
| $this->admin = User::factory()->create([ | ||
| 'username' => 'guisaliba', | ||
| ]); | ||
| }); | ||
|
|
||
| test('a captain promotes a member to sub-captain and records the transition', function (): void { | ||
| $squad = Squad::factory()->create(); | ||
| $captain = User::factory()->create(); | ||
| $subject = User::factory()->create(); | ||
|
|
||
| SquadMember::factory()->create([ | ||
| 'squad_id' => $squad->id, | ||
| 'user_id' => $captain->id, | ||
| 'role' => SquadRole::Captain, | ||
| ]); | ||
| SquadMember::factory()->create([ | ||
| 'squad_id' => $squad->id, | ||
| 'user_id' => $subject->id, | ||
| 'role' => SquadRole::Member, | ||
| ]); | ||
|
|
||
| $member = resolve(PromoteToSubCaptain::class)->handle( | ||
| actor: $captain, | ||
| squad: $squad, | ||
| subject: $subject, | ||
| reason: 'Elected off-system.', | ||
| ); | ||
|
|
||
| expect($member->role)->toBe(SquadRole::SubCaptain); | ||
|
|
||
| $this->assertDatabaseHas('squad_membership_events', [ | ||
| 'squad_id' => $squad->id, | ||
| 'user_id' => $subject->id, | ||
| 'actor_id' => $captain->id, | ||
| 'action' => MembershipAction::Promote->value, | ||
| 'from_role' => SquadRole::Member->value, | ||
| 'to_role' => SquadRole::SubCaptain->value, | ||
| 'reason' => 'Elected off-system.', | ||
| ]); | ||
| }); | ||
|
|
||
| test('a captain demotes a sub-captain to member and records the transition', function (): void { | ||
| $squad = Squad::factory()->create(); | ||
| $captain = User::factory()->create(); | ||
| $subject = User::factory()->create(); | ||
|
|
||
| SquadMember::factory()->create([ | ||
| 'squad_id' => $squad->id, | ||
| 'user_id' => $captain->id, | ||
| 'role' => SquadRole::Captain, | ||
| ]); | ||
| SquadMember::factory()->create([ | ||
| 'squad_id' => $squad->id, | ||
| 'user_id' => $subject->id, | ||
| 'role' => SquadRole::SubCaptain, | ||
| ]); | ||
|
|
||
| $member = resolve(PromoteToSubCaptain::class)->demote( | ||
| actor: $captain, | ||
| squad: $squad, | ||
| subject: $subject, | ||
| ); | ||
|
|
||
| expect($member->role)->toBe(SquadRole::Member); | ||
|
|
||
| $this->assertDatabaseHas('squad_membership_events', [ | ||
| 'squad_id' => $squad->id, | ||
| 'user_id' => $subject->id, | ||
| 'actor_id' => $captain->id, | ||
| 'action' => MembershipAction::Demote->value, | ||
| 'from_role' => SquadRole::SubCaptain->value, | ||
| 'to_role' => SquadRole::Member->value, | ||
| ]); | ||
| }); | ||
|
|
||
| test('a captain can become a sub-captain and vacate the captain seat', function (): void { | ||
| $squad = Squad::factory()->create(); | ||
| $captain = User::factory()->create(); | ||
|
|
||
| SquadMember::factory()->create([ | ||
| 'squad_id' => $squad->id, | ||
| 'user_id' => $captain->id, | ||
| 'role' => SquadRole::Captain, | ||
| ]); | ||
|
|
||
| $member = resolve(PromoteToSubCaptain::class)->handle( | ||
| actor: $captain, | ||
| squad: $squad, | ||
| subject: $captain, | ||
| ); | ||
|
|
||
| expect($member->role)->toBe(SquadRole::SubCaptain) | ||
| ->and($squad->captain()->first())->toBeNull(); | ||
|
|
||
| $this->assertDatabaseHas('squad_membership_events', [ | ||
| 'squad_id' => $squad->id, | ||
| 'user_id' => $captain->id, | ||
| 'actor_id' => $captain->id, | ||
| 'action' => MembershipAction::Demote->value, | ||
| 'from_role' => SquadRole::Captain->value, | ||
| 'to_role' => SquadRole::SubCaptain->value, | ||
| ]); | ||
| }); | ||
|
|
||
| test('a super-admin can demote a captain to member', function (): void { | ||
| $squad = Squad::factory()->create(); | ||
| $captain = User::factory()->create(); | ||
|
|
||
| SquadMember::factory()->create([ | ||
| 'squad_id' => $squad->id, | ||
| 'user_id' => $captain->id, | ||
| 'role' => SquadRole::Captain, | ||
| ]); | ||
|
|
||
| $member = resolve(PromoteToSubCaptain::class)->demote( | ||
| actor: $this->admin, | ||
| squad: $squad, | ||
| subject: $captain, | ||
| ); | ||
|
|
||
| expect($member->role)->toBe(SquadRole::Member) | ||
| ->and($squad->captain()->first())->toBeNull(); | ||
|
|
||
| $this->assertDatabaseHas('squad_membership_events', [ | ||
| 'squad_id' => $squad->id, | ||
| 'user_id' => $captain->id, | ||
| 'actor_id' => $this->admin->id, | ||
| 'action' => MembershipAction::Demote->value, | ||
| 'from_role' => SquadRole::Captain->value, | ||
| 'to_role' => SquadRole::Member->value, | ||
| ]); | ||
| }); | ||
|
|
||
| test('a sub-captain can promote a member in their own squad', function (): void { | ||
| $squad = Squad::factory()->create(); | ||
| $subCaptain = User::factory()->create(); | ||
| $subject = User::factory()->create(); | ||
|
|
||
| SquadMember::factory()->create([ | ||
| 'squad_id' => $squad->id, | ||
| 'user_id' => $subCaptain->id, | ||
| 'role' => SquadRole::SubCaptain, | ||
| ]); | ||
| SquadMember::factory()->create([ | ||
| 'squad_id' => $squad->id, | ||
| 'user_id' => $subject->id, | ||
| 'role' => SquadRole::Member, | ||
| ]); | ||
|
|
||
| resolve(PromoteToSubCaptain::class)->handle( | ||
| actor: $subCaptain, | ||
| squad: $squad, | ||
| subject: $subject, | ||
| ); | ||
|
|
||
| $this->assertDatabaseHas('squad_members', [ | ||
| 'squad_id' => $squad->id, | ||
| 'user_id' => $subject->id, | ||
| 'role' => SquadRole::SubCaptain->value, | ||
| ]); | ||
| }); | ||
|
|
||
| test('a common member cannot change a squad role', function (): void { | ||
| $squad = Squad::factory()->create(); | ||
| $actor = User::factory()->create(); | ||
| $subject = User::factory()->create(); | ||
|
|
||
| SquadMember::factory()->create([ | ||
| 'squad_id' => $squad->id, | ||
| 'user_id' => $actor->id, | ||
| 'role' => SquadRole::Member, | ||
| ]); | ||
| SquadMember::factory()->create([ | ||
| 'squad_id' => $squad->id, | ||
| 'user_id' => $subject->id, | ||
| 'role' => SquadRole::Member, | ||
| ]); | ||
|
|
||
| resolve(PromoteToSubCaptain::class)->handle( | ||
| actor: $actor, | ||
| squad: $squad, | ||
| subject: $subject, | ||
| ); | ||
| })->throws(AuthorizationException::class); | ||
|
|
||
| test('a leader cannot change a different squad', function (): void { | ||
| $ownSquad = Squad::factory()->create(); | ||
| $otherSquad = Squad::factory()->create(); | ||
| $captain = User::factory()->create(); | ||
| $subject = User::factory()->create(); | ||
|
|
||
| SquadMember::factory()->create([ | ||
| 'squad_id' => $ownSquad->id, | ||
| 'user_id' => $captain->id, | ||
| 'role' => SquadRole::Captain, | ||
| ]); | ||
| SquadMember::factory()->create([ | ||
| 'squad_id' => $otherSquad->id, | ||
| 'user_id' => $subject->id, | ||
| 'role' => SquadRole::Member, | ||
| ]); | ||
|
|
||
| resolve(PromoteToSubCaptain::class)->handle( | ||
| actor: $captain, | ||
| squad: $otherSquad, | ||
| subject: $subject, | ||
| ); | ||
| })->throws(AuthorizationException::class); | ||
|
|
||
| test('a missing active membership cannot be changed', function (): void { | ||
| $squad = Squad::factory()->create(); | ||
| $subject = User::factory()->create(); | ||
|
|
||
| resolve(PromoteToSubCaptain::class)->handle( | ||
| actor: $this->admin, | ||
| squad: $squad, | ||
| subject: $subject, | ||
| ); | ||
| })->throws(NotAnActiveSquadMember::class); | ||
|
|
||
| test('an ex-member cannot be changed', function (): void { | ||
| $squad = Squad::factory()->create(); | ||
| $subject = User::factory()->create(); | ||
|
|
||
| SquadMember::factory()->create([ | ||
| 'squad_id' => $squad->id, | ||
| 'user_id' => $subject->id, | ||
| 'role' => SquadRole::ExMember, | ||
| ]); | ||
|
|
||
| resolve(PromoteToSubCaptain::class)->handle( | ||
| actor: $this->admin, | ||
| squad: $squad, | ||
| subject: $subject, | ||
| ); | ||
| })->throws(NotAnActiveSquadMember::class); | ||
|
|
||
| test('same-state role changes create no event', function (): void { | ||
| $squad = Squad::factory()->create(); | ||
| $captain = User::factory()->create(); | ||
| $subCaptain = User::factory()->create(); | ||
| $member = User::factory()->create(); | ||
|
|
||
| SquadMember::factory()->create([ | ||
| 'squad_id' => $squad->id, | ||
| 'user_id' => $captain->id, | ||
| 'role' => SquadRole::Captain, | ||
| ]); | ||
| SquadMember::factory()->create([ | ||
| 'squad_id' => $squad->id, | ||
| 'user_id' => $subCaptain->id, | ||
| 'role' => SquadRole::SubCaptain, | ||
| ]); | ||
| SquadMember::factory()->create([ | ||
| 'squad_id' => $squad->id, | ||
| 'user_id' => $member->id, | ||
| 'role' => SquadRole::Member, | ||
| ]); | ||
|
|
||
| resolve(PromoteToSubCaptain::class)->handle( | ||
| actor: $captain, | ||
| squad: $squad, | ||
| subject: $subCaptain, | ||
| ); | ||
| resolve(PromoteToSubCaptain::class)->demote( | ||
| actor: $captain, | ||
| squad: $squad, | ||
| subject: $member, | ||
| ); | ||
|
|
||
| expect(SquadMembershipEvent::query()->where('squad_id', $squad->id)->count())->toBe(0); | ||
| }); | ||
|
|
||
| test('a sub-captain can demote themselves', function (): void { | ||
| $squad = Squad::factory()->create(); | ||
| $subCaptain = User::factory()->create(); | ||
|
|
||
| SquadMember::factory()->create([ | ||
| 'squad_id' => $squad->id, | ||
| 'user_id' => $subCaptain->id, | ||
| 'role' => SquadRole::SubCaptain, | ||
| ]); | ||
|
|
||
| resolve(PromoteToSubCaptain::class)->demote( | ||
| actor: $subCaptain, | ||
| squad: $squad, | ||
| subject: $subCaptain, | ||
| ); | ||
|
|
||
| $this->assertDatabaseHas('squad_members', [ | ||
| 'squad_id' => $squad->id, | ||
| 'user_id' => $subCaptain->id, | ||
| 'role' => SquadRole::Member->value, | ||
| ]); | ||
| }); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
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.
Posso estar errado, porém quando usamos o padrão
Actioné para não ter dois comportamentos na mesma classe.Uma pergunta a nivel de feature inteira: O que o sub-capitão tem de tão importante que ele precisa de uma action só para ele? A ação não seria
Promote/Demotede alguém?Se me recordo da modelagem, um usuário pode estar ativo em somente uma squad por vez, logo vc já tem acesso a squad deles por relacionamento direto também.