From e02cb2f646936221dd3ce9c999eae027c4cc9cce Mon Sep 17 00:00:00 2001 From: Bryce Covert Date: Fri, 12 Apr 2019 22:07:19 -0700 Subject: [PATCH] supports starting from scratch. --- src/clj/auto_ap/datomic/migrate.clj | 2 +- .../datomic/migrate/invoice_converter.clj | 24 +++++++++++++------ src/clj/auto_ap/datomic/users.clj | 10 ++++++-- 3 files changed, 26 insertions(+), 10 deletions(-) diff --git a/src/clj/auto_ap/datomic/migrate.clj b/src/clj/auto_ap/datomic/migrate.clj index d4492090..98270076 100644 --- a/src/clj/auto_ap/datomic/migrate.clj +++ b/src/clj/auto_ap/datomic/migrate.clj @@ -70,7 +70,7 @@ :auto-ap/add-nick-the-greek {:txes [[{:client/name "Nick the Greek" :client/code "NGAK" :client/locations ["MH"] :client/bank-accounts [{:bank-account/code "NGAK-0" :bank-account/type :bank-account-type/cash :bank-account/name "Cash"}]}]] :requires [:auto-ap/add-bank-account-codes]} :auto-ap/rename-codes-1 {:txes-fn 'auto-ap.datomic.migrate.rename-codes/rename-codes-1 :requires [:auto-ap/add-nick-the-greek]} :auto-ap/invoice-converter {:txes auto-ap.datomic.migrate.invoice-converter/add-matches :requires [:auto-ap/rename-codes-1]} - :auto-ap/starter {:txes auto-ap.datomic.migrate.invoice-converter/add-starter :requires [:auto-ap/invoice-converter]} + :auto-ap/starter {:txes-fn 'auto-ap.datomic.migrate.invoice-converter/add-starter :requires [:auto-ap/invoice-converter]} :auto-ap/add-default-location {:txes-fn 'auto-ap.datomic.migrate.invoice-converter/add-default-location :requires [:auto-ap/invoice-converter]} :auto-ap/add-default-location-2 {:txes-fn 'auto-ap.datomic.migrate.invoice-converter/add-default-location-2 :requires [:auto-ap/add-default-location]} :auto-ap/add-import-status {:txes auto-ap.datomic.migrate.invoice-converter/add-import-status :requires [:auto-ap/add-default-location-2]} diff --git a/src/clj/auto_ap/datomic/migrate/invoice_converter.clj b/src/clj/auto_ap/datomic/migrate/invoice_converter.clj index 96d5dc63..4b379669 100644 --- a/src/clj/auto_ap/datomic/migrate/invoice_converter.clj +++ b/src/clj/auto_ap/datomic/migrate/invoice_converter.clj @@ -23,11 +23,16 @@ :db/doc "The location of the location match"} ]]) -(def add-starter - [[{:db/id [:client/code "CBC"] - :client/matches ["campbell brewing company"] - :client/location-matches [{:location-match/location "CB" - :location-match/matches ["campbell brewing company"]}]}]]) +(defn add-starter [conn] + (if (seq (d/query {:query {:find '[?e] + :in ['$] + :where ['[?e :client/code "CBC"]]} + :args [(d/db conn)]})) + [[{:db/id [:client/code "CBC"] + :client/matches ["campbell brewing company"] + :client/location-matches [{:location-match/location "CB" + :location-match/matches ["campbell brewing company"]}]}]] + [[]])) (defn add-default-location [conn] [[{:db/ident :client/default-location @@ -36,8 +41,13 @@ :db/doc "The default location if one can't be found"}]]) (defn add-default-location-2 [conn] - [[{:db/id [:client/code "CBC"] - :client/default-location "CB"}]]) + (if (seq (d/query {:query {:find '[?e] + :in ['$] + :where ['[?e :client/code "CBC"]]} + :args [(d/db conn)]})) + [[{:db/id [:client/code "CBC"] + :client/default-location "CB"}]] + [[]])) (def add-import-status [[{:db/ident :invoice/import-status diff --git a/src/clj/auto_ap/datomic/users.clj b/src/clj/auto_ap/datomic/users.clj index 64a7bf80..62b450e2 100644 --- a/src/clj/auto_ap/datomic/users.clj +++ b/src/clj/auto_ap/datomic/users.clj @@ -27,7 +27,11 @@ first))) (defn find-or-insert! [{:keys [:user/provider :user/provider-id] :as new-user}] - (let [user (some-> {:query [:find '(pull ?e [* + (let [is-first-user? (not (seq (d/query {:query [:find '?e + :in '$ + :where '[?e :user/provider]] + :args [(d/db (d/connect uri))]}))) + user (some-> {:query [:find '(pull ?e [* {:user/clients [*]} {:user/role [:db/ident]}]) :in '$ '?provider '?provider-id @@ -40,7 +44,9 @@ (update :user/role :db/ident))] (if user user - (let [new-user-trans @(d/transact (d/connect uri) [(assoc new-user :db/id "user")])] + (let [new-user-trans @(d/transact (d/connect uri) [(cond-> new-user + true (assoc :db/id "user") + is-first-user? (assoc :user/role :user-role/admin))])] (println new-user-trans) (get-by-id (-> new-user-trans :tempids (get "user")))))))