feat(osi): 优化人群分类查询接口(T-209)
This commit is contained in:
@@ -51,6 +51,37 @@ func (h *HealthRecordHandler) Find(w http.ResponseWriter, r *http.Request) {
|
||||
writeJSONError(w, http.StatusBadGateway, "empty response from OSI")
|
||||
}
|
||||
|
||||
// Crowd 处理 GET /api/health-record/crowd?idCard=..|phrid=..
|
||||
// 原样回写 JKDA00005 人群分类与子档案标记响应。
|
||||
func (h *HealthRecordHandler) Crowd(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodGet {
|
||||
writeJSONError(w, http.StatusMethodNotAllowed, "only GET is supported")
|
||||
return
|
||||
}
|
||||
|
||||
q := r.URL.Query()
|
||||
query := osi.FindRqbjQuery{
|
||||
IDCard: q.Get("idCard"),
|
||||
PHRID: q.Get("phrid"),
|
||||
}
|
||||
if query.IDCard == "" && query.PHRID == "" {
|
||||
writeJSONError(w, http.StatusBadRequest, "provide one of: idCard, phrid")
|
||||
return
|
||||
}
|
||||
|
||||
_, result, err := h.client.FindRqbj(r.Context(), query)
|
||||
if len(result.Raw) > 0 {
|
||||
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
||||
_, _ = w.Write(result.Raw)
|
||||
return
|
||||
}
|
||||
if err != nil {
|
||||
writeJSONError(w, http.StatusBadGateway, err.Error())
|
||||
return
|
||||
}
|
||||
writeJSONError(w, http.StatusBadGateway, "empty response from OSI")
|
||||
}
|
||||
|
||||
func writeJSONError(w http.ResponseWriter, status int, msg string) {
|
||||
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
||||
w.WriteHeader(status)
|
||||
|
||||
@@ -39,6 +39,38 @@ func TestHealthRecordFindReturnsRawPlatformResponse(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestHealthRecordCrowdReturnsRawPlatformResponse(t *testing.T) {
|
||||
var seenPath string
|
||||
osiServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
seenPath = r.URL.Path
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
_, _ = w.Write([]byte(`{"code":"01","message":"操作成功","data":{"personSign":"LAO","idCard":"440100199001011234","phrId":"PHR001"}}`))
|
||||
}))
|
||||
defer osiServer.Close()
|
||||
|
||||
transport, err := osi.NewTransport(osi.TransportConfig{Timeout: time.Second})
|
||||
if err != nil {
|
||||
t.Fatalf("NewTransport: %v", err)
|
||||
}
|
||||
client := osi.NewClient(osi.ClientConfig{BaseURL: osiServer.URL, UserName: "dyytgw", Ask: "secret", Transport: transport})
|
||||
h := NewHealthRecordHandler(client)
|
||||
|
||||
req := httptest.NewRequest(http.MethodGet, "/api/health-record/crowd?idCard=440100199001011234", nil)
|
||||
rec := httptest.NewRecorder()
|
||||
h.Crowd(rec, req)
|
||||
|
||||
if rec.Code != http.StatusOK {
|
||||
t.Fatalf("status = %d", rec.Code)
|
||||
}
|
||||
if seenPath != "/osi/api/auto/jkda/findrqbj" {
|
||||
t.Fatalf("upstream path = %q", seenPath)
|
||||
}
|
||||
body, _ := io.ReadAll(rec.Body)
|
||||
if !strings.Contains(string(body), `"personSign":"LAO"`) || !strings.Contains(string(body), `"phrId":"PHR001"`) {
|
||||
t.Fatalf("body missing crowd fields: %s", body)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHealthRecordFindRequiresIdentifier(t *testing.T) {
|
||||
h := NewHealthRecordHandler(nil) // 无标识符时在触达 client 前就返回 400
|
||||
req := httptest.NewRequest(http.MethodGet, "/api/health-record/find", nil)
|
||||
|
||||
Reference in New Issue
Block a user