Skip to content

Commit

Permalink
Instrument submission API to build image URLs
Browse files Browse the repository at this point in the history
[Re #1543]
  • Loading branch information
lucassousaf committed Aug 9, 2023
1 parent 2e74387 commit 703abc8
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 42 deletions.
58 changes: 38 additions & 20 deletions backend/src/gpml/db/submission.sql
Original file line number Diff line number Diff line change
Expand Up @@ -2,49 +2,67 @@
-- :doc Get paginated submission contents
WITH
submission AS (
SELECT s.id, 'stakeholder' AS type, 'stakeholder' AS topic, CONCAT(s.title, '. ', s.last_name,' ', s.first_name) as title, s.id as created_by, s.created, s.role, s.review_status, s.picture as image, NULL::boolean as featured
SELECT s.id, 'stakeholder' AS type, 'stakeholder' AS topic, CONCAT(s.title, '. ', s.last_name,' ', s.first_name) as title, s.id as created_by, s.created, s.role, s.review_status, jsonb_build_object('id', f.id, 'object-key', f.object_key, 'visibility', f.visibility) as image, NULL::boolean as featured
FROM stakeholder s
LEFT JOIN file f ON s.picture_id = f.id
--~ (when (= "experts" (:only params)) " JOIN stakeholder_tag st ON s.id = st.stakeholder AND st.tag_relation_category = 'expertise' ")
--~ (when (:review_status params) " WHERE review_status = :review_status::review_status ")
GROUP BY s.id, f.id
UNION
SELECT id, 'organisation' AS type, 'organisation' AS topic, name as title, id as created_by, created, 'USER' as role, review_status, logo as image, NULL::boolean as featured
FROM organisation
SELECT o.id, 'organisation' AS type, 'organisation' AS topic, o.name as title, o.id as created_by, o.created, 'USER' as role, o.review_status, jsonb_build_object('id', f.id, 'object-key', f.object_key, 'visibility', f.visibility) as image, NULL::boolean as featured
FROM organisation o
LEFT JOIN file f ON o.logo_id = f.id
WHERE is_member = true
--~ (when (:review_status params) " AND review_status = :review_status::review_status ")
GROUP BY o.id, f.id
UNION
SELECT id, 'organisation' AS type, 'non_member_organisation' AS topic, name as title, id as created_by, created, 'USER' as role, review_status, logo as image, NULL::boolean as featured
FROM organisation
SELECT o.id, 'organisation' AS type, 'non_member_organisation' AS topic, o.name as title, o.id as created_by, o.created, 'USER' as role, o.review_status, jsonb_build_object('id', f.id, 'object-key', f.object_key, 'visibility', f.visibility) as image, NULL::boolean as featured
FROM organisation o
LEFT JOIN file f ON o.logo_id = f.id
WHERE is_member = false
--~ (when (:review_status params) " AND review_status = :review_status::review_status ")
GROUP BY o.id, f.id
UNION
SELECT id, 'tag' AS type, 'tag' AS topic, tag as title, NULL as created_by, NULL as created, NULL as role, review_status, NULL as image, NULL::boolean as featured
FROM tag
--~ (when (:review_status params) " WHERE review_status = :review_status::review_status ")
UNION
SELECT id, 'event' AS type, 'event' AS topic, title, created_by, created, 'USER' as role, review_status, image, featured
FROM event
SELECT e.id, 'event' AS type, 'event' AS topic, e.title, e.created_by, e.created, 'USER' as role, e.review_status, jsonb_build_object('id', f.id, 'object-key', f.object_key, 'visibility', f.visibility) as image, e.featured
FROM event e
LEFT JOIN file f ON e.image_id = f.id
--~ (when (:review_status params) " WHERE review_status = :review_status::review_status ")
GROUP BY e.id, f.id
UNION
SELECT id, 'technology' AS type, 'technology' AS topic, name as title, created_by, created, 'USER' as role, review_status, image, featured
FROM technology
SELECT t.id, 'technology' AS type, 'technology' AS topic, t.name as title, t.created_by, t.created, 'USER' as role, t.review_status, jsonb_build_object('id', f.id, 'object-key', f.object_key, 'visibility', f.visibility) as image, t.featured
FROM technology t
LEFT JOIN file f ON t.image_id = f.id
--~ (when (:review_status params) " WHERE review_status = :review_status::review_status ")
GROUP BY t.id, f.id
UNION
SELECT id, 'policy' AS type, 'policy' AS topic, title, created_by, created, 'USER' as role, review_status, image as picture, featured
FROM policy
SELECT p.id, 'policy' AS type, 'policy' AS topic, p.title, p.created_by, p.created, 'USER' as role, p.review_status, jsonb_build_object('id', f.id, 'object-key', f.object_key, 'visibility', f.visibility) as image, p.featured
FROM policy p
LEFT JOIN file f ON p.image_id = f.id
--~ (when (:review_status params) " WHERE review_status = :review_status::review_status ")
GROUP BY p.id, f.id
UNION
SELECT id, REPLACE(LOWER(type), ' ', '_') AS type, 'resource' AS topic, title, created_by, created, 'USER' as role, review_status, image, featured
FROM resource
SELECT r.id, REPLACE(LOWER(r.type), ' ', '_') AS type, 'resource' AS topic, r.title, r.created_by, r.created, 'USER' as role, r.review_status, jsonb_build_object('id', f.id, 'object-key', f.object_key, 'visibility', f.visibility) AS image, r.featured
FROM resource r
LEFT JOIN file f ON r.image_id = f.id
--~ (when (:review_status params) " WHERE review_status = :review_status::review_status ")
GROUP BY r.id, f.id
UNION
SELECT id, 'initiative' AS type, 'initiative' AS topic, replace(q2::text,'"','') as title, created_by, created, 'USER' as role, review_status, '' as image, featured
FROM initiative
SELECT i.id, 'initiative' AS type, 'initiative' AS topic, replace(i.q2::text,'"','') as title, i.created_by, i.created, 'USER' as role, i.review_status, jsonb_build_object('id', f.id, 'object-key', f.object_key, 'visibility', f.visibility) AS image, i.featured
FROM initiative i
LEFT JOIN file f ON i.image_id = f.id
--~ (when (:review_status params) " WHERE review_status = :review_status::review_status ")
GROUP BY i.id, f.id
UNION
SELECT id, 'case_study' AS type, 'case_study' AS topic, title, created_by, created, 'USER' as role, review_status, image, featured
FROM case_study
SELECT cs.id, 'case_study' AS type, 'case_study' AS topic, cs.title, cs.created_by, cs.created, 'USER' as role, cs.review_status, jsonb_build_object('id', f.id, 'object-key', f.object_key, 'visibility', f.visibility) AS image, cs.featured
FROM case_study cs
LEFT JOIN file f ON cs.image_id = f.id
--~ (when (:review_status params) " WHERE review_status = :review_status::review_status ")
order by created
GROUP BY cs.id, f.id
ORDER BY created
),
authz AS (
SELECT s.id, 'stakeholder' as type, '[]'::jsonb AS owners, '[]'::jsonb AS focal_points
Expand Down Expand Up @@ -183,8 +201,8 @@ SELECT json_build_object(
-- :name detail :query :one
-- :doc get detail of submission
SELECT *
from :i:table-name
where id = :id::integer;
FROM :i:table-name
WHERE id = :id::integer;

-- :name update-submission :execute :affected
-- :doc approve or reject submission
Expand Down
91 changes: 69 additions & 22 deletions backend/src/gpml/handler/submission.clj
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,19 @@
[gpml.db.resource.tag :as db.resource.tag]
[gpml.db.stakeholder :as db.stakeholder]
[gpml.db.submission :as db.submission]
[gpml.domain.file :as dom.file]
[gpml.handler.resource.permission :as h.r.permission]
[gpml.handler.responses :as r]
[gpml.handler.stakeholder.tag :as handler.stakeholder.tag]
[gpml.handler.util :as handler.util]
[gpml.service.file :as srv.file]
[gpml.service.permissions :as srv.permissions]
[gpml.util.auth0 :as auth0]
[gpml.util.email :as email]
[gpml.util.postgresql :as pg-util]
[integrant.core :as ig]
[malli.core :as m]
[malli.transform :as mt]
[ring.util.response :as resp])
(:import [java.sql SQLException]))

Expand Down Expand Up @@ -59,11 +63,29 @@
d)) data)]
data))

(defn- submission-detail [conn params]
(let [data (db.submission/detail conn params)
(defn- get-image-key
[entity-key]
(case entity-key
:stakeholder :picture_id
:organisation :logo_id
:image_id))

(defn- submission-detail
[config conn params]
(let [entity-key (keyword (:table-name params))
data (db.submission/detail conn params)
creator-id (:created_by data)
creator (db.stakeholder/stakeholder-by-id conn {:id creator-id})]
creator (db.stakeholder/stakeholder-by-id conn {:id creator-id})
image-id (get data (get-image-key entity-key))
thumbnail-id (when-not (get #{:organisation :stakeholder} entity-key)
(:thumbnail_id data))
image-file (when image-id
(:file (srv.file/get-file config conn {:filters {:id image-id}})))
thumbnail-file (when thumbnail-id
(:file (srv.file/get-file config conn {:filters {:id thumbnail-id}})))]
(assoc data
:image (:url image-file)
:thumbnail (:url thumbnail-file)
:created_by_email (:email creator)
:creator creator)))

Expand All @@ -79,18 +101,40 @@
(merge item (handler.stakeholder.tag/unwrap-tags (assoc item :tags tags))))))
submission-data))

(defn- add-images-urls
[config submissions]
(map
(fn [submission]
(if-not (get-in submission [:image :id])
(assoc submission :image nil)
(let [image-file (m/decode dom.file/file-schema (:image submission) mt/string-transformer)
{:keys [url]} (srv.file/get-file-url config image-file)]
(assoc submission :image url))))
submissions))

(defmethod ig/init-key :gpml.handler.submission/get
[_ {:keys [db auth0] :as config}]
[_ {:keys [db auth0 logger] :as config}]
(fn [{:keys [user] {:keys [query]} :parameters}]
(if-not (h.r.permission/super-admin? config (:id user))
(r/forbidden {:message "Unauthorized"})
(let [submission (-> (db.submission/pages (:spec db) query) :result)
profiles (filter #(= "profile" (:type %)) (:data submission))
submission (-> (if (not-empty profiles)
(assoc submission :data (pending-profiles-response (:data submission) auth0))
submission)
(update :data #(add-stakeholder-tags (:spec db) %)))]
(resp/response submission)))))
(try
(if-not (h.r.permission/super-admin? config (:id user))
(r/forbidden {:message "Unauthorized"})
(let [submission (-> (db.submission/pages (:spec db) query) :result)
profiles (filter #(= "profile" (:type %)) (:data submission))
submission (-> (if (not-empty profiles)
(assoc submission :data (pending-profiles-response (:data submission) auth0))
submission)
(update :data #(->> %
(add-stakeholder-tags (:spec db))
(add-images-urls config))))]
(resp/response submission)))
(catch Throwable t
(let [log-data {:exception-message (ex-message t)
:exception-data (ex-data t)
:context-data query}]
(log logger :error :failed-to-update-submission log-data)
(log logger :debug :failed-to-update-submission (assoc log-data :stack-trace (.getStackTrace t)))
(r/server-error {:success? false
:reason :failed-to-update-submission}))))))

(defn- handle-stakeholder-role-change
[config stakeholder-id review-status]
Expand Down Expand Up @@ -137,7 +181,7 @@
(throw (ex-info "Failed to update submission" {:reason :update-unexpected-number-of-affected-records
:expected 1
:actual affected}))
(let [resource-details (submission-detail tx submission)]
(let [resource-details (submission-detail config tx submission)]
(when (= resource-type "stakeholder")
(handle-stakeholder-role-change {:conn tx
:logger logger}
Expand All @@ -148,12 +192,15 @@
:message "Successfuly Updated"
:data resource-details}))))))
(catch Throwable t
(log logger :error :failed-to-update-submission {:exception-message (ex-message t)
:exception-data (ex-data t)
:context-data body-params
:stack-trace (map str (.getStackTrace t))})
(r/server-error {:success? false
:reason :failed-to-update-submission})))))
(let [log-data {:exception-message (ex-message t)
:exception-data (ex-data t)
:context-data body-params}]
(log logger :error :failed-to-update-submission {:exception-message (ex-message t)
:exception-data (ex-data t)
:context-data body-params})
(log logger :debug :failed-to-update-submission (assoc log-data :stack-trace (.getStackTrace t)))
(r/server-error {:success? false
:reason :failed-to-update-submission}))))))

(defn- add-stakeholder-extra-details
[conn stakeholder]
Expand All @@ -173,13 +220,13 @@
(assoc :email email))))

(defn- get-detail
[{:keys [db]} params]
[{:keys [db] :as config} params]
(let [conn (:spec db)
submission (:submission params)
initiative? (= submission "initiative")
table-name (handler.util/get-internal-topic-type submission)
params (conj params {:table-name table-name})
detail (submission-detail conn params)
detail (submission-detail config conn params)
detail (if (= submission "stakeholder")
(add-stakeholder-extra-details conn detail)
detail)
Expand Down

0 comments on commit 703abc8

Please # to comment.