Skip to content

Commit

Permalink
remove mrn field from patient entity
Browse files Browse the repository at this point in the history
  • Loading branch information
ashutoshgngwr committed Oct 17, 2023
1 parent 4beada2 commit 2d16d6b
Show file tree
Hide file tree
Showing 6 changed files with 10 additions and 46 deletions.
1 change: 0 additions & 1 deletion clinic/src/clj/clinic/routes/patient.clj
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
(catch Exception e
(case (:type (ex-data e))
:invalid-params (r/status 400)
:mrn-conflict (r/status 503)
(throw e))))))

(defroutes handler
Expand Down
38 changes: 9 additions & 29 deletions clinic/src/clj/clinic/service/patient.clj
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,12 @@
[clojure.spec.alpha :as s]
[clojure.string :as string]))

(def mrn-system "urn:nilenso:clinic:mrn")
(def marital-status-system "http://hl7.org/fhir/ValueSet/marital-status")
(def email-system "email")
(def phone-system "phone")

(defn generate-mrn []
(String/format "%03d-%03d-%03d"
(into-array [(rand-int 999)
(rand-int 999)
(rand-int 999)])))

(defn- domain->fhir [params]
(cond-> {:resourceType "Patient"
:identifier [{:system mrn-system
:value (params :mrn)}]
:name [{:family (params :last-name)
:given [(params :first-name)]}]
:birthDate (params :birth-date)
Expand All @@ -45,9 +36,6 @@
(:telecom)
(fu/find-value phone-system))
entity (cond-> {:id (resource :id)
:mrn (->> resource
(:identifier)
(fu/find-value mrn-system))
:first-name (string/join " " (get-in resource [:name 0 :given]))
:last-name (get-in resource [:name 0 :family])
:birth-date (resource :birthDate)
Expand All @@ -66,20 +54,12 @@
(throw (ex-info "invalid create params"
{:type :invalid-params
:details (s/explain-data ::specs/create-params params)})))
(loop [retry-count 3]
(let [mrn (generate-mrn)
resource (-> params
(assoc :mrn mrn)
(domain->fhir))
response (fc/create! fhir-server-url
resource
{"If-None-Exist" (str "identifier=" mrn-system "|" mrn)})
{status :status body :body} response]
(cond
(= 0 retry-count) (throw (ex-info "couldn't generate a unique MRN"
{:type :mrn-conflict}))
(= status 200) (recur (dec retry-count)) ;; MRN conflict
(= status 201) (fhir->domain body)
:else (throw (ex-info "upstream service error"
{:type :upstream-error
:response {:status status :body body}}))))))
(let [{status :status
body :body} (fc/create! fhir-server-url
(domain->fhir params)
nil)]
(cond
(= status 201) (fhir->domain body)
:else (throw (ex-info "upstream service error"
{:type :upstream-error
:response {:status status :body body}})))))
3 changes: 1 addition & 2 deletions clinic/src/cljc/clinic/specs/patient.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
(def ^:private not-blank? (complement string/blank?))

(s/def ::id (s/and string? not-blank?))
(s/def ::mrn (s/and string? (partial re-matches #"\d{3}-\d{3}-\d{3}")))
(s/def ::first-name (s/and string? not-blank?))
(s/def ::last-name (s/and string? not-blank?))
(s/def ::birth-date (s/and string? (partial re-matches #"\d{4}-\d{2}-\d{2}")))
Expand All @@ -19,5 +18,5 @@
:opt-un [::marital-status ::email ::phone]))

(s/def ::patient
(s/keys :req-un [::id ::mrn ::first-name ::last-name ::birth-date ::gender]
(s/keys :req-un [::id ::first-name ::last-name ::birth-date ::gender]
:opt-un [::marital-status ::email ::phone]))
1 change: 0 additions & 1 deletion clinic/test/clj/clinic/routes/patient_integration_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
(update :body json/parse-string true))]
(is (= 201 status))
(is (contains? body :id))
(is (contains? body :mrn))
(is (= (params :first-name) (body :first-name)))
(is (= (params :last-name) (body :last-name)))
(is (= (params :birth-date) (body :birth-date)))
Expand Down
5 changes: 0 additions & 5 deletions clinic/test/clj/clinic/routes/patient_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,6 @@
{:type :invalid-params})))
(is (= 400 (:status (routes/handler (create-patient-request))))))

(testing "with mrn conflict service error"
(reset! response-fn #(throw (ex-info "test-error"
{:type :mrn-conflict})))
(is (= 503 (:status (routes/handler (create-patient-request))))))

(testing "with unknown service error"
(reset! response-fn #(throw (RuntimeException. "test-error")))
(is (= 500 (binding [log/*logger-factory* log-impl/disabled-logger-factory]
Expand Down
8 changes: 0 additions & 8 deletions clinic/test/clj/clinic/service/patient_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
patient (svc/create! "test-server-url" params)]
(is (= "test-server-url" (@call-args 0)))
(is (= "test-id" (patient :id)))
(is (re-matches #"\d{3}-\d{3}-\d{3}" (patient :mrn)))
(is (= (params :first-name) (patient :first-name)))
(is (= (params :last-name) (patient :last-name)))
(is (= (params :birth-date) (patient :birth-date)))
Expand All @@ -40,13 +39,6 @@
(is (= (params :email) (patient :email)))
(is (= (params :phone) (patient :phone))))))

(testing "with mrn conflict"
(reset! response-fn (constantly {:status 200}))
(is (= :mrn-conflict (-> (factory/create-params)
((partial svc/create! "test-server-url"))
(catch-thrown-data)
(get :type)))))

(testing "with upstream service non-20x response"
(reset! response-fn (constantly {:status 400}))
(is (= :upstream-error (-> (factory/create-params)
Expand Down

0 comments on commit 2d16d6b

Please sign in to comment.