Added the ability to remove them in the ui.3

This commit is contained in:
Bryce Covert
2020-04-20 07:49:38 -07:00
parent 47babdfe7c
commit 773ae19f5c
7 changed files with 100 additions and 45 deletions

View File

@@ -1,14 +1,27 @@
(ns auto-ap.datomic.vendors
(:require [datomic.api :as d]
[auto-ap.graphql.utils :refer [limited-clients]]
[auto-ap.datomic :refer [uri]]))
(defn cleanse [id vendor]
(let [clients (if-let [clients (limited-clients id)]
(set (map :db/id clients))
nil)]
(if clients
(-> vendor
(update :vendor/account-overrides (fn [ao] (filter #(clients (:db/id (:vendor-account-override/client %))) ao)))
(update :vendor/terms-overrides (fn [to] (filter #(clients (:db/id (:vendor-terms-override/client %))) to))))
vendor)))
(defn get-graphql [args]
(->> (d/q '[:find (pull ?e [* {:vendor/account-overrides [* {:vendor-account-override/client [:client/name :db/id]
:vendor-account-override/account [:account/name :account/numeric-code :db/id]}]
:vendor/terms-overrides [* {:vendor-terms-override/client [:client/name :db/id]}]}])
:where [?e :vendor/name]]
(d/db (d/connect uri)))
(map first)))
(->> (cond-> {:query {:find ['(pull ?e [* {:vendor/account-overrides [* {:vendor-account-override/client [:client/name :db/id]
:vendor-account-override/account [:account/name :account/numeric-code :db/id]}]
:vendor/terms-overrides [* {:vendor-terms-override/client [:client/name :db/id]}]}])]
:in ['$]
:where ['[?e :vendor/name]]}
:args [(d/db (d/connect uri))]})
(d/query)
(map first)
(map #(cleanse (:id args) %))))
(defn get-by-id [id]
@@ -24,6 +37,7 @@
(map first)
(first)))
(defn terms-for-client-id [vendor client-id]
(->>
(filter

View File

@@ -793,7 +793,7 @@
(defn get-vendor [context args value]
(->graphql
(d-vendors/get-graphql args)))
(d-vendors/get-graphql (assoc args :id (:id context)))))
(defn print-checks [context args value]

View File

@@ -42,6 +42,9 @@
node))
m))
(defn is-admin? [id]
(println "role" id)
(= "admin" (:user/role id)))
(defn assert-admin [id]
(println "role" id)

View File

@@ -1,5 +1,5 @@
(ns auto-ap.graphql.vendors
(:require [auto-ap.graphql.utils :refer [->graphql assert-can-see-client]]
(:require [auto-ap.graphql.utils :refer [->graphql assert-can-see-client assert-admin is-admin?]]
[auto-ap.datomic.vendors :as d-vendors]
[auto-ap.time :refer [parse iso-date]]
[datomic.api :as d]
@@ -9,7 +9,22 @@
(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]
(let [_ (println default_account_id)
(when id
(assert-admin (:id context)))
(let [term-overrides (mapv
(fn [to]
(cond->
#:vendor-terms-override {:client (:client_id to)
:terms (:terms to)}
(:id to) (assoc :db/id (:id to))))
terms_overrides)
account-overrides (mapv
(fn [ao]
(cond->
#:vendor-account-override {:client (:client_id ao)
:account (:account_id ao)}
(:id ao) (assoc :db/id (:id ao))))
account_overrides)
transaction [(remove-nils #:vendor {:db/id (if id
id
"vendor")
@@ -20,20 +35,10 @@
:print-as print_as
:default-account default_account_id
:invoice-reminder-schedule (keyword invoice_reminder_schedule)
:terms-overrides (mapv
(fn [to]
(cond->
#:vendor-terms-override {:client (:client_id to)
:terms (:terms to)}
(:id to) (assoc :db/id (:id to))))
terms_overrides)
:account-overrides (mapv
(fn [ao]
(cond->
#:vendor-account-override {:client (:client_id ao)
:account (:account_id ao)}
(:id ao) (assoc :db/id (:id ao))))
account_overrides)
:term-overrides (when (is-admin? (:id context))
term-overrides)
:account-overrides (when (is-admin? (:id context))
account-overrides)
:address (when address
(remove-nils #:address {:db/id (if (:id address)
(:id address)

View File

@@ -48,7 +48,9 @@
[:id :name :code :email :matches :locations [:location-matches [:location :match]] [:bank-accounts [:id :code :number :bank-name :bank-code :check-number :name :routing :type :sort-order :visible :yodlee-account-id :locations] ]
[:address [:street1 :street2 :city :state :zip]]]]
[:vendor
[:id :name :hidden [:default-account [:name :id :location]] [:primary-contact [:name :phone :email :id]] [:secondary-contact [:id :name :phone :email]] :print-as :invoice-reminder-schedule :code]]
[:id :name :hidden [:default-account [:name :id :location]] [:primary-contact [:name :phone :email :id]] [:secondary-contact [:id :name :phone :email]] :print-as :invoice-reminder-schedule :code
[:account-overrides [[:client [:id :name]] :id [:account [:id :numeric-code :name]]]]
[:terms-overrides [[:client [:id :name]] :id :terms]]]]
[:accounts [:numeric-code :location :name :type :account_set :id]]]}
:on-success [::received-initial]}}))))
(def vendor-query

View File

@@ -22,7 +22,7 @@
[:footer.modal-card-foot
foot])]])
(defn action-modal [{:keys [title warning action-text id save-event can-submit? status-from] :or {can-submit? true}} & rest]
(defn action-modal [{:keys [title class warning action-text id save-event can-submit? status-from] :or {can-submit? true}} & rest]
(let [{:keys [visible? saving? error-message]} @(re-frame/subscribe [::subs/modal-state id status-from])]
(println id visible?)
(when visible?
@@ -32,6 +32,7 @@
(re-frame/dispatch [::events/modal-status id {:saving? true :error-message nil}])
(re-frame/dispatch save-event))}
(-> [modal {:title [:span title]
:class class
:foot [:input.button.is-primary (cond-> {:type "submit"
:form id
:class (when saving?

View File

@@ -33,12 +33,25 @@
(map first)
first))))
(re-frame/reg-event-db
::removed-override
[(forms/in-form ::vendor-form)]
(fn [form [_ override-key index]]
(update-in form [:data override-key]
(fn [overrides]
(reduce
(fn [overrides [i override]]
(if (= i index)
overrides
(conj overrides override)))
[]
(map vector (range) overrides))))))
(re-frame/reg-event-fx
::started
(fn [{:keys [db]} [_ vendor]]
(println vendor)
{:db (-> db (forms/start-form ::vendor-form (-> vendor
(update :account-overrides #(mapv
(fn [ao]
@@ -61,6 +74,17 @@
@(re-frame/subscribe [::subs/account (:id da)]))))))
:dispatch [::events/modal-status ::dialog {:visible? true}]}))
(re-frame/reg-event-db
::changed
(forms/change-handler
::vendor-form
(fn [data field value]
(let [[override-key? i?] field]
(if (and (#{:account-overrides :terms-overrides} override-key?)
(nil? (get-in data [override-key? i? :key])))
[[override-key? i? :key] (random-uuid)]
[])))))
(re-frame/reg-event-fx
::save-complete
[(forms/triggers-stop ::vendor-form)]
@@ -106,29 +130,34 @@
:on-error [::forms/save-error ::vendor-form]}})))
(defn default-with-overrides [{:keys [override-key override-value-key change-event default-key data]} template]
(let [clients @(re-frame/subscribe [::subs/clients])]
(let [clients @(re-frame/subscribe [::subs/clients])
is-admin? @(re-frame/subscribe [::subs/is-admin?])]
[:div
[horizontal-field
[:label.label "Default"]
[bind-field
(assoc-in template [1 :field ] default-key)]]
[horizontal-field
[:label.label "Overrides"]
(for [[i overrides] (map vector (range) (conj (override-key data) {}))]
^{:key i}
[:div
[:div.columns
[:div.column
[bind-field
[typeahead-entity {:matches clients
:match->text :name
:type "typeahead-entity"
:field [override-key i :client]
:event change-event
:subscription data}]]]
[:div.column
[bind-field
(assoc-in template [1 :field ] [override-key i :override])]]]])]]))
(when is-admin?
[horizontal-field
[:label.label "Overrides"]
(for [[i override] (map vector (range) (conj (override-key data) {:key (random-uuid)}))]
^{:key (or
(:id override)
(:key override))}
[:div.columns
[:div.column
[bind-field
[typeahead-entity {:matches clients
:match->text :name
:type "typeahead-entity"
:field [override-key i :client]
:event change-event
:subscription data}]]]
[:div.column
[bind-field
(assoc-in template [1 :field ] [override-key i :override])]]
[:div.column.is-1
[:a.button {:on-click (dispatch-event [::removed-override override-key i])} [:span.icon [:span.icon-remove]]]]])])]))
(defn form-content [{:keys [data change-event]}]
(let [chooseable-expense-accounts @(re-frame/subscribe [::subs/chooseable-expense-accounts])
@@ -280,8 +309,9 @@
(let [clients @(re-frame/subscribe [::subs/clients])
all-vendors @(re-frame/subscribe [::subs/vendors])
{:keys [data error ] :as f} @(re-frame/subscribe [::forms/form ::vendor-form])
change-event [::forms/change ::vendor-form]]
change-event [::changed]]
[action-modal {:id ::dialog
:class ["wide"]
:title [:span (if (:id data)
(str "Edit " (or (:name data) "<vendor>"))
(str "Add " (or (:name data) "<new vendor>")))