Commit b796027b by Leonard Gram

teams: refactor.

parent 13ed1049
...@@ -259,28 +259,21 @@ func AddTeamMember(cmd *m.AddTeamMemberCommand) error { ...@@ -259,28 +259,21 @@ func AddTeamMember(cmd *m.AddTeamMemberCommand) error {
func UpdateTeamMember(cmd *m.UpdateTeamMemberCommand) error { func UpdateTeamMember(cmd *m.UpdateTeamMemberCommand) error {
return inTransaction(func(sess *DBSession) error { return inTransaction(func(sess *DBSession) error {
rawSql := `SELECT * FROM team_member WHERE org_id=? and team_id=? and user_id=?` rawSql := `SELECT * FROM team_member WHERE org_id=? and team_id=? and user_id=?`
var member m.TeamMember var member m.TeamMember
exists, err := sess.SQL(rawSql, cmd.OrgId, cmd.TeamId, cmd.UserId).Get(&member) exists, err := sess.SQL(rawSql, cmd.OrgId, cmd.TeamId, cmd.UserId).Get(&member)
if err != nil { if err != nil {
return err return err
} }
if !exists { if !exists {
return m.ErrTeamMemberNotFound return m.ErrTeamMemberNotFound
} }
if cmd.ProtectLastAdmin { if cmd.ProtectLastAdmin {
lastAdmin, err := isLastAdmin(sess, cmd.OrgId, cmd.TeamId, cmd.UserId) _, err := isLastAdmin(sess, cmd.OrgId, cmd.TeamId, cmd.UserId)
if err != nil { if err != nil {
return err return err
} }
if lastAdmin {
return m.ErrLastTeamAdmin
}
} }
if cmd.Permission != m.PERMISSION_ADMIN { // make sure we don't get invalid permission levels in store if cmd.Permission != m.PERMISSION_ADMIN { // make sure we don't get invalid permission levels in store
...@@ -302,15 +295,10 @@ func RemoveTeamMember(cmd *m.RemoveTeamMemberCommand) error { ...@@ -302,15 +295,10 @@ func RemoveTeamMember(cmd *m.RemoveTeamMemberCommand) error {
} }
if cmd.ProtectLastAdmin { if cmd.ProtectLastAdmin {
lastAdmin, err := isLastAdmin(sess, cmd.OrgId, cmd.TeamId, cmd.UserId) _, err := isLastAdmin(sess, cmd.OrgId, cmd.TeamId, cmd.UserId)
if err != nil { if err != nil {
return err return err
} }
if lastAdmin {
return m.ErrLastTeamAdmin
}
} }
var rawSql = "DELETE FROM team_member WHERE org_id=? and team_id=? and user_id=?" var rawSql = "DELETE FROM team_member WHERE org_id=? and team_id=? and user_id=?"
...@@ -344,7 +332,7 @@ func isLastAdmin(sess *DBSession, orgId int64, teamId int64, userId int64) (bool ...@@ -344,7 +332,7 @@ func isLastAdmin(sess *DBSession, orgId int64, teamId int64, userId int64) (bool
} }
if isAdmin && len(userIds) == 1 { if isAdmin && len(userIds) == 1 {
return true, nil return true, m.ErrLastTeamAdmin
} }
return false, err return false, err
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment