Skip to content

The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:

Default new document cards to the document's collection on update (#83154)

applebyecc0eb1612ea
diff --git a/src/metabase/documents/models/document.clj b/src/metabase/documents/models/document.clj
index 1d9c5538761..e0267f9a9d5 100644
--- a/src/metabase/documents/models/document.clj
+++ b/src/metabase/documents/models/document.clj
@@ -386,18 +386,22 @@
[:cards {:optional true} [:maybe [:map-of :int CardCreateSchema]]]
[:archived {:optional true} [:maybe :boolean]]]]
(let [document-id (:id existing-document)
- document-updates (dissoc (api/updates-with-archived-directly existing-document body) :cards)]
+ document-updates (dissoc (api/updates-with-archived-directly existing-document body) :cards)
+ ;; The frontend omits `:collection_id` when saving an existing document, so fall back to the document's
+ ;; current collection rather than root.
+ target-collection-id (if (contains? body :collection_id)
+ collection_id
+ (:collection_id existing-document))]
(t2/with-transaction [_conn]
(when collection_position
- (api/maybe-reconcile-collection-position! (select-keys existing-document [:collection_id :collection_position]) {:collection_id (if (contains? body :collection_id)
- collection_id
- (:collection_id existing-document))
- :collection_position collection_position}))
+ (api/maybe-reconcile-collection-position! (select-keys existing-document [:collection_id :collection_position])
+ {:collection_id target-collection-id
+ :collection_position collection_position}))
(let [card-id-map (when document
(merge
(clone-cards-in-document! (assoc existing-document :document document))
(when-not (empty? cards)
- (create-cards-for-document! cards document-id collection_id @api/*current-user*))))
+ (create-cards-for-document! cards document-id target-collection-id @api/*current-user*))))
draft-card-id-map (into {} (filter (comp neg? key) card-id-map))
pairings (draft-stored-result-pairings document
(:content_type existing-document)
diff --git a/test/metabase/documents/api/document_test.clj b/test/metabase/documents/api/document_test.clj
index 4b0b042b1e3..4ee52d9f051 100644
--- a/test/metabase/documents/api/document_test.clj
+++ b/test/metabase/documents/api/document_test.clj
@@ -2677,6 +2677,28 @@
(testing "the embedded card is cloned into the document"
(is (t2/exists? :model/Card :document_id doc-id)))))))))
+(deftest put-document-create-card-without-collection-id-in-body-test
+ (testing "PUT /api/document/:id - new cards default to the document's collection when the body omits :collection_id"
+ (mt/with-non-admin-groups-no-root-collection-perms
+ (mt/with-model-cleanup [:model/Card]
+ (mt/with-temp [:model/Collection {coll-id :id} {}
+ :model/Document {doc-id :id} {:name "My Doc"
+ :collection_id coll-id
+ :document (documents.test-util/text->prose-mirror-ast "")}]
+ (perms/grant-collection-readwrite-permissions! (perms/all-users-group) coll-id)
+ (mt/user-http-request :rasta
+ :put 200 (str "document/" doc-id)
+ {:document {:type "doc"
+ :content [{:type "cardEmbed" :attrs {:id -1 :name nil}}]}
+ :cards {-1 {:name "New Card"
+ :type :question
+ :dataset_query (mt/mbql-query venues)
+ :display :table
+ :visualization_settings {}}}})
+ (testing "the card is created in the document's collection"
+ (is (=? [{:name "New Card" :collection_id coll-id}]
+ (t2/select :model/Card :document_id doc-id)))))))))
+
(deftest copy-document-containing-native-card-without-native-perms-test
(testing "POST /api/document/:id/copy - user without native perms can copy a document containing a native card (UXW-5037)"
(mt/with-non-admin-groups-no-root-collection-perms