2022-08-25 19:26:29 -06:00
|
|
|
package handler
|
|
|
|
|
|
|
|
import (
|
2022-09-15 23:26:37 -06:00
|
|
|
"fmt"
|
2022-09-13 19:02:26 -06:00
|
|
|
"github.com/fastenhealth/fastenhealth-onprem/backend/pkg"
|
2022-08-25 19:26:29 -06:00
|
|
|
"github.com/fastenhealth/fastenhealth-onprem/backend/pkg/database"
|
2022-09-08 22:14:03 -06:00
|
|
|
"github.com/fastenhealth/fastenhealth-onprem/backend/pkg/hub"
|
2022-08-25 19:26:29 -06:00
|
|
|
"github.com/fastenhealth/fastenhealth-onprem/backend/pkg/models"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"github.com/sirupsen/logrus"
|
2022-09-15 23:26:37 -06:00
|
|
|
"io/ioutil"
|
2022-08-25 19:26:29 -06:00
|
|
|
"net/http"
|
2022-09-21 23:02:55 -06:00
|
|
|
"net/url"
|
2022-09-11 21:59:13 -06:00
|
|
|
"strings"
|
2022-08-25 19:26:29 -06:00
|
|
|
)
|
|
|
|
|
2022-08-30 20:03:24 -06:00
|
|
|
func CreateSource(c *gin.Context) {
|
2022-08-25 19:26:29 -06:00
|
|
|
logger := c.MustGet("LOGGER").(*logrus.Entry)
|
|
|
|
databaseRepo := c.MustGet("REPOSITORY").(database.DatabaseRepository)
|
|
|
|
|
2022-09-13 19:02:26 -06:00
|
|
|
sourceCred := models.Source{}
|
|
|
|
if err := c.ShouldBindJSON(&sourceCred); err != nil {
|
|
|
|
logger.Errorln("An error occurred while parsing posted source credential", err)
|
2022-08-25 19:26:29 -06:00
|
|
|
c.JSON(http.StatusBadRequest, gin.H{"success": false})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-09-13 19:02:26 -06:00
|
|
|
logger.Infof("Parsed Create Source Credentials Payload: %v", sourceCred)
|
2022-08-25 22:57:29 -06:00
|
|
|
|
2022-09-13 19:02:26 -06:00
|
|
|
err := databaseRepo.CreateSource(c, &sourceCred)
|
2022-08-25 19:26:29 -06:00
|
|
|
if err != nil {
|
2022-09-13 19:02:26 -06:00
|
|
|
logger.Errorln("An error occurred while storing source credential", err)
|
2022-08-25 19:26:29 -06:00
|
|
|
c.JSON(http.StatusInternalServerError, gin.H{"success": false})
|
|
|
|
return
|
|
|
|
}
|
2022-09-08 22:14:03 -06:00
|
|
|
|
|
|
|
// after creating the source, we should do a bulk import
|
2022-09-23 23:42:01 -06:00
|
|
|
err = syncSourceResources(c, logger, databaseRepo, sourceCred)
|
2022-09-08 22:14:03 -06:00
|
|
|
if err != nil {
|
|
|
|
c.JSON(http.StatusInternalServerError, gin.H{"success": false})
|
|
|
|
return
|
|
|
|
}
|
2022-09-09 00:51:46 -06:00
|
|
|
|
2022-09-23 23:42:01 -06:00
|
|
|
c.JSON(http.StatusOK, gin.H{"success": true, "data": sourceCred})
|
|
|
|
}
|
|
|
|
|
|
|
|
func SourceSync(c *gin.Context) {
|
|
|
|
logger := c.MustGet("LOGGER").(*logrus.Entry)
|
|
|
|
databaseRepo := c.MustGet("REPOSITORY").(database.DatabaseRepository)
|
|
|
|
|
|
|
|
logger.Infof("Get Source Credentials: %v", c.Param("sourceId"))
|
|
|
|
|
|
|
|
sourceCred, err := databaseRepo.GetSource(c, c.Param("sourceId"))
|
2022-09-08 22:14:03 -06:00
|
|
|
if err != nil {
|
2022-09-23 23:42:01 -06:00
|
|
|
logger.Errorln("An error occurred while retrieving source credential", err)
|
2022-09-08 22:14:03 -06:00
|
|
|
c.JSON(http.StatusInternalServerError, gin.H{"success": false})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-09-23 23:42:01 -06:00
|
|
|
// after creating the source, we should do a bulk import
|
|
|
|
err = syncSourceResources(c, logger, databaseRepo, *sourceCred)
|
|
|
|
if err != nil {
|
|
|
|
c.JSON(http.StatusInternalServerError, gin.H{"success": false})
|
|
|
|
return
|
|
|
|
}
|
2022-09-13 19:02:26 -06:00
|
|
|
c.JSON(http.StatusOK, gin.H{"success": true, "data": sourceCred})
|
2022-08-25 19:26:29 -06:00
|
|
|
}
|
2022-08-27 20:34:48 -06:00
|
|
|
|
2022-09-15 23:26:37 -06:00
|
|
|
func CreateManualSource(c *gin.Context) {
|
|
|
|
logger := c.MustGet("LOGGER").(*logrus.Entry)
|
|
|
|
databaseRepo := c.MustGet("REPOSITORY").(database.DatabaseRepository)
|
|
|
|
|
|
|
|
// single file
|
|
|
|
file, err := c.FormFile("file")
|
|
|
|
if err != nil {
|
|
|
|
c.JSON(http.StatusInternalServerError, gin.H{"success": false, "error": "could not extract file from form"})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
fmt.Printf("Uploaded filename: %s", file.Filename)
|
|
|
|
|
|
|
|
// create a temporary file to store this uploaded file
|
|
|
|
bundleFile, err := ioutil.TempFile("", file.Filename)
|
|
|
|
if err != nil {
|
|
|
|
c.JSON(http.StatusInternalServerError, gin.H{"success": false, "error": "could not create temp file"})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Upload the file to specific bundleFile.
|
|
|
|
err = c.SaveUploadedFile(file, bundleFile.Name())
|
|
|
|
if err != nil {
|
|
|
|
c.JSON(http.StatusInternalServerError, gin.H{"success": false, "error": "could not save temp file"})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// We cannot save the "Source" object yet, as we do not know the patientID
|
|
|
|
|
|
|
|
// create a "manual" client, which we can use to parse the
|
|
|
|
manualSourceClient, _, err := hub.NewClient(pkg.SourceTypeManual, c, nil, logger, models.Source{})
|
|
|
|
if err != nil {
|
|
|
|
logger.Errorln("An error occurred while initializing hub client using manual source without credentials", err)
|
|
|
|
c.JSON(http.StatusInternalServerError, gin.H{"success": false})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err = manualSourceClient.SyncAllBundle(databaseRepo, bundleFile)
|
|
|
|
if err != nil {
|
|
|
|
logger.Errorln("An error occurred while processing bundle", err)
|
|
|
|
c.JSON(http.StatusInternalServerError, gin.H{"success": false, "error": err.Error()})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
c.JSON(http.StatusOK, gin.H{"success": true, "data": fmt.Sprintf("'%s' uploaded!", file.Filename)})
|
|
|
|
}
|
|
|
|
|
2022-09-14 20:59:16 -06:00
|
|
|
func GetSource(c *gin.Context) {
|
|
|
|
logger := c.MustGet("LOGGER").(*logrus.Entry)
|
|
|
|
databaseRepo := c.MustGet("REPOSITORY").(database.DatabaseRepository)
|
|
|
|
|
|
|
|
sourceCred, err := databaseRepo.GetSource(c, c.Param("sourceId"))
|
|
|
|
if err != nil {
|
|
|
|
logger.Errorln("An error occurred while retrieving source credential", err)
|
|
|
|
c.JSON(http.StatusInternalServerError, gin.H{"success": false})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
c.JSON(http.StatusOK, gin.H{"success": true, "data": sourceCred})
|
|
|
|
}
|
|
|
|
|
2022-09-17 01:12:12 -06:00
|
|
|
func GetSourceSummary(c *gin.Context) {
|
|
|
|
logger := c.MustGet("LOGGER").(*logrus.Entry)
|
|
|
|
databaseRepo := c.MustGet("REPOSITORY").(database.DatabaseRepository)
|
|
|
|
|
|
|
|
sourceSummary, err := databaseRepo.GetSourceSummary(c, c.Param("sourceId"))
|
|
|
|
if err != nil {
|
|
|
|
logger.Errorln("An error occurred while retrieving source summary", err)
|
|
|
|
c.JSON(http.StatusInternalServerError, gin.H{"success": false})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
c.JSON(http.StatusOK, gin.H{"success": true, "data": sourceSummary})
|
|
|
|
}
|
|
|
|
|
2022-08-30 20:03:24 -06:00
|
|
|
func ListSource(c *gin.Context) {
|
2022-08-27 20:34:48 -06:00
|
|
|
logger := c.MustGet("LOGGER").(*logrus.Entry)
|
|
|
|
databaseRepo := c.MustGet("REPOSITORY").(database.DatabaseRepository)
|
|
|
|
|
2022-09-13 19:02:26 -06:00
|
|
|
sourceCreds, err := databaseRepo.GetSources(c)
|
2022-08-27 20:34:48 -06:00
|
|
|
if err != nil {
|
2022-09-14 20:59:16 -06:00
|
|
|
logger.Errorln("An error occurred while listing source credentials", err)
|
2022-08-27 20:34:48 -06:00
|
|
|
c.JSON(http.StatusInternalServerError, gin.H{"success": false})
|
|
|
|
return
|
|
|
|
}
|
2022-09-13 19:02:26 -06:00
|
|
|
c.JSON(http.StatusOK, gin.H{"success": true, "data": sourceCreds})
|
2022-08-27 20:34:48 -06:00
|
|
|
}
|
2022-09-08 23:53:54 -06:00
|
|
|
|
|
|
|
func RawRequestSource(c *gin.Context) {
|
|
|
|
logger := c.MustGet("LOGGER").(*logrus.Entry)
|
|
|
|
databaseRepo := c.MustGet("REPOSITORY").(database.DatabaseRepository)
|
|
|
|
|
2022-09-18 01:36:53 -06:00
|
|
|
//!!!!!!INSECURE!!!!!!S
|
|
|
|
//We're setting the username to a user provided value, this is insecure, but required for calling databaseRepo fns
|
|
|
|
c.Set("AUTH_USERNAME", c.Param("username"))
|
|
|
|
|
|
|
|
foundSource, err := databaseRepo.GetSource(c, c.Param("sourceId"))
|
2022-09-08 23:53:54 -06:00
|
|
|
if err != nil {
|
2022-09-21 19:57:38 -06:00
|
|
|
logger.Errorf("An error occurred while finding source credential: %v", err)
|
2022-09-18 01:36:53 -06:00
|
|
|
c.JSON(http.StatusInternalServerError, gin.H{"success": false, "error": err.Error()})
|
2022-09-08 23:53:54 -06:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if foundSource == nil {
|
|
|
|
logger.Errorf("Did not source credentials for %s", c.Param("sourceType"))
|
2022-09-18 01:36:53 -06:00
|
|
|
c.JSON(http.StatusNotFound, gin.H{"success": false, "error": err.Error()})
|
2022-09-08 23:53:54 -06:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-09-18 01:36:53 -06:00
|
|
|
client, updatedSource, err := hub.NewClient(foundSource.SourceType, c, nil, logger, *foundSource)
|
2022-09-08 23:53:54 -06:00
|
|
|
if err != nil {
|
2022-09-21 19:57:38 -06:00
|
|
|
logger.Errorf("Could not initialize source client %v", err)
|
2022-09-18 01:36:53 -06:00
|
|
|
c.JSON(http.StatusInternalServerError, gin.H{"success": false, "error": err.Error()})
|
2022-09-08 23:53:54 -06:00
|
|
|
return
|
|
|
|
}
|
2022-09-09 00:51:46 -06:00
|
|
|
if updatedSource != nil {
|
|
|
|
err := databaseRepo.CreateSource(c, updatedSource)
|
|
|
|
if err != nil {
|
2022-09-21 19:57:38 -06:00
|
|
|
logger.Errorf("An error occurred while updating source credential %v", err)
|
2022-09-18 01:36:53 -06:00
|
|
|
c.JSON(http.StatusInternalServerError, gin.H{"success": false, "error": err.Error()})
|
2022-09-09 00:51:46 -06:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-08 23:53:54 -06:00
|
|
|
var resp map[string]interface{}
|
2022-09-21 23:02:55 -06:00
|
|
|
|
|
|
|
parsedUrl, err := url.Parse(strings.TrimSuffix(c.Param("path"), "/"))
|
|
|
|
if err != nil {
|
|
|
|
logger.Errorf("Error parsing request, %v", err)
|
|
|
|
c.JSON(http.StatusInternalServerError, gin.H{"success": false, "error": err.Error()})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
//make sure we include all query string parameters with the raw request.
|
|
|
|
parsedUrl.RawQuery = c.Request.URL.Query().Encode()
|
|
|
|
|
|
|
|
err = client.GetRequest(parsedUrl.String(), &resp)
|
2022-09-08 23:53:54 -06:00
|
|
|
if err != nil {
|
2022-09-21 19:57:38 -06:00
|
|
|
logger.Errorf("Error making raw request, %v", err)
|
2022-09-18 01:36:53 -06:00
|
|
|
c.JSON(http.StatusInternalServerError, gin.H{"success": false, "error": err.Error()})
|
2022-09-08 23:53:54 -06:00
|
|
|
return
|
|
|
|
}
|
|
|
|
c.JSON(http.StatusOK, gin.H{"success": true, "data": resp})
|
|
|
|
}
|
2022-09-23 23:42:01 -06:00
|
|
|
|
|
|
|
////// private functions
|
|
|
|
func syncSourceResources(c *gin.Context, logger *logrus.Entry, databaseRepo database.DatabaseRepository, sourceCred models.Source) error {
|
|
|
|
// after creating the source, we should do a bulk import
|
|
|
|
sourceClient, updatedSource, err := hub.NewClient(sourceCred.SourceType, c, nil, logger, sourceCred)
|
|
|
|
if err != nil {
|
|
|
|
logger.Errorln("An error occurred while initializing hub client using source credential", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if updatedSource != nil {
|
|
|
|
err := databaseRepo.CreateSource(c, updatedSource)
|
|
|
|
if err != nil {
|
|
|
|
logger.Errorln("An error occurred while updating source credential", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
err = sourceClient.SyncAll(databaseRepo)
|
|
|
|
if err != nil {
|
|
|
|
logger.Errorln("An error occurred while bulk import of resources from source", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|