2024-06-12 09:23:48 -10:00
|
|
|
-- 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
|
2022-04-01 15:45:23 -05:00
|
|
|
SELECT
|
2024-06-12 09:23:48 -10:00
|
|
|
sqlc.embed(organization_members),
|
2024-07-24 11:36:45 -06:00
|
|
|
users.username, users.avatar_url, users.name, users.email, users.rbac_roles as "global_roles"
|
2022-04-01 15:45:23 -05:00
|
|
|
FROM
|
|
|
|
|
organization_members
|
2024-06-12 09:23:48 -10:00
|
|
|
INNER JOIN
|
2025-03-06 11:50:08 -07:00
|
|
|
users ON organization_members.user_id = users.id AND users.deleted = false
|
2022-04-01 15:45:23 -05:00
|
|
|
WHERE
|
2024-06-12 09:23:48 -10:00
|
|
|
-- 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
|
2025-03-25 14:18:06 +02:00
|
|
|
END
|
|
|
|
|
-- Filter by system type
|
|
|
|
|
AND CASE
|
|
|
|
|
WHEN @include_system::bool THEN TRUE
|
|
|
|
|
ELSE
|
|
|
|
|
is_system = false
|
2025-09-30 23:54:21 +01:00
|
|
|
END
|
|
|
|
|
-- Filter by github user ID. Note that this requires a join on the users table.
|
|
|
|
|
AND CASE
|
|
|
|
|
WHEN @github_user_id :: bigint != 0 THEN
|
|
|
|
|
users.github_com_user_id = @github_user_id
|
|
|
|
|
ELSE true
|
|
|
|
|
END;
|
2022-04-01 15:45:23 -05:00
|
|
|
|
|
|
|
|
-- name: InsertOrganizationMember :one
|
|
|
|
|
INSERT INTO
|
|
|
|
|
organization_members (
|
|
|
|
|
organization_id,
|
|
|
|
|
user_id,
|
|
|
|
|
created_at,
|
|
|
|
|
updated_at,
|
|
|
|
|
roles
|
|
|
|
|
)
|
|
|
|
|
VALUES
|
|
|
|
|
($1, $2, $3, $4, $5) RETURNING *;
|
2022-04-28 09:10:17 -05:00
|
|
|
|
2024-06-20 05:06:37 -10:00
|
|
|
-- name: DeleteOrganizationMember :exec
|
|
|
|
|
DELETE
|
|
|
|
|
FROM
|
|
|
|
|
organization_members
|
|
|
|
|
WHERE
|
|
|
|
|
organization_id = @organization_id AND
|
|
|
|
|
user_id = @user_id
|
|
|
|
|
;
|
|
|
|
|
|
2022-04-29 09:04:19 -05:00
|
|
|
|
2022-04-28 09:10:17 -05:00
|
|
|
-- name: GetOrganizationIDsByMemberIDs :many
|
|
|
|
|
SELECT
|
|
|
|
|
user_id, array_agg(organization_id) :: uuid [ ] AS "organization_IDs"
|
|
|
|
|
FROM
|
|
|
|
|
organization_members
|
|
|
|
|
WHERE
|
|
|
|
|
user_id = ANY(@ids :: uuid [ ])
|
|
|
|
|
GROUP BY
|
|
|
|
|
user_id;
|
2022-04-29 09:04:19 -05:00
|
|
|
|
|
|
|
|
-- name: UpdateMemberRoles :one
|
|
|
|
|
UPDATE
|
|
|
|
|
organization_members
|
|
|
|
|
SET
|
|
|
|
|
-- Remove all duplicates from the roles.
|
|
|
|
|
roles = ARRAY(SELECT DISTINCT UNNEST(@granted_roles :: text[]))
|
|
|
|
|
WHERE
|
2022-05-16 11:20:11 -05:00
|
|
|
user_id = @user_id
|
|
|
|
|
AND organization_id = @org_id
|
2022-05-13 20:41:21 -05:00
|
|
|
RETURNING *;
|
2025-03-10 14:42:07 -04:00
|
|
|
|
|
|
|
|
-- name: PaginatedOrganizationMembers :many
|
|
|
|
|
SELECT
|
|
|
|
|
sqlc.embed(organization_members),
|
|
|
|
|
users.username, users.avatar_url, users.name, users.email, users.rbac_roles as "global_roles",
|
|
|
|
|
COUNT(*) OVER() AS count
|
|
|
|
|
FROM
|
|
|
|
|
organization_members
|
|
|
|
|
INNER JOIN
|
|
|
|
|
users ON organization_members.user_id = users.id AND users.deleted = false
|
|
|
|
|
WHERE
|
|
|
|
|
-- Filter by organization id
|
|
|
|
|
CASE
|
|
|
|
|
WHEN @organization_id :: uuid != '00000000-0000-0000-0000-000000000000'::uuid THEN
|
|
|
|
|
organization_id = @organization_id
|
|
|
|
|
ELSE true
|
|
|
|
|
END
|
2025-08-27 16:57:59 +02:00
|
|
|
-- Filter by system type
|
|
|
|
|
AND CASE WHEN @include_system::bool THEN TRUE ELSE is_system = false END
|
2025-03-10 14:42:07 -04:00
|
|
|
ORDER BY
|
|
|
|
|
-- Deterministic and consistent ordering of all users. This is to ensure consistent pagination.
|
|
|
|
|
LOWER(username) ASC OFFSET @offset_opt
|
|
|
|
|
LIMIT
|
|
|
|
|
-- A null limit means "no limit", so 0 means return all
|
|
|
|
|
NULLIF(@limit_opt :: int, 0);
|