From 13e21b580b0bfad960ad095dc012a010a43414f9 Mon Sep 17 00:00:00 2001 From: Jason Kulatunga Date: Thu, 12 Oct 2023 22:06:35 -0700 Subject: [PATCH] always update source credential when doing unsafe raw request. --- backend/pkg/web/handler/unsafe.go | 33 ++++++++++++++++--------------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/backend/pkg/web/handler/unsafe.go b/backend/pkg/web/handler/unsafe.go index 84db3f64..5b6de167 100644 --- a/backend/pkg/web/handler/unsafe.go +++ b/backend/pkg/web/handler/unsafe.go @@ -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}) }