mastering bridging
This commit is contained in:
@@ -0,0 +1,230 @@
|
||||
package integration
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"satusehat-rssa/internal/constant"
|
||||
"satusehat-rssa/internal/model"
|
||||
"satusehat-rssa/pkg/httputil"
|
||||
)
|
||||
|
||||
type ConditionInterface interface {
|
||||
CreateCondition(request model.ConditionRequest) (map[string]interface{}, error)
|
||||
GetConditionByPatient(id string) (map[string]interface{}, error)
|
||||
HandleCheckCondition(id string) (string, bool, error)
|
||||
UpdateCondition(request model.ConditionRequest) (map[string]interface{}, error)
|
||||
}
|
||||
|
||||
type ConditionRepository struct {
|
||||
akses *model.Akses
|
||||
}
|
||||
|
||||
// HandleCheckCondition implements ConditionInterface.
|
||||
func (c *ConditionRepository) HandleCheckCondition(id string) (string, bool, error) {
|
||||
condition, err := c.GetConditionByPatient(id)
|
||||
if err != nil {
|
||||
return "", false, err
|
||||
}
|
||||
|
||||
// You can process 'encounter' here if needed
|
||||
if entries, ok := condition["entry"].([]interface{}); ok && len(entries) != 0 {
|
||||
if entries, ok := (condition)["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)
|
||||
return id, true, nil
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return "", true, nil
|
||||
}
|
||||
|
||||
return "", false, nil
|
||||
}
|
||||
|
||||
func NewConditionRepo(akses *model.Akses) ConditionInterface {
|
||||
return &ConditionRepository{akses: akses}
|
||||
}
|
||||
|
||||
// Implement the ConditionInterface methods for ConditionRepository
|
||||
func (c *ConditionRepository) CreateCondition(req model.ConditionRequest) (map[string]interface{}, error) {
|
||||
// TODO: implement the logic here
|
||||
var (
|
||||
data map[string]interface{}
|
||||
)
|
||||
|
||||
req.ClinicalStatus.Coding = append(req.ClinicalStatus.Coding, model.Coding{
|
||||
System: "http://terminology.hl7.org/CodeSystem/condition-clinical",
|
||||
Code: "active",
|
||||
Display: "Active",
|
||||
})
|
||||
|
||||
var patient string
|
||||
if req.Subject.Reference != "" {
|
||||
patientInterface := NewPatientRepo(c.akses)
|
||||
var err error
|
||||
patient, err = patientInterface.HandleCheckPatient(req.Subject.Reference)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if patient == "" { // Belum ada di satu sehat
|
||||
return nil, errors.New("patient not found")
|
||||
} else {
|
||||
req.Subject.Reference = "Patient/" + patient
|
||||
}
|
||||
}
|
||||
|
||||
// Setup Encounter
|
||||
if patient != "" {
|
||||
encounterInterface := NewEncounterRepo(c.akses)
|
||||
encounterId, encounterExist, err := encounterInterface.HandleCheckEncounter(patient)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if encounterExist {
|
||||
req.Encounter.Reference = "Encounter/" + encounterId
|
||||
} else {
|
||||
return nil, errors.New("encounter not found")
|
||||
}
|
||||
}
|
||||
|
||||
url := c.akses.BaseUrl + "/Condition"
|
||||
method := "POST"
|
||||
payload, err := json.Marshal(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
client := &http.Client{}
|
||||
request, err := http.NewRequest(method, url, bytes.NewBuffer(payload))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
oauth := model.OauthRequest{
|
||||
ClientId: c.akses.ClientId,
|
||||
ClientSecret: c.akses.ClientSecret,
|
||||
}
|
||||
OauthInterface := NewOauthRequestRepo(c.akses)
|
||||
token, err := OauthInterface.GenerateToken(oauth)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if token != nil {
|
||||
request.Header.Add("Authorization", "Bearer "+token.AccessToken)
|
||||
} else {
|
||||
return nil, errors.New(constant.ErrGenerateToken)
|
||||
}
|
||||
request.Header.Set("Content-Type", 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
|
||||
}
|
||||
|
||||
func (c *ConditionRepository) GetConditionByPatient(id string) (map[string]interface{}, error) {
|
||||
// TODO: implement the logic here
|
||||
var (
|
||||
data map[string]interface{}
|
||||
)
|
||||
url := c.akses.BaseUrl + "/Condition?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,
|
||||
}
|
||||
OauthInterface := NewOauthRequestRepo(c.akses)
|
||||
token, err := OauthInterface.GenerateToken(oauth)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if token != nil {
|
||||
request.Header.Add("Authorization", "Bearer "+token.AccessToken)
|
||||
} else {
|
||||
return nil, errors.New(constant.ErrGenerateToken)
|
||||
}
|
||||
request.Header.Set("Content-Type", 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
|
||||
}
|
||||
|
||||
func (c *ConditionRepository) UpdateCondition(req model.ConditionRequest) (map[string]interface{}, error) {
|
||||
var patient string
|
||||
if req.Subject.Reference != "" {
|
||||
patientInterface := NewPatientRepo(c.akses)
|
||||
var err error
|
||||
patient, err = patientInterface.HandleCheckPatient(req.Subject.Reference)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if patient == "" { // Belum ada di satu sehat
|
||||
return nil, errors.New("patient not found")
|
||||
} else {
|
||||
req.Subject.Reference = "Patient/" + patient
|
||||
}
|
||||
}
|
||||
|
||||
// Setup Encounter
|
||||
if patient != "" {
|
||||
encounterInterface := NewEncounterRepo(c.akses)
|
||||
encounterId, encounterExist, err := encounterInterface.HandleCheckEncounter(patient)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if encounterExist {
|
||||
req.Encounter.Reference = "Encounter/" + encounterId
|
||||
} else {
|
||||
return nil, errors.New("encounter not found")
|
||||
}
|
||||
}
|
||||
|
||||
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("/Condition/%s", req.Id)
|
||||
return httputil.DoRequest(httputil.RequestOption{
|
||||
Method: "PUT",
|
||||
URL: url,
|
||||
Body: req,
|
||||
BearerToken: token.AccessToken,
|
||||
Headers: httputil.DefaultFHIRHeaders(),
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user