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 @@
[db :as db]
[hydrate :refer [hydrate]]]))
(declare root-collection)
(api/defendpoint GET "/"
"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.)
......@@ -28,10 +30,18 @@
`?archived=true`."
[archived]
{archived (s/maybe su/BooleanString)}
(as-> (db/select Collection :archived (Boolean/parseBoolean archived)
{:order-by [[:%lower.name :asc]]}) collections
(filter mi/can-read? collections)
(hydrate collections :can_write)))
(let [archived? (Boolean/parseBoolean archived)]
(as-> (db/select Collection :archived archived?
{:order-by [[:%lower.name :asc]]}) collections
(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' ----------------------------------
......@@ -121,18 +131,20 @@
{:model (keyword model)
:archived? (Boolean/parseBoolean archived)}))
;;; -------------------------------------------- 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"
"Return the 'Root' Collection object with standard details added"
[]
(-> (collection-detail collection/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
:name (tru "Our analytics")
:id "root")
(dissoc ::collection/is-root?)))
(dissoc (root-collection) ::collection/is-root?))
(api/defendpoint GET "/root/items"
"Fetch objects that the current user should see at their root level. As mentioned elsewhere, the 'Root' Collection
......
......@@ -28,14 +28,16 @@
;; check that we can get a basic list of collections
;; (for the purposes of test purposes remove the personal collections)
(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")
:when (not (:personal_owner_id collection))]
collection))
;; We should only see our own Personal Collections!
(expect
["Lucky Pigeon's Personal Collection"]
["Our analytics"
"Lucky Pigeon's Personal Collection"]
(do
(collection-test/force-create-personal-collections!)
;; now fetch those Collections as the Lucky bird
......@@ -43,7 +45,8 @@
;; ...unless we are *admins*
(expect
["Crowberto Corv's Personal Collection"
["Our analytics"
"Crowberto Corv's Personal Collection"
"Lucky Pigeon's Personal Collection"
"Rasta Toucan's Personal Collection"
"Trash Bird's Personal Collection"]
......@@ -54,7 +57,8 @@
;; check that we don't see collections if we don't have permissions for them
(expect
["Collection 1"
["Our analytics"
"Collection 1"
"Rasta Toucan's Personal Collection"]
(tt/with-temp* [Collection [collection-1 {:name "Collection 1"}]
Collection [collection-2 {:name "Collection 2"}]]
......@@ -64,7 +68,8 @@
;; check that we don't see collections if they're archived
(expect
["Rasta Toucan's Personal Collection"
["Our analytics"
"Rasta Toucan's Personal Collection"
"Regular Collection"]
(tt/with-temp* [Collection [collection-1 {:name "Archived Collection", :archived true}]
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