| 1 | // Code generated by sqlc. DO NOT EDIT. |
| 2 | // versions: |
| 3 | // sqlc v1.31.1 |
| 4 | // source: invitations.sql |
| 5 | |
| 6 | package orgsdb |
| 7 | |
| 8 | import ( |
| 9 | "context" |
| 10 | |
| 11 | "github.com/jackc/pgx/v5/pgtype" |
| 12 | ) |
| 13 | |
| 14 | const acceptOrgInvitation = `-- name: AcceptOrgInvitation :exec |
| 15 | UPDATE org_invitations SET accepted_at = now() WHERE id = $1 |
| 16 | ` |
| 17 | |
| 18 | func (q *Queries) AcceptOrgInvitation(ctx context.Context, db DBTX, id int64) error { |
| 19 | _, err := db.Exec(ctx, acceptOrgInvitation, id) |
| 20 | return err |
| 21 | } |
| 22 | |
| 23 | const cancelOrgInvitation = `-- name: CancelOrgInvitation :exec |
| 24 | UPDATE org_invitations SET canceled_at = now() WHERE id = $1 |
| 25 | ` |
| 26 | |
| 27 | func (q *Queries) CancelOrgInvitation(ctx context.Context, db DBTX, id int64) error { |
| 28 | _, err := db.Exec(ctx, cancelOrgInvitation, id) |
| 29 | return err |
| 30 | } |
| 31 | |
| 32 | const createOrgInvitation = `-- name: CreateOrgInvitation :one |
| 33 | |
| 34 | |
| 35 | INSERT INTO org_invitations ( |
| 36 | org_id, invited_by_user_id, target_user_id, target_email, |
| 37 | role, token_hash, expires_at |
| 38 | ) VALUES ( |
| 39 | $1, $2, $6::bigint, $7::citext, |
| 40 | $3, $4, $5 |
| 41 | ) |
| 42 | RETURNING id, org_id, invited_by_user_id, target_user_id, target_email, role, token_hash, expires_at, accepted_at, declined_at, canceled_at, created_at |
| 43 | ` |
| 44 | |
| 45 | type CreateOrgInvitationParams struct { |
| 46 | OrgID int64 |
| 47 | InvitedByUserID pgtype.Int8 |
| 48 | Role OrgRole |
| 49 | TokenHash []byte |
| 50 | ExpiresAt pgtype.Timestamptz |
| 51 | TargetUserID pgtype.Int8 |
| 52 | TargetEmail pgtype.Text |
| 53 | } |
| 54 | |
| 55 | // SPDX-License-Identifier: AGPL-3.0-or-later |
| 56 | // ─── org_invitations ─────────────────────────────────────────────── |
| 57 | func (q *Queries) CreateOrgInvitation(ctx context.Context, db DBTX, arg CreateOrgInvitationParams) (OrgInvitation, error) { |
| 58 | row := db.QueryRow(ctx, createOrgInvitation, |
| 59 | arg.OrgID, |
| 60 | arg.InvitedByUserID, |
| 61 | arg.Role, |
| 62 | arg.TokenHash, |
| 63 | arg.ExpiresAt, |
| 64 | arg.TargetUserID, |
| 65 | arg.TargetEmail, |
| 66 | ) |
| 67 | var i OrgInvitation |
| 68 | err := row.Scan( |
| 69 | &i.ID, |
| 70 | &i.OrgID, |
| 71 | &i.InvitedByUserID, |
| 72 | &i.TargetUserID, |
| 73 | &i.TargetEmail, |
| 74 | &i.Role, |
| 75 | &i.TokenHash, |
| 76 | &i.ExpiresAt, |
| 77 | &i.AcceptedAt, |
| 78 | &i.DeclinedAt, |
| 79 | &i.CanceledAt, |
| 80 | &i.CreatedAt, |
| 81 | ) |
| 82 | return i, err |
| 83 | } |
| 84 | |
| 85 | const declineOrgInvitation = `-- name: DeclineOrgInvitation :exec |
| 86 | UPDATE org_invitations SET declined_at = now() WHERE id = $1 |
| 87 | ` |
| 88 | |
| 89 | func (q *Queries) DeclineOrgInvitation(ctx context.Context, db DBTX, id int64) error { |
| 90 | _, err := db.Exec(ctx, declineOrgInvitation, id) |
| 91 | return err |
| 92 | } |
| 93 | |
| 94 | const getExistingPendingInvitation = `-- name: GetExistingPendingInvitation :one |
| 95 | SELECT id, org_id, invited_by_user_id, target_user_id, target_email, role, token_hash, expires_at, accepted_at, declined_at, canceled_at, created_at FROM org_invitations |
| 96 | WHERE org_id = $1 |
| 97 | AND ( (target_user_id = $2::bigint) |
| 98 | OR (target_email = $3::citext) ) |
| 99 | AND accepted_at IS NULL |
| 100 | AND declined_at IS NULL |
| 101 | AND canceled_at IS NULL |
| 102 | AND expires_at > now() |
| 103 | LIMIT 1 |
| 104 | ` |
| 105 | |
| 106 | type GetExistingPendingInvitationParams struct { |
| 107 | OrgID int64 |
| 108 | TargetUserID pgtype.Int8 |
| 109 | TargetEmail pgtype.Text |
| 110 | } |
| 111 | |
| 112 | // Idempotency check before creating a new invite — so a re-issued |
| 113 | // invite to the same target doesn't accumulate stale rows. |
| 114 | func (q *Queries) GetExistingPendingInvitation(ctx context.Context, db DBTX, arg GetExistingPendingInvitationParams) (OrgInvitation, error) { |
| 115 | row := db.QueryRow(ctx, getExistingPendingInvitation, arg.OrgID, arg.TargetUserID, arg.TargetEmail) |
| 116 | var i OrgInvitation |
| 117 | err := row.Scan( |
| 118 | &i.ID, |
| 119 | &i.OrgID, |
| 120 | &i.InvitedByUserID, |
| 121 | &i.TargetUserID, |
| 122 | &i.TargetEmail, |
| 123 | &i.Role, |
| 124 | &i.TokenHash, |
| 125 | &i.ExpiresAt, |
| 126 | &i.AcceptedAt, |
| 127 | &i.DeclinedAt, |
| 128 | &i.CanceledAt, |
| 129 | &i.CreatedAt, |
| 130 | ) |
| 131 | return i, err |
| 132 | } |
| 133 | |
| 134 | const getOrgInvitationByID = `-- name: GetOrgInvitationByID :one |
| 135 | SELECT id, org_id, invited_by_user_id, target_user_id, target_email, role, token_hash, expires_at, accepted_at, declined_at, canceled_at, created_at FROM org_invitations WHERE id = $1 |
| 136 | ` |
| 137 | |
| 138 | func (q *Queries) GetOrgInvitationByID(ctx context.Context, db DBTX, id int64) (OrgInvitation, error) { |
| 139 | row := db.QueryRow(ctx, getOrgInvitationByID, id) |
| 140 | var i OrgInvitation |
| 141 | err := row.Scan( |
| 142 | &i.ID, |
| 143 | &i.OrgID, |
| 144 | &i.InvitedByUserID, |
| 145 | &i.TargetUserID, |
| 146 | &i.TargetEmail, |
| 147 | &i.Role, |
| 148 | &i.TokenHash, |
| 149 | &i.ExpiresAt, |
| 150 | &i.AcceptedAt, |
| 151 | &i.DeclinedAt, |
| 152 | &i.CanceledAt, |
| 153 | &i.CreatedAt, |
| 154 | ) |
| 155 | return i, err |
| 156 | } |
| 157 | |
| 158 | const getOrgInvitationByTokenHash = `-- name: GetOrgInvitationByTokenHash :one |
| 159 | SELECT id, org_id, invited_by_user_id, target_user_id, target_email, role, token_hash, expires_at, accepted_at, declined_at, canceled_at, created_at FROM org_invitations WHERE token_hash = $1 |
| 160 | ` |
| 161 | |
| 162 | func (q *Queries) GetOrgInvitationByTokenHash(ctx context.Context, db DBTX, tokenHash []byte) (OrgInvitation, error) { |
| 163 | row := db.QueryRow(ctx, getOrgInvitationByTokenHash, tokenHash) |
| 164 | var i OrgInvitation |
| 165 | err := row.Scan( |
| 166 | &i.ID, |
| 167 | &i.OrgID, |
| 168 | &i.InvitedByUserID, |
| 169 | &i.TargetUserID, |
| 170 | &i.TargetEmail, |
| 171 | &i.Role, |
| 172 | &i.TokenHash, |
| 173 | &i.ExpiresAt, |
| 174 | &i.AcceptedAt, |
| 175 | &i.DeclinedAt, |
| 176 | &i.CanceledAt, |
| 177 | &i.CreatedAt, |
| 178 | ) |
| 179 | return i, err |
| 180 | } |
| 181 | |
| 182 | const listPendingInvitationsForEmail = `-- name: ListPendingInvitationsForEmail :many |
| 183 | SELECT i.id, i.org_id, i.invited_by_user_id, i.target_user_id, i.target_email, i.role, i.token_hash, i.expires_at, i.accepted_at, i.declined_at, i.canceled_at, i.created_at, o.slug AS org_slug, o.display_name AS org_display_name |
| 184 | FROM org_invitations i |
| 185 | JOIN orgs o ON o.id = i.org_id |
| 186 | WHERE i.target_email = $1 |
| 187 | AND i.accepted_at IS NULL |
| 188 | AND i.declined_at IS NULL |
| 189 | AND i.canceled_at IS NULL |
| 190 | AND i.expires_at > now() |
| 191 | AND o.deleted_at IS NULL |
| 192 | ORDER BY i.created_at DESC |
| 193 | ` |
| 194 | |
| 195 | type ListPendingInvitationsForEmailRow struct { |
| 196 | ID int64 |
| 197 | OrgID int64 |
| 198 | InvitedByUserID pgtype.Int8 |
| 199 | TargetUserID pgtype.Int8 |
| 200 | TargetEmail pgtype.Text |
| 201 | Role OrgRole |
| 202 | TokenHash []byte |
| 203 | ExpiresAt pgtype.Timestamptz |
| 204 | AcceptedAt pgtype.Timestamptz |
| 205 | DeclinedAt pgtype.Timestamptz |
| 206 | CanceledAt pgtype.Timestamptz |
| 207 | CreatedAt pgtype.Timestamptz |
| 208 | OrgSlug string |
| 209 | OrgDisplayName string |
| 210 | } |
| 211 | |
| 212 | func (q *Queries) ListPendingInvitationsForEmail(ctx context.Context, db DBTX, targetEmail pgtype.Text) ([]ListPendingInvitationsForEmailRow, error) { |
| 213 | rows, err := db.Query(ctx, listPendingInvitationsForEmail, targetEmail) |
| 214 | if err != nil { |
| 215 | return nil, err |
| 216 | } |
| 217 | defer rows.Close() |
| 218 | items := []ListPendingInvitationsForEmailRow{} |
| 219 | for rows.Next() { |
| 220 | var i ListPendingInvitationsForEmailRow |
| 221 | if err := rows.Scan( |
| 222 | &i.ID, |
| 223 | &i.OrgID, |
| 224 | &i.InvitedByUserID, |
| 225 | &i.TargetUserID, |
| 226 | &i.TargetEmail, |
| 227 | &i.Role, |
| 228 | &i.TokenHash, |
| 229 | &i.ExpiresAt, |
| 230 | &i.AcceptedAt, |
| 231 | &i.DeclinedAt, |
| 232 | &i.CanceledAt, |
| 233 | &i.CreatedAt, |
| 234 | &i.OrgSlug, |
| 235 | &i.OrgDisplayName, |
| 236 | ); err != nil { |
| 237 | return nil, err |
| 238 | } |
| 239 | items = append(items, i) |
| 240 | } |
| 241 | if err := rows.Err(); err != nil { |
| 242 | return nil, err |
| 243 | } |
| 244 | return items, nil |
| 245 | } |
| 246 | |
| 247 | const listPendingInvitationsForOrg = `-- name: ListPendingInvitationsForOrg :many |
| 248 | SELECT i.id, i.org_id, i.invited_by_user_id, i.target_user_id, i.target_email, i.role, i.token_hash, i.expires_at, i.accepted_at, i.declined_at, i.canceled_at, i.created_at, u.username AS target_username, ib.username AS invited_by_username |
| 249 | FROM org_invitations i |
| 250 | LEFT JOIN users u ON u.id = i.target_user_id |
| 251 | LEFT JOIN users ib ON ib.id = i.invited_by_user_id |
| 252 | WHERE i.org_id = $1 |
| 253 | AND i.accepted_at IS NULL |
| 254 | AND i.declined_at IS NULL |
| 255 | AND i.canceled_at IS NULL |
| 256 | AND i.expires_at > now() |
| 257 | ORDER BY i.created_at DESC |
| 258 | ` |
| 259 | |
| 260 | type ListPendingInvitationsForOrgRow struct { |
| 261 | ID int64 |
| 262 | OrgID int64 |
| 263 | InvitedByUserID pgtype.Int8 |
| 264 | TargetUserID pgtype.Int8 |
| 265 | TargetEmail pgtype.Text |
| 266 | Role OrgRole |
| 267 | TokenHash []byte |
| 268 | ExpiresAt pgtype.Timestamptz |
| 269 | AcceptedAt pgtype.Timestamptz |
| 270 | DeclinedAt pgtype.Timestamptz |
| 271 | CanceledAt pgtype.Timestamptz |
| 272 | CreatedAt pgtype.Timestamptz |
| 273 | TargetUsername pgtype.Text |
| 274 | InvitedByUsername pgtype.Text |
| 275 | } |
| 276 | |
| 277 | func (q *Queries) ListPendingInvitationsForOrg(ctx context.Context, db DBTX, orgID int64) ([]ListPendingInvitationsForOrgRow, error) { |
| 278 | rows, err := db.Query(ctx, listPendingInvitationsForOrg, orgID) |
| 279 | if err != nil { |
| 280 | return nil, err |
| 281 | } |
| 282 | defer rows.Close() |
| 283 | items := []ListPendingInvitationsForOrgRow{} |
| 284 | for rows.Next() { |
| 285 | var i ListPendingInvitationsForOrgRow |
| 286 | if err := rows.Scan( |
| 287 | &i.ID, |
| 288 | &i.OrgID, |
| 289 | &i.InvitedByUserID, |
| 290 | &i.TargetUserID, |
| 291 | &i.TargetEmail, |
| 292 | &i.Role, |
| 293 | &i.TokenHash, |
| 294 | &i.ExpiresAt, |
| 295 | &i.AcceptedAt, |
| 296 | &i.DeclinedAt, |
| 297 | &i.CanceledAt, |
| 298 | &i.CreatedAt, |
| 299 | &i.TargetUsername, |
| 300 | &i.InvitedByUsername, |
| 301 | ); err != nil { |
| 302 | return nil, err |
| 303 | } |
| 304 | items = append(items, i) |
| 305 | } |
| 306 | if err := rows.Err(); err != nil { |
| 307 | return nil, err |
| 308 | } |
| 309 | return items, nil |
| 310 | } |
| 311 | |
| 312 | const listPendingInvitationsForUser = `-- name: ListPendingInvitationsForUser :many |
| 313 | SELECT i.id, i.org_id, i.invited_by_user_id, i.target_user_id, i.target_email, i.role, i.token_hash, i.expires_at, i.accepted_at, i.declined_at, i.canceled_at, i.created_at, o.slug AS org_slug, o.display_name AS org_display_name |
| 314 | FROM org_invitations i |
| 315 | JOIN orgs o ON o.id = i.org_id |
| 316 | WHERE i.target_user_id = $1 |
| 317 | AND i.accepted_at IS NULL |
| 318 | AND i.declined_at IS NULL |
| 319 | AND i.canceled_at IS NULL |
| 320 | AND i.expires_at > now() |
| 321 | AND o.deleted_at IS NULL |
| 322 | ORDER BY i.created_at DESC |
| 323 | ` |
| 324 | |
| 325 | type ListPendingInvitationsForUserRow struct { |
| 326 | ID int64 |
| 327 | OrgID int64 |
| 328 | InvitedByUserID pgtype.Int8 |
| 329 | TargetUserID pgtype.Int8 |
| 330 | TargetEmail pgtype.Text |
| 331 | Role OrgRole |
| 332 | TokenHash []byte |
| 333 | ExpiresAt pgtype.Timestamptz |
| 334 | AcceptedAt pgtype.Timestamptz |
| 335 | DeclinedAt pgtype.Timestamptz |
| 336 | CanceledAt pgtype.Timestamptz |
| 337 | CreatedAt pgtype.Timestamptz |
| 338 | OrgSlug string |
| 339 | OrgDisplayName string |
| 340 | } |
| 341 | |
| 342 | // Two flavors: by user_id (already-claimed invites) and by email |
| 343 | // (claim-on-signup). Caller unions the two lookups when surfacing |
| 344 | // to the user. |
| 345 | func (q *Queries) ListPendingInvitationsForUser(ctx context.Context, db DBTX, targetUserID pgtype.Int8) ([]ListPendingInvitationsForUserRow, error) { |
| 346 | rows, err := db.Query(ctx, listPendingInvitationsForUser, targetUserID) |
| 347 | if err != nil { |
| 348 | return nil, err |
| 349 | } |
| 350 | defer rows.Close() |
| 351 | items := []ListPendingInvitationsForUserRow{} |
| 352 | for rows.Next() { |
| 353 | var i ListPendingInvitationsForUserRow |
| 354 | if err := rows.Scan( |
| 355 | &i.ID, |
| 356 | &i.OrgID, |
| 357 | &i.InvitedByUserID, |
| 358 | &i.TargetUserID, |
| 359 | &i.TargetEmail, |
| 360 | &i.Role, |
| 361 | &i.TokenHash, |
| 362 | &i.ExpiresAt, |
| 363 | &i.AcceptedAt, |
| 364 | &i.DeclinedAt, |
| 365 | &i.CanceledAt, |
| 366 | &i.CreatedAt, |
| 367 | &i.OrgSlug, |
| 368 | &i.OrgDisplayName, |
| 369 | ); err != nil { |
| 370 | return nil, err |
| 371 | } |
| 372 | items = append(items, i) |
| 373 | } |
| 374 | if err := rows.Err(); err != nil { |
| 375 | return nil, err |
| 376 | } |
| 377 | return items, nil |
| 378 | } |
| 379 |