chore: merge organization member db queries (#13542)

Merge members queries into 1 that also joins in the user table for username.
Required to list organization members on UI/cli
This commit is contained in:
Steven Masley
2024-06-12 09:23:48 -10:00
committed by GitHub
parent 1ca5dc0328
commit de9e6889bb
18 changed files with 293 additions and 214 deletions

View File

@ -1,13 +1,28 @@
-- name: GetOrganizationMemberByUserID :one
-- name: OrganizationMembers :many
-- Arguments are optional with uuid.Nil to ignore.
-- - Use just 'organization_id' to get all members of an org
-- - Use just 'user_id' to get all orgs a user is a member of
-- - Use both to get a specific org member row
SELECT
*
sqlc.embed(organization_members),
users.username
FROM
organization_members
INNER JOIN
users ON organization_members.user_id = users.id
WHERE
organization_id = $1
AND user_id = $2
LIMIT
1;
-- Filter by organization id
CASE
WHEN @organization_id :: uuid != '00000000-0000-0000-0000-000000000000'::uuid THEN
organization_id = @organization_id
ELSE true
END
-- Filter by user id
AND CASE
WHEN @user_id :: uuid != '00000000-0000-0000-0000-000000000000'::uuid THEN
user_id = @user_id
ELSE true
END;
-- name: InsertOrganizationMember :one
INSERT INTO
@ -22,14 +37,6 @@ VALUES
($1, $2, $3, $4, $5) RETURNING *;
-- name: GetOrganizationMembershipsByUserID :many
SELECT
*
FROM
organization_members
WHERE
user_id = $1;
-- name: GetOrganizationIDsByMemberIDs :many
SELECT
user_id, array_agg(organization_id) :: uuid [ ] AS "organization_IDs"