diff --git a/backend/src/gpml/db/organisation.clj b/backend/src/gpml/db/organisation.clj index 4bbff024f..23097d1db 100644 --- a/backend/src/gpml/db/organisation.clj +++ b/backend/src/gpml/db/organisation.clj @@ -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 @@ -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") @@ -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 "")))) diff --git a/backend/src/gpml/db/organisation.sql b/backend/src/gpml/db/organisation.sql index 29668e222..82095ec76 100644 --- a/backend/src/gpml/db/organisation.sql +++ b/backend/src/gpml/db/organisation.sql @@ -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') diff --git a/backend/src/gpml/handler/organisation.clj b/backend/src/gpml/handler/organisation.clj index 67e8c7cd6..cc86567ca 100644 --- a/backend/src/gpml/handler/organisation.clj +++ b/backend/src/gpml/handler/organisation.clj @@ -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}] @@ -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}] @@ -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]]])