diff --git a/backend/pkg/database/interface.go b/backend/pkg/database/interface.go index 8dc30a48..81a5c8ca 100644 --- a/backend/pkg/database/interface.go +++ b/backend/pkg/database/interface.go @@ -10,4 +10,5 @@ type DatabaseRepository interface { GetCurrentUser() models.User CreateProviderCredentials(ctx context.Context, providerCreds *models.ProviderCredential) error + GetProviderCredentials(ctx context.Context) ([]models.ProviderCredential, error) } diff --git a/backend/pkg/database/sqlite_repository.go b/backend/pkg/database/sqlite_repository.go index a602569d..4bcd55f6 100644 --- a/backend/pkg/database/sqlite_repository.go +++ b/backend/pkg/database/sqlite_repository.go @@ -88,7 +88,25 @@ func (sr *sqliteRepository) GetCurrentUser() models.User { func (sr *sqliteRepository) CreateProviderCredentials(ctx context.Context, providerCreds *models.ProviderCredential) error { providerCreds.UserId = sr.GetCurrentUser().ID - return sr.gormClient.WithContext(ctx).Create(providerCreds).Error + + if sr.gormClient.WithContext(ctx).Model(&providerCreds). + Where(models.ProviderCredential{ + UserId: providerCreds.UserId, + ProviderId: providerCreds.ProviderId, + PatientId: providerCreds.PatientId}).Updates(&providerCreds).RowsAffected == 0 { + return sr.gormClient.WithContext(ctx).Create(&providerCreds).Error + } + return nil +} + +func (sr *sqliteRepository) GetProviderCredentials(ctx context.Context) ([]models.ProviderCredential, error) { + + var providerCredentials []models.ProviderCredential + results := sr.gormClient.WithContext(ctx). + Where(models.ProviderCredential{UserId: sr.GetCurrentUser().ID}). + Find(&providerCredentials) + + return providerCredentials, results.Error } //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// diff --git a/backend/pkg/models/provider_credential.go b/backend/pkg/models/provider_credential.go index d4f0fd78..a2416b7b 100644 --- a/backend/pkg/models/provider_credential.go +++ b/backend/pkg/models/provider_credential.go @@ -6,8 +6,8 @@ type ProviderCredential struct { 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"` + ProviderId string `json:"provider_id" gorm:"primaryKey"` + PatientId string `json:"patient_id" gorm:"primaryKey"` OauthEndpointBaseUrl string `json:"oauth_endpoint_base_url"` ApiEndpointBaseUrl string `json:"api_endpoint_base_url"` diff --git a/backend/pkg/web/handler/create_provider_credentials.go b/backend/pkg/web/handler/provider_credentials.go similarity index 68% rename from backend/pkg/web/handler/create_provider_credentials.go rename to backend/pkg/web/handler/provider_credentials.go index 1da90482..eb41264b 100644 --- a/backend/pkg/web/handler/create_provider_credentials.go +++ b/backend/pkg/web/handler/provider_credentials.go @@ -29,3 +29,16 @@ func CreateProviderCredentials(c *gin.Context) { } c.JSON(http.StatusOK, gin.H{"success": true, "data": providerCred}) } + +func ListProviderCredentials(c *gin.Context) { + logger := c.MustGet("LOGGER").(*logrus.Entry) + databaseRepo := c.MustGet("REPOSITORY").(database.DatabaseRepository) + + providerCredentials, err := databaseRepo.GetProviderCredentials(c) + if err != nil { + logger.Errorln("An error occurred while storing provider credential", err) + c.JSON(http.StatusInternalServerError, gin.H{"success": false}) + return + } + c.JSON(http.StatusOK, gin.H{"success": true, "data": providerCredentials}) +} diff --git a/backend/pkg/web/server.go b/backend/pkg/web/server.go index c3c63387..954651e4 100644 --- a/backend/pkg/web/server.go +++ b/backend/pkg/web/server.go @@ -39,7 +39,8 @@ func (ae *AppEngine) Setup(logger *logrus.Entry) *gin.Engine { "success": true, }) }) - api.POST("/provider_credential", handler.CreateProviderCredentials) //used to save settings + api.POST("/provider_credential", handler.CreateProviderCredentials) + api.GET("/provider_credential", handler.ListProviderCredentials) } } diff --git a/frontend/src/app/models/fasten/provider-credential.ts b/frontend/src/app/models/fasten/provider-credential.ts index 8b4ca632..7c8cb43f 100644 --- a/frontend/src/app/models/fasten/provider-credential.ts +++ b/frontend/src/app/models/fasten/provider-credential.ts @@ -1,12 +1,13 @@ export class ProviderCredential { - id?: number + user_id?: number + provider_id: string + patient_id: string oauth_endpoint_base_url: string api_endpoint_base_url: string client_id: string redirect_uri: string scopes: string //space seperated string - patient: string access_token: string refresh_token: string id_token: string diff --git a/frontend/src/app/pages/medical-providers/medical-providers.component.ts b/frontend/src/app/pages/medical-providers/medical-providers.component.ts index aa5f44f7..4dcd37f9 100644 --- a/frontend/src/app/pages/medical-providers/medical-providers.component.ts +++ b/frontend/src/app/pages/medical-providers/medical-providers.component.ts @@ -29,10 +29,10 @@ export class MedicalProvidersComponent implements OnInit { ngOnInit(): void { } - connect($event: MouseEvent, provider: string) { + connect($event: MouseEvent, providerId: string) { ($event.currentTarget as HTMLButtonElement).disabled = true; - this.passportApi.getProviderConfig(provider) + this.passportApi.getProviderConfig(providerId) .subscribe(async (connectData: ProviderConfig) => { console.log(connectData); @@ -49,7 +49,7 @@ export class MedicalProvidersComponent implements OnInit { window.open(authorizationUrl.toString(), "_blank"); //wait for response - this.waitForClaimOrTimeout(provider, state).subscribe(async (claimData: AuthorizeClaim) => { + this.waitForClaimOrTimeout(providerId, state).subscribe(async (claimData: AuthorizeClaim) => { console.log("claim response:", claimData) @@ -91,12 +91,13 @@ export class MedicalProvidersComponent implements OnInit { //Create FHIR Client const providerCredential: ProviderCredential = { + provider_id: providerId, oauth_endpoint_base_url: connectData.oauth_endpoint_base_url, api_endpoint_base_url: connectData.api_endpoint_base_url, client_id: connectData.client_id, redirect_uri: connectData.redirect_uri, scopes: connectData.scopes.join(' '), - patient: payload.patient, + patient_id: payload.patient, access_token: payload.access_token, refresh_token: payload.refresh_token, id_token: payload.id_token,