diff --git a/backend/internal/controller/test_controller.go b/backend/internal/controller/test_controller.go index 7fb3081..e526d84 100644 --- a/backend/internal/controller/test_controller.go +++ b/backend/internal/controller/test_controller.go @@ -33,5 +33,10 @@ func (tc *TestController) resetAndSeedHandler(c *gin.Context) { return } + if err := tc.TestService.ResetAppConfig(); err != nil { + utils.ControllerError(c, err) + return + } + c.Status(http.StatusNoContent) } diff --git a/backend/internal/model/app_config.go b/backend/internal/model/app_config.go index fae0fd7..34c5f6f 100644 --- a/backend/internal/model/app_config.go +++ b/backend/internal/model/app_config.go @@ -1,11 +1,12 @@ package model type AppConfigVariable struct { - Key string `gorm:"primaryKey;not null"` - Type string - IsPublic bool - IsInternal bool - Value string + Key string `gorm:"primaryKey;not null"` + Type string + IsPublic bool + IsInternal bool + Value string + DefaultValue string } type AppConfig struct { diff --git a/backend/internal/service/app_config_service.go b/backend/internal/service/app_config_service.go index 8181752..61b17fe 100644 --- a/backend/internal/service/app_config_service.go +++ b/backend/internal/service/app_config_service.go @@ -31,43 +31,43 @@ func NewAppConfigService(db *gorm.DB) *AppConfigService { var defaultDbConfig = model.AppConfig{ AppName: model.AppConfigVariable{ - Key: "appName", - Type: "string", - IsPublic: true, - Value: "Pocket ID", + Key: "appName", + Type: "string", + IsPublic: true, + DefaultValue: "Pocket ID", }, SessionDuration: model.AppConfigVariable{ - Key: "sessionDuration", - Type: "number", - Value: "60", + Key: "sessionDuration", + Type: "number", + DefaultValue: "60", }, EmailsVerified: model.AppConfigVariable{ - Key: "emailsVerified", - Type: "bool", - Value: "false", + Key: "emailsVerified", + Type: "bool", + DefaultValue: "false", }, BackgroundImageType: model.AppConfigVariable{ - Key: "backgroundImageType", - Type: "string", - IsInternal: true, - Value: "jpg", + Key: "backgroundImageType", + Type: "string", + IsInternal: true, + DefaultValue: "jpg", }, LogoLightImageType: model.AppConfigVariable{ - Key: "logoLightImageType", - Type: "string", - IsInternal: true, - Value: "svg", + Key: "logoLightImageType", + Type: "string", + IsInternal: true, + DefaultValue: "svg", }, LogoDarkImageType: model.AppConfigVariable{ - Key: "logoDarkImageType", - Type: "string", - IsInternal: true, - Value: "svg", + Key: "logoDarkImageType", + Type: "string", + IsInternal: true, + DefaultValue: "svg", }, EmailEnabled: model.AppConfigVariable{ - Key: "emailEnabled", - Type: "bool", - Value: "false", + Key: "emailEnabled", + Type: "bool", + DefaultValue: "false", }, SmtpHost: model.AppConfigVariable{ Key: "smtpHost", @@ -120,7 +120,7 @@ func (s *AppConfigService) UpdateAppConfig(input dto.AppConfigUpdateDto) ([]mode tx.Commit() - if err := s.loadDbConfigFromDb(); err != nil { + if err := s.LoadDbConfigFromDb(); err != nil { return nil, err } @@ -134,7 +134,7 @@ func (s *AppConfigService) UpdateImageType(imageName string, fileType string) er return err } - return s.loadDbConfigFromDb() + return s.LoadDbConfigFromDb() } func (s *AppConfigService) ListAppConfig(showAll bool) ([]model.AppConfigVariable, error) { @@ -151,6 +151,13 @@ func (s *AppConfigService) ListAppConfig(showAll bool) ([]model.AppConfigVariabl return nil, err } + // Set the value to the default value if it is empty + for i := range configuration { + if configuration[i].Value == "" && configuration[i].DefaultValue != "" { + configuration[i].Value = configuration[i].DefaultValue + } + } + return configuration, nil } @@ -206,10 +213,11 @@ func (s *AppConfigService) InitDbConfig() error { } // Update existing configuration if it differs from the default - if storedConfigVar.Type != defaultConfigVar.Type || storedConfigVar.IsPublic != defaultConfigVar.IsPublic || storedConfigVar.IsInternal != defaultConfigVar.IsInternal { + if storedConfigVar.Type != defaultConfigVar.Type || storedConfigVar.IsPublic != defaultConfigVar.IsPublic || storedConfigVar.IsInternal != defaultConfigVar.IsInternal || storedConfigVar.DefaultValue != defaultConfigVar.DefaultValue { storedConfigVar.Type = defaultConfigVar.Type storedConfigVar.IsPublic = defaultConfigVar.IsPublic storedConfigVar.IsInternal = defaultConfigVar.IsInternal + storedConfigVar.DefaultValue = defaultConfigVar.DefaultValue if err := s.db.Save(&storedConfigVar).Error; err != nil { return err } @@ -229,10 +237,11 @@ func (s *AppConfigService) InitDbConfig() error { } } } - return s.loadDbConfigFromDb() + return s.LoadDbConfigFromDb() } -func (s *AppConfigService) loadDbConfigFromDb() error { +// LoadDbConfigFromDb loads the configuration values from the database into the DbConfig struct. +func (s *AppConfigService) LoadDbConfigFromDb() error { dbConfigReflectValue := reflect.ValueOf(s.DbConfig).Elem() for i := 0; i < dbConfigReflectValue.NumField(); i++ { @@ -243,6 +252,10 @@ func (s *AppConfigService) loadDbConfigFromDb() error { return err } + if storedConfigVar.Value == "" && storedConfigVar.DefaultValue != "" { + storedConfigVar.Value = storedConfigVar.DefaultValue + } + dbConfigField.Set(reflect.ValueOf(storedConfigVar)) } diff --git a/backend/internal/service/test_service.go b/backend/internal/service/test_service.go index 5fba0c3..62f0568 100644 --- a/backend/internal/service/test_service.go +++ b/backend/internal/service/test_service.go @@ -138,8 +138,8 @@ func (s *TestService) SeedDatabase() error { return err } - publicKey1, err := getCborPublicKey("MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEwcOo5KV169KR67QEHrcYkeXE3CCxv2BgwnSq4VYTQxyLtdmKxegexa8JdwFKhKXa2BMI9xaN15BoL6wSCRFJhg==") - publicKey2, err := getCborPublicKey("MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAESq/wR8QbBu3dKnpaw/v0mDxFFDwnJ/L5XHSg2tAmq5x1BpSMmIr3+DxCbybVvGRmWGh8kKhy7SMnK91M6rFHTA==") + publicKey1, err := s.getCborPublicKey("MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEwcOo5KV169KR67QEHrcYkeXE3CCxv2BgwnSq4VYTQxyLtdmKxegexa8JdwFKhKXa2BMI9xaN15BoL6wSCRFJhg==") + publicKey2, err := s.getCborPublicKey("MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAESq/wR8QbBu3dKnpaw/v0mDxFFDwnJ/L5XHSg2tAmq5x1BpSMmIr3+DxCbybVvGRmWGh8kKhy7SMnK91M6rFHTA==") if err != nil { return err } @@ -187,17 +187,16 @@ func (s *TestService) ResetDatabase() error { return err } + // Delete all rows from all tables for _, table := range tables { if err := tx.Exec("DELETE FROM " + table).Error; err != nil { return err } } + return nil }) - if err != nil { - return err - } - err = s.appConfigService.InitDbConfig() + return err } @@ -215,8 +214,23 @@ func (s *TestService) ResetApplicationImages() error { return nil } +func (s *TestService) ResetAppConfig() error { + // Reseed the config variables + if err := s.appConfigService.InitDbConfig(); err != nil { + return err + } + + // Reset all app config variables to their default values + if err := s.db.Session(&gorm.Session{AllowGlobalUpdate: true}).Model(&model.AppConfigVariable{}).Update("value", "").Error; err != nil { + return err + } + + // Reload the app config from the database after resetting the values + return s.appConfigService.LoadDbConfigFromDb() +} + // getCborPublicKey decodes a Base64 encoded public key and returns the CBOR encoded COSE key -func getCborPublicKey(base64PublicKey string) ([]byte, error) { +func (s *TestService) getCborPublicKey(base64PublicKey string) ([]byte, error) { decodedKey, err := base64.StdEncoding.DecodeString(base64PublicKey) if err != nil { return nil, fmt.Errorf("failed to decode base64 key: %w", err) diff --git a/backend/migrations/20241025214824_app_config_default_value.down.sql b/backend/migrations/20241025214824_app_config_default_value.down.sql new file mode 100644 index 0000000..4acd64d --- /dev/null +++ b/backend/migrations/20241025214824_app_config_default_value.down.sql @@ -0,0 +1 @@ +ALTER TABLE app_config_variables DROP COLUMN default_value; \ No newline at end of file diff --git a/backend/migrations/20241025214824_app_config_default_value.up.sql b/backend/migrations/20241025214824_app_config_default_value.up.sql new file mode 100644 index 0000000..572af61 --- /dev/null +++ b/backend/migrations/20241025214824_app_config_default_value.up.sql @@ -0,0 +1 @@ +ALTER TABLE app_config_variables ADD COLUMN default_value TEXT; \ No newline at end of file diff --git a/frontend/src/lib/services/app-config-service.ts b/frontend/src/lib/services/app-config-service.ts index e9d4703..fb29877 100644 --- a/frontend/src/lib/services/app-config-service.ts +++ b/frontend/src/lib/services/app-config-service.ts @@ -73,8 +73,8 @@ export default class AppConfigService extends APIService { return true; } else if (value === 'false') { return false; - } else if (!isNaN(Number(value))) { - return Number(value); + } else if (!isNaN(parseFloat(value))) { + return parseFloat(value); } else { return value; }