From a13c14aed55934f3027b0e662b6d9abc3685a4f9 Mon Sep 17 00:00:00 2001 From: Afaz Khatri Date: Tue, 7 Mar 2023 14:19:19 +0530 Subject: [PATCH 1/3] createOwnedTeam helper function for user --- src/Traits/UserHasTeams.php | 16 ++++++++++++++++ tests/Feature/UserHasTeamsTraitTest.php | 9 +++++++++ 2 files changed, 25 insertions(+) diff --git a/src/Traits/UserHasTeams.php b/src/Traits/UserHasTeams.php index db867c9..2c1ac3f 100644 --- a/src/Traits/UserHasTeams.php +++ b/src/Traits/UserHasTeams.php @@ -258,4 +258,20 @@ public function switchTeam($team) return $this; } + + /** + * Create team for owner, add owner to the users and switch to the team. + * + * @param array $data + * @return mixed + */ + public function createOwnedTeam($data) + { + $teamModel = Config::get('teamwork.team_model'); + $team = $teamModel::create(array_merge($data, ['owner_id' => $this->id])); + + $this->attachTeam($team); + + return $team; + } } diff --git a/tests/Feature/UserHasTeamsTraitTest.php b/tests/Feature/UserHasTeamsTraitTest.php index 416e5b8..3d5a42e 100644 --- a/tests/Feature/UserHasTeamsTraitTest.php +++ b/tests/Feature/UserHasTeamsTraitTest.php @@ -315,4 +315,13 @@ public function testUserCannotSwitchToNotExistingTeam() $this->expectException('Illuminate\Database\Eloquent\ModelNotFoundException'); $this->user->switchTeam(3); } + + public function testTeamForOwnerCanBeCreated() + { + $team = $this->user->createOwnedTeam(['name' => 'test']); + + $this->assertTrue($this->user->isOwnerOfTeam($team)); + $this->assertTrue($team->hasUser($this->user)); + $this->assertEquals($this->user->currentTeam->id, $team->id); + } } From fdb2be579369c7305d0b96e91b9e0de7cf08c4f9 Mon Sep 17 00:00:00 2001 From: Afaz Khatri Date: Tue, 7 Mar 2023 14:29:25 +0530 Subject: [PATCH 2/3] Added forceSwitchTeam flag --- src/Traits/UserHasTeams.php | 10 +++++++++- tests/Feature/UserHasTeamsTraitTest.php | 18 +++++++++++++++++- 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/src/Traits/UserHasTeams.php b/src/Traits/UserHasTeams.php index 2c1ac3f..31f14fe 100644 --- a/src/Traits/UserHasTeams.php +++ b/src/Traits/UserHasTeams.php @@ -263,15 +263,23 @@ public function switchTeam($team) * Create team for owner, add owner to the users and switch to the team. * * @param array $data + * @param bool $forceSwitchTeam * @return mixed */ - public function createOwnedTeam($data) + public function createOwnedTeam($data, $forceSwitchTeam = false) { $teamModel = Config::get('teamwork.team_model'); $team = $teamModel::create(array_merge($data, ['owner_id' => $this->id])); $this->attachTeam($team); + if ( + $this->current_team_id !== $team->id && + $forceSwitchTeam + ) { + $this->switchTeam($team); + } + return $team; } } diff --git a/tests/Feature/UserHasTeamsTraitTest.php b/tests/Feature/UserHasTeamsTraitTest.php index 3d5a42e..371e1be 100644 --- a/tests/Feature/UserHasTeamsTraitTest.php +++ b/tests/Feature/UserHasTeamsTraitTest.php @@ -316,7 +316,7 @@ public function testUserCannotSwitchToNotExistingTeam() $this->user->switchTeam(3); } - public function testTeamForOwnerCanBeCreated() + public function testTeamForOwnerCanBeCreatedAndSwitchToTeamIfNoTeamExists() { $team = $this->user->createOwnedTeam(['name' => 'test']); @@ -324,4 +324,20 @@ public function testTeamForOwnerCanBeCreated() $this->assertTrue($team->hasUser($this->user)); $this->assertEquals($this->user->currentTeam->id, $team->id); } + + public function testTeamForOwnerCanBeCreatedAndNotSwitchToTeamIfTeamExists() + { + $firstTeam = $this->user->createOwnedTeam(['name' => 'test']); + $anotherTeam = $this->user->createOwnedTeam(['name' => 'Another test']); + + $this->assertEquals($this->user->currentTeam->id, $firstTeam->id); + } + + public function testTeamForOwnerCanBeCreatedAndCanSwitchToNewTeam() + { + $firstTeam = $this->user->createOwnedTeam(['name' => 'test']); + $anotherTeam = $this->user->createOwnedTeam(['name' => 'Another test'], true); + + $this->assertEquals($this->user->currentTeam->id, $anotherTeam->id); + } } From 5f459959e0bdd663b497f05cdd503af36c32e8e3 Mon Sep 17 00:00:00 2001 From: Afaz Khatri Date: Tue, 7 Mar 2023 14:40:32 +0530 Subject: [PATCH 3/3] Documented the function createOwnedTeam --- README.md | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/README.md b/README.md index 27f4aae..e7304da 100644 --- a/README.md +++ b/README.md @@ -172,6 +172,18 @@ $user->teams()->attach($team->id); // id only By using the `attachTeam` method, if the User has no Teams assigned, the `current_team_id` column will automatically be set. +Alternatively, you can also use `createOwnedTeam` method from `UserHasTeams` trait. It will create team for the user as owner, attach the user to the team and switch to the newly created team. + +```php +// Create user owned team and switch the current team to this new team. +$team = $user->createOwnedTeam(['name' => 'My awesome team']); + +// If user has another current team active, you should pass second parameter as true to force switch to the new team. +$team = $user->createOwnedTeam(['name' => 'My awesome team'], true); +``` + +The function will return the newly created instance of your team model. + ### Get to know my team(s) The currently assigned Team of a user can be accessed through the `currentTeam` relation like this: