diff --git a/src/clj/auto_ap/graphql.clj b/src/clj/auto_ap/graphql.clj index bb477832..9ceb7ef3 100644 --- a/src/clj/auto_ap/graphql.clj +++ b/src/clj/auto_ap/graphql.clj @@ -648,10 +648,10 @@ :payment_id {:type :id}} :resolve :mutation/match-transaction} - :match_transaction_rule {:type :transaction - :args {:transaction_id {:type :id} - :transaction_rule_id {:type :id}} - :resolve :mutation/match-transaction-rule} + :match_transaction_rules {:type '(list :transaction) + :args {:transaction_ids {:type '(list :id)} + :transaction_rule_id {:type :id}} + :resolve :mutation/match-transaction-rules} :void_invoice {:type :invoice :args {:invoice_id {:type :id}} :resolve :mutation/void-invoice} @@ -823,7 +823,7 @@ :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/match-transaction-rules gq-transactions/match-transaction-rules :mutation/edit-client gq-clients/edit-client :mutation/upsert-vendor gq-vendors/upsert-vendor :mutation/upsert-account gq-accounts/upsert-account diff --git a/src/clj/auto_ap/graphql/transaction_rules.clj b/src/clj/auto_ap/graphql/transaction_rules.clj index fd77706f..19f6d672 100644 --- a/src/clj/auto_ap/graphql/transaction_rules.clj +++ b/src/clj/auto_ap/graphql/transaction_rules.clj @@ -92,7 +92,7 @@ (defn tr [z x] (re-find (re-pattern z) x)) -(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]}] +(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]} include-coded?] (->> (d/query (cond-> {:query {:find ['(pull ?e [* {:transaction/client [:client/name] @@ -157,12 +157,18 @@ (merge-query {:query {:in ['?client-id] :where ['[?e :transaction/client ?client-id]]} :args [(:db/id client)]}) + + + (not include-coded?) + (merge-query {:query {:where ['[?e :transaction/approval-status :transaction-approval-status/unapproved]]}}) + true (merge-query {:query {:where ['[?e :transaction/id]]}}))) (transduce (comp (take 15) (map first) + (map #(dissoc % :transaction/id)) (map (fn [x] (update x :transaction/date c/from-date))) (map ->graphql)) @@ -171,15 +177,16 @@ (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})})) + :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})} + true)) (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))) + (-test-transaction-rule id (tr/get-by-id transaction_rule_id) false)) diff --git a/src/clj/auto_ap/graphql/transactions.clj b/src/clj/auto_ap/graphql/transactions.clj index aa8d5160..55768909 100644 --- a/src/clj/auto_ap/graphql/transactions.clj +++ b/src/clj/auto_ap/graphql/transactions.clj @@ -122,26 +122,40 @@ approval-status->graphql ->graphql)) -(defn match-transaction-rule [context {:keys [transaction_id transaction_rule_id]} value] +(defn match-transaction-rules [context {:keys [transaction_ids transaction_rule_id]} value] (let [_ (assert-admin (:id context)) - transaction (update (d-transactions/get-by-id transaction_id) :transaction/date coerce/to-date) + transactions (transduce + (comp + (map d-transactions/get-by-id) + (map #(update % :transaction/date coerce/to-date))) + conj + [] + transaction_ids) transaction-rule (update (tr/get-by-id transaction_rule_id) :transaction-rule/description #(some-> % re-pattern))] - (when (not (rm/rule-applies? transaction transaction-rule)) - (throw (ex-info "Transaction rule does not apply" {:validation-error "Transaction rule does not apply"}))) + (doseq [transaction transactions] + (when (not (rm/rule-applies? transaction transaction-rule)) + (throw (ex-info "Transaction rule does not apply" {:validation-error "Transaction rule does not apply"}))) - (when (:transaction/payment transaction) - (throw (ex-info "Transaction already associated with a payment" {:validation-error "Transaction already associated with a payment"}))) + (when (:transaction/payment transaction) - @(d/transact (d/connect uri) - (into - [(remove-nils (rm/apply-rule {:db/id (:db/id transaction) - :transaction/amount (:transaction/amount transaction)} - transaction-rule + (throw (ex-info "Transaction already associated with a payment" {:validation-error "Transaction already associated with a payment"}))) + - (or (-> transaction :transaction/bank-account :bank-account/locations) - (-> transaction :transaction/client :client/locations))))] - (map (fn [x] [:db/retractEntity (:db/id x)] ) - (:transaction/accounts transaction))))) - (-> (d-transactions/get-by-id transaction_id) - approval-status->graphql - ->graphql)) + @(d/transact (d/connect uri) + (into + [(remove-nils (rm/apply-rule {:db/id (:db/id transaction) + :transaction/amount (:transaction/amount transaction)} + transaction-rule + + (or (-> transaction :transaction/bank-account :bank-account/locations) + (-> transaction :transaction/client :client/locations))))] + (map (fn [x] [:db/retractEntity (:db/id x)] ) + (:transaction/accounts transaction)))))) + (transduce + (comp + (map d-transactions/get-by-id) + (map approval-status->graphql) + (map ->graphql)) + conj + [] + transaction_ids )) diff --git a/src/clj/auto_ap/yodlee/import.clj b/src/clj/auto_ap/yodlee/import.clj index b24701fc..aaa36d1b 100644 --- a/src/clj/auto_ap/yodlee/import.clj +++ b/src/clj/auto_ap/yodlee/import.clj @@ -89,8 +89,9 @@ :account-id account-id :date (coerce/to-date (time/parse date "YYYY-MM-dd")) :yodlee-merchant (when (and merchant-id merchant-name (not (str/blank? merchant-id))) + nil - {:yodlee-merchant/yodlee-id merchant-id + #_{:yodlee-merchant/yodlee-id merchant-id :yodlee-merchant/name merchant-name}) :amount (double amount) :description-original description-original 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 00ddcdef..e5bc9525 100644 --- a/src/cljs/auto_ap/views/pages/admin/rules/form.cljs +++ b/src/cljs/auto_ap/views/pages/admin/rules/form.cljs @@ -178,7 +178,7 @@ [(forms/triggers-stop-loading ::form)] (fn [{:keys [db]} [_ result]] - {:dispatch [::results-modal/opening (:test-transaction-rule result) false]})) + {:dispatch [::results-modal/opening (:test-transaction-rule result) nil false]})) 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 index 4c506b46..9c49d7ac 100644 --- a/src/cljs/auto_ap/views/pages/admin/rules/results_modal.cljs +++ b/src/cljs/auto_ap/views/pages/admin/rules/results_modal.cljs @@ -2,7 +2,8 @@ (: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 dispatch-event]] + [auto-ap.views.utils :refer [date->str dispatch-event with-user]] + [auto-ap.views.pages.transactions.common :refer [transaction-read]] [re-frame.core :as re-frame])) @@ -30,9 +31,10 @@ (re-frame/reg-event-fx ::opening - (fn [{:keys [db]} [_ results runnable?]] + (fn [{:keys [db]} [_ results transaction-rule-id runnable?]] {:db (-> db (assoc ::test-results results) + (assoc ::transaction-rule-id transaction-rule-id) (assoc ::checked #{}) (assoc ::runnable? runnable?)) :dispatch [::events/modal-status ::test-results {:visible? true}]})) @@ -46,8 +48,23 @@ (disj checked which) (conj checked which))))) +(re-frame/reg-event-fx + ::apply-rule + [with-user] + (fn [{:keys [db user]} [_ params]] + {:graphql + {:token user + :query-obj {:venia/operation {:operation/type :mutation + :operation/name "MatchTransactionRules"} + :venia/queries [{:query/data [:match-transaction-rules + {:transaction-ids (seq @(re-frame/subscribe [::checked])) + :transaction-rule-id (::transaction-rule-id db)} + transaction-read]}]} + #_#_:on-success [:edited params] + #_#_:on-error [:forms/save-error ::form]}})) -(defn test-results-modal [] + +(defn test-results-modal [params] (let [runnable? @(re-frame/subscribe [::runnable?]) checked @(re-frame/subscribe [::checked]) checked-count @(re-frame/subscribe [::checked-count])] @@ -55,7 +72,8 @@ [modal {:title "Rule results" :hide-event [::events/modal-status ::test-results {:visible? false}] :foot (when runnable? - [:button.button.is-primary {:disabled (boolean (= checked-count 0))} + [:button.button.is-primary {:disabled (boolean (= checked-count 0)) + :on-click (dispatch-event [::apply-rule params])} (str "Apply to " checked-count " transactions")])} [:table.table.is-fullwidth.compact [:tr 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 358ddbf2..85358ae6 100644 --- a/src/cljs/auto_ap/views/pages/admin/rules/table.cljs +++ b/src/cljs/auto_ap/views/pages/admin/rules/table.cljs @@ -23,14 +23,14 @@ [:client [:name]] [:bank-account [:name]] :description-original]]}]} - :on-success [::succeeded-run] + :on-success [::succeeded-run (:id which)] #_#_: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]})) + (fn [{:keys [db]} [_ transaction-rule-id result]] + {:dispatch [::results-modal/opening (:run-transaction-rule result) transaction-rule-id true]})) (defn table [{:keys [id rule-page on-params-change params status]}] (let [opc (fn [p] diff --git a/src/cljs/auto_ap/views/pages/transactions/form.cljs b/src/cljs/auto_ap/views/pages/transactions/form.cljs index a9ca3157..448cd479 100644 --- a/src/cljs/auto_ap/views/pages/transactions/form.cljs +++ b/src/cljs/auto_ap/views/pages/transactions/form.cljs @@ -107,9 +107,9 @@ {:graphql {:token user :query-obj {:venia/operation {:operation/type :mutation - :operation/name "MatchTransactionRule"} - :venia/queries [{:query/data [:match-transaction-rule - {:transaction-id id + :operation/name "MatchTransactionRules"} + :venia/queries [{:query/data [:match-transaction-rules + {:transaction-ids [id] :transaction-rule-id transaction-rule-id} transaction-read]}]} :on-success [::edited params] @@ -118,8 +118,8 @@ (re-frame/reg-event-fx ::edited [(forms/triggers-stop ::form)] - (fn [{:keys [db]} [_ {:keys [edit-completed]} {:keys [edit-transaction match-transaction match-transaction-rule]}]] - {:dispatch (conj edit-completed (or edit-transaction match-transaction match-transaction-rule))})) + (fn [{:keys [db]} [_ {:keys [edit-completed]} {:keys [edit-transaction match-transaction match-transaction-rules]}]] + {:dispatch (conj edit-completed (or edit-transaction match-transaction (first match-transaction-rules)))})) (re-frame/reg-event-db ::manual-match