mastering bridging
This commit is contained in:
284
internal/integration/imagingstudy_integration.go
Normal file
284
internal/integration/imagingstudy_integration.go
Normal file
@@ -0,0 +1,284 @@
|
||||
package integration
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"os"
|
||||
"satusehat-rssa/internal/constant"
|
||||
"satusehat-rssa/internal/model"
|
||||
"satusehat-rssa/pkg/httputil"
|
||||
)
|
||||
|
||||
type ImagingStudyInterface interface {
|
||||
// Define methods for ImagingStudyInterface
|
||||
CreateImagingStudy(req model.ImagingStudyRequest) (map[string]interface{}, error)
|
||||
UpdateImagingStudy(req model.ImagingStudyRequest) (map[string]interface{}, error)
|
||||
GetImagingStudyByPatient(id string) (map[string]interface{}, error)
|
||||
HandleCheckImagingStudy(id string) ([]string, bool, error)
|
||||
}
|
||||
|
||||
type ImagingStudyRepository struct {
|
||||
// Define fields for ImagingStudyRepository
|
||||
akses *model.Akses
|
||||
}
|
||||
|
||||
func NewImagingStudyRepo(akses *model.Akses) ImagingStudyInterface {
|
||||
return &ImagingStudyRepository{
|
||||
akses: akses,
|
||||
}
|
||||
}
|
||||
|
||||
// GetImagingStudyByPatient implements ImagingStudyInterface.
|
||||
func (c *ImagingStudyRepository) GetImagingStudyByPatient(id string) (map[string]interface{}, error) {
|
||||
var data map[string]interface{}
|
||||
|
||||
url := c.akses.BaseUrl + "/ImagingStudy?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
|
||||
|
||||
}
|
||||
|
||||
// HandleCheckImagingStudy implements ImagingStudyInterface.
|
||||
func (c *ImagingStudyRepository) HandleCheckImagingStudy(id string) ([]string, bool, error) {
|
||||
imaging, err := c.GetImagingStudyByPatient(id)
|
||||
if err != nil {
|
||||
return nil, false, err
|
||||
}
|
||||
var ids []string
|
||||
if entries, ok := imaging["entry"].([]interface{}); ok && len(entries) != 0 {
|
||||
if entries, ok := (imaging)["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
|
||||
}
|
||||
|
||||
// CreateImagingStudy implements ImagingStudyInterface.
|
||||
func (c *ImagingStudyRepository) CreateImagingStudy(req model.ImagingStudyRequest) (map[string]interface{}, error) {
|
||||
req.ResourceType = constant.ImagingStudyResourceType
|
||||
|
||||
if len(req.Identifier) > 0 {
|
||||
identifier := []model.Identifier{}
|
||||
for k, i := range req.Identifier {
|
||||
if k == 0 {
|
||||
identifier = append(identifier, model.Identifier{
|
||||
Use: "official",
|
||||
System: "http://sys-ids.kemkes.go.id/acsn/" + os.Getenv("ORGANIZATION_ID"), // Set this if needed, or remove if not present in IdentifierObject
|
||||
Value: i.Value,
|
||||
Type: &model.CodeableConcept{
|
||||
Coding: []model.Coding{
|
||||
{
|
||||
System: "http://terminology.hl7.org/CodeSystem/v2-0203",
|
||||
Code: "ACSN",
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
// if k == 1 {
|
||||
// identifier = append(identifier, model.Identifier{
|
||||
// System: "urn:dicom:uid",
|
||||
// Value: i.Value,
|
||||
// })
|
||||
// }
|
||||
}
|
||||
req.Identifier = identifier
|
||||
}
|
||||
|
||||
patient, err := c.setupPatient(&req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Setup Service Request
|
||||
err = c.setupServiceRequest(&req, patient)
|
||||
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 + "/ImagingStudy"
|
||||
data, err := httputil.DoRequest(httputil.RequestOption{
|
||||
Method: "POST",
|
||||
URL: url,
|
||||
Body: req,
|
||||
BearerToken: token.AccessToken,
|
||||
Headers: httputil.DefaultFHIRHeaders(),
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if !isOperationOutcomeError(data) {
|
||||
// Setup Encounter
|
||||
if patient != "" {
|
||||
encounterInterface := NewEncounterRepo(c.akses)
|
||||
encounterId, encounterExist, err := encounterInterface.HandleCheckEncounter(patient)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if encounterExist {
|
||||
data["encounter"] = map[string]interface{}{
|
||||
"reference": "Encounter/" + encounterId,
|
||||
}
|
||||
} else {
|
||||
return nil, errors.New("encounter not found")
|
||||
}
|
||||
}
|
||||
}
|
||||
return data, nil
|
||||
}
|
||||
|
||||
func (c *ImagingStudyRepository) UpdateImagingStudy(req model.ImagingStudyRequest) (map[string]interface{}, error) {
|
||||
req.ResourceType = constant.ImagingStudyResourceType
|
||||
|
||||
if len(req.Identifier) > 0 {
|
||||
identifier := []model.Identifier{}
|
||||
for k, i := range req.Identifier {
|
||||
if k == 0 {
|
||||
identifier = append(identifier, model.Identifier{
|
||||
Use: "official",
|
||||
System: "http://sys-ids.kemkes.go.id/acsn/" + os.Getenv("ORGANIZATION_ID"), // Set this if needed, or remove if not present in IdentifierObject
|
||||
Value: i.Value,
|
||||
Type: &model.CodeableConcept{
|
||||
Coding: []model.Coding{
|
||||
{
|
||||
System: "http://terminology.hl7.org/CodeSystem/v2-0203",
|
||||
Code: "ACSN",
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
// if k == 1 {
|
||||
// identifier = append(identifier, model.Identifier{
|
||||
// System: "urn:dicom:uid",
|
||||
// Value: i.Value,
|
||||
// })
|
||||
// }
|
||||
}
|
||||
req.Identifier = identifier
|
||||
}
|
||||
|
||||
patient, err := c.setupPatient(&req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Setup Service Request
|
||||
err = c.setupServiceRequest(&req, patient)
|
||||
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("/ImagingStudy/%s", req.Id)
|
||||
return httputil.DoRequest(httputil.RequestOption{
|
||||
Method: "PUT",
|
||||
URL: url,
|
||||
Body: req,
|
||||
BearerToken: token.AccessToken,
|
||||
Headers: httputil.DefaultFHIRHeaders(),
|
||||
})
|
||||
}
|
||||
|
||||
func (c *ImagingStudyRepository) setupPatient(req *model.ImagingStudyRequest) (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 *ImagingStudyRepository) setupServiceRequest(req *model.ImagingStudyRequest, patient string) error {
|
||||
if patient == "" {
|
||||
return nil
|
||||
}
|
||||
serviceReq := NewServiceRequestRepository(c.akses)
|
||||
ids, _, err := serviceReq.HandleCheckServiceRequest(patient)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if len(ids) > 0 {
|
||||
req.BasedOn = []model.Reference{
|
||||
{
|
||||
Reference: "ServiceRequest/" + ids[0],
|
||||
},
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user