Skip to content

Commit

Permalink
fix: is_anonymous should be a default column
Browse files Browse the repository at this point in the history
  • Loading branch information
kangmingtay committed Mar 1, 2024
1 parent 30e2be4 commit 0ce369e
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 10 deletions.
9 changes: 6 additions & 3 deletions internal/api/identity.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,12 @@ func (a *API) linkIdentityToUser(r *http.Request, ctx context.Context, tx *stora
if terr := targetUser.Confirm(tx); terr != nil {
return nil, terr
}
// if the user is an anonymous user, we need to reload the user object again to fetch the is_anonymous value
if terr := tx.Reload(targetUser); terr != nil {
return nil, terr

if targetUser.IsAnonymous {
targetUser.IsAnonymous = false
if terr := tx.UpdateOnly(targetUser, "is_anonymous"); terr != nil {
return nil, terr
}
}
}

Expand Down
1 change: 1 addition & 0 deletions internal/api/signup.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ func (params *SignupParams) ToUserModel(isSSOUser bool) (user *models.User, err
user, err = models.NewUser(params.Phone, "", params.Password, params.Aud, params.Data)
case "anonymous":
user, err = models.NewUser("", "", "", params.Aud, params.Data)
user.IsAnonymous = true
default:
// handles external provider case
user, err = models.NewUser("", params.Email, params.Password, params.Aud, params.Data)
Expand Down
6 changes: 6 additions & 0 deletions internal/api/verify.go
Original file line number Diff line number Diff line change
Expand Up @@ -531,6 +531,12 @@ func (a *API) emailChangeVerify(r *http.Request, ctx context.Context, conn *stor
return terr
}
}
if user.IsAnonymous {
user.IsAnonymous = false
if terr := tx.UpdateOnly(user, "is_anonymous"); terr != nil {
return terr
}
}
if terr := tx.Load(user, "Identities"); terr != nil {
return internalServerError("Error refetching identities").WithInternalError(terr)
}
Expand Down
2 changes: 1 addition & 1 deletion internal/models/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ type User struct {
UpdatedAt time.Time `json:"updated_at" db:"updated_at"`
BannedUntil *time.Time `json:"banned_until,omitempty" db:"banned_until"`
DeletedAt *time.Time `json:"deleted_at,omitempty" db:"deleted_at"`
IsAnonymous bool `json:"is_anonymous" db:"is_anonymous" rw:"r"`
IsAnonymous bool `json:"is_anonymous" db:"is_anonymous"`

DONTUSEINSTANCEID uuid.UUID `json:"-" db:"instance_id"`
}
Expand Down
7 changes: 1 addition & 6 deletions migrations/20240214120130_add_is_anonymous_column.up.sql
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
do $$
begin
alter table {{ index .Options "Namespace" }}.users
add column if not exists is_anonymous boolean generated always as (
case
when (email is null or email = '') and (phone is null or phone = '') then true
else false
end
) stored;
add column if not exists is_anonymous boolean not null default false;

create index if not exists users_is_anonymous_idx on {{ index .Options "Namespace" }}.users using btree (is_anonymous);
end
Expand Down

0 comments on commit 0ce369e

Please sign in to comment.