From 8da153f604da33461d143cf18be18153866f160a Mon Sep 17 00:00:00 2001 From: Andrey Antukh Date: Wed, 19 Jun 2024 14:58:04 +0200 Subject: [PATCH] :sparkles: Emit create-file action events on clone-template --- backend/src/app/rpc/commands/management.clj | 33 +++++++++++++++------ 1 file changed, 24 insertions(+), 9 deletions(-) diff --git a/backend/src/app/rpc/commands/management.clj b/backend/src/app/rpc/commands/management.clj index 5d01d9ec6..ef36b85d5 100644 --- a/backend/src/app/rpc/commands/management.clj +++ b/backend/src/app/rpc/commands/management.clj @@ -16,6 +16,7 @@ [app.config :as cf] [app.db :as db] [app.http.sse :as sse] + [app.loggers.audit :as audit] [app.loggers.webhooks :as-alias webhooks] [app.rpc :as-alias rpc] [app.rpc.commands.files :as files] @@ -397,17 +398,33 @@ ;; --- COMMAND: Clone Template (defn- clone-template - [{:keys [::wrk/executor ::bf.v1/project-id] :as cfg} template] - (db/tx-run! cfg (fn [{:keys [::db/conn] :as cfg}] + [cfg {:keys [project-id ::rpc/profile-id ::rpc/external-session-id] :as params} template] + (db/tx-run! cfg (fn [{:keys [::db/conn ::wrk/executor] :as cfg}] ;; NOTE: the importation process performs some operations that ;; are not very friendly with virtual threads, and for avoid ;; unexpected blocking of other concurrent operations we ;; dispatch that operation to a dedicated executor. - (let [result (px/submit! executor (partial bf.v1/import-files! cfg template))] + (let [cfg (-> cfg + (assoc ::bf.v1/project-id project-id) + (assoc ::bf.v1/profile-id profile-id)) + result (px/invoke! executor (partial bf.v1/import-files! cfg template))] + (db/update! conn :project {:modified-at (dt/now)} {:id project-id}) - (deref result))))) + + (let [props (-> (audit/clean-props params) + (assoc :triggered-by "clone-template")) + context {:external-session-id external-session-id}] + (doseq [file-id result] + (audit/submit! cfg + {::audit/type "action" + ::audit/name "create-file" + ::audit/profile-id profile-id + ::audit/props (assoc props :id file-id) + ::audit/context context}))) + + result)))) (def ^:private schema:clone-template @@ -425,16 +442,14 @@ [{:keys [::db/pool] :as cfg} {:keys [::rpc/profile-id project-id template-id] :as params}] (let [project (db/get-by-id pool :project project-id {:columns [:id :team-id]}) _ (teams/check-edition-permissions! pool profile-id (:team-id project)) - template (tmpl/get-template-stream cfg template-id) - params (-> cfg - (assoc ::bf.v1/project-id (:id project)) - (assoc ::bf.v1/profile-id profile-id))] + template (tmpl/get-template-stream cfg template-id)] + (when-not template (ex/raise :type :not-found :code :template-not-found :hint "template not found")) - (sse/response #(clone-template params template)))) + (sse/response #(clone-template cfg params template)))) ;; --- COMMAND: Get list of builtin templates