Files
uptimesebi/internal/store/queries/support.sql
T
Sebastian UnterschützandClaude Opus 5.5 f3d402705e
ci / lint (push) Skipped
ci / test (push) Skipped
ci / build (push) Skipped
ci / image (push) Skipped
ci / lint (pull_request) Successful in 7m0s
ci / test (pull_request) Successful in 12m1s
ci / build (pull_request) Successful in 18m14s
ci / image (pull_request) Successful in 5m9s
feat: support tickets between accounts and platform admins
Accounts open tickets under Support and admins answer them under Admin →
Support tickets.

- A ticket is open while it waits for the admins and answered while it
  waits for the account. Either side can close or reopen it, and a reply
  reopens it.
- The admins are mailed about new tickets and replies, and the account
  about answers.
- A ticket can name the active organization and point at a monitor,
  notification channel, alert group, status page or incident in it.
  Each must be something the account can read.
- Next to the conversation, admins see the account at a glance: state,
  sign-in, organizations with their limits and usage, and open tickets.
- Admins switch support off and limit how many tickets an account has
  open at once.
- Suspended accounts and those yet to accept the terms reach support too;
  a suspended account opens one ticket.
- Tickets are part of the account export and are deleted with the
  account.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
2026-11-24 18:01:50 +01:00

98 lines
4.3 KiB
SQL

-- name: CreateSupportTicket :one
INSERT INTO support_tickets (user_id, org_id, subject, monitor_id, channel_id, alert_group_id, status_page_id, incident_id)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8)
RETURNING *;
-- name: SupportReferencesExist :one
-- Whether everything a new ticket points at belongs to org; run in the
-- organization's scope so only what the account may see counts.
SELECT (sqlc.narg(monitor_id)::uuid IS NULL OR EXISTS (SELECT 1 FROM monitors m WHERE m.id = sqlc.narg(monitor_id) AND m.org_id = sqlc.arg(org_id)::uuid))
AND (sqlc.narg(channel_id)::uuid IS NULL OR EXISTS (SELECT 1 FROM notification_channels c WHERE c.id = sqlc.narg(channel_id) AND c.org_id = sqlc.arg(org_id)::uuid))
AND (sqlc.narg(alert_group_id)::uuid IS NULL OR EXISTS (SELECT 1 FROM alert_groups g WHERE g.id = sqlc.narg(alert_group_id) AND g.org_id = sqlc.arg(org_id)::uuid))
AND (sqlc.narg(status_page_id)::uuid IS NULL OR EXISTS (SELECT 1 FROM status_pages p WHERE p.id = sqlc.narg(status_page_id) AND p.org_id = sqlc.arg(org_id)::uuid))
AND (sqlc.narg(incident_id)::uuid IS NULL OR EXISTS (SELECT 1 FROM incidents i WHERE i.id = sqlc.narg(incident_id) AND i.org_id = sqlc.arg(org_id)::uuid))
AS ok;
-- name: SupportTicketReferences :one
-- The names of what a ticket points at, for showing it.
SELECT o.name AS org_name, o.slug AS org_slug,
m.name AS monitor_name, m.type AS monitor_type,
ch.name AS channel_name, ch.type AS channel_type,
ag.name AS alert_group_name,
sp.title AS status_page_title, sp.slug AS status_page_slug,
i.title AS incident_title, i.started_at AS incident_started_at, im.name AS incident_monitor
FROM support_tickets t
LEFT JOIN orgs o ON o.id = t.org_id
LEFT JOIN monitors m ON m.id = t.monitor_id
LEFT JOIN notification_channels ch ON ch.id = t.channel_id
LEFT JOIN alert_groups ag ON ag.id = t.alert_group_id
LEFT JOIN status_pages sp ON sp.id = t.status_page_id
LEFT JOIN incidents i ON i.id = t.incident_id
LEFT JOIN monitors im ON im.id = i.monitor_id
WHERE t.id = $1;
-- name: SupportOptionsMonitors :many
SELECT id, name FROM monitors WHERE org_id = $1 ORDER BY lower(name) LIMIT 500;
-- name: SupportOptionsChannels :many
SELECT id, name, type FROM notification_channels WHERE org_id = $1 ORDER BY lower(name) LIMIT 500;
-- name: SupportOptionsAlertGroups :many
SELECT id, name FROM alert_groups WHERE org_id = $1 ORDER BY lower(name) LIMIT 500;
-- name: SupportOptionsStatusPages :many
SELECT id, title FROM status_pages WHERE org_id = $1 ORDER BY lower(title) LIMIT 500;
-- name: SupportOptionsIncidents :many
SELECT i.id, i.title, i.started_at, m.name AS monitor_name
FROM incidents i JOIN monitors m ON m.id = i.monitor_id
WHERE i.org_id = $1 ORDER BY i.started_at DESC LIMIT 50;
-- name: AddSupportMessage :one
INSERT INTO support_messages (ticket_id, author_id, from_staff, body) VALUES ($1, $2, $3, $4)
RETURNING *;
-- name: SetSupportTicketStatus :execrows
UPDATE support_tickets SET status = $2, updated_at = now() WHERE id = $1;
-- name: GetSupportTicket :one
SELECT t.*, u.email AS user_email, o.name AS org_name
FROM support_tickets t
JOIN users u ON u.id = t.user_id
LEFT JOIN orgs o ON o.id = t.org_id
WHERE t.id = $1;
-- name: ListUserSupportTickets :many
-- The tickets row-level security lets the account see, newest activity
-- first.
SELECT * FROM support_tickets WHERE user_id = $1 ORDER BY updated_at DESC LIMIT 200;
-- name: ListSupportTickets :many
-- Every account's tickets for the admins, those waiting for them first.
SELECT t.*, u.email AS user_email, o.name AS org_name
FROM support_tickets t
JOIN users u ON u.id = t.user_id
LEFT JOIN orgs o ON o.id = t.org_id
WHERE sqlc.narg(status)::text IS NULL OR t.status = sqlc.narg(status)::text
ORDER BY t.status = 'open' DESC, t.updated_at DESC
LIMIT 200;
-- name: SupportMessages :many
SELECT m.*, coalesce(u.email, '')::text AS author_email
FROM support_messages m
LEFT JOIN users u ON u.id = m.author_id
WHERE m.ticket_id = $1
ORDER BY m.created_at, m.id;
-- name: AdminEmails :many
SELECT email FROM users WHERE is_admin AND disabled_at IS NULL ORDER BY email;
-- name: CountOpenSupportTickets :one
SELECT count(*) FROM support_tickets WHERE user_id = $1 AND status <> 'closed';
-- name: CountSupportTicketsSince :one
SELECT count(*) FROM support_tickets WHERE user_id = $1 AND created_at >= $2;
-- name: SupportTicketStatus :one
SELECT status FROM support_tickets WHERE id = $1;