users can now edit vendors.

This commit is contained in:
2022-04-12 15:49:45 -07:00
parent 6ad5541124
commit f73b406abd
3 changed files with 162 additions and 84 deletions

View File

@@ -3,19 +3,41 @@
[auto-ap.datomic :refer [audit-transact conn remove-nils]]
[auto-ap.datomic.vendors :as d-vendors]
[auto-ap.graphql.utils
:refer [->graphql <-graphql assert-admin enum->keyword is-admin? result->page]]
:refer [->graphql
<-graphql
assert-admin
assert-failure
enum->keyword
is-admin?
result->page]]
[clojure.set :as set]
[clojure.tools.logging :as log]
[datomic.api :as d]))
(defn can-user-edit-vendor? [vendor-id id]
(if (is-admin? id)
true
(empty?
(set/difference (set (d/q '[:find [?c ...]
:in $ ?v
:where [?vu :vendor-usage/vendor ?v]
[?vu :vendor-usage/client ?c]
[?vu :vendor-usage/count ?d]
[(>= ?d 0)]]
(d/db conn)
vendor-id))
(set (map :db/id (:user/clients id)))))))
(defn upsert-vendor [context {{:keys [id name hidden terms code print_as primary_contact secondary_contact address default_account_id invoice_reminder_schedule schedule_payment_dom terms_overrides account_overrides] :as in} :vendor} value]
(when id
(assert-admin (:id context)))
#_(Thread/sleep 3000)
#_(throw (ex-info "" {:validation-error "can't do that."}))
(let [hidden (if (is-admin? (:id context))
(when (and id (not (can-user-edit-vendor? id (:id context))))
(assert-failure "This vendor is managed by Integreat. Please reach out to ben@integreatconsult.com for your changes."))
(let [
hidden (if (is-admin? (:id context))
hidden
false)
existing (when id
(d/pull (d/db conn) '[:vendor/name] id))
terms-overrides (mapv
(fn [to]
(cond->
@@ -82,14 +104,14 @@
:vendor/legal-entity-tin-type (enum->keyword (:legal_entity_tin_type in) "legal-entity-tin-type")
:vendor/legal-entity-1099-type (enum->keyword (:legal_entity_1099_type in) "legal-entity-1099-type"))))]
(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/search-terms [name]])
(is-admin? (:id context)) (conj [:reset (if id id "vendor") :vendor/terms-overrides terms-overrides])
(is-admin? (:id context)) (conj [:reset (if id id "vendor") :vendor/schedule-payment-dom schedule-payment-dom])
(is-admin? (:id context)) (conj [:reset (if id id "vendor") :vendor/automatically-paid-when-due
(mapv
(fn [apwd]
{:db/id apwd})
(:automatically_paid_when_due in))]))
(:automatically_paid_when_due in))])
(not= (:vendor/name existing) name) (conj [:reset (if id id "vendor") :vendor/search-terms [name]]))
_ (log/info "Upserting vendor" transaction)
transaction-result (audit-transact transaction (:id context))]