lots of fixes.

This commit is contained in:
Bryce Covert
2020-04-21 07:13:42 -07:00
parent 01c776a72e
commit 9cb42be9e8
5 changed files with 108 additions and 72 deletions

View File

@@ -53,6 +53,24 @@
:db/valueType :db.type/string :db/valueType :db.type/string
:db/cardinality :db.cardinality/one}]]) :db/cardinality :db.cardinality/one}]])
(defn reset-function [conn]
[[{:db/ident :reset
:db/doc "Resets a relationship to the values specified "
:db/fn (d/function '{:lang "clojure"
:params [db e a vs]
:code (let [ids (when-not (string? e)
(->> (d/query {:query {:find ['?z]
:in ['$ '?e '?a]
:where [['?e '?a '?z]]}
:args [db e a]})
(map first)))
new-id-set (set (map :db/id vs))
retract-ids (filter (complement new-id-set) ids)]
(cond-> []
true (into (map (fn [i] [:db/retractEntity i ]) retract-ids))
(seq vs) (conj {:db/id e
a vs})))})}]])
(defn -main [& args] (defn -main [& args]
(println "Creating database ..." uri) (println "Creating database ..." uri)
(doto (d/create-database uri) println) (doto (d/create-database uri) println)
@@ -174,7 +192,7 @@
:db/doc "Overrides per-client" :db/doc "Overrides per-client"
:db/valueType :db.type/ref :db/valueType :db.type/ref
:db/cardinality :db.cardinality/many}]]} :db/cardinality :db.cardinality/many}]]}
}] :auto-ap/add-reset-rels {:txes-fn `reset-function}}]
(println "Conforming database...") (println "Conforming database...")
(c/ensure-conforms conn norms-map) (c/ensure-conforms conn norms-map)
(when (not (seq args)) (when (not (seq args))

View File

@@ -39,6 +39,7 @@
(defn terms-for-client-id [vendor client-id] (defn terms-for-client-id [vendor client-id]
(or
(->> (->>
(filter (filter
(fn [to] (fn [to]
@@ -46,4 +47,17 @@
client-id)) client-id))
(:vendor/terms-overrides vendor)) (:vendor/terms-overrides vendor))
first first
:vendor-terms-override/terms)) :vendor-terms-override/terms)
(:vendor/terms vendor)))
(defn account-for-client-id [vendor client-id]
(or
(->>
(filter
(fn [to]
(= (:db/id (:vendor-account-override/client to))
client-id))
(:vendor/account-overrides vendor))
first
:vendor-account-override/account)
(:vendor/default-account vendor)))

View File

@@ -11,7 +11,7 @@
(defn upsert-vendor [context {{:keys [id name hidden terms code print_as primary_contact secondary_contact address default_account_id invoice_reminder_schedule terms_overrides account_overrides] :as in} :vendor} value] (defn upsert-vendor [context {{:keys [id name hidden terms code print_as primary_contact secondary_contact address default_account_id invoice_reminder_schedule terms_overrides account_overrides] :as in} :vendor} value]
(when id (when id
(assert-admin (:id context))) (assert-admin (:id context)))
(let [term-overrides (mapv (let [terms-overrides (mapv
(fn [to] (fn [to]
(cond-> (cond->
#:vendor-terms-override {:client (:client_id to) #:vendor-terms-override {:client (:client_id to)
@@ -25,7 +25,8 @@
:account (:account_id ao)} :account (:account_id ao)}
(:id ao) (assoc :db/id (:id ao)))) (:id ao) (assoc :db/id (:id ao))))
account_overrides) account_overrides)
transaction [(remove-nils #:vendor {:db/id (if id transaction (cond->
[(remove-nils #:vendor {:db/id (if id
id id
"vendor") "vendor")
:name name :name name
@@ -35,10 +36,8 @@
:print-as print_as :print-as print_as
:default-account default_account_id :default-account default_account_id
:invoice-reminder-schedule (keyword invoice_reminder_schedule) :invoice-reminder-schedule (keyword invoice_reminder_schedule)
:term-overrides (when (is-admin? (:id context))
term-overrides)
:account-overrides (when (is-admin? (:id context))
account-overrides)
:address (when address :address (when address
(remove-nils #:address {:db/id (if (:id address) (remove-nils #:address {:db/id (if (:id address)
(:id address) (:id address)
@@ -66,6 +65,8 @@
:phone (:phone secondary_contact) :phone (:phone secondary_contact)
:email (:email secondary_contact)}) :email (:email secondary_contact)})
)})] )})]
(is-admin? (:id context)) (conj [:reset (if id id "vendor") :vendor/account-overrides account-overrides])
(is-admin? (:id context)) (conj [:reset (if id id "vendor") :vendor/terms-overrides terms-overrides]))
transaction-result @(d/transact (d/connect uri) transaction)] transaction-result @(d/transact (d/connect uri) transaction)]
(-> (d-vendors/get-by-id (or (-> transaction-result :tempids (get "vendor")) (-> (d-vendors/get-by-id (or (-> transaction-result :tempids (get "vendor"))

View File

@@ -15,7 +15,6 @@
(defn template-applies? [text {:keys [keywords]}] (defn template-applies? [text {:keys [keywords]}]
(println text)
(every? #(re-find % text) keywords)) (every? #(re-find % text) keywords))
(defn extract-template (defn extract-template

View File

@@ -11,7 +11,9 @@
[auto-ap.parse.util :as parse-u] [auto-ap.parse.util :as parse-u]
[auto-ap.graphql.utils :refer [assert-admin]] [auto-ap.graphql.utils :refer [assert-admin]]
[auto-ap.routes.utils :refer [wrap-secure]] [auto-ap.routes.utils :refer [wrap-secure]]
[clj-time.coerce :refer [to-date]]
[clj-time.core :as time]
[clj-time.coerce :as coerce :refer [to-date]]
[ring.middleware.json :refer [wrap-json-response]] [ring.middleware.json :refer [wrap-json-response]]
[compojure.core :refer [GET POST context defroutes [compojure.core :refer [GET POST context defroutes
wrap-routes]] wrap-routes]]
@@ -126,8 +128,8 @@
(defn invoice-rows->transaction [rows] (defn invoice-rows->transaction [rows]
(->> rows (->> rows
(mapcat (fn [{:keys [vendor-id total client-id amount date invoice-number default-location account-id check vendor]}] (mapcat (fn [{:keys [vendor-id total client-id amount date invoice-number default-location account-id check vendor]}]
(println account-id vendor) (let [invoice (cond->
(let [invoice #:invoice {:db/id (.toString (java.util.UUID/randomUUID)) #:invoice {:db/id (.toString (java.util.UUID/randomUUID))
:vendor vendor-id :vendor vendor-id
:client client-id :client client-id
:default-location default-location :default-location default-location
@@ -143,9 +145,11 @@
:invoice-number invoice-number :invoice-number invoice-number
:date (to-date date) :date (to-date date)
:expense-accounts [#:invoice-expense-account {:account (or account-id :expense-accounts [#:invoice-expense-account {:account (or account-id
(-> vendor :vendor/default-account :db/id)) (:db/id (d-vendors/account-for-client-id vendor client-id)))
:location default-location :location default-location
:amount total}]} :amount total}]}
(:vendor/terms vendor) (assoc :invoice/due (coerce/to-date
(time/plus date (time/days (d-vendors/terms-for-client-id vendor client-id))))))
payment (if (= :invoice-status/paid (:invoice/status invoice)) payment (if (= :invoice-status/paid (:invoice/status invoice))
#:invoice-payment {:invoice (:db/id invoice) #:invoice-payment {:invoice (:db/id invoice)
:amount (:invoice/total invoice) :amount (:invoice/total invoice)