2022-12-02 20:40:58 -07:00
|
|
|
package handler
|
|
|
|
|
|
|
|
import (
|
2023-08-27 18:09:46 -06:00
|
|
|
"github.com/fastenhealth/fasten-onprem/backend/pkg"
|
|
|
|
"github.com/fastenhealth/fasten-onprem/backend/pkg/database"
|
|
|
|
"github.com/fastenhealth/fasten-onprem/backend/pkg/models"
|
|
|
|
"github.com/fastenhealth/fasten-onprem/backend/pkg/utils"
|
2022-12-02 20:40:58 -07:00
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
"net/http"
|
2023-08-11 23:44:34 -06:00
|
|
|
"strconv"
|
2022-12-02 20:40:58 -07:00
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2023-07-08 08:43:30 -06:00
|
|
|
func QueryResourceFhir(c *gin.Context) {
|
|
|
|
logger := c.MustGet(pkg.ContextKeyTypeLogger).(*logrus.Entry)
|
|
|
|
databaseRepo := c.MustGet(pkg.ContextKeyTypeDatabase).(database.DatabaseRepository)
|
|
|
|
|
|
|
|
var query models.QueryResource
|
|
|
|
if err := c.ShouldBindJSON(&query); err != nil {
|
|
|
|
logger.Errorln("An error occurred while parsing queries", err)
|
|
|
|
c.JSON(http.StatusBadRequest, gin.H{"success": false})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
queryResults, err := databaseRepo.QueryResources(c, query)
|
|
|
|
if err != nil {
|
|
|
|
logger.Errorln("An error occurred while querying resources", err)
|
|
|
|
c.JSON(http.StatusInternalServerError, gin.H{"success": false})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
//sort by date
|
|
|
|
//queryResults = utils.SortResourceListByDate(queryResults)
|
|
|
|
|
|
|
|
c.JSON(http.StatusOK, gin.H{"success": true, "data": queryResults})
|
|
|
|
}
|
|
|
|
|
2022-12-02 20:40:58 -07:00
|
|
|
func ListResourceFhir(c *gin.Context) {
|
2023-01-10 20:23:47 -07:00
|
|
|
logger := c.MustGet(pkg.ContextKeyTypeLogger).(*logrus.Entry)
|
|
|
|
databaseRepo := c.MustGet(pkg.ContextKeyTypeDatabase).(database.DatabaseRepository)
|
2022-12-02 20:40:58 -07:00
|
|
|
|
|
|
|
listResourceQueryOptions := models.ListResourceQueryOptions{}
|
|
|
|
if len(c.Query("sourceResourceType")) > 0 {
|
|
|
|
listResourceQueryOptions.SourceResourceType = c.Query("sourceResourceType")
|
|
|
|
}
|
|
|
|
if len(c.Query("sourceID")) > 0 {
|
|
|
|
listResourceQueryOptions.SourceID = c.Query("sourceID")
|
|
|
|
}
|
2022-12-17 16:10:19 -07:00
|
|
|
if len(c.Query("sourceResourceID")) > 0 {
|
|
|
|
listResourceQueryOptions.SourceResourceID = c.Query("sourceResourceID")
|
|
|
|
}
|
2023-08-11 23:44:34 -06:00
|
|
|
if len(c.Query("page")) > 0 {
|
|
|
|
listResourceQueryOptions.Limit = pkg.ResourceListPageSize //hardcoded number of resources per page
|
|
|
|
pageNumb, err := strconv.Atoi(c.Query("page"))
|
|
|
|
if err != nil {
|
|
|
|
logger.Errorln("An error occurred while calculating page number", err)
|
|
|
|
c.JSON(http.StatusInternalServerError, gin.H{"success": false})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
listResourceQueryOptions.Offset = pageNumb * listResourceQueryOptions.Limit
|
|
|
|
}
|
2022-12-02 20:40:58 -07:00
|
|
|
wrappedResourceModels, err := databaseRepo.ListResources(c, listResourceQueryOptions)
|
|
|
|
|
2023-01-10 20:23:47 -07:00
|
|
|
if c.Query("sortBy") == "title" {
|
|
|
|
wrappedResourceModels = utils.SortResourceListByTitle(wrappedResourceModels)
|
|
|
|
} else {
|
|
|
|
wrappedResourceModels = utils.SortResourceListByDate(wrappedResourceModels)
|
|
|
|
}
|
|
|
|
|
2022-12-02 20:40:58 -07:00
|
|
|
if err != nil {
|
|
|
|
logger.Errorln("An error occurred while retrieving resources", err)
|
|
|
|
c.JSON(http.StatusInternalServerError, gin.H{"success": false})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
c.JSON(http.StatusOK, gin.H{"success": true, "data": wrappedResourceModels})
|
|
|
|
}
|
|
|
|
|
|
|
|
//this endpoint retrieves a specific resource by its ID
|
|
|
|
func GetResourceFhir(c *gin.Context) {
|
2023-01-10 20:23:47 -07:00
|
|
|
logger := c.MustGet(pkg.ContextKeyTypeLogger).(*logrus.Entry)
|
|
|
|
databaseRepo := c.MustGet(pkg.ContextKeyTypeDatabase).(database.DatabaseRepository)
|
2022-12-02 20:40:58 -07:00
|
|
|
|
|
|
|
resourceId := strings.Trim(c.Param("resourceId"), "/")
|
|
|
|
sourceId := strings.Trim(c.Param("sourceId"), "/")
|
|
|
|
wrappedResourceModel, err := databaseRepo.GetResourceBySourceId(c, sourceId, resourceId)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
logger.Errorln("An error occurred while retrieving resource", err)
|
|
|
|
c.JSON(http.StatusInternalServerError, gin.H{"success": false})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
c.JSON(http.StatusOK, gin.H{"success": true, "data": wrappedResourceModel})
|
|
|
|
}
|
2022-12-17 16:10:19 -07:00
|
|
|
|
2023-01-10 20:23:47 -07:00
|
|
|
func CreateResourceComposition(c *gin.Context) {
|
2022-12-17 16:10:19 -07:00
|
|
|
|
2023-01-10 20:23:47 -07:00
|
|
|
logger := c.MustGet(pkg.ContextKeyTypeLogger).(*logrus.Entry)
|
|
|
|
databaseRepo := c.MustGet(pkg.ContextKeyTypeDatabase).(database.DatabaseRepository)
|
2022-12-17 16:10:19 -07:00
|
|
|
|
2023-01-10 20:23:47 -07:00
|
|
|
type jsonPayload struct {
|
2023-07-08 08:43:30 -06:00
|
|
|
Resources []*models.ResourceBase `json:"resources"`
|
2023-01-10 20:23:47 -07:00
|
|
|
Title string `json:"title"`
|
2022-12-17 16:10:19 -07:00
|
|
|
}
|
2023-01-10 20:23:47 -07:00
|
|
|
var payload jsonPayload
|
|
|
|
if err := c.ShouldBindJSON(&payload); err != nil {
|
|
|
|
logger.Errorln("An error occurred while parsing posted resources & title", err)
|
|
|
|
c.JSON(http.StatusBadRequest, gin.H{"success": false})
|
2022-12-17 16:10:19 -07:00
|
|
|
return
|
|
|
|
}
|
2023-01-10 20:23:47 -07:00
|
|
|
err := databaseRepo.AddResourceComposition(c, payload.Title, payload.Resources)
|
2022-12-17 16:10:19 -07:00
|
|
|
if err != nil {
|
2023-01-10 20:23:47 -07:00
|
|
|
logger.Errorln("An error occurred while creating resource group (composition)", err)
|
2022-12-17 16:10:19 -07:00
|
|
|
c.JSON(http.StatusInternalServerError, gin.H{"success": false})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
c.JSON(http.StatusOK, gin.H{"success": true})
|
|
|
|
}
|
2022-12-21 20:51:02 -07:00
|
|
|
|
|
|
|
// GetResourceFhirGraph
|
|
|
|
// Retrieve a list of all fhir resources (vertex), and a list of all associations (edge)
|
|
|
|
// Generate a graph
|
|
|
|
// find the PredecessorMap
|
|
|
|
// - filter to only vertices that are "Condition" or "Encounter" and are "root" nodes (have no edges directed to this node)
|
|
|
|
func GetResourceFhirGraph(c *gin.Context) {
|
2023-01-10 20:23:47 -07:00
|
|
|
logger := c.MustGet(pkg.ContextKeyTypeLogger).(*logrus.Entry)
|
|
|
|
databaseRepo := c.MustGet(pkg.ContextKeyTypeDatabase).(database.DatabaseRepository)
|
2022-12-21 20:51:02 -07:00
|
|
|
|
2023-04-22 23:08:58 -06:00
|
|
|
graphType := strings.Trim(c.Param("graphType"), "/")
|
|
|
|
|
2023-08-12 23:30:53 -06:00
|
|
|
graphOptions := models.ResourceGraphOptions{}
|
|
|
|
if len(c.Query("page")) > 0 {
|
|
|
|
pageNumb, err := strconv.Atoi(c.Query("page"))
|
|
|
|
if err != nil {
|
|
|
|
logger.Errorln("An error occurred while calculating page number", err)
|
|
|
|
c.JSON(http.StatusInternalServerError, gin.H{"success": false})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
graphOptions.Page = pageNumb
|
|
|
|
}
|
|
|
|
|
|
|
|
resourceListDictionary, err := databaseRepo.GetFlattenedResourceGraph(c, pkg.ResourceGraphType(graphType), graphOptions)
|
2022-12-21 20:51:02 -07:00
|
|
|
if err != nil {
|
|
|
|
logger.Errorln("An error occurred while retrieving list of resources", err)
|
|
|
|
c.JSON(http.StatusInternalServerError, gin.H{"success": false})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-04-22 23:08:58 -06:00
|
|
|
c.JSON(http.StatusOK, gin.H{"success": true, "data": resourceListDictionary})
|
2022-12-21 20:51:02 -07:00
|
|
|
}
|