diff --git a/src/clj/auto_ap/graphql.clj b/src/clj/auto_ap/graphql.clj index 669f229c..bb477832 100644 --- a/src/clj/auto_ap/graphql.clj +++ b/src/clj/auto_ap/graphql.clj @@ -324,6 +324,10 @@ :args {:transaction_rule {:type :edit_transaction_rule}} :resolve :test-transaction-rule} + :run_transaction_rule {:type '(list :transaction) + :args {:transaction_rule_id {:type :id}} + :resolve :run-transaction-rule} + :invoice_stats {:type '(list :invoice_stat) :args {:client_id {:type :id}} :resolve :get-invoice-stats} @@ -817,6 +821,7 @@ :mutation/edit-transaction gq-transactions/edit-transaction :mutation/upsert-transaction-rule gq-transaction-rules/upsert-transaction-rule :test-transaction-rule gq-transaction-rules/test-transaction-rule + :run-transaction-rule gq-transaction-rules/run-transaction-rule :mutation/match-transaction gq-transactions/match-transaction :mutation/match-transaction-rule gq-transactions/match-transaction-rule :mutation/edit-client gq-clients/edit-client diff --git a/src/clj/auto_ap/graphql/transaction_rules.clj b/src/clj/auto_ap/graphql/transaction_rules.clj index 8ec89292..fd77706f 100644 --- a/src/clj/auto_ap/graphql/transaction_rules.clj +++ b/src/clj/auto_ap/graphql/transaction_rules.clj @@ -92,8 +92,7 @@ (defn tr [z x] (re-find (re-pattern z) x)) -(defn test-transaction-rule [{:keys [id]} {{:keys [description note client_id bank_account_id amount_lte amount_gte dom_lte dom_gte yodlee_merchant_id]} :transaction_rule :as z} value] - (assert-admin id) +(defn -test-transaction-rule [id {:keys [:transaction-rule/description :transaction-rule/note :transaction-rule/client :transaction-rule/bank-account :transaction-rule/amount-lte :transaction-rule/amount-gte :transaction-rule/dom-lte :transaction-rule/dom-gte :transaction-rule/yodlee-merchant]}] (->> (d/query (cond-> {:query {:find ['(pull ?e [* {:transaction/client [:client/name] @@ -108,10 +107,10 @@ :where ['[?e :transaction/client ?xx]]} :args [(set (map :db/id (limited-clients id)))]}) - bank_account_id + bank-account (merge-query {:query {:in ['?bank-account-id] :where ['[?e :transaction/bank-account ?bank-account-id]]} - :args [bank_account_id]}) + :args [(:db/id bank-account)]}) description (merge-query {:query {:in ['?description-regex] @@ -119,45 +118,45 @@ '[(re-find ?description-regex ?do)]]} :args [(re-pattern description)]}) - yodlee_merchant_id + yodlee-merchant (merge-query {:query {:in ['?yodlee-merchant-id] :where ['[?e :transaction/yodlee-merchant ?yodlee-merchant-id]]} - :args [yodlee_merchant_id]}) + :args [(:db/id yodlee-merchant)]}) - amount_gte + amount-gte (merge-query {:query {:in ['?amount-gte] :where ['[?e :transaction/amount ?ta] '[(>= ?ta ?amount-gte)]]} - :args [amount_gte]}) + :args [amount-gte]}) - amount_lte + amount-lte (merge-query {:query {:in ['?amount-lte] :where ['[?e :transaction/amount ?ta] '[(<= ?ta ?amount-lte)]]} - :args [amount_lte]}) + :args [amount-lte]}) - dom_lte + dom-lte (merge-query {:query {:in ['?dom-lte] :where ['[?e :transaction/date ?transaction-date] '[(.toInstant ^java.util.Date ?transaction-date ) ?transaction-instant] '[(.atZone ^java.time.Instant ?transaction-instant (java.time.ZoneId/of "US/Pacific")) ?transaction-local] '[(.get ?transaction-local java.time.temporal.ChronoField/DAY_OF_MONTH) ?dom] '[(<= ?dom ?dom-lte)]]} - :args [dom_lte]}) + :args [dom-lte]}) - dom_gte + dom-gte (merge-query {:query {:in ['?dom-gte] :where ['[?e :transaction/date ?transaction-date] '[(.toInstant ^java.util.Date ?transaction-date ) ?transaction-instant] '[(.atZone ^java.time.Instant ?transaction-instant (java.time.ZoneId/of "US/Pacific")) ?transaction-local] '[(.get ?transaction-local java.time.temporal.ChronoField/DAY_OF_MONTH) ?dom] '[(>= ?dom ?dom-gte)]]} - :args [dom_gte]}) + :args [dom-gte]}) - client_id + client (merge-query {:query {:in ['?client-id] :where ['[?e :transaction/client ?client-id]]} - :args [client_id]}) + :args [(:db/id client)]}) true (merge-query {:query {:where ['[?e :transaction/id]]}}))) @@ -168,3 +167,19 @@ (update x :transaction/date c/from-date))) (map ->graphql)) conj []))) + +(defn test-transaction-rule [{:keys [id]} {{:keys [description note client_id bank_account_id amount_lte amount_gte dom_lte dom_gte yodlee_merchant_id]} :transaction_rule :as z} value] + (assert-admin id) + (-test-transaction-rule id #:transaction-rule {:description description + :client (when client_id {:db/id client_id}) + :bank-account (when bank_account_id {:db/id bank_account_id}) + :amount-lte amount_lte + :amount-gte amount_gte + :dom-lte dom_lte + :dom-gte dom_gte + :yodlee-merchant (when yodlee_merchant_id {:db/id yodlee_merchant_id})})) + + +(defn run-transaction-rule [{:keys [id]} {:keys [transaction_rule_id]} value] + (assert-admin id) + (-test-transaction-rule id (tr/get-by-id transaction_rule_id))) diff --git a/src/cljs/auto_ap/views/pages/admin/rules.cljs b/src/cljs/auto_ap/views/pages/admin/rules.cljs index 3eeca8ad..a3d39ce7 100644 --- a/src/cljs/auto_ap/views/pages/admin/rules.cljs +++ b/src/cljs/auto_ap/views/pages/admin/rules.cljs @@ -4,6 +4,7 @@ [auto-ap.views.components.admin.side-bar :refer [admin-side-bar]] [auto-ap.views.components.layouts :refer [appearing-side-bar side-bar-layout]] [auto-ap.views.pages.admin.rules.table :as table] + [auto-ap.views.pages.admin.rules.results-modal :as results-modal] [auto-ap.views.pages.admin.rules.form :as form] [auto-ap.views.pages.admin.rules.common :refer [default-read]] [auto-ap.views.utils :refer [dispatch-event with-user]] @@ -109,4 +110,4 @@ :main [rules-content] :right-side-bar [appearing-side-bar {:visible? active?} [form/form {:rule-saved [::edit-completed]}]] - :bottom [form/test-results-modal]}])) + :bottom [results-modal/test-results-modal]}])) diff --git a/src/cljs/auto_ap/views/pages/admin/rules/form.cljs b/src/cljs/auto_ap/views/pages/admin/rules/form.cljs index f7769914..00ddcdef 100644 --- a/src/cljs/auto_ap/views/pages/admin/rules/form.cljs +++ b/src/cljs/auto_ap/views/pages/admin/rules/form.cljs @@ -11,11 +11,13 @@ [auto-ap.views.components.layouts :as layouts] [auto-ap.views.components.typeahead :refer [typeahead-entity]] [auto-ap.views.pages.admin.rules.common :refer [default-read]] + [auto-ap.views.pages.admin.rules.results-modal :as results-modal] [auto-ap.views.utils :refer [date->str date-picker dispatch-event standard with-user]] [cljs-time.core :as c] [clojure.spec.alpha :as s] [clojure.string :as str] - [re-frame.core :as re-frame])) + [re-frame.core :as re-frame] + [auto-ap.views.pages.admin.rules.results-modal :as results-modal])) ;; SUBS @@ -83,10 +85,7 @@ [:bank-account [:name]] :description-original]]}]})) -(re-frame/reg-sub - ::test-results - (fn [db] - (::test-results db))) + ;; EVENTS @@ -179,9 +178,7 @@ [(forms/triggers-stop-loading ::form)] (fn [{:keys [db]} [_ result]] - (println result) - {:db (assoc db ::test-results (:test-transaction-rule result)) - :dispatch [::events/modal-status ::test-results {:visible? true}]})) + {:dispatch [::results-modal/opening (:test-transaction-rule result) false]})) @@ -311,24 +308,3 @@ [:a.button.is-medium.is-fullwidth.is-outlined {:on-click (dispatch-event [::test-clicked])} "Test Rule"]] [:div.column [submit-button "Save"]]]])]) - -(defn test-results-modal [] - (when (:visible? @(re-frame/subscribe [::subs/modal-state ::test-results])) - [modal {:title "Rule results" - :hide-event [::events/modal-status ::test-results {:visible? false}]} - [:table.table.is-fullwidth.compact - [:tr - [:th "Client"] - [:th "Bank Account"] - [:th "Description"] - [:th "Date"] - [:th "Amount"]] - (for [{:keys [id client bank-account description-original date amount]} @(re-frame/subscribe [::test-results])] - - ^{:key id} - [:tr - [:td (:name client)] - [:td (:name bank-account)] - [:td description-original] - [:td (when date (date->str date))] - [:td amount]])]])) diff --git a/src/cljs/auto_ap/views/pages/admin/rules/results_modal.cljs b/src/cljs/auto_ap/views/pages/admin/rules/results_modal.cljs new file mode 100644 index 00000000..f70ca3cd --- /dev/null +++ b/src/cljs/auto_ap/views/pages/admin/rules/results_modal.cljs @@ -0,0 +1,54 @@ +(ns auto-ap.views.pages.admin.rules.results-modal + (:require [auto-ap.events :as events] + [auto-ap.subs :as subs] + [auto-ap.views.components.modal :refer [modal]] + [auto-ap.views.utils :refer [date->str]] + [re-frame.core :as re-frame])) + + + +(re-frame/reg-sub + ::test-results + (fn [db] + (::test-results db))) + +(re-frame/reg-sub + ::runnable? + (fn [db] + (::runnable? db))) + +(re-frame/reg-event-fx + ::opening + (fn [{:keys [db]} [_ results runnable?]] + {:db (-> db + (assoc ::test-results results) + (assoc ::runnable? runnable?)) + :dispatch [::events/modal-status ::test-results {:visible? true}]})) + + +(defn test-results-modal [] + (let [runnable @(re-frame/subscribe [::runnable?])] + (when (:visible? @(re-frame/subscribe [::subs/modal-state ::test-results])) + [modal {:title "Rule results" + :hide-event [::events/modal-status ::test-results {:visible? false}]} + [:table.table.is-fullwidth.compact + [:tr + (when runnable + [:th {:style {:width "2em"}}]) + [:th "Client"] + [:th "Bank Account"] + [:th "Description"] + [:th "Date"] + [:th "Amount"]] + (for [{:keys [id client bank-account description-original date amount]} @(re-frame/subscribe [::test-results])] + + ^{:key id} + [:tr + (when runnable + [:td + [:input.checkbox {:type "checkbox"}]]) + [:td (:name client)] + [:td (:name bank-account)] + [:td description-original] + [:td (when date (date->str date))] + [:td amount]])]]))) diff --git a/src/cljs/auto_ap/views/pages/admin/rules/table.cljs b/src/cljs/auto_ap/views/pages/admin/rules/table.cljs index 6cf808ec..358ddbf2 100644 --- a/src/cljs/auto_ap/views/pages/admin/rules/table.cljs +++ b/src/cljs/auto_ap/views/pages/admin/rules/table.cljs @@ -1,11 +1,37 @@ (ns auto-ap.views.pages.admin.rules.table (:require [auto-ap.subs :as subs] - [auto-ap.views.utils :refer [dispatch-event ->$]] + [auto-ap.views.utils :refer [dispatch-event ->$ with-user]] [auto-ap.views.pages.admin.rules.form :as form] [auto-ap.views.components.paginator :refer [paginator]] + [auto-ap.views.pages.admin.rules.results-modal :as results-modal] [auto-ap.views.components.sorter :refer [sorted-column]] [re-frame.core :as re-frame])) +(re-frame/reg-event-fx + ::run-clicked + [with-user] + (fn [{:keys [user db]} [_ which]] + {:graphql + {:token user + :query-obj {:venia/operation {:operation/type :query + :operation/name "RunTransactionRule"} + :venia/queries [{:query/data [:run-transaction-rule + {:transaction-rule-id (:id which)} + [:id + :date + :amount + [:client [:name]] + [:bank-account [:name]] + :description-original]]}]} + :on-success [::succeeded-run] + #_#_:on-error [:forms/save-error ::form]}})) + + +(re-frame/reg-event-fx + ::succeeded-run + (fn [{:keys [db]} [_ result]] + {:dispatch [::results-modal/opening (:run-transaction-rule result) true]})) + (defn table [{:keys [id rule-page on-params-change params status]}] (let [opc (fn [p] (on-params-change (merge @params p )))] @@ -42,7 +68,7 @@ "Description"] #_[sorted-column {:on-sort opc - :style {:width "8em" :cursor "pointer"} + :style {:width "8em" :cukjsor "pointer"} :class "has-text-right" :sort-key "amount-gte" :sort-by sort-by @@ -63,7 +89,7 @@ :sort-by sort-by :asc asc} "Note"] - [:th {:style {:width "6em"}} + [:th {:style {:width "9em"}} ]]] [:tbody (if (:loading @status) @@ -90,4 +116,6 @@ "")] [:td note] [:td - [:a.button {:on-click (dispatch-event [::form/editing r])} [:span.icon [:i.fa.fa-pencil]]]]]))]]])))) + [:div.buttons + [:a.button {:on-click (dispatch-event [::run-clicked r])} [:span.icon [:i.fa.fa-play]]] + [:a.button {:on-click (dispatch-event [::form/editing r])} [:span.icon [:i.fa.fa-pencil]]]]]]))]]]))))