| 1 | -- SPDX-License-Identifier: AGPL-3.0-or-later |
| 2 | |
| 3 | -- name: CreateUserEmail :one |
| 4 | INSERT INTO user_emails (user_id, email, is_primary, verified, verification_token_hash, verification_sent_at) |
| 5 | VALUES ($1, $2, $3, $4, $5, CASE WHEN $5::bytea IS NULL THEN NULL ELSE now() END) |
| 6 | RETURNING id, user_id, email, is_primary, verified, verification_token_hash, |
| 7 | verification_sent_at, verified_at, created_at; |
| 8 | |
| 9 | -- name: GetUserEmailByAddress :one |
| 10 | SELECT id, user_id, email, is_primary, verified, verification_token_hash, |
| 11 | verification_sent_at, verified_at, created_at |
| 12 | FROM user_emails |
| 13 | WHERE email = $1; |
| 14 | |
| 15 | -- name: GetUserEmailByID :one |
| 16 | SELECT id, user_id, email, is_primary, verified, verification_token_hash, |
| 17 | verification_sent_at, verified_at, created_at |
| 18 | FROM user_emails |
| 19 | WHERE id = $1; |
| 20 | |
| 21 | -- name: ListUserEmailsForUser :many |
| 22 | SELECT id, user_id, email, is_primary, verified, verification_token_hash, |
| 23 | verification_sent_at, verified_at, created_at |
| 24 | FROM user_emails |
| 25 | WHERE user_id = $1 |
| 26 | ORDER BY is_primary DESC, created_at; |
| 27 | |
| 28 | -- name: MarkUserEmailVerified :exec |
| 29 | UPDATE user_emails |
| 30 | SET verified = true, |
| 31 | verified_at = now(), |
| 32 | verification_token_hash = NULL |
| 33 | WHERE id = $1; |
| 34 | |
| 35 | -- name: SetVerificationToken :exec |
| 36 | UPDATE user_emails |
| 37 | SET verification_token_hash = $2, |
| 38 | verification_sent_at = now() |
| 39 | WHERE id = $1; |
| 40 | |
| 41 | -- name: GetUserEmailByVerificationHash :one |
| 42 | SELECT id, user_id, email, is_primary, verified, verification_token_hash, |
| 43 | verification_sent_at, verified_at, created_at |
| 44 | FROM user_emails |
| 45 | WHERE verification_token_hash = $1; |
| 46 | |
| 47 | -- name: SetUserEmailPrimary :exec |
| 48 | -- Atomically unset the existing primary and set the supplied row as |
| 49 | -- primary. Caller MUST have already verified the row belongs to the |
| 50 | -- user and is verified. |
| 51 | UPDATE user_emails SET is_primary = (id = $2) WHERE user_id = $1; |
| 52 | |
| 53 | -- name: DeleteUserEmail :execrows |
| 54 | -- Scoped delete: caller must pass owning user_id. Refuses to delete |
| 55 | -- the primary email (UI must guide the user to set a different primary first). |
| 56 | DELETE FROM user_emails |
| 57 | WHERE id = $1 AND user_id = $2 AND is_primary = false; |
| 58 | |
| 59 | -- name: CountVerifiedUserEmails :one |
| 60 | SELECT count(*) FROM user_emails WHERE user_id = $1 AND verified = true; |
| 61 |