always update source credential when doing unsafe raw request.

This commit is contained in:
Jason Kulatunga 2023-10-12 22:06:35 -07:00
parent 0ddfe68636
commit 13e21b580b
No known key found for this signature in database
1 changed files with 17 additions and 16 deletions

View File

@ -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"
@ -65,6 +64,23 @@ 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)
@ -72,21 +88,6 @@ func UnsafeRequestSource(c *gin.Context) {
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})
}