From 82d7a0163d19c51f20acc907cefd33abc8005477 Mon Sep 17 00:00:00 2001 From: Andrey Antukh Date: Tue, 23 Feb 2021 17:10:57 +0100 Subject: [PATCH] :sparkles: Rename sprops to setup module. --- backend/src/app/cli/fixtures.clj | 2 +- backend/src/app/main.clj | 6 ++-- backend/src/app/rpc/mutations/demo.clj | 2 +- backend/src/app/rpc/mutations/profile.clj | 38 +++++++++++------------ backend/src/app/{sprops.clj => setup.clj} | 28 ++++++++--------- backend/tests/app/tests/helpers.clj | 4 +-- 6 files changed, 38 insertions(+), 42 deletions(-) rename backend/src/app/{sprops.clj => setup.clj} (83%) diff --git a/backend/src/app/cli/fixtures.clj b/backend/src/app/cli/fixtures.clj index ed986d36d..f863d970c 100644 --- a/backend/src/app/cli/fixtures.clj +++ b/backend/src/app/cli/fixtures.clj @@ -81,7 +81,7 @@ {:id id :fullname (str "Profile " index) :password "123123" - :demo? true + :is-demo true :email (str "profile" index "@example.com")}) team-id (:default-team-id prof) owner-id id] diff --git a/backend/src/app/main.clj b/backend/src/app/main.clj index f93d787f4..20d4177db 100644 --- a/backend/src/app/main.clj +++ b/backend/src/app/main.clj @@ -61,7 +61,7 @@ {:uri (:redis-uri config)} :app.tokens/tokens - {:sprops (ig/ref :app.sprops/props)} + {:sprops (ig/ref :app.setup/props)} :app.storage/gc-deleted-task {:pool (ig/ref :app.db/pool) @@ -295,13 +295,13 @@ {:pool (ig/ref :app.db/pool) :version (:full cfg/version) :uri (:telemetry-uri config) - :sprops (ig/ref :app.sprops/props)} + :sprops (ig/ref :app.setup/props)} :app.srepl/server {:port (:srepl-port config) :host (:srepl-host config)} - :app.sprops/props + :app.setup/props {:pool (ig/ref :app.db/pool)} :app.loggers.zmq/receiver diff --git a/backend/src/app/rpc/mutations/demo.clj b/backend/src/app/rpc/mutations/demo.clj index c5959f91d..b7ce34695 100644 --- a/backend/src/app/rpc/mutations/demo.clj +++ b/backend/src/app/rpc/mutations/demo.clj @@ -36,7 +36,7 @@ params {:id id :email email :fullname fullname - :demo? true + :is-demo true :password password :props {:onboarding-viewed true}}] diff --git a/backend/src/app/rpc/mutations/profile.clj b/backend/src/app/rpc/mutations/profile.clj index b54933a42..53660f315 100644 --- a/backend/src/app/rpc/mutations/profile.clj +++ b/backend/src/app/rpc/mutations/profile.clj @@ -163,28 +163,26 @@ {:update false :valid false}))) -(defn- create-profile +(defn create-profile "Create the profile entry on the database with limited input filling all the other fields with defaults." - [conn {:keys [id fullname email password demo? props is-active is-muted] - :or {is-active false is-muted false} - :as params}] - (let [id (or id (uuid/next)) - demo? (if (boolean? demo?) demo? false) - active? (if demo? true is-active) - props (db/tjson (or props {})) - password (derive-password password)] + [conn {:keys [id fullname email password props is-active is-muted is-demo opts] + :or {is-active false is-muted false is-demo false}}] + (let [id (or id (uuid/next)) + is-active (if is-demo true is-active) + props (db/tjson (or props {})) + password (derive-password password) + params {:id id + :fullname fullname + :email (str/lower email) + :auth-backend "penpot" + :password password + :props props + :is-active is-active + :is-muted is-muted + :is-demo is-demo}] (try - (-> (db/insert! conn :profile - {:id id - :fullname fullname - :email (str/lower email) - :auth-backend "penpot" - :password password - :props props - :is-active active? - :is-muted is-muted - :is-demo demo?}) + (-> (db/insert! conn :profile params opts) (update :props db/decode-transit-pgobject)) (catch org.postgresql.util.PSQLException e (let [state (.getSQLState e)] @@ -195,7 +193,7 @@ :cause e))))))) -(defn- create-profile-relations +(defn create-profile-relations [conn profile] (let [team (teams/create-team conn {:profile-id (:id profile) :name "Default" diff --git a/backend/src/app/sprops.clj b/backend/src/app/setup.clj similarity index 83% rename from backend/src/app/sprops.clj rename to backend/src/app/setup.clj index a46b7258e..3dbbab6d7 100644 --- a/backend/src/app/sprops.clj +++ b/backend/src/app/setup.clj @@ -7,8 +7,8 @@ ;; ;; Copyright (c) 2020-2021 UXBOX Labs SL -(ns app.sprops - "Server props module." +(ns app.setup + "Initial data setup of instance." (:require [app.common.uuid :as uuid] [app.db :as db] @@ -17,16 +17,22 @@ [clojure.spec.alpha :as s] [integrant.core :as ig])) -(declare initialize) +(declare initialize-instance-id!) +(declare initialize-secret-key!) +(declare retrieve-all) (defmethod ig/pre-init-spec ::props [_] (s/keys :req-un [::db/pool])) (defmethod ig/init-key ::props - [_ cfg] - (initialize cfg)) + [_ {:keys [pool] :as cfg}] + (db/with-atomic [conn pool] + (let [cfg (assoc cfg :conn conn)] + (initialize-secret-key! cfg) + (initialize-instance-id! cfg) + (retrieve-all cfg)))) -(defn- initialize-secret-key +(defn- initialize-secret-key! [{:keys [conn] :as cfg}] (let [key (-> (bn/random-bytes 64) (bc/bytes->b64u) @@ -35,7 +41,7 @@ values ('secret-key', ?) on conflict do nothing" (db/tjson key)]))) -(defn- initialize-instance-id +(defn- initialize-instance-id! [{:keys [conn] :as cfg}] (let [iid (uuid/random)] (db/exec-one! conn ["insert into server_prop (id, content) @@ -48,11 +54,3 @@ (assoc acc (keyword (:id row)) (db/decode-transit-pgobject (:content row)))) {} (db/exec! conn ["select * from server_prop;"]))) - -(defn- initialize - [{:keys [pool] :as cfg}] - (db/with-atomic [conn pool] - (let [cfg (assoc cfg :conn conn)] - (initialize-secret-key cfg) - (initialize-instance-id cfg) - (retrieve-all cfg)))) diff --git a/backend/tests/app/tests/helpers.clj b/backend/tests/app/tests/helpers.clj index 81c442e68..a11295263 100644 --- a/backend/tests/app/tests/helpers.clj +++ b/backend/tests/app/tests/helpers.clj @@ -115,7 +115,7 @@ :fullname (str "Profile " i) :email (str "profile" i ".test@nodomain.com") :password "123123" - :demo? true}] + :is-demo true}] (->> (#'profile/create-profile conn params) (#'profile/create-profile-relations conn)))) @@ -159,7 +159,7 @@ :fullname (str "Profile " i) :email (str "profile" i ".test@nodomain.com") :password "123123" - :demo? false} + :is-demo false} params)] (->> (#'profile/create-profile conn params) (#'profile/create-profile-relations conn)))))