Skip to content

Commit 2c8425e

Browse files
committed
Instrument cummunity API to add files URLs
[Re #1543]
1 parent 2647c97 commit 2c8425e

File tree

1 file changed

+24
-4
lines changed

1 file changed

+24
-4
lines changed

backend/src/gpml/handler/community.clj

+24-4
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,12 @@
33
[duct.logger :refer [log]]
44
[gpml.db.community :as db.community]
55
[gpml.db.country-group :as db.country-group]
6+
[gpml.domain.file :as dom.file]
67
[gpml.handler.responses :as r]
8+
[gpml.service.file :as srv.file]
79
[gpml.util.regular-expressions :as util.regex]
8-
[integrant.core :as ig]))
10+
[integrant.core :as ig]
11+
[medley.core :as medley]))
912

1013
(def ^:const community-network-types ["organisation" "stakeholder"])
1114
(def ^:const geo-coverage-types ["Transnational" "National" "Global" "Sub-national"])
@@ -163,8 +166,23 @@
163166
(re-seq #"\w+")
164167
(str/join " & ")))))
165168

169+
(defn- community-member->api-community-member
170+
[config community-member]
171+
(let [{files :files member-type :type
172+
picture-id :picture_id logo-id :logo_id} community-member
173+
files-w-urls (->> files
174+
(map dom.file/decode-file)
175+
(srv.file/add-files-urls config)
176+
(medley/index-by (comp str :id)))]
177+
(cond-> community-member
178+
(= "stakeholder" member-type)
179+
(assoc :picture (get-in files-w-urls [picture-id :url]))
180+
181+
(= "organisation" member-type)
182+
(assoc :logo (get-in files-w-urls [logo-id :url])))))
183+
166184
(defn get-community-members
167-
[{:keys [db logger]} req]
185+
[{:keys [db logger] :as config} req]
168186
(try
169187
(let [conn (:spec db)
170188
query-params (get-in req [:parameters :query])
@@ -176,8 +194,10 @@
176194
(assoc-in opts [:filters :country] (set (concat
177195
(get-in opts [:filters :country])
178196
geo-coverage-countries))))
179-
opts)]
180-
(r/ok {:results (db.community/get-community-members conn modified-filters)
197+
opts)
198+
results (->> (db.community/get-community-members conn modified-filters)
199+
(map (partial community-member->api-community-member config)))]
200+
(r/ok {:results results
181201
:counts (db.community/get-community-members conn (assoc modified-filters :count-only? true))}))
182202
(catch Throwable t
183203
(let [log-data {:exception-message (ex-message t)

0 commit comments

Comments
 (0)