From e876dc7b6df8f5c958ad237f5079a6be970d34d3 Mon Sep 17 00:00:00 2001 From: Bryce Covert Date: Fri, 17 May 2019 08:47:35 -0700 Subject: [PATCH] Replacing when matching. --- src/clj/auto_ap/graphql/transactions.clj | 13 +++--- test/clj/auto_ap/graphql.clj | 54 +++++++++++++++++++++++- 2 files changed, 61 insertions(+), 6 deletions(-) diff --git a/src/clj/auto_ap/graphql/transactions.clj b/src/clj/auto_ap/graphql/transactions.clj index f0523b06..d1772b5c 100644 --- a/src/clj/auto_ap/graphql/transactions.clj +++ b/src/clj/auto_ap/graphql/transactions.clj @@ -130,12 +130,15 @@ (throw (ex-info "Transaction already associated with a payment" {:validation-error "Transaction already associated with a payment"}))) @(d/transact (d/connect uri) - [(rm/apply-rule {:db/id (:db/id transaction) - :transaction/amount (:transaction/amount transaction)} - transaction-rule + (into + [(remove-nils (rm/apply-rule {:db/id (:db/id transaction) + :transaction/amount (:transaction/amount transaction)} + transaction-rule - ;; TODO use bank account locations as well - (-> transaction :transaction/client :client/locations))])) + ;; TODO use bank account locations as well + (-> 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)) diff --git a/test/clj/auto_ap/graphql.clj b/test/clj/auto_ap/graphql.clj index 02bbbc6e..a9e17a90 100644 --- a/test/clj/auto_ap/graphql.clj +++ b/test/clj/auto_ap/graphql.clj @@ -3,8 +3,11 @@ [auto-ap.datomic.migrate :as m] [venia.core :as v] [clojure.test :as t :refer [deftest is testing use-fixtures]] + [clj-time.core :as time] [datomic.api :as d] - [auto-ap.datomic :refer [uri]])) + [auto-ap.datomic :refer [uri]] + [buddy.sign.jwt :as jwt] + [config.core :refer [env]])) (defn wrap-setup [f] (with-redefs [auto-ap.datomic/uri "datomic:mem://datomic-transactor:4334/invoice"] @@ -14,6 +17,12 @@ (d/release (d/connect uri)) (d/delete-database uri))) +(defn admin-token [] + {:user "TEST ADMIN" + :exp (time/plus (time/now) (time/days 1)) + :user/role "admin" + :user/name "TEST ADMIN"}) + (use-fixtures :each wrap-setup) (deftest ledger-page (testing "ledger" @@ -200,3 +209,46 @@ (is (= [{:id "2019-01-05 matching-desc 1"}] (rule-test {:bank-account-id bank-account-1}))) (is (= [{:id "2019-01-15 nonmatching-desc 2"}] (rule-test {:bank-account-id bank-account-2}))))))) +(deftest test-match-transaction-rule + (testing "it should apply a rules" + (let [{:strs [transaction-id transaction-rule-id]} (-> @(d/transact (d/connect uri) + [{:transaction/description-original "matching-desc" + :transaction/date #inst "2019-01-05T00:00:00.000-08:00" + :transaction/client {:client/name "1" + :db/id "client-1"} + :transaction/bank-account {:db/id "bank-account-1" + :bank-account/name "1"} + :transaction/amount 1.00 + :db/id "transaction-id"} + + {:db/id "transaction-rule-id" + :transaction-rule/note "transaction rule note" + :transaction-rule/description "matching-desc" + :transaction-rule/accounts [{:transaction-rule-account/location "A" + :transaction-rule-account/account {:account/numeric-code 123 :db/id "123"} + :transaction-rule-account/percentage 1.0}]}]) + :tempids) + rule-test (-> (sut/query (admin-token) (v/graphql-query {:venia/operation {:operation/type :mutation + :operation/name "MatchTransactionRule"} + :venia/queries [{:query/data (sut/->graphql [:match-transaction-rule + {:transaction-rule-id transaction-rule-id + :transaction-id transaction-id} + [[:matched-rule [:id :note]] [:accounts [:id]] ]])}]})) + :data + :match-transaction-rule)] + + (is (= "transaction rule note" (-> rule-test :matched-rule :note))) + (is (= 1 (-> rule-test :accounts count))) + + (testing "Should replace accounts when matching a second time" + (let [rule-test (-> (sut/query (admin-token) (v/graphql-query {:venia/operation {:operation/type :mutation + :operation/name "MatchTransactionRule"} + :venia/queries [{:query/data (sut/->graphql [:match-transaction-rule + {:transaction-rule-id transaction-rule-id + :transaction-id transaction-id} + [[:matched-rule [:id :note]] [:accounts [:id]] ]])}]})) + :data + :match-transaction-rule)] + (is (= 1 (-> rule-test :accounts count)))))))) + +