Files
multica/server/pkg/db/queries/share_link.sql
T
da7131ab59 MUL-6625: enforce prepaid member seat capacity (#7491)
* MUL-6625: enforce prepaid member seat capacity

Co-authored-by: multica-agent <[email protected]>

* fix(seat-capacity): address review findings

Co-authored-by: multica-agent <[email protected]>

* fix(seat-capacity): close retry and worker races

Co-authored-by: multica-agent <[email protected]>

* fix(seat-capacity): bound lock waits during rollback

Co-authored-by: multica-agent <[email protected]>

* fix(seat-capacity): pin the cross-repo confirm settlement budget

Multica Cloud must wait longer than this worker's whole retry budget before it
treats a member row as one its ledger never recorded; adopting a member whose
confirm is still retrying counts one person as two seats, and an over-estimate
is what asks a customer to buy seats they do not owe.

That budget was only implicit here — 10 attempts of exponential backoff plus a
tick and a lock wait each — and Cloud's horizon had been set from a single
retry. Extract the backoff into retryBackoff, expose SettlementBudget, and
assert the worst case (29m15s) stays inside Cloud's 45m horizon, so raising
maxAttempts or the backoff cap fails review here instead of widening the window
in which Cloud cannot quantify a ledger.

Co-authored-by: multica-agent <[email protected]>

* Revert "fix(seat-capacity): pin the cross-repo confirm settlement budget"

This reverts commit d0ec36f647.

Co-authored-by: multica-agent <[email protected]>

---------

Co-authored-by: Sol-Boy <[email protected]>
Co-authored-by: multica-agent <[email protected]>
2026-08-25 12:51:44 +08:00

55 lines
1.8 KiB
SQL

-- name: CreateShareLink :one
INSERT INTO workspace_share_link (workspace_id, code, created_by, role, expires_at, max_uses)
VALUES ($1, $2, $3, $4, $5, $6)
RETURNING *;
-- name: ClaimShareLinkByID :one
-- The caller first resolves the opaque code and then atomically consumes the
-- non-secret row ID. The conditional UPDATE revalidates validity and the use
-- limit so a join cannot race past revocation or expiry.
UPDATE workspace_share_link
SET use_count = use_count + 1
WHERE id = $1
AND is_active = true
AND (expires_at IS NULL OR expires_at > now())
AND (max_uses IS NULL OR use_count < max_uses)
RETURNING *;
-- name: GetActiveShareLinkByCode :one
SELECT * FROM workspace_share_link
WHERE code = $1
AND is_active = true
AND (expires_at IS NULL OR expires_at > now())
AND (max_uses IS NULL OR use_count < max_uses);
-- name: GetShareLinkInfoByCode :one
SELECT wsl.role,
w.name AS workspace_name,
w.slug AS workspace_slug,
u.name AS creator_name
FROM workspace_share_link wsl
JOIN workspace w ON w.id = wsl.workspace_id
JOIN "user" u ON u.id = wsl.created_by
WHERE wsl.code = $1 AND wsl.is_active = true
AND (wsl.expires_at IS NULL OR wsl.expires_at > now())
AND (wsl.max_uses IS NULL OR wsl.use_count < wsl.max_uses);
-- name: ListShareLinksByWorkspace :many
SELECT wsl.*,
u.name AS creator_name,
u.email AS creator_email
FROM workspace_share_link wsl
JOIN "user" u ON u.id = wsl.created_by
WHERE wsl.workspace_id = $1 AND wsl.is_active = true
ORDER BY wsl.created_at DESC;
-- name: DeactivateWorkspaceShareLinks :exec
UPDATE workspace_share_link
SET is_active = false
WHERE workspace_id = $1 AND is_active = true;
-- name: RevokeShareLink :exec
UPDATE workspace_share_link
SET is_active = false
WHERE id = $1 AND workspace_id = $2;