mastering bridging
This commit is contained in:
105
internal/integration/practitioner_integration.go
Normal file
105
internal/integration/practitioner_integration.go
Normal file
@@ -0,0 +1,105 @@
|
||||
package integration
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"net/http"
|
||||
"satusehat-rssa/internal/constant"
|
||||
"satusehat-rssa/internal/model"
|
||||
)
|
||||
|
||||
type PracticionerInterface interface {
|
||||
// Add fields as necessary for the integration
|
||||
GetPracticionerByNIK(nik string) (*map[string]interface{}, error)
|
||||
HandleCheckPartitioner(nik string) (string, string, error)
|
||||
}
|
||||
|
||||
type PracticionerRepository struct {
|
||||
akses *model.Akses
|
||||
}
|
||||
|
||||
// HandleCheckPartitioner implements PracticionerInterface.
|
||||
func (p *PracticionerRepository) HandleCheckPartitioner(nik string) (string, string, error) {
|
||||
practitioner, err := p.GetPracticionerByNIK("https://fhir.kemkes.go.id/id/nik|" + nik)
|
||||
if err != nil {
|
||||
return "", "", err
|
||||
}
|
||||
if practitioner != nil {
|
||||
id := ""
|
||||
nameText := ""
|
||||
if entriesRaw, ok := (*practitioner)["entry"]; ok {
|
||||
if entries, ok := entriesRaw.([]interface{}); ok && len(entries) > 0 {
|
||||
if entryMap, ok := entries[0].(map[string]interface{}); ok {
|
||||
if resource, ok := entryMap["resource"].(map[string]interface{}); ok {
|
||||
// Ambil id
|
||||
if idRaw, ok := resource["id"].(string); ok {
|
||||
id = idRaw
|
||||
}
|
||||
|
||||
// Ambil name[0].text
|
||||
if namesRaw, ok := resource["name"]; ok {
|
||||
if names, ok := namesRaw.([]interface{}); ok && len(names) > 0 {
|
||||
if nameObj, ok := names[0].(map[string]interface{}); ok {
|
||||
if text, ok := nameObj["text"].(string); ok {
|
||||
nameText = text
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return id, nameText, nil
|
||||
} else {
|
||||
return "", "", errors.New("practitioner not found")
|
||||
}
|
||||
}
|
||||
|
||||
func NewPracticionerRepo(akses *model.Akses) PracticionerInterface {
|
||||
return &PracticionerRepository{akses: akses}
|
||||
}
|
||||
|
||||
func (p *PracticionerRepository) GetPracticionerByNIK(nik string) (*map[string]interface{}, error) {
|
||||
var (
|
||||
data *map[string]interface{}
|
||||
)
|
||||
url := p.akses.BaseUrl + "/Practitioner?identifier=" + nik
|
||||
method := "GET"
|
||||
|
||||
client := &http.Client{}
|
||||
request, err := http.NewRequest(method, url, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
oauth := model.OauthRequest{
|
||||
ClientId: p.akses.ClientId,
|
||||
ClientSecret: p.akses.ClientSecret,
|
||||
}
|
||||
OauthInterface := NewOauthRequestRepo(p.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("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
|
||||
}
|
||||
Reference in New Issue
Block a user