Skip to content

Commit

Permalink
Merge pull request #155 from khatriafaz/master
Browse files Browse the repository at this point in the history
Added function createOwnedTeam to UserHasTeams
  • Loading branch information
okaufmann authored Mar 24, 2023
2 parents 4129a26 + 5f45995 commit 46da329
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 0 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
24 changes: 24 additions & 0 deletions src/Traits/UserHasTeams.php
Original file line number Diff line number Diff line change
Expand Up @@ -258,4 +258,28 @@ public function switchTeam($team)

return $this;
}

/**
* 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, $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;
}
}
25 changes: 25 additions & 0 deletions tests/Feature/UserHasTeamsTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -315,4 +315,29 @@ public function testUserCannotSwitchToNotExistingTeam()
$this->expectException('Illuminate\Database\Eloquent\ModelNotFoundException');
$this->user->switchTeam(3);
}

public function testTeamForOwnerCanBeCreatedAndSwitchToTeamIfNoTeamExists()
{
$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);
}

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);
}
}

0 comments on commit 46da329

Please sign in to comment.