refac: several unverified users can exist at once: an empty reset or verification code is stored as no value so it doesn't break the unique constraint, and deleting a user removes the row so the same username and email can register again

This commit is contained in:
fsylvain committed 2026-09-24 01:59:24 +02:00
1 parent 0b4e614ec9
commit 6e9e108033
6 files changed
+377 -16

No files matched your search

@@ -5,15 +5,29 @@ import (
"RenewCMS/internal/infrastructure/persistence/models"
)
func nullableStringToDomain(value *string) string {
if value == nil {
return ""
}
return *value
}
func DomainToNullableString(value string) *string {
if value == "" {
return nil
}
return &value
}
func UserToDomain(m models.User) user.User {
return user.User{
ID: m.ID,
Username: m.Username,
Password: m.Password,
PasswordResetCode: m.PasswordResetCode,
PasswordResetCode: nullableStringToDomain(m.PasswordResetCode),
Email: m.Email,
IsVerified: m.IsVerified,
VerificationCode: m.VerificationCode,
VerificationCode: nullableStringToDomain(m.VerificationCode),
VerificationExpiration: m.VerificationExpiration,
CreatedAt: m.CreatedAt,
UpdatedAt: m.UpdatedAt,
@@ -24,10 +38,10 @@ func UserToModel(a user.User) models.User {
return models.User{
Username: a.Username,
Password: a.Password,
PasswordResetCode: a.PasswordResetCode,
PasswordResetCode: DomainToNullableString(a.PasswordResetCode),
Email: a.Email,
IsVerified: a.IsVerified,
VerificationCode: a.VerificationCode,
VerificationCode: DomainToNullableString(a.VerificationCode),
VerificationExpiration: a.VerificationExpiration,
CreatedAt: a.CreatedAt,
UpdatedAt: a.UpdatedAt,
@@ -2,19 +2,16 @@ package models
import (
"time"
"gorm.io/gorm"
)
type User struct {
gorm.Model
ID uint32 `gorm:"primaryKey;autoIncrement"`
Username string `gorm:"unique;not null"`
Password string `gorm:"not null"`
PasswordResetCode string `gorm:"unique"`
Email string `gorm:"unique;not null"`
IsVerified bool `gorm:"default=false;not null"`
VerificationCode string `gorm:"unique"`
ID uint32 `gorm:"primaryKey;autoIncrement"`
Username string `gorm:"unique;not null"`
Password string `gorm:"not null"`
PasswordResetCode *string `gorm:"unique"`
Email string `gorm:"unique;not null"`
IsVerified bool `gorm:"default=false;not null"`
VerificationCode *string `gorm:"unique"`
VerificationExpiration time.Time
CreatedAt time.Time `gorm:"autoCreateTime"`
UpdatedAt time.Time `gorm:"autoUpdateTime"`
@@ -30,8 +30,9 @@ func (u *UserRepository) Create(user domainUser.User) (domainUser.User, error) {
creationResult := u.db.Create(&entity.User{
Username: user.Username,
Password: user.Password,
PasswordResetCode: mappers.DomainToNullableString(user.PasswordResetCode),
Email: user.Email,
VerificationCode: user.VerificationCode,
VerificationCode: mappers.DomainToNullableString(user.VerificationCode),
VerificationExpiration: user.VerificationExpiration,
})
if creationResult.Error != nil {
@@ -121,7 +122,7 @@ func (u *UserRepository) UpdatePasswordResetCode(userId uint32, code string) (do
return domainUser.User{}, mapNotFoundErr(err, domainUser.ErrUserNotFound)
}
localUser.PasswordResetCode = code
localUser.PasswordResetCode = mappers.DomainToNullableString(code)
err = u.db.Save(&localUser).Error
if err != nil {
return domainUser.User{}, err