Skip to content

Commit

Permalink
models: Add GetUserTeams().
Browse files Browse the repository at this point in the history
Returns all the teams that a user belongs to.
  • Loading branch information
HarshitOnGitHub committed Jan 3, 2019
1 parent fdf1d98 commit c0211a1
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
12 changes: 12 additions & 0 deletions models/org_team.go
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,13 @@ func GetTeamMembers(teamID int64) ([]*User, error) {
return getTeamMembers(x, teamID)
}

func getUserTeams(e Engine, userID int64) (teams []*Team, err error) {
return teams, e.
Join("INNER", "team_user", "team_user.team_id = team.id").
Where("team_user.uid=?", userID).
Find(&teams)
}

func getUserOrgTeams(e Engine, orgID, userID int64) (teams []*Team, err error) {
return teams, e.
Join("INNER", "team_user", "team_user.team_id = team.id").
Expand All @@ -566,6 +573,11 @@ func GetUserOrgTeams(orgID, userID int64) ([]*Team, error) {
return getUserOrgTeams(x, orgID, userID)
}

// GetUserTeams returns all teams that user belongs across all organizations.
func GetUserTeams(userID int64) ([]*Team, error) {
return getUserTeams(x, userID)
}

// AddTeamMember adds new membership of given team to given organization,
// the user will have membership to given organization automatically when needed.
func AddTeamMember(team *Team, userID int64) error {
Expand Down
14 changes: 14 additions & 0 deletions models/org_team_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,20 @@ func TestGetTeamMembers(t *testing.T) {
test(3)
}

func TestGetUserTeams(t *testing.T) {
assert.NoError(t, PrepareTestDatabase())
test := func(userID int64) {
teams, err := GetUserTeams(userID)
assert.NoError(t, err)
for _, team := range teams {
AssertExistsAndLoadBean(t, &TeamUser{TeamID: team.ID, UID: userID})
}
}
test(2)
test(5)
test(NonexistentID)
}

func TestGetUserOrgTeams(t *testing.T) {
assert.NoError(t, PrepareTestDatabase())
test := func(orgID, userID int64) {
Expand Down

0 comments on commit c0211a1

Please sign in to comment.