Chore: Add user service method Update (#53300)

* Chore: Add user service method Update

* Remove UpdateUser from store interface
This commit is contained in:
idafurjes
2022-08-04 14:22:44 +02:00
committed by GitHub
parent 191ab3bb01
commit 6fdb6ea6f6
8 changed files with 43 additions and 22 deletions
+11 -11
View File
@@ -209,28 +209,28 @@ func (ls *Implementation) createUser(extUser *models.ExternalUserInfo) (*user.Us
return ls.CreateUser(cmd)
}
func (ls *Implementation) updateUser(ctx context.Context, user *user.User, extUser *models.ExternalUserInfo) error {
func (ls *Implementation) updateUser(ctx context.Context, usr *user.User, extUser *models.ExternalUserInfo) error {
// sync user info
updateCmd := &models.UpdateUserCommand{
UserId: user.ID,
updateCmd := &user.UpdateUserCommand{
UserID: usr.ID,
}
needsUpdate := false
if extUser.Login != "" && extUser.Login != user.Login {
if extUser.Login != "" && extUser.Login != usr.Login {
updateCmd.Login = extUser.Login
user.Login = extUser.Login
usr.Login = extUser.Login
needsUpdate = true
}
if extUser.Email != "" && extUser.Email != user.Email {
if extUser.Email != "" && extUser.Email != usr.Email {
updateCmd.Email = extUser.Email
user.Email = extUser.Email
usr.Email = extUser.Email
needsUpdate = true
}
if extUser.Name != "" && extUser.Name != user.Name {
if extUser.Name != "" && extUser.Name != usr.Name {
updateCmd.Name = extUser.Name
user.Name = extUser.Name
usr.Name = extUser.Name
needsUpdate = true
}
@@ -238,8 +238,8 @@ func (ls *Implementation) updateUser(ctx context.Context, user *user.User, extUs
return nil
}
logger.Debug("Syncing user info", "id", user.ID, "update", updateCmd)
return ls.SQLStore.UpdateUser(ctx, updateCmd)
logger.Debug("Syncing user info", "id", usr.ID, "update", updateCmd)
return ls.userService.Update(ctx, updateCmd)
}
func (ls *Implementation) updateUserAuth(ctx context.Context, user *user.User, extUser *models.ExternalUserInfo) error {
@@ -137,10 +137,6 @@ func (m *SQLStoreMock) CreateUser(ctx context.Context, cmd user.CreateUserComman
return nil, m.ExpectedError
}
func (m *SQLStoreMock) UpdateUser(ctx context.Context, cmd *models.UpdateUserCommand) error {
return m.ExpectedError
}
func (m *SQLStoreMock) ChangeUserPassword(ctx context.Context, cmd *models.ChangeUserPasswordCommand) error {
return m.ExpectedError
}
-1
View File
@@ -31,7 +31,6 @@ type Store interface {
GetUserLoginAttemptCount(ctx context.Context, query *models.GetUserLoginAttemptCountQuery) error
DeleteOldLoginAttempts(ctx context.Context, cmd *models.DeleteOldLoginAttemptsCommand) error
CreateUser(ctx context.Context, cmd user.CreateUserCommand) (*user.User, error)
UpdateUser(ctx context.Context, cmd *models.UpdateUserCommand) error
ChangeUserPassword(ctx context.Context, cmd *models.ChangeUserPasswordCommand) error
UpdateUserLastSeenAt(ctx context.Context, cmd *models.UpdateUserLastSeenAtCommand) error
SetUsingOrg(ctx context.Context, cmd *models.SetUsingOrgCommand) error
+9
View File
@@ -66,6 +66,15 @@ type GetUserByEmailQuery struct {
Email string
}
type UpdateUserCommand struct {
Name string `json:"name"`
Email string `json:"email"`
Login string `json:"login"`
Theme string `json:"theme"`
UserID int64 `json:"-"`
}
func (u *User) NameOrFallback() string {
if u.Name != "" {
return u.Name
+1
View File
@@ -10,4 +10,5 @@ type Service interface {
GetByID(context.Context, *GetUserByIDQuery) (*User, error)
GetByLogin(context.Context, *GetUserByLoginQuery) (*User, error)
GetByEmail(context.Context, *GetUserByEmailQuery) (*User, error)
Update(context.Context, *UpdateUserCommand) error
}
+12
View File
@@ -267,3 +267,15 @@ func (s *Service) GetByEmail(ctx context.Context, query *user.GetUserByEmailQuer
}
return q.Result, nil
}
// TODO: remove wrapper around sqlstore
func (s *Service) Update(ctx context.Context, cmd *user.UpdateUserCommand) error {
q := &models.UpdateUserCommand{
Name: cmd.Name,
Email: cmd.Email,
Login: cmd.Login,
Theme: cmd.Theme,
UserId: cmd.UserID,
}
return s.sqlStore.UpdateUser(ctx, q)
}
+4
View File
@@ -34,3 +34,7 @@ func (f *FakeUserService) GetByLogin(ctx context.Context, query *user.GetUserByL
func (f *FakeUserService) GetByEmail(ctx context.Context, query *user.GetUserByEmailQuery) (*user.User, error) {
return f.ExpectedUser, f.ExpectedError
}
func (f *FakeUserService) Update(ctx context.Context, cmd *user.UpdateUserCommand) error {
return f.ExpectedError
}