Skip to content

Commit 6ef4b03

Browse files
committed
refactor(api): update User interface to use entity objects
- Change UserUpdate to accept *models.User instead of separate id/changes parameters - Change UserDelete to accept *models.User instead of id string - Update service layer to resolve users before update/delete operations - Remove UserChanges model usage in favor of direct entity updates - Add applyUserChanges helper function for user field updates - Update all service methods to use new interface signatures - Add proper BSON marshaling with ObjectID conversion in UserUpdate - Update AuthLocalUser to modify user object directly before update - Update CreateUserToken to handle preferred namespace updates - Update LeaveNamespace to resolve user before clearing preferences - Update Setup to pass user entity to UserDelete on rollback - Simplify UserDelete implementation using DeleteOne directly - Add case-insensitive field comparison in applyUserChanges - Normalize string fields (username, email, recovery_email) to lowercase - Update password hashing to return complete UserPassword object - Update all mock expectations to use entity objects - Add complete user and updatedUser fixtures in test cases - Reorder mock setup in tests for better clarity - Fix expected error user state in UpdatePasswordUser tests BREAKING CHANGE: UserUpdate and UserDelete now accept *models.User and return only error
1 parent 5aab4be commit 6ef4b03

File tree

14 files changed

+587
-318
lines changed

14 files changed

+587
-318
lines changed

api/services/auth.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -310,15 +310,16 @@ func (s *service) AuthLocalUser(ctx context.Context, req *requests.AuthLocalUser
310310
}
311311

312312
// Updates last_login and the hash algorithm to bcrypt if still using SHA256
313-
changes := &models.UserChanges{LastLogin: clock.Now(), PreferredNamespace: &tenantID}
313+
user.LastLogin = clock.Now()
314+
user.Preferences.PreferredNamespace = tenantID
314315
if !strings.HasPrefix(user.Password.Hash, "$") {
315316
if neo, _ := models.HashUserPassword(req.Password); neo.Hash != "" {
316-
changes.Password = neo.Hash
317+
user.Password = neo
317318
}
318319
}
319320

320321
// TODO: evaluate make this update in a go routine.
321-
if err := s.store.UserUpdate(ctx, user.ID, changes); err != nil {
322+
if err := s.store.UserUpdate(ctx, user); err != nil {
322323
return nil, 0, "", NewErrUserUpdate(user, err)
323324
}
324325

@@ -391,7 +392,11 @@ func (s *service) CreateUserToken(ctx context.Context, req *requests.CreateUserT
391392
role = member.Role.String()
392393

393394
if user.Preferences.PreferredNamespace != namespace.TenantID {
394-
_ = s.store.UserUpdate(ctx, user.ID, &models.UserChanges{PreferredNamespace: &tenantID})
395+
user.Preferences.PreferredNamespace = tenantID
396+
// TODO: evaluate make this update in a go routine.
397+
if err := s.store.UserUpdate(ctx, user); err != nil {
398+
return nil, NewErrUserUpdate(user, err)
399+
}
395400
}
396401
}
397402

0 commit comments

Comments
 (0)