make sure that we print the error response body if we can.

Added an update source method.
After sync, if sourcecredential access token/refresh token has been updated make sure we store it int he database.
This commit is contained in:
Jason Kulatunga 2023-07-20 21:43:37 -07:00
parent a5b37159c1
commit f1b7e80e13
No known key found for this signature in database
6 changed files with 56 additions and 16 deletions

View File

@ -36,6 +36,7 @@ type DatabaseRepository interface {
GetSource(context.Context, string) (*models.SourceCredential, error)
GetSourceSummary(context.Context, string) (*models.SourceSummary, error)
GetSources(context.Context) ([]models.SourceCredential, error)
UpdateSource(ctx context.Context, sourceCreds *models.SourceCredential) error
CreateGlossaryEntry(ctx context.Context, glossaryEntry *models.Glossary) error
GetGlossaryEntry(ctx context.Context, code string, codeSystem string) (*models.Glossary, error)

View File

@ -359,6 +359,20 @@ func (mr *MockDatabaseRepositoryMockRecorder) RemoveResourceAssociation(ctx, sou
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "RemoveResourceAssociation", reflect.TypeOf((*MockDatabaseRepository)(nil).RemoveResourceAssociation), ctx, source, resourceType, resourceId, relatedSource, relatedResourceType, relatedResourceId)
}
// UpdateSource mocks base method.
func (m *MockDatabaseRepository) UpdateSource(ctx context.Context, sourceCreds *models0.SourceCredential) error {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "UpdateSource", ctx, sourceCreds)
ret0, _ := ret[0].(error)
return ret0
}
// UpdateSource indicates an expected call of UpdateSource.
func (mr *MockDatabaseRepositoryMockRecorder) UpdateSource(ctx, sourceCreds interface{}) *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "UpdateSource", reflect.TypeOf((*MockDatabaseRepository)(nil).UpdateSource), ctx, sourceCreds)
}
// UpsertRawResource mocks base method.
func (m *MockDatabaseRepository) UpsertRawResource(ctx context.Context, sourceCredentials models.SourceCredential, rawResource models.RawResourceFhir) (bool, error) {
m.ctrl.T.Helper()

View File

@ -775,6 +775,29 @@ func (sr *SqliteRepository) CreateSource(ctx context.Context, sourceCreds *model
Assign(*sourceCreds).FirstOrCreate(sourceCreds).Error
}
func (sr *SqliteRepository) UpdateSource(ctx context.Context, sourceCreds *models.SourceCredential) error {
currentUser, currentUserErr := sr.GetCurrentUser(ctx)
if currentUserErr != nil {
return currentUserErr
}
sourceCreds.UserID = currentUser.ID
//Assign will **always** update the source credential in the DB with data passed into this function.
return sr.GormClient.WithContext(ctx).
Where(models.SourceCredential{
ModelBase: models.ModelBase{ID: sourceCreds.ID},
UserID: sourceCreds.UserID,
SourceType: sourceCreds.SourceType,
}).Updates(models.SourceCredential{
AccessToken: sourceCreds.AccessToken,
RefreshToken: sourceCreds.RefreshToken,
ExpiresAt: sourceCreds.ExpiresAt,
DynamicClientId: sourceCreds.DynamicClientId,
DynamicClientRegistrationMode: sourceCreds.DynamicClientRegistrationMode,
DynamicClientJWKS: sourceCreds.DynamicClientJWKS,
}).Error
}
func (sr *SqliteRepository) GetSource(ctx context.Context, sourceId string) (*models.SourceCredential, error) {
currentUser, currentUserErr := sr.GetCurrentUser(ctx)
if currentUserErr != nil {

View File

@ -23,9 +23,6 @@ func TestJWKSerialize(t *testing.T) {
require.NotEmpty(t, keypair.KeyID())
require.ElementsMatch(t, []string{"d", "dp", "dq", "e", "kty", "n", "p", "q", "qi", "kid"}, keys)
require.Equal(t, "RSA", dict["kty"])
//require.Equal(t, map[string]string{}, dict)
require.Equal(t, string(keypair.N()), dict["n"])
}
func TestJWKDeserialize(t *testing.T) {

View File

@ -185,8 +185,8 @@ func (s *SourceCredential) RefreshDynamicClientAccessToken() error {
if tokenResp.StatusCode >= 300 || tokenResp.StatusCode < 200 {
b, err := io.ReadAll(tokenResp.Body)
if err != nil {
log.Printf("Response body: %s", string(b))
if err == nil {
log.Printf("Error Response body: %s", string(b))
}
return fmt.Errorf("an error occurred while reading dynamic client token response, status code was not 200: %d", tokenResp.StatusCode)

View File

@ -105,8 +105,8 @@ func CreateSource(c *gin.Context) {
if registrationResponse.StatusCode >= 300 || registrationResponse.StatusCode < 200 {
logger.Errorln("An error occurred while reading dynamic client registration response, status code was not 200", registrationResponse.StatusCode)
b, err := io.ReadAll(registrationResponse.Body)
if err != nil {
logger.Printf("Response body: %s", string(b))
if err == nil {
logger.Printf("Error Response body: %s", string(b))
}
c.JSON(http.StatusBadRequest, gin.H{"success": false})
@ -291,20 +291,25 @@ func SyncSourceResources(c context.Context, logger *logrus.Entry, databaseRepo d
logger.Errorln("An error occurred while initializing hub client using source credential", err)
return sourceModels.UpsertSummary{}, err
}
//TODO: update source
//if updatedSource != nil {
// logger.Warnf("TODO: source credential has been updated, we should store it in the database: %v", updatedSource)
// //err := databaseRepo.CreateSource(c, updatedSource)
// //if err != nil {
// // logger.Errorln("An error occurred while updating source credential", err)
// // return err
// //}
//}
summary, err := sourceClient.SyncAll(databaseRepo)
if err != nil {
logger.Errorln("An error occurred while bulk import of resources from source", err)
return summary, err
}
//update source incase the access token/refresh token has been updated
sourceCredential := sourceClient.GetSourceCredential()
sourceCredentialConcrete, ok := sourceCredential.(*models.SourceCredential)
if !ok {
logger.Errorln("An error occurred while updating source credential, source credential is not of type *models.SourceCredential")
return summary, fmt.Errorf("source credential is not of type *models.SourceCredential")
}
err = databaseRepo.UpdateSource(c, sourceCredentialConcrete)
if err != nil {
logger.Errorf("An error occurred while updating source credential: %v", err)
return summary, err
}
return summary, nil
}