From c290c34ec78917e4dead70a07b5514c1344edc42 Mon Sep 17 00:00:00 2001 From: Bryce Date: Thu, 8 Feb 2024 15:56:46 -0800 Subject: [PATCH] Allows making rules by client group --- project.clj | 2 +- resources/schema.edn | 4 ++ src/clj/auto_ap/datomic/transaction_rules.clj | 8 +++- src/clj/auto_ap/datomic/transactions.clj | 2 +- src/clj/auto_ap/graphql/transaction_rules.clj | 2 +- src/clj/auto_ap/rule_matching.clj | 34 +++++++++------ src/clj/auto_ap/server.clj | 5 ++- .../auto_ap/ssr/admin/transaction_rules.clj | 41 +++++++++++++++++-- 8 files changed, 75 insertions(+), 23 deletions(-) diff --git a/project.clj b/project.clj index 634dca39..9508ebc4 100644 --- a/project.clj +++ b/project.clj @@ -148,7 +148,7 @@ :uberjar { - :java-cmd "/usr/lib/jvm/java-11-openjdk/bin/java" + ;;:java-cmd "/usr/lib/jvm/java-11-openjdk/bin/java" :prep-tasks ["fig:min" ] :aot [auto-ap.server auto-ap.time clj-time.core clj-time.coerce clj-time.format clojure.tools.logging.impl ] :dependencies [[com.bhauman/figwheel-main "0.2.18" :exclusions [org.clojure/clojurescript diff --git a/resources/schema.edn b/resources/schema.edn index 700ef49e..9176c9f6 100644 --- a/resources/schema.edn +++ b/resources/schema.edn @@ -1334,6 +1334,10 @@ :db/cardinality #:db{:ident :db.cardinality/one}, :db/doc "The specific client this rule is for", :db/ident :transaction-rule/client} + {:db/valueType #:db{:ident :db.type/string}, + :db/cardinality #:db{:ident :db.cardinality/one}, + :db/doc "The specific client group this rule is for", + :db/ident :transaction-rule/client-group} {:db/valueType #:db{:ident :db.type/ref}, :db/cardinality #:db{:ident :db.cardinality/one}, :db/doc "The specific bank account this rule is for", diff --git a/src/clj/auto_ap/datomic/transaction_rules.clj b/src/clj/auto_ap/datomic/transaction_rules.clj index 72bd9f23..959ee04a 100644 --- a/src/clj/auto_ap/datomic/transaction_rules.clj +++ b/src/clj/auto_ap/datomic/transaction_rules.clj @@ -22,6 +22,7 @@ :transaction-rule/amount-gte :transaction-rule/dom-lte :transaction-rule/dom-gte + :transaction-rule/client-group {:transaction-rule/client [:client/name :db/id :client/code]} {:transaction-rule/bank-account [:db/id :bank-account/name]} {:transaction-rule/yodlee-merchant [:db/id :yodlee-merchant/name :yodlee-merchant/yodlee-id]} @@ -117,7 +118,10 @@ [?e :transaction-rule/transaction-approval-status] (or-join [?e ?c] [?e :transaction-rule/client ?c] - (not [?e :transaction-rule/client]))] + (and [?c :client/groups ?g] + [?e :transaction-rule/client-group ?g]) + (and (not [?e :transaction-rule/client]) + (not [?e :transaction-rule/client-group])))] (dc/db conn) client-id - default-read))) + default-read))) \ No newline at end of file diff --git a/src/clj/auto_ap/datomic/transactions.clj b/src/clj/auto_ap/datomic/transactions.clj index 42ede8aa..da7e90b5 100644 --- a/src/clj/auto_ap/datomic/transactions.clj +++ b/src/clj/auto_ap/datomic/transactions.clj @@ -231,7 +231,7 @@ (defn get-by-id [id] (-> (dc/pull (dc/db conn) - '[* {:transaction/client [:client/name :db/id :client/code :client/locations] + '[* {:transaction/client [:client/name :db/id :client/code :client/locations :client/groups] :transaction/approval-status [:db/ident :db/id] :transaction/bank-account [:bank-account/name :bank-account/code :bank-account/yodlee-account-id :db/id :bank-account/locations :bank-account/current-balance] :transaction/vendor [:db/id :vendor/name] diff --git a/src/clj/auto_ap/graphql/transaction_rules.clj b/src/clj/auto_ap/graphql/transaction_rules.clj index 7e56be6f..ed2a1c5e 100644 --- a/src/clj/auto_ap/graphql/transaction_rules.clj +++ b/src/clj/auto_ap/graphql/transaction_rules.clj @@ -34,7 +34,7 @@ ] (mu/log ::counted :count (count all-rules)) - (map ->graphql (rm/get-matching-rules transaction all-rules))) + (doto (map ->graphql (rm/get-matching-rules transaction all-rules)) (#(println (count %))))) nil)) (defn transaction-rule-account->entity [{:keys [id account_id percentage location]}] diff --git a/src/clj/auto_ap/rule_matching.clj b/src/clj/auto_ap/rule_matching.clj index c399eaf8..74b9756e 100644 --- a/src/clj/auto_ap/rule_matching.clj +++ b/src/clj/auto_ap/rule_matching.clj @@ -1,8 +1,10 @@ (ns auto-ap.rule-matching - (:require [iol-ion.tx :refer [random-tempid]] - [iol-ion.query :refer [->pattern]])) + (:require [auto-ap.logging :as alog] + [iol-ion.query :refer [->pattern]] + [iol-ion.tx :refer [random-tempid]])) (defn rule-applies? [transaction {:keys [:transaction-rule/description + :transaction-rule/client-group :transaction-rule/dom-gte :transaction-rule/dom-lte :transaction-rule/amount-gte :transaction-rule/amount-lte :transaction-rule/client :transaction-rule/bank-account @@ -23,13 +25,18 @@ (<= transaction-dom dom-lte) true) (if amount-gte - (>= (:transaction/amount transaction) amount-gte) + (>= (:transaction/amount transaction) amount-gte) true) (if amount-lte (<= (:transaction/amount transaction) amount-lte) true) + (if client-group + (do + (println "CHECKING HERE" client-group) + ((set (-> transaction :transaction/client :client/groups)) client-group)) + true) (if client - (or + (or (= (:transaction/client transaction) (:db/id client)) (= (:db/id (:transaction/client transaction)) @@ -40,7 +47,7 @@ (:yodlee-merchant/yodlee-id yodlee-merchant)) true) (if bank-account - (or + (or (= (:db/id (:transaction/bank-account transaction)) (:db/id bank-account)) (= (:transaction/bank-account transaction) @@ -51,12 +58,13 @@ (or (->> [[:transaction-rule/bank-account 0] [:transaction-rule/client 1] - [:transaction-rule/dom-lte 2] - [:transaction-rule/dom-gte 2] - [:transaction-rule/amount-lte 3] - [:transaction-rule/amount-gte 3] - [:transaction-rule/description 4] - [:transaction-rule/yodlee-merchant 5]] + [:transaction-rule/client-group 2] + [:transaction-rule/dom-lte 3] + [:transaction-rule/dom-gte 4] + [:transaction-rule/amount-lte 4] + [:transaction-rule/amount-gte 4] + [:transaction-rule/description 5] + [:transaction-rule/yodlee-merchant 6]] (filter (fn [[key]] (get rule key))) (map second) @@ -83,8 +91,8 @@ (defn get-matching-rules [transaction all-rules] (->> all-rules - (map (fn [r] (update r :transaction-rule/description #(some-> % ->pattern)))) - (filter #(rule-applies? transaction %)))) + (map (fn [r] (update r :transaction-rule/description #(some-> % ->pattern)))) + (filter #(rule-applies? transaction %)))) (defn spread-cents [cents n] (let [default-spread (for [_ (range n)] diff --git a/src/clj/auto_ap/server.clj b/src/clj/auto_ap/server.clj index 77c11d3b..cbcf22a3 100644 --- a/src/clj/auto_ap/server.clj +++ b/src/clj/auto_ap/server.clj @@ -33,7 +33,6 @@ (println "LOADING SERVER") - (defn add-shutdown-hook! [^Runnable f] (.addShutdownHook (Runtime/getRuntime) (Thread. f))) @@ -157,7 +156,9 @@ :else (do (add-shutdown-hook! shutdown-mount) + (println "HI?") (start-server :port 9000 :bind "0.0.0.0" #_#_:handler (cider-nrepl-handler)) - (mount/start) + (println "HI 2?") + (mount/start (mount/except (mount/only #{#'user/jetty}))) #_(alter-var-root #'nrepl.middleware.print/*print-fn* (constantly clojure.pprint/pprint)))))) diff --git a/src/clj/auto_ap/ssr/admin/transaction_rules.clj b/src/clj/auto_ap/ssr/admin/transaction_rules.clj index 3dec9d8f..2482a7b4 100644 --- a/src/clj/auto_ap/ssr/admin/transaction_rules.clj +++ b/src/clj/auto_ap/ssr/admin/transaction_rules.clj @@ -70,12 +70,20 @@ :class "hot-filter" :value (:description (:parsed-query-params request)) :placeholder "LOWES" + :size :small})) + (com/field {:label "Client group"} + (com/text-input {:name "client-group" + :id "client-group" + :class "hot-filter" + :value (:client-group (:parsed-query-params request)) + :placeholder "NTG" :size :small}))]]) (def default-read '[:db/id :transaction-rule/description :transaction-rule/note :transaction-rule/amount-lte + :transaction-rule/client-group :transaction-rule/amount-gte :transaction-rule/dom-lte :transaction-rule/dom-gte @@ -139,6 +147,11 @@ '[(clojure.string/includes? ?d2 ?description)]]} :args [(clojure.string/lower-case (:description query-params))]}) + (not (str/blank? (:client-group query-params))) + (merge-query {:query {:in ['?client-group] + :where ['[?e :transaction-rule/client-group ?client-group] ]} + :args [(clojure.string/upper-case (:client-group query-params))]}) + true (merge-query {:query {:find ['?e] :where ['[?e :transaction-rule/transaction-approval-status]]}}))] @@ -198,7 +211,8 @@ :headers [{:key "client" :name "Client" :sort-key "client" - :render #(-> % :transaction-rule/client :client/name)} + :render #(or (-> % :transaction-rule/client :client/name) + (some->> % :transaction-rule/client-group (str "group ") (com/pill {:color :primary})))} {:key "bank-account" :name "Bank account" :sort-key "bank-account" @@ -281,7 +295,7 @@ :db/id [:transaction/date :xform clj-time.coerce/from-date]]) -(defn transactions-matching-rule [{{:transaction-rule/keys [description client bank-account amount-lte amount-gte dom-lte dom-gte]} +(defn transactions-matching-rule [{{:transaction-rule/keys [description client bank-account amount-lte amount-gte dom-lte dom-gte client-group]} :entity clients :clients only-uncoded? :only-uncoded?}] @@ -297,7 +311,11 @@ (merge-query {:query {:in ['?descr] :where ['[(iol-ion.query/->pattern ?descr) ?description-regex]]} :args [description]}) - + client-group + (merge-query {:query {:in ['?client-group] + :where ['[?e :transaction/client ?client-group-client] + '[?client-group-client :client/groups ?client-group]]} + :args [client-group]}) valid-clients (merge-query {:query {:in ['[?xx ...]] :where ['[?e :transaction/client ?xx]]} @@ -553,6 +571,7 @@ [:map [:db/id {:optional true} [:maybe entity-id]] [:transaction-rule/client {:optional true} [:maybe entity-id]] + [:transaction-rule/client-group {:optional true} [:maybe :string]] [:transaction-rule/description [:and regex [:string {:min 3}]]] [:transaction-rule/bank-account [:maybe entity-id]] @@ -652,6 +671,7 @@ :class "w-96" :value (fc/field-value)}))) [:div.filters {:x-data (hx/json {:clientFilter (boolean (fc/field-value (:transaction-rule/client fc/*current*))) + :clientGroupFilter (boolean (fc/field-value (:transaction-rule/client-group fc/*current*))) :bankAccountFilter (boolean (fc/field-value (:transaction-rule/bank-account fc/*current*))) :amountFilter (boolean (or (fc/field-value (:transaction-rule/amount-gte fc/*current*)) (fc/field-value (:transaction-rule/amount-lte fc/*current*)))) @@ -661,6 +681,8 @@ [:div.flex.gap-2.mb-2 (com/a-button {"@click" "clientFilter=true" "x-show" "!clientFilter"} "Filter client") + (com/a-button {"@click" "clientGroupFilter=true" + "x-show" "!clientGroupFilter"} "Filter client group") (com/a-button {"@click" "bankAccountFilter=true" "x-show" "clientFilter && !bankAccountFilter"} "Filter bank account") (com/a-button {"@click" "amountFilter=true" @@ -683,7 +705,19 @@ :x-model "clientId" :value (fc/field-value) :content-fn (fn [c] (pull-attr (dc/db conn) :client/name c))})])) + (fc/with-field :transaction-rule/client-group + (com/validated-field + (-> {:label "Client Group" + :errors (fc/field-errors) + :x-show "clientGroupFilter"} + (hx/alpine-appear)) + [:div.w-96 + (com/text-input {:name (fc/field-name) + :error? (fc/error?) + :class "w-24" + :placeholder "NTG" + :value (fc/field-value)})])) (fc/with-field :transaction-rule/bank-account (com/validated-field (-> {:label "Bank Account" @@ -846,6 +880,7 @@ (let [transaction-rule (:snapshot multi-form-state) _ (validate-transaction-rule transaction-rule) entity (cond-> transaction-rule + (:transaction-rule/client-group transaction-rule) (update :transaction-rule/client-group str/upper-case) (= :post request-method) (assoc :db/id "new") true (assoc :transaction-rule/note (entity->note transaction-rule))) {:keys [tempids]} (audit-transact [[:upsert-entity entity]]