made Licence optional

This commit is contained in:
Alex
2024-11-04 17:19:32 +01:00
parent eb7fc359e1
commit 0fa57bfe75
8 changed files with 69 additions and 38 deletions

View File

@@ -42,7 +42,8 @@ func (ur *UserRepository) UpdateUser(user *models.User) (*models.User, error) {
err := database.DB.Transaction(func(tx *gorm.DB) error {
// Check if the user exists in the database
var existingUser models.User
if err := tx.Preload("Licence.Categories").
if err := tx.Preload("Licence").
Preload("Licence.Categories").
Preload("Membership").
First(&existingUser, user.ID).Error; err != nil {
return err
@@ -56,13 +57,29 @@ func (ur *UserRepository) UpdateUser(user *models.User) (*models.User, error) {
return errors.ErrNoRowsAffected
}
// Handle the update of the LicenceCategories explicitly
if user.Licence.ID != 0 {
// Replace the Categories with the new list
if err := tx.Model(&existingUser.Licence).Association("Categories").Replace(user.Licence.Categories); err != nil {
return err
}
}
// Handle the update or creation of Licence and its Categories
// if user.Licence != nil {
// if existingUser.Licence == nil {
// // Create new Licence if it doesn't exist
// logger.Error.Printf("Licence creation: %+v", user.Licence)
// if err := tx.Create(user.Licence).Error; err != nil {
// return err
// }
// // Update user with new licence ID
// // if err := tx.Model(&existingUser).Update("licence_id", user.Licence.ID).Error; err != nil {
// // return err
// // }
// } else {
// // Update existing licence
// if err := tx.Model(&existingUser.Licence).Updates(user.Licence).Error; err != nil {
// return err
// }
// }
// // Replace the Categories with the new list
// if err := tx.Model(&existingUser.Licence).Association("Categories").Replace(user.Licence.Categories); err != nil {
// return err
// }
// }
// Update the Membership if provided
if user.Membership.ID != 0 {
@@ -71,13 +88,6 @@ func (ur *UserRepository) UpdateUser(user *models.User) (*models.User, error) {
}
}
// Update the Licence fields if provided
if user.Licence.ID != 0 {
if err := tx.Model(&existingUser.Licence).Updates(user.Licence).Error; err != nil {
return err
}
}
return nil
})