diff --git a/backend/src/app/rpc/commands/binfile.clj b/backend/src/app/rpc/commands/binfile.clj index 36c51b0e6..c7bdf54df 100644 --- a/backend/src/app/rpc/commands/binfile.clj +++ b/backend/src/app/rpc/commands/binfile.clj @@ -536,7 +536,7 @@ :or {overwrite? false migrate? false timestamp (dt/now)} :as options}] - (us/assert! ::read-import-options options) + (us/verify! ::read-import-options options) (letfn [(lookup-index [id] (if ignore-index-errors? @@ -752,7 +752,11 @@ (case section :v1/rels (read-rels-section! input) :v1/files (read-files-section! input files) - :v1/sobjects (read-sobjects-section! input)))))))))) + :v1/sobjects (read-sobjects-section! input))) + + ;; Knowing that the ids of the created files are in + ;; index, just lookup them and return it as a set + (into #{} (keep #(get @*index* %)) files)))))))) (defn export! [cfg] diff --git a/backend/src/app/rpc/commands/management.clj b/backend/src/app/rpc/commands/management.clj index a78bfe310..25cccad76 100644 --- a/backend/src/app/rpc/commands/management.clj +++ b/backend/src/app/rpc/commands/management.clj @@ -13,6 +13,7 @@ [app.common.spec :as us] [app.common.uuid :as uuid] [app.db :as db] + [app.rpc.commands.binfile :as binfile] [app.rpc.doc :as-alias doc] [app.rpc.mutations.projects :refer [create-project-role create-project]] [app.rpc.queries.projects :as proj] @@ -204,7 +205,9 @@ :opt-un [::name])) (sv/defmethod ::duplicate-project - [{:keys [pool] :as cfg} {:keys [profile-id project-id] :as params}] + "Duplicate an entire project with all the files" + {::doc/added "1.16"} + [{:keys [pool] :as cfg} params] (db/with-atomic [conn pool] (duplicate-project conn params))) @@ -310,6 +313,8 @@ (s/keys :req-un [::profile-id ::ids ::project-id])) (sv/defmethod ::move-files + "Move a set of files from one project to other." + {::doc/added "1.16"} [{:keys [pool] :as cfg} params] (db/with-atomic [conn pool] (move-files conn params))) @@ -347,6 +352,43 @@ (s/keys :req-un [::profile-id ::team-id ::project-id])) (sv/defmethod ::move-project + "Move projects between teams." + {::doc/added "1.16"} [{:keys [pool] :as cfg} params] (db/with-atomic [conn pool] (move-project conn params))) + +;; --- COMMAND: Clone Template + +(declare clone-template) + +(s/def ::template-id ::us/not-empty-string) +(s/def ::clone-template + (s/keys :req-un [::profile-id ::project-id ::template-id])) + +(sv/defmethod ::clone-template + "Clone into the specified project the template by its id." + {::doc/added "1.16"} + [{:keys [pool] :as cfg} params] + (db/with-atomic [conn pool] + (-> (assoc cfg :conn conn) + (clone-template params)))) + +(defn- clone-template + [{:keys [conn templates] :as cfg} {:keys [profile-id template-id project-id]}] + (let [template (d/seek #(= (:id %) template-id) templates) + project (db/get-by-id conn :project project-id {:columns [:id :team-id]})] + + (teams/check-edition-permissions! conn profile-id (:team-id project)) + + (when-not template + (ex/raise :type :not-found + :code :template-not-found + :hint "template not found")) + + (-> cfg + (assoc ::binfile/input (:path template)) + (assoc ::binfile/project-id (:id project)) + (assoc ::binfile/ignore-index-errors? true) + (assoc ::binfile/migrate? true) + (binfile/import!)))) diff --git a/backend/src/app/rpc/mutations/management.clj b/backend/src/app/rpc/mutations/management.clj index c37d3a645..c2038868a 100644 --- a/backend/src/app/rpc/mutations/management.clj +++ b/backend/src/app/rpc/mutations/management.clj @@ -31,7 +31,7 @@ (sv/defmethod ::duplicate-project {::doc/added "1.2" ::doc/deprecated "1.16"} - [{:keys [pool] :as cfg} {:keys [profile-id project-id] :as params}] + [{:keys [pool] :as cfg} params] (db/with-atomic [conn pool] (cmd.mgm/duplicate-project conn params))) diff --git a/backend/test/app/services_management_test.clj b/backend/test/app/services_management_test.clj index bcfa277f9..2724ee1db 100644 --- a/backend/test/app/services_management_test.clj +++ b/backend/test/app/services_management_test.clj @@ -604,3 +604,19 @@ (t/is (= (:library-file-id item1) (:id file2)))) ))) + +(t/deftest clone-template + (let [prof (th/create-profile* 1 {:is-active true}) + data {::th/type :clone-template + :profile-id (:id prof) + :project-id (:default-project-id prof) + :template-id "test"} + + out (th/command! data)] + ;; (th/print-result! out) + + (t/is (nil? (:error out))) + (let [result (:result out)] + (t/is (set? result)) + (t/is (uuid? (first result))) + (t/is (= 1 (count result)))))) diff --git a/backend/test/app/test_files/template.penpot b/backend/test/app/test_files/template.penpot new file mode 100644 index 000000000..1375c6d52 Binary files /dev/null and b/backend/test/app/test_files/template.penpot differ diff --git a/backend/test/app/test_helpers.clj b/backend/test/app/test_helpers.clj index 8f436ea02..13c5f0fd0 100644 --- a/backend/test/app/test_helpers.clj +++ b/backend/test/app/test_helpers.clj @@ -50,11 +50,18 @@ (defn state-init [next] - (let [config (-> main/system-config + (let [templates [{:id "test" + :name "test" + :file-uri "test" + :thumbnail-uri "test" + :path (-> "app/test_files/template.penpot" io/resource fs/path)}] + + config (-> main/system-config (assoc-in [:app.msgbus/msgbus :redis-uri] (:redis-uri config)) (assoc-in [:app.db/pool :uri] (:database-uri config)) (assoc-in [:app.db/pool :username] (:database-username config)) (assoc-in [:app.db/pool :password] (:database-password config)) + (assoc-in [:app.rpc/methods :templates] templates) (dissoc :app.srepl/server :app.http/server :app.http/router @@ -64,6 +71,7 @@ :app.auth.oidc/gitlab-provider :app.auth.oidc/github-provider :app.auth.oidc/generic-provider + :app.setup/builtin-templates :app.auth.oidc/routes ;; :app.auth.ldap/provider :app.worker/executors-monitor