Introduces invite, verify, accept, resend, and revoke flows using team_members and invitation tokens, sends the branded invitation template, and requires account activation before team login. Co-authored-by: Cursor <cursoragent@cursor.com>
285 lines
7.1 KiB
Go
285 lines
7.1 KiB
Go
// Code generated by sqlc. DO NOT EDIT.
|
|
// versions:
|
|
// sqlc v1.30.0
|
|
// source: team_invitations.sql
|
|
|
|
package dbgen
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/jackc/pgx/v5/pgtype"
|
|
)
|
|
|
|
const AcceptTeamInvitation = `-- name: AcceptTeamInvitation :one
|
|
UPDATE team_invitations
|
|
SET status = 'accepted',
|
|
accepted_at = CURRENT_TIMESTAMP,
|
|
updated_at = CURRENT_TIMESTAMP
|
|
WHERE id = $1
|
|
AND status = 'pending'
|
|
RETURNING id, team_member_id, token, status, expires_at, invited_by, accepted_at, created_at, updated_at
|
|
`
|
|
|
|
func (q *Queries) AcceptTeamInvitation(ctx context.Context, id int64) (TeamInvitation, error) {
|
|
row := q.db.QueryRow(ctx, AcceptTeamInvitation, id)
|
|
var i TeamInvitation
|
|
err := row.Scan(
|
|
&i.ID,
|
|
&i.TeamMemberID,
|
|
&i.Token,
|
|
&i.Status,
|
|
&i.ExpiresAt,
|
|
&i.InvitedBy,
|
|
&i.AcceptedAt,
|
|
&i.CreatedAt,
|
|
&i.UpdatedAt,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const CreateTeamInvitation = `-- name: CreateTeamInvitation :one
|
|
INSERT INTO team_invitations (
|
|
team_member_id,
|
|
token,
|
|
status,
|
|
expires_at,
|
|
invited_by,
|
|
updated_at
|
|
)
|
|
VALUES ($1, $2, 'pending', $3, $4, CURRENT_TIMESTAMP)
|
|
RETURNING id, team_member_id, token, status, expires_at, invited_by, accepted_at, created_at, updated_at
|
|
`
|
|
|
|
type CreateTeamInvitationParams struct {
|
|
TeamMemberID int64 `json:"team_member_id"`
|
|
Token string `json:"token"`
|
|
ExpiresAt pgtype.Timestamptz `json:"expires_at"`
|
|
InvitedBy pgtype.Int8 `json:"invited_by"`
|
|
}
|
|
|
|
func (q *Queries) CreateTeamInvitation(ctx context.Context, arg CreateTeamInvitationParams) (TeamInvitation, error) {
|
|
row := q.db.QueryRow(ctx, CreateTeamInvitation,
|
|
arg.TeamMemberID,
|
|
arg.Token,
|
|
arg.ExpiresAt,
|
|
arg.InvitedBy,
|
|
)
|
|
var i TeamInvitation
|
|
err := row.Scan(
|
|
&i.ID,
|
|
&i.TeamMemberID,
|
|
&i.Token,
|
|
&i.Status,
|
|
&i.ExpiresAt,
|
|
&i.InvitedBy,
|
|
&i.AcceptedAt,
|
|
&i.CreatedAt,
|
|
&i.UpdatedAt,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const ExpireTeamInvitation = `-- name: ExpireTeamInvitation :exec
|
|
UPDATE team_invitations
|
|
SET status = 'expired',
|
|
updated_at = CURRENT_TIMESTAMP
|
|
WHERE id = $1
|
|
AND status = 'pending'
|
|
`
|
|
|
|
func (q *Queries) ExpireTeamInvitation(ctx context.Context, id int64) error {
|
|
_, err := q.db.Exec(ctx, ExpireTeamInvitation, id)
|
|
return err
|
|
}
|
|
|
|
const GetPendingTeamInvitationByMemberID = `-- name: GetPendingTeamInvitationByMemberID :one
|
|
SELECT id, team_member_id, token, status, expires_at, invited_by, accepted_at, created_at, updated_at FROM team_invitations
|
|
WHERE team_member_id = $1
|
|
AND status = 'pending'
|
|
ORDER BY created_at DESC
|
|
LIMIT 1
|
|
`
|
|
|
|
func (q *Queries) GetPendingTeamInvitationByMemberID(ctx context.Context, teamMemberID int64) (TeamInvitation, error) {
|
|
row := q.db.QueryRow(ctx, GetPendingTeamInvitationByMemberID, teamMemberID)
|
|
var i TeamInvitation
|
|
err := row.Scan(
|
|
&i.ID,
|
|
&i.TeamMemberID,
|
|
&i.Token,
|
|
&i.Status,
|
|
&i.ExpiresAt,
|
|
&i.InvitedBy,
|
|
&i.AcceptedAt,
|
|
&i.CreatedAt,
|
|
&i.UpdatedAt,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const GetTeamInvitationByID = `-- name: GetTeamInvitationByID :one
|
|
SELECT id, team_member_id, token, status, expires_at, invited_by, accepted_at, created_at, updated_at FROM team_invitations
|
|
WHERE id = $1
|
|
`
|
|
|
|
func (q *Queries) GetTeamInvitationByID(ctx context.Context, id int64) (TeamInvitation, error) {
|
|
row := q.db.QueryRow(ctx, GetTeamInvitationByID, id)
|
|
var i TeamInvitation
|
|
err := row.Scan(
|
|
&i.ID,
|
|
&i.TeamMemberID,
|
|
&i.Token,
|
|
&i.Status,
|
|
&i.ExpiresAt,
|
|
&i.InvitedBy,
|
|
&i.AcceptedAt,
|
|
&i.CreatedAt,
|
|
&i.UpdatedAt,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const GetTeamInvitationByToken = `-- name: GetTeamInvitationByToken :one
|
|
SELECT id, team_member_id, token, status, expires_at, invited_by, accepted_at, created_at, updated_at FROM team_invitations
|
|
WHERE token = $1
|
|
`
|
|
|
|
func (q *Queries) GetTeamInvitationByToken(ctx context.Context, token string) (TeamInvitation, error) {
|
|
row := q.db.QueryRow(ctx, GetTeamInvitationByToken, token)
|
|
var i TeamInvitation
|
|
err := row.Scan(
|
|
&i.ID,
|
|
&i.TeamMemberID,
|
|
&i.Token,
|
|
&i.Status,
|
|
&i.ExpiresAt,
|
|
&i.InvitedBy,
|
|
&i.AcceptedAt,
|
|
&i.CreatedAt,
|
|
&i.UpdatedAt,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const ListTeamInvitations = `-- name: ListTeamInvitations :many
|
|
SELECT
|
|
ti.id,
|
|
ti.team_member_id,
|
|
ti.token,
|
|
ti.status,
|
|
ti.expires_at,
|
|
ti.invited_by,
|
|
ti.accepted_at,
|
|
ti.created_at,
|
|
ti.updated_at,
|
|
tm.email,
|
|
tm.first_name,
|
|
tm.last_name,
|
|
tm.team_role,
|
|
COUNT(*) OVER () AS total_count
|
|
FROM team_invitations ti
|
|
INNER JOIN team_members tm ON tm.id = ti.team_member_id
|
|
WHERE ($1::text IS NULL OR ti.status = $1::text)
|
|
ORDER BY ti.created_at DESC
|
|
LIMIT $3::INT
|
|
OFFSET $2::INT
|
|
`
|
|
|
|
type ListTeamInvitationsParams struct {
|
|
Status pgtype.Text `json:"status"`
|
|
Offset pgtype.Int4 `json:"offset"`
|
|
Limit pgtype.Int4 `json:"limit"`
|
|
}
|
|
|
|
type ListTeamInvitationsRow struct {
|
|
ID int64 `json:"id"`
|
|
TeamMemberID int64 `json:"team_member_id"`
|
|
Token string `json:"token"`
|
|
Status string `json:"status"`
|
|
ExpiresAt pgtype.Timestamptz `json:"expires_at"`
|
|
InvitedBy pgtype.Int8 `json:"invited_by"`
|
|
AcceptedAt pgtype.Timestamptz `json:"accepted_at"`
|
|
CreatedAt pgtype.Timestamptz `json:"created_at"`
|
|
UpdatedAt pgtype.Timestamptz `json:"updated_at"`
|
|
Email string `json:"email"`
|
|
FirstName string `json:"first_name"`
|
|
LastName string `json:"last_name"`
|
|
TeamRole string `json:"team_role"`
|
|
TotalCount int64 `json:"total_count"`
|
|
}
|
|
|
|
func (q *Queries) ListTeamInvitations(ctx context.Context, arg ListTeamInvitationsParams) ([]ListTeamInvitationsRow, error) {
|
|
rows, err := q.db.Query(ctx, ListTeamInvitations, arg.Status, arg.Offset, arg.Limit)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
var items []ListTeamInvitationsRow
|
|
for rows.Next() {
|
|
var i ListTeamInvitationsRow
|
|
if err := rows.Scan(
|
|
&i.ID,
|
|
&i.TeamMemberID,
|
|
&i.Token,
|
|
&i.Status,
|
|
&i.ExpiresAt,
|
|
&i.InvitedBy,
|
|
&i.AcceptedAt,
|
|
&i.CreatedAt,
|
|
&i.UpdatedAt,
|
|
&i.Email,
|
|
&i.FirstName,
|
|
&i.LastName,
|
|
&i.TeamRole,
|
|
&i.TotalCount,
|
|
); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, i)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const RevokePendingTeamInvitationsForMember = `-- name: RevokePendingTeamInvitationsForMember :exec
|
|
UPDATE team_invitations
|
|
SET status = 'revoked',
|
|
updated_at = CURRENT_TIMESTAMP
|
|
WHERE team_member_id = $1
|
|
AND status = 'pending'
|
|
`
|
|
|
|
func (q *Queries) RevokePendingTeamInvitationsForMember(ctx context.Context, teamMemberID int64) error {
|
|
_, err := q.db.Exec(ctx, RevokePendingTeamInvitationsForMember, teamMemberID)
|
|
return err
|
|
}
|
|
|
|
const RevokeTeamInvitation = `-- name: RevokeTeamInvitation :one
|
|
UPDATE team_invitations
|
|
SET status = 'revoked',
|
|
updated_at = CURRENT_TIMESTAMP
|
|
WHERE id = $1
|
|
AND status = 'pending'
|
|
RETURNING id, team_member_id, token, status, expires_at, invited_by, accepted_at, created_at, updated_at
|
|
`
|
|
|
|
func (q *Queries) RevokeTeamInvitation(ctx context.Context, id int64) (TeamInvitation, error) {
|
|
row := q.db.QueryRow(ctx, RevokeTeamInvitation, id)
|
|
var i TeamInvitation
|
|
err := row.Scan(
|
|
&i.ID,
|
|
&i.TeamMemberID,
|
|
&i.Token,
|
|
&i.Status,
|
|
&i.ExpiresAt,
|
|
&i.InvitedBy,
|
|
&i.AcceptedAt,
|
|
&i.CreatedAt,
|
|
&i.UpdatedAt,
|
|
)
|
|
return i, err
|
|
}
|