feat: add active hospital catalog API
This commit is contained in:
29
internal/http/hospitals.go
Normal file
29
internal/http/hospitals.go
Normal file
@@ -0,0 +1,29 @@
|
||||
package http
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"primaya-api/cpone-middleware/internal/databaseconfig"
|
||||
)
|
||||
|
||||
type HospitalSummary struct {
|
||||
RSCode string `json:"kode_rs"`
|
||||
Name string `json:"nama"`
|
||||
Status bool `json:"status"`
|
||||
}
|
||||
|
||||
func (h DatabaseSettingsHandler) ListHospitals(w http.ResponseWriter, _ *http.Request) {
|
||||
settings := h.store.List()
|
||||
hospitals := make([]HospitalSummary, 0, len(settings))
|
||||
for _, setting := range settings {
|
||||
if !setting.Active {
|
||||
continue
|
||||
}
|
||||
hospitals = append(hospitals, hospitalSummary(setting))
|
||||
}
|
||||
WriteJSON(w, http.StatusOK, Response{Success: true, Message: "ok", Data: hospitals})
|
||||
}
|
||||
|
||||
func hospitalSummary(setting databaseconfig.PublicSetting) HospitalSummary {
|
||||
return HospitalSummary{RSCode: setting.RSCode, Name: setting.Name, Status: setting.Active}
|
||||
}
|
||||
50
internal/http/hospitals_test.go
Normal file
50
internal/http/hospitals_test.go
Normal file
@@ -0,0 +1,50 @@
|
||||
package http
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"primaya-api/cpone-middleware/internal/databaseconfig"
|
||||
)
|
||||
|
||||
type fakeHospitalStore struct {
|
||||
fakeDatabaseSettingsStore
|
||||
settings []databaseconfig.PublicSetting
|
||||
}
|
||||
|
||||
func (f *fakeHospitalStore) List() []databaseconfig.PublicSetting { return f.settings }
|
||||
|
||||
func TestListHospitalsOnlyReturnsActiveWithoutDatabaseCredentials(t *testing.T) {
|
||||
store := &fakeHospitalStore{settings: []databaseconfig.PublicSetting{
|
||||
{RSCode: "AWALBROS_DEV", Name: "RS Dev Awalbros", Host: "secret-host", Username: "secret-user", Active: true},
|
||||
{RSCode: "INACTIVE_RS", Name: "RS Tidak Aktif", Host: "inactive-host", Active: false},
|
||||
}}
|
||||
handler := NewDatabaseSettingsHandler(store)
|
||||
recorder := httptest.NewRecorder()
|
||||
handler.ListHospitals(recorder, httptest.NewRequest(http.MethodGet, "/api/cpone/hospitals", nil))
|
||||
|
||||
if recorder.Code != http.StatusOK {
|
||||
t.Fatalf("status=%d body=%s", recorder.Code, recorder.Body.String())
|
||||
}
|
||||
for _, expected := range []string{`"kode_rs":"AWALBROS_DEV"`, `"nama":"RS Dev Awalbros"`, `"status":true`} {
|
||||
if !strings.Contains(recorder.Body.String(), expected) {
|
||||
t.Fatalf("response missing %s: %s", expected, recorder.Body.String())
|
||||
}
|
||||
}
|
||||
for _, forbidden := range []string{"INACTIVE_RS", "RS Tidak Aktif", "secret-host", "secret-user", `"host"`, `"username"`, `"password"`} {
|
||||
if strings.Contains(recorder.Body.String(), forbidden) {
|
||||
t.Fatalf("response contains %q: %s", forbidden, recorder.Body.String())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestListHospitalsReturnsEmptyArray(t *testing.T) {
|
||||
handler := NewDatabaseSettingsHandler(&fakeHospitalStore{})
|
||||
recorder := httptest.NewRecorder()
|
||||
handler.ListHospitals(recorder, httptest.NewRequest(http.MethodGet, "/api/cpone/hospitals", nil))
|
||||
if recorder.Body.String() != `{"success":true,"message":"ok","data":[]}`+"\n" {
|
||||
t.Fatalf("body=%s", recorder.Body.String())
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user