-
Notifications
You must be signed in to change notification settings - Fork 43
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
6
commits 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.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
5401268
feat(squads): add sub-captain role transitions
guisaliba 305e0e2
fix(squads): protect the captain seat from sub-captain transitions (#…
guisaliba 1558660
docs(squads): document the PromoteToSubCaptain governance scope (#358)
guisaliba 9fdd48e
Merge remote-tracking branch 'origin/feat/squads' into story/358-prom…
guisaliba fea823b
fix(squads): enforce the sub-captain transition contract (#358)
guisaliba 62ab525
docs(squads): clarify leadership transition authority (#358)
guisaliba 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
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,117 @@ | ||
| <?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\InvalidSquadRoleTransition; | ||
| 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; | ||
|
|
||
| /** | ||
| * Records the off-system Member -> SubCaptain result and its inverse, | ||
| * SubCaptain -> Member, in the membership ledger. | ||
| * | ||
| * This action never mutates the captain seat. `AssignCaptain` owns captain | ||
| * assignment and replacement, while `MarkExMember` currently owns vacancy. | ||
| * The governance decision remains off-platform; this action records its result. | ||
| */ | ||
| 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->authorizeSubCaptainManagement($actor, $squad); | ||
|
|
||
| return $this->transition( | ||
| squad: $squad, | ||
| subject: $subject, | ||
| actor: $actor, | ||
| expectedRole: SquadRole::Member, | ||
| targetRole: SquadRole::SubCaptain, | ||
| action: MembershipAction::Promote, | ||
| reason: $reason, | ||
| ); | ||
| } | ||
|
|
||
| public function demote(User $actor, Squad $squad, User $subject, ?string $reason = null): SquadMember | ||
| { | ||
| $this->squadPolicy->authorizeSubCaptainManagement($actor, $squad); | ||
|
|
||
| return $this->transition( | ||
| squad: $squad, | ||
| subject: $subject, | ||
| actor: $actor, | ||
| expectedRole: SquadRole::SubCaptain, | ||
| targetRole: SquadRole::Member, | ||
| action: MembershipAction::Demote, | ||
| reason: $reason, | ||
| ); | ||
| } | ||
|
guisaliba marked this conversation as resolved.
|
||
|
|
||
| private function transition( | ||
| Squad $squad, | ||
| User $subject, | ||
| User $actor, | ||
| SquadRole $expectedRole, | ||
| SquadRole $targetRole, | ||
| MembershipAction $action, | ||
| ?string $reason, | ||
| ): SquadMember { | ||
| return DB::transaction(function () use ( | ||
| $squad, | ||
| $subject, | ||
| $actor, | ||
| $expectedRole, | ||
| $targetRole, | ||
| $action, | ||
| $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; | ||
| } | ||
|
|
||
| throw_if( | ||
| $fromRole !== $expectedRole, | ||
| InvalidSquadRoleTransition::between($fromRole, $targetRole) | ||
| ); | ||
|
|
||
| $member->update([ | ||
| 'role' => $targetRole, | ||
| ]); | ||
|
|
||
| $this->recordMembershipEvent->handle( | ||
| squad: $squad, | ||
| subject: $subject, | ||
| action: $action, | ||
| fromRole: $fromRole, | ||
| toRole: $targetRole, | ||
| actor: $actor, | ||
| reason: $reason, | ||
| ); | ||
|
|
||
| return $member->refresh(); | ||
| }); | ||
| } | ||
| } | ||
18 changes: 18 additions & 0 deletions
18
app-modules/squads/src/Exceptions/InvalidSquadRoleTransition.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,18 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace He4rt\Squads\Exceptions; | ||
|
|
||
| use Exception; | ||
| use He4rt\Squads\Enums\SquadRole; | ||
|
|
||
| final class InvalidSquadRoleTransition extends Exception | ||
| { | ||
| public static function between(SquadRole $from, SquadRole $to): self | ||
| { | ||
| return new self( | ||
| sprintf('Cannot transition squad member role from "%s" to "%s".', $from->value, $to->value) | ||
| ); | ||
| } | ||
| } |
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
Oops, something went wrong.
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.