Rename sprops to setup module.

This commit is contained in:
Andrey Antukh 2021-02-23 17:10:57 +01:00 committed by Andrés Moya
parent 5b200fd6a2
commit 82d7a0163d
6 changed files with 38 additions and 42 deletions

View file

@ -81,7 +81,7 @@
{:id id {:id id
:fullname (str "Profile " index) :fullname (str "Profile " index)
:password "123123" :password "123123"
:demo? true :is-demo true
:email (str "profile" index "@example.com")}) :email (str "profile" index "@example.com")})
team-id (:default-team-id prof) team-id (:default-team-id prof)
owner-id id] owner-id id]

View file

@ -61,7 +61,7 @@
{:uri (:redis-uri config)} {:uri (:redis-uri config)}
:app.tokens/tokens :app.tokens/tokens
{:sprops (ig/ref :app.sprops/props)} {:sprops (ig/ref :app.setup/props)}
:app.storage/gc-deleted-task :app.storage/gc-deleted-task
{:pool (ig/ref :app.db/pool) {:pool (ig/ref :app.db/pool)
@ -295,13 +295,13 @@
{:pool (ig/ref :app.db/pool) {:pool (ig/ref :app.db/pool)
:version (:full cfg/version) :version (:full cfg/version)
:uri (:telemetry-uri config) :uri (:telemetry-uri config)
:sprops (ig/ref :app.sprops/props)} :sprops (ig/ref :app.setup/props)}
:app.srepl/server :app.srepl/server
{:port (:srepl-port config) {:port (:srepl-port config)
:host (:srepl-host config)} :host (:srepl-host config)}
:app.sprops/props :app.setup/props
{:pool (ig/ref :app.db/pool)} {:pool (ig/ref :app.db/pool)}
:app.loggers.zmq/receiver :app.loggers.zmq/receiver

View file

@ -36,7 +36,7 @@
params {:id id params {:id id
:email email :email email
:fullname fullname :fullname fullname
:demo? true :is-demo true
:password password :password password
:props {:onboarding-viewed true}}] :props {:onboarding-viewed true}}]

View file

@ -163,28 +163,26 @@
{:update false {:update false
:valid false}))) :valid false})))
(defn- create-profile (defn create-profile
"Create the profile entry on the database with limited input "Create the profile entry on the database with limited input
filling all the other fields with defaults." filling all the other fields with defaults."
[conn {:keys [id fullname email password demo? props is-active is-muted] [conn {:keys [id fullname email password props is-active is-muted is-demo opts]
:or {is-active false is-muted false} :or {is-active false is-muted false is-demo false}}]
:as params}] (let [id (or id (uuid/next))
(let [id (or id (uuid/next)) is-active (if is-demo true is-active)
demo? (if (boolean? demo?) demo? false) props (db/tjson (or props {}))
active? (if demo? true is-active) password (derive-password password)
props (db/tjson (or props {})) params {:id id
password (derive-password password)] :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 (try
(-> (db/insert! conn :profile (-> (db/insert! conn :profile params opts)
{: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?})
(update :props db/decode-transit-pgobject)) (update :props db/decode-transit-pgobject))
(catch org.postgresql.util.PSQLException e (catch org.postgresql.util.PSQLException e
(let [state (.getSQLState e)] (let [state (.getSQLState e)]
@ -195,7 +193,7 @@
:cause e))))))) :cause e)))))))
(defn- create-profile-relations (defn create-profile-relations
[conn profile] [conn profile]
(let [team (teams/create-team conn {:profile-id (:id profile) (let [team (teams/create-team conn {:profile-id (:id profile)
:name "Default" :name "Default"

View file

@ -7,8 +7,8 @@
;; ;;
;; Copyright (c) 2020-2021 UXBOX Labs SL ;; Copyright (c) 2020-2021 UXBOX Labs SL
(ns app.sprops (ns app.setup
"Server props module." "Initial data setup of instance."
(:require (:require
[app.common.uuid :as uuid] [app.common.uuid :as uuid]
[app.db :as db] [app.db :as db]
@ -17,16 +17,22 @@
[clojure.spec.alpha :as s] [clojure.spec.alpha :as s]
[integrant.core :as ig])) [integrant.core :as ig]))
(declare initialize) (declare initialize-instance-id!)
(declare initialize-secret-key!)
(declare retrieve-all)
(defmethod ig/pre-init-spec ::props [_] (defmethod ig/pre-init-spec ::props [_]
(s/keys :req-un [::db/pool])) (s/keys :req-un [::db/pool]))
(defmethod ig/init-key ::props (defmethod ig/init-key ::props
[_ cfg] [_ {:keys [pool] :as cfg}]
(initialize 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}] [{:keys [conn] :as cfg}]
(let [key (-> (bn/random-bytes 64) (let [key (-> (bn/random-bytes 64)
(bc/bytes->b64u) (bc/bytes->b64u)
@ -35,7 +41,7 @@
values ('secret-key', ?) on conflict do nothing" values ('secret-key', ?) on conflict do nothing"
(db/tjson key)]))) (db/tjson key)])))
(defn- initialize-instance-id (defn- initialize-instance-id!
[{:keys [conn] :as cfg}] [{:keys [conn] :as cfg}]
(let [iid (uuid/random)] (let [iid (uuid/random)]
(db/exec-one! conn ["insert into server_prop (id, content) (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)))) (assoc acc (keyword (:id row)) (db/decode-transit-pgobject (:content row))))
{} {}
(db/exec! conn ["select * from server_prop;"]))) (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))))

View file

@ -115,7 +115,7 @@
:fullname (str "Profile " i) :fullname (str "Profile " i)
:email (str "profile" i ".test@nodomain.com") :email (str "profile" i ".test@nodomain.com")
:password "123123" :password "123123"
:demo? true}] :is-demo true}]
(->> (#'profile/create-profile conn params) (->> (#'profile/create-profile conn params)
(#'profile/create-profile-relations conn)))) (#'profile/create-profile-relations conn))))
@ -159,7 +159,7 @@
:fullname (str "Profile " i) :fullname (str "Profile " i)
:email (str "profile" i ".test@nodomain.com") :email (str "profile" i ".test@nodomain.com")
:password "123123" :password "123123"
:demo? false} :is-demo false}
params)] params)]
(->> (#'profile/create-profile conn params) (->> (#'profile/create-profile conn params)
(#'profile/create-profile-relations conn))))) (#'profile/create-profile-relations conn)))))