tenseleyflow/shithub / d392c48

Browse files

Split SetUserPrimaryEmail into LinkUserPrimaryEmail and MarkUserEmailPrimaryVerified

Authored by mfwolffe <wolffemf@dukes.jmu.edu>
SHA
d392c48b5076e1502e3fcd172cc8cbd368d94d16
Parents
5167aef
Tree
a50615c

4 changed files

StatusFile+-
M internal/meta/sqlc/models.go 61 0
M internal/users/queries/users.sql 11 3
M internal/users/sqlc/querier.go 6 1
M internal/users/sqlc/users.sql.go 20 6
internal/meta/sqlc/models.gomodified
@@ -8,8 +8,69 @@ import (
88
 	"github.com/jackc/pgx/v5/pgtype"
99
 )
1010
 
11
+type AuthThrottle struct {
12
+	ID              int64
13
+	Scope           string
14
+	Identifier      string
15
+	Hits            int32
16
+	WindowStartedAt pgtype.Timestamptz
17
+}
18
+
19
+type EmailVerification struct {
20
+	ID          int64
21
+	UserEmailID int64
22
+	TokenHash   []byte
23
+	ExpiresAt   pgtype.Timestamptz
24
+	UsedAt      pgtype.Timestamptz
25
+	CreatedAt   pgtype.Timestamptz
26
+}
27
+
1128
 type Meta struct {
1229
 	Key       string
1330
 	Value     []byte
1431
 	UpdatedAt pgtype.Timestamptz
1532
 }
33
+
34
+type PasswordReset struct {
35
+	ID        int64
36
+	UserID    int64
37
+	TokenHash []byte
38
+	ExpiresAt pgtype.Timestamptz
39
+	UsedAt    pgtype.Timestamptz
40
+	CreatedAt pgtype.Timestamptz
41
+}
42
+
43
+type User struct {
44
+	ID                int64
45
+	Username          string
46
+	DisplayName       string
47
+	PrimaryEmailID    pgtype.Int8
48
+	PasswordHash      string
49
+	PasswordAlgo      string
50
+	PasswordUpdatedAt pgtype.Timestamptz
51
+	EmailVerified     bool
52
+	LastLoginAt       pgtype.Timestamptz
53
+	SuspendedAt       pgtype.Timestamptz
54
+	SuspendedReason   pgtype.Text
55
+	DeletedAt         pgtype.Timestamptz
56
+	CreatedAt         pgtype.Timestamptz
57
+	UpdatedAt         pgtype.Timestamptz
58
+}
59
+
60
+type UserEmail struct {
61
+	ID                    int64
62
+	UserID                int64
63
+	Email                 string
64
+	IsPrimary             bool
65
+	Verified              bool
66
+	VerificationTokenHash []byte
67
+	VerificationSentAt    pgtype.Timestamptz
68
+	VerifiedAt            pgtype.Timestamptz
69
+	CreatedAt             pgtype.Timestamptz
70
+}
71
+
72
+type UsernameRedirect struct {
73
+	OldUsername string
74
+	UserID      int64
75
+	ChangedAt   pgtype.Timestamptz
76
+}
internal/users/queries/users.sqlmodified
@@ -28,10 +28,18 @@ SET password_hash = $2,
2828
     password_updated_at = now()
2929
 WHERE id = $1;
3030
 
31
--- name: SetUserPrimaryEmail :exec
31
+-- name: LinkUserPrimaryEmail :exec
32
+-- Sets the FK only. Does NOT flip users.email_verified — that happens via
33
+-- MarkUserEmailPrimaryVerified after the user clicks the verification link.
3234
 UPDATE users
33
-SET primary_email_id = $2,
34
-    email_verified   = true
35
+SET primary_email_id = $2
36
+WHERE id = $1;
37
+
38
+-- name: MarkUserEmailPrimaryVerified :exec
39
+-- Called after MarkUserEmailVerified for the primary email, to flip the
40
+-- denormalized users.email_verified flag.
41
+UPDATE users
42
+SET email_verified = true
3543
 WHERE id = $1;
3644
 
3745
 -- name: TouchUserLastLogin :exec
internal/users/sqlc/querier.gomodified
@@ -36,11 +36,16 @@ type Querier interface {
3636
 	GetUserEmailByAddress(ctx context.Context, db DBTX, email string) (UserEmail, error)
3737
 	GetUserEmailByID(ctx context.Context, db DBTX, id int64) (UserEmail, error)
3838
 	GetUserEmailByVerificationHash(ctx context.Context, db DBTX, verificationTokenHash []byte) (UserEmail, error)
39
+	// Sets the FK only. Does NOT flip users.email_verified — that happens via
40
+	// MarkUserEmailPrimaryVerified after the user clicks the verification link.
41
+	LinkUserPrimaryEmail(ctx context.Context, db DBTX, arg LinkUserPrimaryEmailParams) error
3942
 	ListUserEmailsForUser(ctx context.Context, db DBTX, userID int64) ([]UserEmail, error)
43
+	// Called after MarkUserEmailVerified for the primary email, to flip the
44
+	// denormalized users.email_verified flag.
45
+	MarkUserEmailPrimaryVerified(ctx context.Context, db DBTX, id int64) error
4046
 	MarkUserEmailVerified(ctx context.Context, db DBTX, id int64) error
4147
 	PurgeStaleAuthThrottle(ctx context.Context, db DBTX, windowStartedAt pgtype.Timestamptz) error
4248
 	ResetAuthThrottle(ctx context.Context, db DBTX, arg ResetAuthThrottleParams) error
43
-	SetUserPrimaryEmail(ctx context.Context, db DBTX, arg SetUserPrimaryEmailParams) error
4449
 	SetVerificationToken(ctx context.Context, db DBTX, arg SetVerificationTokenParams) error
4550
 	SoftDeleteUser(ctx context.Context, db DBTX, id int64) error
4651
 	SuspendUser(ctx context.Context, db DBTX, arg SuspendUserParams) error
internal/users/sqlc/users.sql.gomodified
@@ -120,20 +120,34 @@ func (q *Queries) GetUserByUsername(ctx context.Context, db DBTX, username strin
120120
 	return i, err
121121
 }
122122
 
123
-const setUserPrimaryEmail = `-- name: SetUserPrimaryEmail :exec
123
+const linkUserPrimaryEmail = `-- name: LinkUserPrimaryEmail :exec
124124
 UPDATE users
125
-SET primary_email_id = $2,
126
-    email_verified   = true
125
+SET primary_email_id = $2
127126
 WHERE id = $1
128127
 `
129128
 
130
-type SetUserPrimaryEmailParams struct {
129
+type LinkUserPrimaryEmailParams struct {
131130
 	ID             int64
132131
 	PrimaryEmailID pgtype.Int8
133132
 }
134133
 
135
-func (q *Queries) SetUserPrimaryEmail(ctx context.Context, db DBTX, arg SetUserPrimaryEmailParams) error {
136
-	_, err := db.Exec(ctx, setUserPrimaryEmail, arg.ID, arg.PrimaryEmailID)
134
+// Sets the FK only. Does NOT flip users.email_verified — that happens via
135
+// MarkUserEmailPrimaryVerified after the user clicks the verification link.
136
+func (q *Queries) LinkUserPrimaryEmail(ctx context.Context, db DBTX, arg LinkUserPrimaryEmailParams) error {
137
+	_, err := db.Exec(ctx, linkUserPrimaryEmail, arg.ID, arg.PrimaryEmailID)
138
+	return err
139
+}
140
+
141
+const markUserEmailPrimaryVerified = `-- name: MarkUserEmailPrimaryVerified :exec
142
+UPDATE users
143
+SET email_verified = true
144
+WHERE id = $1
145
+`
146
+
147
+// Called after MarkUserEmailVerified for the primary email, to flip the
148
+// denormalized users.email_verified flag.
149
+func (q *Queries) MarkUserEmailPrimaryVerified(ctx context.Context, db DBTX, id int64) error {
150
+	_, err := db.Exec(ctx, markUserEmailPrimaryVerified, id)
137151
 	return err
138152
 }
139153