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 (ns auto-ap.datomic.vendors
(:require [datomic.api :as d] (:require [datomic.api :as d]
[auto-ap.graphql.utils :refer [limited-clients]]
[auto-ap.datomic :refer [uri]])) [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] (defn get-graphql [args]
(->> (d/q '[:find (pull ?e [* {:vendor/account-overrides [* {:vendor-account-override/client [:client/name :db/id] (->> (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-account-override/account [:account/name :account/numeric-code :db/id]}]
:vendor/terms-overrides [* {:vendor-terms-override/client [:client/name :db/id]}]}]) :vendor/terms-overrides [* {:vendor-terms-override/client [:client/name :db/id]}]}])]
:where [?e :vendor/name]] :in ['$]
(d/db (d/connect uri))) :where ['[?e :vendor/name]]}
(map first))) :args [(d/db (d/connect uri))]})
(d/query)
(map first)
(map #(cleanse (:id args) %))))
(defn get-by-id [id] (defn get-by-id [id]
@@ -24,6 +37,7 @@
(map first) (map first)
(first))) (first)))
(defn terms-for-client-id [vendor client-id] (defn terms-for-client-id [vendor client-id]
(->> (->>
(filter (filter

View File

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

View File

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

View File

@@ -1,5 +1,5 @@
(ns auto-ap.graphql.vendors (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.datomic.vendors :as d-vendors]
[auto-ap.time :refer [parse iso-date]] [auto-ap.time :refer [parse iso-date]]
[datomic.api :as d] [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] (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 transaction [(remove-nils #:vendor {:db/id (if id
id id
"vendor") "vendor")
@@ -20,20 +35,10 @@
: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)
:terms-overrides (mapv :term-overrides (when (is-admin? (:id context))
(fn [to] term-overrides)
(cond-> :account-overrides (when (is-admin? (:id context))
#:vendor-terms-override {:client (:client_id to) account-overrides)
: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)
: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)

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] ] [: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]]]] [:address [:street1 :street2 :city :state :zip]]]]
[:vendor [: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]]]} [:accounts [:numeric-code :location :name :type :account_set :id]]]}
:on-success [::received-initial]}})))) :on-success [::received-initial]}}))))
(def vendor-query (def vendor-query

View File

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

View File

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