diff --git a/backend/pkg/database/interface.go b/backend/pkg/database/interface.go index 186fcf0a..8dc30a48 100644 --- a/backend/pkg/database/interface.go +++ b/backend/pkg/database/interface.go @@ -7,6 +7,7 @@ import ( type DatabaseRepository interface { Close() error + GetCurrentUser() models.User - CreateProviderCredentials(ctx context.Context, providerCreds models.ProviderCredential) error + CreateProviderCredentials(ctx context.Context, providerCreds *models.ProviderCredential) error } diff --git a/backend/pkg/database/sqlite_repository.go b/backend/pkg/database/sqlite_repository.go index f6f91aeb..a602569d 100644 --- a/backend/pkg/database/sqlite_repository.go +++ b/backend/pkg/database/sqlite_repository.go @@ -52,6 +52,13 @@ func NewRepository(appConfig config.Interface, globalLogger logrus.FieldLogger) return nil, fmt.Errorf("Failed to automigrate! - %v", err) } + // create/update admin user + adminUser := models.User{} + err = database.FirstOrCreate(&adminUser, models.User{Username: "admin"}).Error + if err != nil { + return nil, fmt.Errorf("Failed to create admin user! - %v", err) + } + deviceRepo := sqliteRepository{ appConfig: appConfig, logger: globalLogger, @@ -71,12 +78,17 @@ func (sr *sqliteRepository) Close() error { return nil } +func (sr *sqliteRepository) GetCurrentUser() models.User { + return models.User{Model: &gorm.Model{ID: 1}} +} + //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // DeviceSummary //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// -func (sr *sqliteRepository) CreateProviderCredentials(ctx context.Context, providerCreds models.ProviderCredential) error { - return sr.gormClient.WithContext(ctx).Create(&providerCreds).Error +func (sr *sqliteRepository) CreateProviderCredentials(ctx context.Context, providerCreds *models.ProviderCredential) error { + providerCreds.UserId = sr.GetCurrentUser().ID + return sr.gormClient.WithContext(ctx).Create(providerCreds).Error } //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// diff --git a/backend/pkg/models/provider_credential.go b/backend/pkg/models/provider_credential.go index 1349d54e..d4f0fd78 100644 --- a/backend/pkg/models/provider_credential.go +++ b/backend/pkg/models/provider_credential.go @@ -3,8 +3,8 @@ package models type ProviderCredential struct { //TODO: PRIMARY KEY should be UserId + ProviderId + PatientId - User User `gorm:"foreignKey:ID;references:UserId"` - UserId int `json:"user_id"` + User User `json:"user,omitempty" gorm:"foreignKey:ID;references:UserId"` + UserId uint `json:"user_id"` ProviderId string `json:"provider" gorm:"primaryKey"` PatientId string `json:"patient" gorm:"primaryKey"` diff --git a/backend/pkg/models/user.go b/backend/pkg/models/user.go index bb80efc4..a081c798 100644 --- a/backend/pkg/models/user.go +++ b/backend/pkg/models/user.go @@ -3,6 +3,6 @@ package models import "gorm.io/gorm" type User struct { - gorm.Model - Username string `json:"username"` + *gorm.Model + Username string `json:"username" gorm:"unique"` } diff --git a/backend/pkg/web/handler/create_provider_credentials.go b/backend/pkg/web/handler/create_provider_credentials.go index fcd039e9..1da90482 100644 --- a/backend/pkg/web/handler/create_provider_credentials.go +++ b/backend/pkg/web/handler/create_provider_credentials.go @@ -12,7 +12,7 @@ func CreateProviderCredentials(c *gin.Context) { logger := c.MustGet("LOGGER").(*logrus.Entry) databaseRepo := c.MustGet("REPOSITORY").(database.DatabaseRepository) - var providerCred models.ProviderCredential + providerCred := models.ProviderCredential{} if err := c.ShouldBindJSON(&providerCred); err != nil { logger.Errorln("An error occurred while parsing posted provider credential", err) c.JSON(http.StatusBadRequest, gin.H{"success": false}) @@ -21,7 +21,7 @@ func CreateProviderCredentials(c *gin.Context) { logger.Infof("Parsed Create Provider Credentials Payload: %v", providerCred) - err := databaseRepo.CreateProviderCredentials(c, providerCred) + err := databaseRepo.CreateProviderCredentials(c, &providerCred) if err != nil { logger.Errorln("An error occurred while storing provider credential", err) c.JSON(http.StatusInternalServerError, gin.H{"success": false})