working. list provider credentials.

This commit is contained in:
Jason Kulatunga 2022-08-27 19:34:48 -07:00
parent cf24362806
commit 93f04802fe
7 changed files with 45 additions and 10 deletions

View File

@ -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)
}

View File

@ -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
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

View File

@ -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"`

View File

@ -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})
}

View File

@ -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)
}
}

View File

@ -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

View File

@ -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,