Skip to content

Commit

Permalink
Support badges-metadata in Organisations List endp.
Browse files Browse the repository at this point in the history
[Re #1611]

We need to include the same information we are adding in other APIs such
as Submission one, in order to conditionally load Badges
assignation-related metadata for the listed organisations.
  • Loading branch information
joseAyudarte91 committed Oct 26, 2023
1 parent 2c5716a commit 28c257e
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 2 deletions.
22 changes: 22 additions & 0 deletions backend/src/gpml/db/organisation.clj
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,22 @@
) FILTER (WHERE psb.plastic_strategy_id IS NOT NULL) AS plastic_strategy_bookmarks,
(psb.organisation_id IS NOT NULL)::boolean AS ps_bookmarked,")))

(defn list-organisations-badges-partial-select
[params]
(let [{:keys [badges]} params]
(when-not (nil? badges)
"COALESCE(
jsonb_agg(
DISTINCT jsonb_build_object(
'badge_id', oba.badge_id,
'badge_name', ba.name,
'organisation_id', oba.organisation_id,
'assigned_by', oba.assigned_by,
'assigned_at', oba.assigned_at
)
) FILTER (WHERE oba.badge_id IS NOT NULL),
'[]'::jsonb) AS assigned_badges,")))

(defn list-organisations-cto-query-filter-and-params
[params]
(let [{:keys [filters plastic-strategy-id]} params
Expand All @@ -83,6 +99,10 @@
(format "LEFT JOIN plastic_strategy_organisation_bookmark psb ON (o.id = psb.organisation_id %s AND psb.plastic_strategy_id = %d)"
ps-bookmark-section-keys-join-cond
plastic-strategy-id))
badges-join (if-not (contains? (set (keys params)) :badges)
""
"LEFT JOIN organisation_badge oba ON oba.organisation_id = o.id
LEFT JOIN badge ba ON ba.id = oba.badge_id")
where-cond (cond-> "WHERE 1=1"
(seq review-status)
(str " AND o.review_status = :filters.review-status::REVIEW_STATUS")
Expand All @@ -109,10 +129,12 @@
""
", psb.organisation_id")]
(format "%s
%s
%s
GROUP BY o.id %s
%s"
ps-bookmark-join
badges-join
where-cond
ps-bookmark-group-by
(or having ""))))
1 change: 1 addition & 0 deletions backend/src/gpml/db/organisation.sql
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ organisations_cte AS (
)
) FILTER (WHERE st.id IS NOT NULL) AS focal_points,
--~ (#'gpml.db.organisation/list-organisations-ps-bookmark-partial-select params)
--~ (#'gpml.db.organisation/list-organisations-badges-partial-select params)
count(DISTINCT os_cte.initiative) as strengths
FROM organisation o
LEFT JOIN stakeholder_organisation sto ON (sto.organisation = o.id AND sto.association = 'focal-point')
Expand Down
13 changes: 11 additions & 2 deletions backend/src/gpml/handler/organisation.clj
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,8 @@

(defn- list-api-params->opts
[{:keys [geo_coverage_types is_member types tags limit ps_bookmarked
page order_by descending ps_country_iso_code_a2 ps_bookmark_sections_keys]
page order_by descending ps_country_iso_code_a2 ps_bookmark_sections_keys
badges]
:or {limit default-list-api-limit
page default-list-api-page}
:as api-params}]
Expand Down Expand Up @@ -348,7 +349,10 @@
(assoc-in [:filters :ps-bookmark-sections-keys] ps_bookmark_sections_keys)

(not (nil? ps_bookmarked))
(assoc-in [:filters :ps-bookmarked] ps_bookmarked)))
(assoc-in [:filters :ps-bookmarked] ps_bookmarked)

(not (nil? badges))
(assoc :badges badges)))

(defmethod ig/init-key :gpml.handler.organisation/list
[_ {:keys [db logger] :as config}]
Expand Down Expand Up @@ -471,4 +475,9 @@
Besides, bookmarking is related to `ps_country_iso_code_a2` param."
:type "boolean"
:allowEmptyValue false}}
[:boolean]]
[:badges {:optional true
:swagger {:description "Boolean flag to load badges-related metadata"
:type "boolean"
:allowEmptyValue false}}
[:boolean]]])

0 comments on commit 28c257e

Please sign in to comment.