mastering bridging
This commit is contained in:
363
internal/integration/diagnosisreport_integration.go
Normal file
363
internal/integration/diagnosisreport_integration.go
Normal file
@@ -0,0 +1,363 @@
|
||||
package integration
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"os"
|
||||
"satusehat-rssa/internal/constant"
|
||||
"satusehat-rssa/internal/model"
|
||||
"satusehat-rssa/pkg/httputil"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type DiagnosisReportInterface interface {
|
||||
// Define methods for DiagnosisReportInterface
|
||||
CreateDiagnosisReport(req model.DiagnosticReportRequest) (map[string]interface{}, error)
|
||||
GetDiagnosisReportByPatient(id string) (map[string]interface{}, error)
|
||||
GetDiagnosisReportByPatientCategory(id string, category string) (map[string]interface{}, error)
|
||||
HandleCheckDiagnosisReport(id string) (string, bool, error)
|
||||
HandleCheckDiagnosisReportCategory(id string, category string) ([]string, bool, error)
|
||||
UpdateDiagnosisReport(req model.DiagnosticReportRequest) (map[string]interface{}, error)
|
||||
}
|
||||
type DiagnosisReportRepository struct {
|
||||
akses *model.Akses
|
||||
}
|
||||
|
||||
// GetDiagnosisReportByPatientCategory implements DiagnosisReportInterface.
|
||||
func (d *DiagnosisReportRepository) GetDiagnosisReportByPatientCategory(id string, category string) (map[string]interface{}, error) {
|
||||
var data map[string]interface{}
|
||||
|
||||
url := d.akses.BaseUrl + "/DiagnosticReport?subject=" + id + "&category=" + category
|
||||
method := "GET"
|
||||
|
||||
client := &http.Client{}
|
||||
request, err := http.NewRequest(method, url, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
oauth := model.OauthRequest{
|
||||
ClientId: d.akses.ClientId,
|
||||
ClientSecret: d.akses.ClientSecret,
|
||||
}
|
||||
OauthInterface := NewOauthRequestRepo(d.akses)
|
||||
token, err := OauthInterface.GenerateToken(oauth)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if token != nil {
|
||||
request.Header.Set("Authorization", "Bearer "+token.AccessToken)
|
||||
}
|
||||
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
|
||||
}
|
||||
|
||||
// HandleCheckDiagnosisReportCategory implements DiagnosisReportInterface.
|
||||
func (d *DiagnosisReportRepository) HandleCheckDiagnosisReportCategory(id string, category string) ([]string, bool, error) {
|
||||
diagnosis, err := d.GetDiagnosisReportByPatientCategory(id, category)
|
||||
if err != nil {
|
||||
return nil, false, err
|
||||
}
|
||||
var ids []string
|
||||
if entries, ok := diagnosis["entry"].([]interface{}); ok && len(entries) != 0 {
|
||||
if entries, ok := (diagnosis)["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
|
||||
}
|
||||
|
||||
// CreateDiagnosisReport implements DiagnosisReportInterface.
|
||||
func (d *DiagnosisReportRepository) CreateDiagnosisReport(req model.DiagnosticReportRequest) (map[string]interface{}, error) {
|
||||
var data map[string]interface{}
|
||||
req.ResourceType = constant.DiagnosticReportResourceType
|
||||
req.Identifier = append(req.Identifier, model.Identifier{
|
||||
Use: "official",
|
||||
System: "http://sys-ids.kemkes.go.id/diagnostic/" + os.Getenv("ORGANIZATION_ID") + "/lab",
|
||||
Value: os.Getenv("ORGANIZATION_ID"),
|
||||
})
|
||||
|
||||
patient, err := d.setupPatient(&req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
err = d.setupEncounter(&req, patient)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
//Setup Performer
|
||||
err = d.setupPractitioner(&req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
err = d.setupOrganization(&req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
//Setup Result
|
||||
err = d.setupObserver(&req, patient)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
//Setup Specimen
|
||||
err = d.setupSpecimen(&req, patient)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
//Setup BasedOn
|
||||
|
||||
url := d.akses.BaseUrl + "/DiagnosticReport"
|
||||
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: d.akses.ClientId,
|
||||
ClientSecret: d.akses.ClientSecret,
|
||||
}
|
||||
OauthInterface := NewOauthRequestRepo(d.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", "application/json")
|
||||
response, err := client.Do(request)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer response.Body.Close()
|
||||
|
||||
err = json.NewDecoder(response.Body).Decode(&data)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return data, nil
|
||||
}
|
||||
|
||||
// CreateDiagnosisReport implements DiagnosisReportInterface.
|
||||
func (d *DiagnosisReportRepository) UpdateDiagnosisReport(req model.DiagnosticReportRequest) (map[string]interface{}, error) {
|
||||
patient, err := d.setupPatient(&req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
err = d.setupEncounter(&req, patient)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
//Setup Performer
|
||||
err = d.setupPractitioner(&req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
err = d.setupOrganization(&req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
//Setup Result
|
||||
err = d.setupObserver(&req, patient)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
//Setup Specimen
|
||||
err = d.setupSpecimen(&req, patient)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
//Setup BasedOn
|
||||
oauth := model.OauthRequest{
|
||||
ClientId: d.akses.ClientId,
|
||||
ClientSecret: d.akses.ClientSecret,
|
||||
}
|
||||
token, err := NewOauthRequestRepo(d.akses).GenerateToken(oauth)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if token == nil {
|
||||
return nil, errors.New(constant.ErrGenerateToken)
|
||||
}
|
||||
|
||||
url := d.akses.BaseUrl + fmt.Sprintf("/DiagnosticReport/%s", req.Id)
|
||||
return httputil.DoRequest(httputil.RequestOption{
|
||||
Method: "PUT",
|
||||
URL: url,
|
||||
Body: req,
|
||||
BearerToken: token.AccessToken,
|
||||
})
|
||||
}
|
||||
|
||||
// setupPatient extracts patient setup logic from CreateDiagnosisReport.
|
||||
func (d *DiagnosisReportRepository) setupPatient(req *model.DiagnosticReportRequest) (string, error) {
|
||||
if req.Subject.Reference == "" {
|
||||
return "", nil
|
||||
}
|
||||
patientInterface := NewPatientRepo(d.akses)
|
||||
patient, err := patientInterface.HandleCheckPatient(req.Subject.Reference)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if patient != "" {
|
||||
req.Subject.Reference = "Patient/" + patient
|
||||
}
|
||||
return patient, nil
|
||||
}
|
||||
|
||||
// setupEncounter extracts encounter setup logic from CreateDiagnosisReport.
|
||||
func (d *DiagnosisReportRepository) setupEncounter(req *model.DiagnosticReportRequest, patient string) error {
|
||||
if patient == "" {
|
||||
return nil
|
||||
}
|
||||
encounterInterface := NewEncounterRepo(d.akses)
|
||||
encounterId, encounterExist, err := encounterInterface.HandleCheckEncounter(patient)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if encounterExist {
|
||||
req.Encounter.Reference = "Encounter/" + encounterId
|
||||
} else {
|
||||
//Buat dulu encounter
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d *DiagnosisReportRepository) setupSpecimen(req *model.DiagnosticReportRequest, patient string) error {
|
||||
if patient == "" {
|
||||
return nil
|
||||
}
|
||||
specimenInterface := NewSpecimenRepository(d.akses)
|
||||
ids, exist, err := specimenInterface.HandleCheckSpecimen(patient)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if exist {
|
||||
for i := range req.Specimen {
|
||||
req.Specimen[i].Reference = "Specimen/" + ids[i]
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d *DiagnosisReportRepository) setupBasedOn(req *model.DiagnosticReportRequest, patient string) error {
|
||||
if patient == "" {
|
||||
return nil
|
||||
}
|
||||
basedOnInterface := NewServiceRequestRepository(d.akses)
|
||||
ids, exist, err := basedOnInterface.HandleCheckServiceRequest(patient)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if exist {
|
||||
for i := range req.BasedOn {
|
||||
req.BasedOn[i].Reference = "ServiceRequest/" + ids[i]
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetDiagnosisReportByPatient implements DiagnosisReportInterface.
|
||||
func (d *DiagnosisReportRepository) GetDiagnosisReportByPatient(id string) (map[string]interface{}, error) {
|
||||
panic("unimplemented")
|
||||
}
|
||||
|
||||
// HandleCheckDiagnosisReport implements DiagnosisReportInterface.
|
||||
func (d *DiagnosisReportRepository) HandleCheckDiagnosisReport(id string) (string, bool, error) {
|
||||
panic("unimplemented")
|
||||
}
|
||||
|
||||
// setupPractitioner handles practitioner reference logic.
|
||||
func (d *DiagnosisReportRepository) setupPractitioner(req *model.DiagnosticReportRequest) error {
|
||||
if len(req.Performer) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
for _, performer := range req.Performer {
|
||||
if strings.Contains(performer.Reference, "Practitioner/") {
|
||||
temp := strings.Split(performer.Reference, "/")
|
||||
practicionerInterface := NewPracticionerRepo(d.akses)
|
||||
ref, _, err := practicionerInterface.HandleCheckPartitioner(temp[1])
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if ref != "" {
|
||||
performer.Reference = "Practitioner/" + ref
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d *DiagnosisReportRepository) setupOrganization(req *model.DiagnosticReportRequest) error {
|
||||
if len(req.Performer) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
for _, performer := range req.Performer {
|
||||
if strings.Contains(performer.Reference, "Organization/") {
|
||||
performer.Reference = "Organization/" + os.Getenv("ORGANIZATION_ID")
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d *DiagnosisReportRepository) setupObserver(req *model.DiagnosticReportRequest, patient string) error {
|
||||
observerInterface := NewObservationRepo(d.akses)
|
||||
//temp := strings.Split(req.Subject.Reference, "/")
|
||||
ids, valid, err := observerInterface.HandleCheckObservation(patient)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if len(ids) > 0 && valid {
|
||||
for _, id := range ids {
|
||||
req.Result = append(req.Result, model.Reference{Reference: "Observation/" + id})
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func NewDiagnosisReportRepo(akses *model.Akses) DiagnosisReportInterface {
|
||||
return &DiagnosisReportRepository{akses: akses}
|
||||
}
|
||||
Reference in New Issue
Block a user