Skip to content

Commit

Permalink
always update source credential when doing unsafe raw request.
Browse files Browse the repository at this point in the history
  • Loading branch information
AnalogJ committed Oct 13, 2023
1 parent 0ddfe68 commit 13e21b5
Showing 1 changed file with 17 additions and 16 deletions.
33 changes: 17 additions & 16 deletions backend/pkg/web/handler/unsafe.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package handler

import (
"fmt"
"github.com/fastenhealth/fasten-onprem/backend/pkg"
"github.com/fastenhealth/fasten-onprem/backend/pkg/config"
"github.com/fastenhealth/fasten-onprem/backend/pkg/database"
Expand Down Expand Up @@ -65,28 +64,30 @@ func UnsafeRequestSource(c *gin.Context) {
//make sure we include all query string parameters with the raw request.
parsedUrl.RawQuery = c.Request.URL.Query().Encode()

//make sure we store the source credential information in the database, even if the request fails
defer func() {
//update source incase the access token/refresh token has been updated
sourceCredential := client.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
}
err = databaseRepo.UpdateSource(c, sourceCredentialConcrete)
if err != nil {
logger.Errorf("An error occurred while updating source credential: %v", err)
return
}
logger.Info("Successfully updated source credential")
}()

_, err = client.GetRequest(parsedUrl.String(), &resp)
if err != nil {
logger.Errorf("Error making raw request, %v", err)
c.JSON(http.StatusInternalServerError, gin.H{"success": false, "error": err.Error(), "data": resp})
return
}

//update source incase the access token/refresh token has been updated
sourceCredential := client.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")
c.JSON(http.StatusOK, gin.H{"success": false, "data": resp, "error": fmt.Errorf("An error occurred while updating source credential, source credential is not of type *models.SourceCredential")})
return
}
err = databaseRepo.UpdateSource(c, sourceCredentialConcrete)
if err != nil {
logger.Errorf("An error occurred while updating source credential: %v", err)
c.JSON(http.StatusOK, gin.H{"success": false, "data": resp, "error": fmt.Errorf("An error occurred while updating source credential: %v", err)})
return
}

c.JSON(http.StatusOK, gin.H{"success": true, "data": resp})
}

Expand Down

0 comments on commit 13e21b5

Please sign in to comment.