Skip to content
Snippets Groups Projects
Unverified Commit 65344a05 authored by Cam Saul's avatar Cam Saul
Browse files

Include Root Collection in GET /api/collection response

parent 9c201c10
No related merge requests found
...@@ -20,6 +20,8 @@ ...@@ -20,6 +20,8 @@
[db :as db] [db :as db]
[hydrate :refer [hydrate]]])) [hydrate :refer [hydrate]]]))
(declare root-collection)
(api/defendpoint GET "/" (api/defendpoint GET "/"
"Fetch a list of all Collections that the current user has read permissions for (`:can_write` is returned as an "Fetch a list of all Collections that the current user has read permissions for (`:can_write` is returned as an
additional property of each Collection so you can tell which of these you have write permissions for.) additional property of each Collection so you can tell which of these you have write permissions for.)
...@@ -28,10 +30,18 @@ ...@@ -28,10 +30,18 @@
`?archived=true`." `?archived=true`."
[archived] [archived]
{archived (s/maybe su/BooleanString)} {archived (s/maybe su/BooleanString)}
(as-> (db/select Collection :archived (Boolean/parseBoolean archived) (let [archived? (Boolean/parseBoolean archived)]
{:order-by [[:%lower.name :asc]]}) collections (as-> (db/select Collection :archived archived?
(filter mi/can-read? collections) {:order-by [[:%lower.name :asc]]}) collections
(hydrate collections :can_write))) (filter mi/can-read? collections)
;; include Root Collection at beginning or results if archived isn't `true`
(if archived?
collections
(cons (root-collection) collections))
(hydrate collections :can_write)
;; remove the :metabase.models.collection/is-root? tag since FE doesn't need it
(for [collection collections]
(dissoc collection ::collection/is-root?)))))
;;; --------------------------------- Fetching a single Collection & its 'children' ---------------------------------- ;;; --------------------------------- Fetching a single Collection & its 'children' ----------------------------------
...@@ -121,18 +131,20 @@ ...@@ -121,18 +131,20 @@
{:model (keyword model) {:model (keyword model)
:archived? (Boolean/parseBoolean archived)})) :archived? (Boolean/parseBoolean archived)}))
;;; -------------------------------------------- GET /api/collection/root -------------------------------------------- ;;; -------------------------------------------- GET /api/collection/root --------------------------------------------
(defn- root-collection []
;; add in some things for the FE to display since the 'Root' Collection isn't real and wouldn't normally have
;; these things
(assoc (collection-detail collection/root-collection)
:name (tru "Our analytics")
:id "root"))
(api/defendpoint GET "/root" (api/defendpoint GET "/root"
"Return the 'Root' Collection object with standard details added" "Return the 'Root' Collection object with standard details added"
[] []
(-> (collection-detail collection/root-collection) (dissoc (root-collection) ::collection/is-root?))
;; add in some things for the FE to display since the 'Root' Collection isn't real and wouldn't normally have
;; these things
(assoc
:name (tru "Our analytics")
:id "root")
(dissoc ::collection/is-root?)))
(api/defendpoint GET "/root/items" (api/defendpoint GET "/root/items"
"Fetch objects that the current user should see at their root level. As mentioned elsewhere, the 'Root' Collection "Fetch objects that the current user should see at their root level. As mentioned elsewhere, the 'Root' Collection
......
...@@ -28,14 +28,16 @@ ...@@ -28,14 +28,16 @@
;; check that we can get a basic list of collections ;; check that we can get a basic list of collections
;; (for the purposes of test purposes remove the personal collections) ;; (for the purposes of test purposes remove the personal collections)
(tt/expect-with-temp [Collection [collection]] (tt/expect-with-temp [Collection [collection]]
[(assoc (into {} collection) :can_write true)] [{:parent_id nil, :effective_location nil, :effective_ancestors (), :can_write true, :name "Our analytics", :id "root"}
(assoc (into {} collection) :can_write true)]
(for [collection ((user->client :crowberto) :get 200 "collection") (for [collection ((user->client :crowberto) :get 200 "collection")
:when (not (:personal_owner_id collection))] :when (not (:personal_owner_id collection))]
collection)) collection))
;; We should only see our own Personal Collections! ;; We should only see our own Personal Collections!
(expect (expect
["Lucky Pigeon's Personal Collection"] ["Our analytics"
"Lucky Pigeon's Personal Collection"]
(do (do
(collection-test/force-create-personal-collections!) (collection-test/force-create-personal-collections!)
;; now fetch those Collections as the Lucky bird ;; now fetch those Collections as the Lucky bird
...@@ -43,7 +45,8 @@ ...@@ -43,7 +45,8 @@
;; ...unless we are *admins* ;; ...unless we are *admins*
(expect (expect
["Crowberto Corv's Personal Collection" ["Our analytics"
"Crowberto Corv's Personal Collection"
"Lucky Pigeon's Personal Collection" "Lucky Pigeon's Personal Collection"
"Rasta Toucan's Personal Collection" "Rasta Toucan's Personal Collection"
"Trash Bird's Personal Collection"] "Trash Bird's Personal Collection"]
...@@ -54,7 +57,8 @@ ...@@ -54,7 +57,8 @@
;; check that we don't see collections if we don't have permissions for them ;; check that we don't see collections if we don't have permissions for them
(expect (expect
["Collection 1" ["Our analytics"
"Collection 1"
"Rasta Toucan's Personal Collection"] "Rasta Toucan's Personal Collection"]
(tt/with-temp* [Collection [collection-1 {:name "Collection 1"}] (tt/with-temp* [Collection [collection-1 {:name "Collection 1"}]
Collection [collection-2 {:name "Collection 2"}]] Collection [collection-2 {:name "Collection 2"}]]
...@@ -64,7 +68,8 @@ ...@@ -64,7 +68,8 @@
;; check that we don't see collections if they're archived ;; check that we don't see collections if they're archived
(expect (expect
["Rasta Toucan's Personal Collection" ["Our analytics"
"Rasta Toucan's Personal Collection"
"Regular Collection"] "Regular Collection"]
(tt/with-temp* [Collection [collection-1 {:name "Archived Collection", :archived true}] (tt/with-temp* [Collection [collection-1 {:name "Archived Collection", :archived true}]
Collection [collection-2 {:name "Regular Collection"}]] Collection [collection-2 {:name "Regular Collection"}]]
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment