2023-06-30 08:38:48 -04:00
|
|
|
-- name: UpdateUserLoginType :one
|
|
|
|
|
UPDATE
|
|
|
|
|
users
|
|
|
|
|
SET
|
|
|
|
|
login_type = @new_login_type,
|
|
|
|
|
hashed_password = CASE WHEN @new_login_type = 'password' :: login_type THEN
|
|
|
|
|
users.hashed_password
|
|
|
|
|
ELSE
|
|
|
|
|
-- If the login type is not password, then the password should be
|
|
|
|
|
-- cleared.
|
|
|
|
|
'':: bytea
|
|
|
|
|
END
|
|
|
|
|
WHERE
|
2025-03-25 14:18:06 +02:00
|
|
|
id = @user_id
|
|
|
|
|
AND NOT is_system
|
|
|
|
|
RETURNING *;
|
2023-06-30 08:38:48 -04:00
|
|
|
|
2022-04-01 15:45:23 -05:00
|
|
|
-- name: GetUserByID :one
|
|
|
|
|
SELECT
|
|
|
|
|
*
|
|
|
|
|
FROM
|
|
|
|
|
users
|
|
|
|
|
WHERE
|
|
|
|
|
id = $1
|
|
|
|
|
LIMIT
|
|
|
|
|
1;
|
|
|
|
|
|
2025-08-07 10:14:58 -06:00
|
|
|
-- name: ValidateUserIDs :one
|
|
|
|
|
WITH input AS (
|
|
|
|
|
SELECT
|
|
|
|
|
unnest(@user_ids::uuid[]) AS id
|
|
|
|
|
)
|
|
|
|
|
SELECT
|
|
|
|
|
array_agg(input.id)::uuid[] as invalid_user_ids,
|
|
|
|
|
COUNT(*) = 0 as ok
|
|
|
|
|
FROM
|
|
|
|
|
-- Preserve rows where there is not a matching left (users) row for each
|
|
|
|
|
-- right (input) row...
|
|
|
|
|
users
|
|
|
|
|
RIGHT JOIN input ON users.id = input.id
|
|
|
|
|
WHERE
|
|
|
|
|
-- ...so that we can retain exactly those rows where an input ID does not
|
|
|
|
|
-- match an existing user...
|
|
|
|
|
users.id IS NULL OR
|
|
|
|
|
-- ...or that only matches a user that was deleted.
|
|
|
|
|
users.deleted = true;
|
|
|
|
|
|
2022-05-13 20:41:21 -05:00
|
|
|
-- name: GetUsersByIDs :many
|
2022-10-10 13:03:54 -05:00
|
|
|
-- This shouldn't check for deleted, because it's frequently used
|
|
|
|
|
-- to look up references to actions. eg. a user could build a workspace
|
|
|
|
|
-- for another user, then be deleted... we still want them to appear!
|
|
|
|
|
SELECT * FROM users WHERE id = ANY(@ids :: uuid [ ]);
|
2022-05-13 20:41:21 -05:00
|
|
|
|
2022-04-01 15:45:23 -05:00
|
|
|
-- name: GetUserByEmailOrUsername :one
|
|
|
|
|
SELECT
|
|
|
|
|
*
|
|
|
|
|
FROM
|
|
|
|
|
users
|
|
|
|
|
WHERE
|
2026-03-11 10:19:08 -07:00
|
|
|
(LOWER(username) = LOWER(@username) OR (@email != '' AND LOWER(email) = LOWER(@email))) AND
|
2022-12-16 01:04:24 +10:00
|
|
|
deleted = false
|
2022-04-01 15:45:23 -05:00
|
|
|
LIMIT
|
|
|
|
|
1;
|
|
|
|
|
|
|
|
|
|
-- name: GetUserCount :one
|
|
|
|
|
SELECT
|
|
|
|
|
COUNT(*)
|
|
|
|
|
FROM
|
2022-12-16 01:04:24 +10:00
|
|
|
users
|
|
|
|
|
WHERE
|
2025-03-25 14:18:06 +02:00
|
|
|
deleted = false
|
|
|
|
|
AND CASE WHEN @include_system::bool THEN TRUE ELSE is_system = false END;
|
2022-04-01 15:45:23 -05:00
|
|
|
|
2022-08-29 16:45:40 -07:00
|
|
|
-- name: GetActiveUserCount :one
|
|
|
|
|
SELECT
|
|
|
|
|
COUNT(*)
|
|
|
|
|
FROM
|
|
|
|
|
users
|
|
|
|
|
WHERE
|
2025-03-25 14:18:06 +02:00
|
|
|
status = 'active'::user_status AND deleted = false
|
|
|
|
|
AND CASE WHEN @include_system::bool THEN TRUE ELSE is_system = false END;
|
2022-08-29 16:45:40 -07:00
|
|
|
|
2022-04-01 15:45:23 -05:00
|
|
|
-- name: InsertUser :one
|
|
|
|
|
INSERT INTO
|
|
|
|
|
users (
|
|
|
|
|
id,
|
|
|
|
|
email,
|
2022-04-24 20:08:26 -05:00
|
|
|
username,
|
2024-06-26 09:00:42 +01:00
|
|
|
name,
|
2022-04-01 15:45:23 -05:00
|
|
|
hashed_password,
|
|
|
|
|
created_at,
|
2022-04-29 09:04:19 -05:00
|
|
|
updated_at,
|
2022-08-17 18:00:53 -05:00
|
|
|
rbac_roles,
|
2024-10-31 17:55:42 -05:00
|
|
|
login_type,
|
2026-03-11 10:19:08 -07:00
|
|
|
status,
|
|
|
|
|
is_service_account
|
2022-04-01 15:45:23 -05:00
|
|
|
)
|
|
|
|
|
VALUES
|
2024-10-31 17:55:42 -05:00
|
|
|
($1, $2, $3, $4, $5, $6, $7, $8, $9,
|
|
|
|
|
-- if the status passed in is empty, fallback to dormant, which is what
|
|
|
|
|
-- we were doing before.
|
2026-03-11 10:19:08 -07:00
|
|
|
COALESCE(NULLIF(@status::text, '')::user_status, 'dormant'::user_status),
|
|
|
|
|
@is_service_account::bool
|
2024-10-31 17:55:42 -05:00
|
|
|
) RETURNING *;
|
2022-04-12 11:05:21 -03:00
|
|
|
|
|
|
|
|
-- name: UpdateUserProfile :one
|
|
|
|
|
UPDATE
|
|
|
|
|
users
|
|
|
|
|
SET
|
|
|
|
|
email = $2,
|
2022-04-24 20:08:26 -05:00
|
|
|
username = $3,
|
2022-09-04 11:44:27 -05:00
|
|
|
avatar_url = $4,
|
2024-01-17 13:20:45 +01:00
|
|
|
updated_at = $5,
|
|
|
|
|
name = $6
|
2022-04-12 11:05:21 -03:00
|
|
|
WHERE
|
2023-12-14 10:38:44 -07:00
|
|
|
id = $1
|
|
|
|
|
RETURNING *;
|
|
|
|
|
|
2024-08-02 11:49:36 -05:00
|
|
|
-- name: UpdateUserGithubComUserID :exec
|
|
|
|
|
UPDATE
|
|
|
|
|
users
|
|
|
|
|
SET
|
|
|
|
|
github_com_user_id = $2
|
|
|
|
|
WHERE
|
|
|
|
|
id = $1;
|
|
|
|
|
|
2025-04-07 14:30:10 +02:00
|
|
|
-- name: GetUserThemePreference :one
|
2025-03-05 10:46:03 -07:00
|
|
|
SELECT
|
|
|
|
|
value as theme_preference
|
|
|
|
|
FROM
|
|
|
|
|
user_configs
|
2023-12-14 10:38:44 -07:00
|
|
|
WHERE
|
2025-03-05 10:46:03 -07:00
|
|
|
user_id = @user_id
|
|
|
|
|
AND key = 'theme_preference';
|
|
|
|
|
|
2025-04-07 14:30:10 +02:00
|
|
|
-- name: UpdateUserThemePreference :one
|
2025-03-05 10:46:03 -07:00
|
|
|
INSERT INTO
|
|
|
|
|
user_configs (user_id, key, value)
|
|
|
|
|
VALUES
|
|
|
|
|
(@user_id, 'theme_preference', @theme_preference)
|
|
|
|
|
ON CONFLICT
|
|
|
|
|
ON CONSTRAINT user_configs_pkey
|
|
|
|
|
DO UPDATE
|
|
|
|
|
SET
|
|
|
|
|
value = @theme_preference
|
|
|
|
|
WHERE user_configs.user_id = @user_id
|
|
|
|
|
AND user_configs.key = 'theme_preference'
|
2023-12-14 10:38:44 -07:00
|
|
|
RETURNING *;
|
2022-04-18 12:19:47 -05:00
|
|
|
|
2025-04-07 14:30:10 +02:00
|
|
|
-- name: GetUserTerminalFont :one
|
|
|
|
|
SELECT
|
|
|
|
|
value as terminal_font
|
|
|
|
|
FROM
|
|
|
|
|
user_configs
|
|
|
|
|
WHERE
|
|
|
|
|
user_id = @user_id
|
|
|
|
|
AND key = 'terminal_font';
|
|
|
|
|
|
|
|
|
|
-- name: UpdateUserTerminalFont :one
|
|
|
|
|
INSERT INTO
|
|
|
|
|
user_configs (user_id, key, value)
|
|
|
|
|
VALUES
|
|
|
|
|
(@user_id, 'terminal_font', @terminal_font)
|
|
|
|
|
ON CONFLICT
|
|
|
|
|
ON CONSTRAINT user_configs_pkey
|
|
|
|
|
DO UPDATE
|
|
|
|
|
SET
|
|
|
|
|
value = @terminal_font
|
|
|
|
|
WHERE user_configs.user_id = @user_id
|
|
|
|
|
AND user_configs.key = 'terminal_font'
|
|
|
|
|
RETURNING *;
|
|
|
|
|
|
2026-03-10 08:17:52 -07:00
|
|
|
-- name: GetUserChatCustomPrompt :one
|
|
|
|
|
SELECT
|
|
|
|
|
value as chat_custom_prompt
|
|
|
|
|
FROM
|
|
|
|
|
user_configs
|
|
|
|
|
WHERE
|
|
|
|
|
user_id = @user_id
|
|
|
|
|
AND key = 'chat_custom_prompt';
|
|
|
|
|
|
|
|
|
|
-- name: UpdateUserChatCustomPrompt :one
|
|
|
|
|
INSERT INTO
|
|
|
|
|
user_configs (user_id, key, value)
|
|
|
|
|
VALUES
|
|
|
|
|
(@user_id, 'chat_custom_prompt', @chat_custom_prompt)
|
|
|
|
|
ON CONFLICT
|
|
|
|
|
ON CONSTRAINT user_configs_pkey
|
|
|
|
|
DO UPDATE
|
|
|
|
|
SET
|
|
|
|
|
value = @chat_custom_prompt
|
|
|
|
|
WHERE user_configs.user_id = @user_id
|
|
|
|
|
AND user_configs.key = 'chat_custom_prompt'
|
|
|
|
|
RETURNING *;
|
|
|
|
|
|
2025-11-28 16:50:59 +00:00
|
|
|
-- name: GetUserTaskNotificationAlertDismissed :one
|
|
|
|
|
SELECT
|
|
|
|
|
value::boolean as task_notification_alert_dismissed
|
|
|
|
|
FROM
|
|
|
|
|
user_configs
|
|
|
|
|
WHERE
|
|
|
|
|
user_id = @user_id
|
|
|
|
|
AND key = 'preference_task_notification_alert_dismissed';
|
|
|
|
|
|
|
|
|
|
-- name: UpdateUserTaskNotificationAlertDismissed :one
|
|
|
|
|
INSERT INTO
|
|
|
|
|
user_configs (user_id, key, value)
|
|
|
|
|
VALUES
|
|
|
|
|
(@user_id, 'preference_task_notification_alert_dismissed', (@task_notification_alert_dismissed::boolean)::text)
|
|
|
|
|
ON CONFLICT
|
|
|
|
|
ON CONSTRAINT user_configs_pkey
|
|
|
|
|
DO UPDATE
|
|
|
|
|
SET
|
|
|
|
|
value = @task_notification_alert_dismissed
|
|
|
|
|
WHERE user_configs.user_id = @user_id
|
|
|
|
|
AND user_configs.key = 'preference_task_notification_alert_dismissed'
|
|
|
|
|
RETURNING value::boolean AS task_notification_alert_dismissed;
|
|
|
|
|
|
2022-04-29 09:04:19 -05:00
|
|
|
-- name: UpdateUserRoles :one
|
|
|
|
|
UPDATE
|
2022-08-17 18:00:53 -05:00
|
|
|
users
|
2022-04-29 09:04:19 -05:00
|
|
|
SET
|
|
|
|
|
-- Remove all duplicates from the roles.
|
|
|
|
|
rbac_roles = ARRAY(SELECT DISTINCT UNNEST(@granted_roles :: text[]))
|
|
|
|
|
WHERE
|
2022-08-17 18:00:53 -05:00
|
|
|
id = @id
|
2022-04-29 09:04:19 -05:00
|
|
|
RETURNING *;
|
|
|
|
|
|
2022-05-06 09:20:08 -05:00
|
|
|
-- name: UpdateUserHashedPassword :exec
|
|
|
|
|
UPDATE
|
|
|
|
|
users
|
|
|
|
|
SET
|
2024-10-04 11:53:25 +01:00
|
|
|
hashed_password = $2,
|
|
|
|
|
hashed_one_time_passcode = NULL,
|
|
|
|
|
one_time_passcode_expires_at = NULL
|
2022-05-06 09:20:08 -05:00
|
|
|
WHERE
|
|
|
|
|
id = $1;
|
|
|
|
|
|
2022-09-12 18:24:20 -05:00
|
|
|
-- name: UpdateUserDeletedByID :exec
|
|
|
|
|
UPDATE
|
|
|
|
|
users
|
|
|
|
|
SET
|
2024-02-20 13:19:38 -06:00
|
|
|
deleted = true
|
2022-09-12 18:24:20 -05:00
|
|
|
WHERE
|
|
|
|
|
id = $1;
|
|
|
|
|
|
2022-04-18 12:19:47 -05:00
|
|
|
-- name: GetUsers :many
|
2022-12-16 01:04:24 +10:00
|
|
|
-- This will never return deleted users.
|
2022-04-18 12:19:47 -05:00
|
|
|
SELECT
|
2022-11-14 17:22:57 -05:00
|
|
|
*, COUNT(*) OVER() AS count
|
2022-04-18 12:19:47 -05:00
|
|
|
FROM
|
2022-04-22 15:27:55 -05:00
|
|
|
users
|
|
|
|
|
WHERE
|
2022-12-16 01:04:24 +10:00
|
|
|
users.deleted = false
|
2022-09-12 18:24:20 -05:00
|
|
|
AND CASE
|
2022-04-22 15:27:55 -05:00
|
|
|
-- This allows using the last element on a page as effectively a cursor.
|
|
|
|
|
-- This is an important option for scripts that need to paginate without
|
|
|
|
|
-- duplicating or missing data.
|
2022-11-06 16:49:43 -06:00
|
|
|
WHEN @after_id :: uuid != '00000000-0000-0000-0000-000000000000'::uuid THEN (
|
2022-05-10 10:44:09 +03:00
|
|
|
-- The pagination cursor is the last ID of the previous page.
|
2023-06-06 08:47:59 +02:00
|
|
|
-- The query is ordered by the username field, so select all
|
2022-05-10 10:44:09 +03:00
|
|
|
-- rows after the cursor.
|
2023-06-06 12:37:41 +02:00
|
|
|
(LOWER(username)) > (
|
2022-05-10 10:44:09 +03:00
|
|
|
SELECT
|
2023-06-06 12:37:41 +02:00
|
|
|
LOWER(username)
|
2022-05-10 10:44:09 +03:00
|
|
|
FROM
|
|
|
|
|
users
|
|
|
|
|
WHERE
|
|
|
|
|
id = @after_id
|
2022-04-22 15:27:55 -05:00
|
|
|
)
|
2022-05-10 10:44:09 +03:00
|
|
|
)
|
|
|
|
|
ELSE true
|
2022-04-22 15:27:55 -05:00
|
|
|
END
|
2022-04-29 08:29:53 -05:00
|
|
|
-- Start filters
|
2026-02-03 00:13:41 +11:00
|
|
|
-- Filter by email or username
|
2022-04-22 15:27:55 -05:00
|
|
|
AND CASE
|
|
|
|
|
WHEN @search :: text != '' THEN (
|
2022-06-24 19:55:28 -04:00
|
|
|
email ILIKE concat('%', @search, '%')
|
|
|
|
|
OR username ILIKE concat('%', @search, '%')
|
2022-05-31 08:06:42 -05:00
|
|
|
)
|
2022-04-29 08:29:53 -05:00
|
|
|
ELSE true
|
|
|
|
|
END
|
2026-02-03 00:13:41 +11:00
|
|
|
-- Filter by name (display name)
|
|
|
|
|
AND CASE
|
|
|
|
|
WHEN @name :: text != '' THEN
|
|
|
|
|
name ILIKE concat('%', @name, '%')
|
|
|
|
|
ELSE true
|
|
|
|
|
END
|
2022-04-29 08:29:53 -05:00
|
|
|
-- Filter by status
|
|
|
|
|
AND CASE
|
|
|
|
|
-- @status needs to be a text because it can be empty, If it was
|
|
|
|
|
-- user_status enum, it would not.
|
2022-06-24 10:02:23 -05:00
|
|
|
WHEN cardinality(@status :: user_status[]) > 0 THEN
|
2022-05-31 08:06:42 -05:00
|
|
|
status = ANY(@status :: user_status[])
|
2022-06-24 10:02:23 -05:00
|
|
|
ELSE true
|
|
|
|
|
END
|
|
|
|
|
-- Filter by rbac_roles
|
|
|
|
|
AND CASE
|
|
|
|
|
-- @rbac_role allows filtering by rbac roles. If 'member' is included, show everyone, as
|
2023-06-30 08:38:48 -04:00
|
|
|
-- everyone is a member.
|
2022-06-24 10:02:23 -05:00
|
|
|
WHEN cardinality(@rbac_role :: text[]) > 0 AND 'member' != ANY(@rbac_role :: text[]) THEN
|
2023-06-30 08:38:48 -04:00
|
|
|
rbac_roles && @rbac_role :: text[]
|
2022-06-24 10:02:23 -05:00
|
|
|
ELSE true
|
2022-04-22 15:27:55 -05:00
|
|
|
END
|
2023-06-22 15:24:48 -05:00
|
|
|
-- Filter by last_seen
|
|
|
|
|
AND CASE
|
|
|
|
|
WHEN @last_seen_before :: timestamp with time zone != '0001-01-01 00:00:00Z' THEN
|
|
|
|
|
last_seen_at <= @last_seen_before
|
|
|
|
|
ELSE true
|
|
|
|
|
END
|
|
|
|
|
AND CASE
|
|
|
|
|
WHEN @last_seen_after :: timestamp with time zone != '0001-01-01 00:00:00Z' THEN
|
|
|
|
|
last_seen_at >= @last_seen_after
|
|
|
|
|
ELSE true
|
|
|
|
|
END
|
2024-12-17 09:54:54 +05:30
|
|
|
-- Filter by created_at
|
|
|
|
|
AND CASE
|
|
|
|
|
WHEN @created_before :: timestamp with time zone != '0001-01-01 00:00:00Z' THEN
|
|
|
|
|
created_at <= @created_before
|
|
|
|
|
ELSE true
|
|
|
|
|
END
|
|
|
|
|
AND CASE
|
|
|
|
|
WHEN @created_after :: timestamp with time zone != '0001-01-01 00:00:00Z' THEN
|
|
|
|
|
created_at >= @created_after
|
|
|
|
|
ELSE true
|
|
|
|
|
END
|
2025-03-25 14:18:06 +02:00
|
|
|
AND CASE
|
|
|
|
|
WHEN @include_system::bool THEN TRUE
|
|
|
|
|
ELSE
|
|
|
|
|
is_system = false
|
|
|
|
|
END
|
2025-03-21 13:30:47 +01:00
|
|
|
AND CASE
|
|
|
|
|
WHEN @github_com_user_id :: bigint != 0 THEN
|
|
|
|
|
github_com_user_id = @github_com_user_id
|
|
|
|
|
ELSE true
|
|
|
|
|
END
|
2025-04-08 23:59:41 -04:00
|
|
|
-- Filter by login_type
|
|
|
|
|
AND CASE
|
|
|
|
|
WHEN cardinality(@login_type :: login_type[]) > 0 THEN
|
|
|
|
|
login_type = ANY(@login_type :: login_type[])
|
|
|
|
|
ELSE true
|
|
|
|
|
END
|
2022-04-29 08:29:53 -05:00
|
|
|
-- End of filters
|
2023-07-17 09:44:58 -04:00
|
|
|
|
|
|
|
|
-- Authorize Filter clause will be injected below in GetAuthorizedUsers
|
|
|
|
|
-- @authorize_filter
|
2022-04-22 15:27:55 -05:00
|
|
|
ORDER BY
|
2023-06-06 08:47:59 +02:00
|
|
|
-- Deterministic and consistent ordering of all users. This is to ensure consistent pagination.
|
2023-06-06 12:37:41 +02:00
|
|
|
LOWER(username) ASC OFFSET @offset_opt
|
2022-04-22 15:27:55 -05:00
|
|
|
LIMIT
|
2022-06-26 15:23:25 -05:00
|
|
|
-- A null limit means "no limit", so 0 means return all
|
|
|
|
|
NULLIF(@limit_opt :: int, 0);
|
2022-04-26 09:00:07 -05:00
|
|
|
|
|
|
|
|
-- name: UpdateUserStatus :one
|
|
|
|
|
UPDATE
|
|
|
|
|
users
|
|
|
|
|
SET
|
|
|
|
|
status = $2,
|
2025-12-17 16:49:40 +04:00
|
|
|
updated_at = $3,
|
|
|
|
|
-- If the user is logging in, set last_seen_at to updated_at.
|
|
|
|
|
last_seen_at = CASE WHEN @user_is_seen :: boolean THEN $3 :: timestamptz ELSE last_seen_at END
|
2022-04-26 09:00:07 -05:00
|
|
|
WHERE
|
|
|
|
|
id = $1 RETURNING *;
|
2022-05-03 16:10:19 -05:00
|
|
|
|
2022-09-26 10:31:03 -05:00
|
|
|
-- name: UpdateUserLastSeenAt :one
|
|
|
|
|
UPDATE
|
|
|
|
|
users
|
|
|
|
|
SET
|
|
|
|
|
last_seen_at = $2,
|
|
|
|
|
updated_at = $3
|
|
|
|
|
WHERE
|
|
|
|
|
id = $1 RETURNING *;
|
|
|
|
|
|
2022-05-03 16:10:19 -05:00
|
|
|
|
2022-06-01 09:07:50 -05:00
|
|
|
-- name: GetAuthorizationUserRoles :one
|
|
|
|
|
-- This function returns roles for authorization purposes. Implied member roles
|
|
|
|
|
-- are included.
|
2022-05-03 16:10:19 -05:00
|
|
|
SELECT
|
2025-04-15 13:27:23 +02:00
|
|
|
-- username and email are returned just to help for logging purposes
|
2022-06-01 09:07:50 -05:00
|
|
|
-- status is used to enforce 'suspended' users, as all roles are ignored
|
|
|
|
|
-- when suspended.
|
2025-04-15 13:27:23 +02:00
|
|
|
id, username, status, email,
|
2022-10-10 15:37:06 -05:00
|
|
|
-- All user roles, including their org roles.
|
2022-06-01 09:07:50 -05:00
|
|
|
array_cat(
|
|
|
|
|
-- All users are members
|
2022-10-10 15:37:06 -05:00
|
|
|
array_append(users.rbac_roles, 'member'),
|
|
|
|
|
(
|
|
|
|
|
SELECT
|
2024-06-05 11:25:02 -05:00
|
|
|
-- The roles are returned as a flat array, org scoped and site side.
|
|
|
|
|
-- Concatenating the organization id scopes the organization roles.
|
|
|
|
|
array_agg(org_roles || ':' || organization_members.organization_id::text)
|
2022-10-10 15:37:06 -05:00
|
|
|
FROM
|
|
|
|
|
organization_members,
|
2024-06-05 11:25:02 -05:00
|
|
|
-- All org_members get the organization-member role for their orgs
|
2022-10-10 15:37:06 -05:00
|
|
|
unnest(
|
2024-06-05 11:25:02 -05:00
|
|
|
array_append(roles, 'organization-member')
|
2022-10-10 15:37:06 -05:00
|
|
|
) AS org_roles
|
|
|
|
|
WHERE
|
|
|
|
|
user_id = users.id
|
|
|
|
|
)
|
|
|
|
|
) :: text[] AS roles,
|
|
|
|
|
-- All groups the user is in.
|
|
|
|
|
(
|
|
|
|
|
SELECT
|
|
|
|
|
array_agg(
|
|
|
|
|
group_members.group_id :: text
|
|
|
|
|
)
|
|
|
|
|
FROM
|
|
|
|
|
group_members
|
|
|
|
|
WHERE
|
|
|
|
|
user_id = users.id
|
|
|
|
|
) :: text[] AS groups
|
2022-05-03 16:10:19 -05:00
|
|
|
FROM
|
|
|
|
|
users
|
|
|
|
|
WHERE
|
2022-08-17 18:00:53 -05:00
|
|
|
id = @user_id;
|
2023-07-20 06:35:41 -07:00
|
|
|
|
|
|
|
|
-- name: UpdateUserQuietHoursSchedule :one
|
|
|
|
|
UPDATE
|
|
|
|
|
users
|
|
|
|
|
SET
|
|
|
|
|
quiet_hours_schedule = $2
|
|
|
|
|
WHERE
|
|
|
|
|
id = $1
|
|
|
|
|
RETURNING *;
|
2023-08-02 16:31:25 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
-- name: UpdateInactiveUsersToDormant :many
|
|
|
|
|
UPDATE
|
|
|
|
|
users
|
|
|
|
|
SET
|
|
|
|
|
status = 'dormant'::user_status,
|
2025-03-25 14:18:06 +02:00
|
|
|
updated_at = @updated_at
|
2023-08-02 16:31:25 +02:00
|
|
|
WHERE
|
|
|
|
|
last_seen_at < @last_seen_after :: timestamp
|
|
|
|
|
AND status = 'active'::user_status
|
2025-03-25 14:18:06 +02:00
|
|
|
AND NOT is_system
|
2024-10-31 17:55:42 -05:00
|
|
|
RETURNING id, email, username, last_seen_at;
|
2023-09-15 15:09:40 +01:00
|
|
|
|
|
|
|
|
-- AllUserIDs returns all UserIDs regardless of user status or deletion.
|
|
|
|
|
-- name: AllUserIDs :many
|
2025-03-25 14:18:06 +02:00
|
|
|
SELECT DISTINCT id FROM USERS
|
|
|
|
|
WHERE CASE WHEN @include_system::bool THEN TRUE ELSE is_system = false END;
|
2024-10-04 11:53:25 +01:00
|
|
|
|
|
|
|
|
-- name: UpdateUserHashedOneTimePasscode :exec
|
|
|
|
|
UPDATE
|
|
|
|
|
users
|
|
|
|
|
SET
|
|
|
|
|
hashed_one_time_passcode = $2,
|
|
|
|
|
one_time_passcode_expires_at = $3
|
|
|
|
|
WHERE
|
|
|
|
|
id = $1
|
|
|
|
|
;
|