261 lines
7.3 KiB
Go
261 lines
7.3 KiB
Go
package integration
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"net/http"
|
|
"os"
|
|
"satusehat-rssa/internal/constant"
|
|
"satusehat-rssa/internal/model"
|
|
"satusehat-rssa/pkg/common"
|
|
"satusehat-rssa/pkg/httputil"
|
|
)
|
|
|
|
type MedicationRequestInterface interface {
|
|
// Define methods for MedicationRequestInterface
|
|
CreateMedicationRequest(req model.MedicationRequestRequest) (map[string]interface{}, error)
|
|
UpdateMedicationRequest(req model.MedicationRequestRequest) (map[string]interface{}, error)
|
|
GetMedicationRequestByPatient(id string, category string) (map[string]interface{}, error)
|
|
HandleCheckMedicationRequest(id string, category string) ([]string, bool, error)
|
|
}
|
|
|
|
type MedicationRequestRepository struct {
|
|
// Define fields for MedicationRequestRepository
|
|
akses *model.Akses
|
|
}
|
|
|
|
func NewMedicationRequestRepo(akses *model.Akses) MedicationRequestInterface {
|
|
return &MedicationRequestRepository{
|
|
akses: akses,
|
|
}
|
|
}
|
|
|
|
// GetMedicationRequestByPatient implements MedicationRequestInterface.
|
|
func (c *MedicationRequestRepository) GetMedicationRequestByPatient(id string, category string) (map[string]interface{}, error) {
|
|
var data map[string]interface{}
|
|
|
|
url := c.akses.BaseUrl + "/MedicationRequest?subject=" + id
|
|
method := "GET"
|
|
|
|
client := &http.Client{}
|
|
request, err := http.NewRequest(method, url, nil)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
oauth := model.OauthRequest{
|
|
ClientId: c.akses.ClientId,
|
|
ClientSecret: c.akses.ClientSecret,
|
|
}
|
|
token, err := NewOauthRequestRepo(c.akses).GenerateToken(oauth)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if token == nil {
|
|
return nil, errors.New(constant.ErrGenerateToken)
|
|
}
|
|
request.Header.Add("Authorization", "Bearer "+token.AccessToken)
|
|
request.Header.Set("Content-Type", constant.ContentTypeFHIRJSON)
|
|
request.Header.Set("Accept", constant.ContentTypeFHIRJSON)
|
|
res, err := client.Do(request)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer res.Body.Close()
|
|
|
|
err = json.NewDecoder(res.Body).Decode(&data)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return data, nil
|
|
}
|
|
|
|
// HandleCheckMedicationRequest implements MedicationRequestInterface.
|
|
func (c *MedicationRequestRepository) HandleCheckMedicationRequest(id string, category string) ([]string, bool, error) {
|
|
medicationReq, err := c.GetMedicationRequestByPatient(id, category)
|
|
if err != nil {
|
|
return nil, false, err
|
|
}
|
|
var ids []string
|
|
if entries, ok := medicationReq["entry"].([]interface{}); ok && len(entries) != 0 {
|
|
if entries, ok := (medicationReq)["entry"].([]interface{}); ok && len(entries) > 0 {
|
|
if entryMap, ok := entries[0].(map[string]interface{}); ok {
|
|
if resource, ok := entryMap["resource"].(map[string]interface{}); ok {
|
|
if id, ok := resource["id"].(string); ok {
|
|
//fmt.Println("resource.id:", id)
|
|
ids = append(ids, id)
|
|
return ids, true, nil
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return nil, true, nil
|
|
}
|
|
|
|
return nil, false, nil
|
|
}
|
|
|
|
// CreateMedicationRequest implements MedicationRequestInterface.
|
|
func (c *MedicationRequestRepository) CreateMedicationRequest(req model.MedicationRequestRequest) (map[string]interface{}, error) {
|
|
req.ResourceType = constant.MedicationRequestResourceType
|
|
req.Identifier = append(req.Identifier, common.GetIdentifier("prescription"))
|
|
identifierItem := common.GetIdentifier("prescription-item")
|
|
identifierItem.Value += "-1"
|
|
req.Identifier = append(req.Identifier, identifierItem)
|
|
|
|
patient, err := c.setupPatient(&req)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
err = c.setupEncounter(&req, patient)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
err = c.setupMedication(&req)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
req.DispenseRequest.Performer.Reference = "Organization/" + os.Getenv("ORGANIZATION_ID")
|
|
|
|
req.MedicationRequest = nil
|
|
|
|
// err = c.setupPractitioner(&req)
|
|
// if err != nil {
|
|
// return nil, err
|
|
// }
|
|
|
|
oauth := model.OauthRequest{
|
|
ClientId: c.akses.ClientId,
|
|
ClientSecret: c.akses.ClientSecret,
|
|
}
|
|
token, err := NewOauthRequestRepo(c.akses).GenerateToken(oauth)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if token == nil {
|
|
return nil, errors.New(constant.ErrGenerateToken)
|
|
}
|
|
|
|
url := c.akses.BaseUrl + "/MedicationRequest"
|
|
return httputil.DoRequest(httputil.RequestOption{
|
|
Method: "POST",
|
|
URL: url,
|
|
Body: req,
|
|
BearerToken: token.AccessToken,
|
|
Headers: httputil.DefaultFHIRHeaders(),
|
|
})
|
|
}
|
|
|
|
func (c *MedicationRequestRepository) UpdateMedicationRequest(req model.MedicationRequestRequest) (map[string]interface{}, error) {
|
|
req.ResourceType = constant.MedicationRequestResourceType
|
|
req.Identifier = append(req.Identifier, common.GetIdentifier("prescription"))
|
|
identifierItem := common.GetIdentifier("prescription-item")
|
|
identifierItem.Value += "-1"
|
|
req.Identifier = append(req.Identifier, identifierItem)
|
|
|
|
patient, err := c.setupPatient(&req)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
err = c.setupEncounter(&req, patient)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
// err = c.setupPractitioner(&req)
|
|
// if err != nil {
|
|
// return nil, err
|
|
// }
|
|
|
|
oauth := model.OauthRequest{
|
|
ClientId: c.akses.ClientId,
|
|
ClientSecret: c.akses.ClientSecret,
|
|
}
|
|
token, err := NewOauthRequestRepo(c.akses).GenerateToken(oauth)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if token == nil {
|
|
return nil, errors.New(constant.ErrGenerateToken)
|
|
}
|
|
|
|
url := c.akses.BaseUrl + fmt.Sprintf("/MedicationRequest/%s", req.Id)
|
|
return httputil.DoRequest(httputil.RequestOption{
|
|
Method: "PUT",
|
|
URL: url,
|
|
Body: req,
|
|
BearerToken: token.AccessToken,
|
|
Headers: httputil.DefaultFHIRHeaders(),
|
|
})
|
|
}
|
|
|
|
func (c *MedicationRequestRepository) setupPatient(req *model.MedicationRequestRequest) (string, error) {
|
|
if req.Subject.Reference == "" {
|
|
return "", nil
|
|
}
|
|
patientInterface := NewPatientRepo(c.akses)
|
|
patient, err := patientInterface.HandleCheckPatient(req.Subject.Reference)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
if patient != "" {
|
|
req.Subject.Reference = "Patient/" + patient
|
|
}
|
|
return patient, nil
|
|
}
|
|
|
|
func (c *MedicationRequestRepository) setupEncounter(req *model.MedicationRequestRequest, patient string) error {
|
|
if patient == "" {
|
|
return nil
|
|
}
|
|
encounterInterface := NewEncounterRepo(c.akses)
|
|
encounterId, encounterExist, err := encounterInterface.HandleCheckEncounter(patient)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if encounterExist {
|
|
req.Encounter.Reference = "Encounter/" + encounterId
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// setupPractitioner handles practitioner reference logic.
|
|
func (c *MedicationRequestRepository) setupPractitioner(req *model.MedicationRequestRequest) error {
|
|
if req.Requester.Reference == "" {
|
|
return nil
|
|
}
|
|
practicionerInterface := NewPracticionerRepo(c.akses)
|
|
ref, display, err := practicionerInterface.HandleCheckPartitioner(req.Requester.Reference)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if ref != "" {
|
|
req.Requester.Reference = "Practitioner/" + ref
|
|
req.Requester.Display = display
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (c *MedicationRequestRepository) setupMedication(req *model.MedicationRequestRequest) error {
|
|
if req.MedicationRequest == nil {
|
|
return errors.New("Need Medication Request")
|
|
}
|
|
MedicationInterface := NewMedicineKfaRepo(c.akses)
|
|
medication, err := MedicationInterface.MedicationCreate(*req.MedicationRequest)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if idRaw, ok := medication["id"].(string); ok {
|
|
if idRaw == "" {
|
|
return errors.New("medication id is empty")
|
|
}
|
|
req.MedicationReference.Reference = "Medication/" + idRaw
|
|
}
|
|
return nil
|
|
}
|