Skip to content

Commit

Permalink
Use new orgmembership table to load users
Browse files Browse the repository at this point in the history
  • Loading branch information
rowanseymour committed Jun 16, 2022
1 parent a009c4e commit d1c0fac
Showing 1 changed file with 8 additions and 26 deletions.
34 changes: 8 additions & 26 deletions core/models/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ type User struct {
Email string `json:"email"`
FirstName string `json:"first_name"`
LastName string `json:"last_name"`
Role UserRole `json:"role"`
Role UserRole `json:"role_code"`
Team *Team `json:"team"`
}
}
Expand Down Expand Up @@ -94,32 +94,14 @@ func (u *User) Team() *Team {
var _ assets.User = (*User)(nil)

const selectOrgUsersSQL = `
SELECT ROW_TO_JSON(r) FROM (SELECT
u.id AS "id",
u.email AS "email",
u.first_name as "first_name",
u.last_name as "last_name",
o.role AS "role",
row_to_json(team_struct) AS team
FROM
auth_user u
INNER JOIN (
SELECT user_id, 'A' AS "role" FROM orgs_org_administrators WHERE org_id = $1
UNION
SELECT user_id, 'E' AS "role" FROM orgs_org_editors WHERE org_id = $1
UNION
SELECT user_id, 'V' AS "role" FROM orgs_org_viewers WHERE org_id = $1
UNION
SELECT user_id, 'T' AS "role" FROM orgs_org_agents WHERE org_id = $1
UNION
SELECT user_id, 'S' AS "role" FROM orgs_org_surveyors WHERE org_id = $1
) o ON o.user_id = u.id
LEFT JOIN orgs_usersettings s ON s.user_id = u.id
SELECT ROW_TO_JSON(r) FROM (
SELECT u.id, u.email, u.first_name, u.last_name, m.role_code, row_to_json(team_struct) AS team
FROM orgs_orgmembership m
INNER JOIN auth_user u ON u.id = m.user_id
LEFT JOIN orgs_usersettings s ON s.user_id = u.id
LEFT JOIN LATERAL (SELECT id, uuid, name FROM tickets_team WHERE tickets_team.id = s.team_id) AS team_struct ON True
WHERE
u.is_active = TRUE
ORDER BY
u.email ASC
WHERE m.org_id = $1 AND u.is_active = TRUE
ORDER BY u.email ASC
) r;`

// loadUsers loads all the users for the passed in org
Expand Down

0 comments on commit d1c0fac

Please sign in to comment.