diff --git a/src/clj/auto_ap/datomic/transaction_rules.clj b/src/clj/auto_ap/datomic/transaction_rules.clj index ad64c166..010d361f 100644 --- a/src/clj/auto_ap/datomic/transaction_rules.clj +++ b/src/clj/auto_ap/datomic/transaction_rules.clj @@ -50,7 +50,7 @@ true (merge-query {:query {:find ['?e] - :where ['[?e :transaction-rule/description]]}}))] + :where ['[?e :transaction-rule/transaction-approval-status]]}}))] (cond->> query @@ -82,5 +82,5 @@ (mapv first (d/query {:query {:find [(list 'pull '?e default-read )] :in ['$] - :where ['[?e :transaction-rule/description]]} + :where ['[?e :transaction-rule/transaction-approval-status]]} :args [(d/db (d/connect uri))]}))) diff --git a/src/clj/auto_ap/graphql/transaction_rules.clj b/src/clj/auto_ap/graphql/transaction_rules.clj index 72f04f5a..a5cb35c9 100644 --- a/src/clj/auto_ap/graphql/transaction_rules.clj +++ b/src/clj/auto_ap/graphql/transaction_rules.clj @@ -4,9 +4,10 @@ [datomic.api :as d] [auto-ap.datomic :refer [remove-nils uri merge-query]] [auto-ap.utils :refer [dollars=]] - [auto-ap.graphql.utils :refer [->graphql <-graphql limited-clients assert-admin result->page]] + [auto-ap.graphql.utils :refer [->graphql <-graphql limited-clients assert-admin result->page snake->kebab]] [clj-time.coerce :as c] - [clojure.set :as set]) + [clojure.set :as set] + [clojure.string :as str]) (:import [java.time.temporal ChronoField])) (defn ident->enum-f [k] @@ -40,12 +41,14 @@ #_(assert-admin (:id context)) (let [existing-transaction (tr/get-by-id id) deleted (deleted-accounts existing-transaction accounts) - _ (println existing-transaction) - _ (println "DELETING" deleted) account-total (reduce + 0 (map (fn [x] (:percentage x)) accounts)) _ (when-not (dollars= 1.0 account-total) (let [error (str "Account total (" account-total ") does not reach 100%")] (throw (ex-info error {:validation-error error})))) + _ (when (and (str/blank? description) + (nil? yodlee_merchant_id)) + (let [error (str "You must provide a description or a yodlee merchant")] + (throw (ex-info error {:validation-error error})))) transaction [(remove-nils #:transaction-rule {:db/id (if id id "transaction-rule") @@ -57,9 +60,15 @@ :amount-lte amount_lte :amount-gte amount_gte :vendor vendor_id - :transaction-approval-status (keyword "transaction-approval-status" (name (<-graphql transaction_approval_status))) + :transaction-approval-status + (some->> transaction_approval_status + name + snake->kebab + (keyword "transaction-approval-status")) :accounts (map transaction-rule-account->entity accounts)})] + _ (println transaction) + transaction (into transaction (map (fn [d] [:db/retract id :transaction-rule/accounts d]) diff --git a/src/clj/auto_ap/yodlee/import.clj b/src/clj/auto_ap/yodlee/import.clj index a832912a..cfa8726e 100644 --- a/src/clj/auto_ap/yodlee/import.clj +++ b/src/clj/auto_ap/yodlee/import.clj @@ -127,7 +127,6 @@ :transaction-rule/amount-gte :transaction-rule/amount-lte :transaction-rule/client :transaction-rule/bank-account :transaction-rule/yodlee-merchant]} ] - (println transaction description) (let [transaction-dom (some-> transaction :transaction/date .toInstant @@ -154,8 +153,8 @@ (:db/id client)) true) (if yodlee-merchant - (= (:transaction/yodlee-merchant transaction) - (:db/id yodlee-merchant)) + (= (:yodlee-merchant/yodlee-id (:transaction/yodlee-merchant transaction)) + (:yodlee-merchant/yodlee-id yodlee-merchant)) true) (if bank-account (= (:transaction/bank-account transaction) diff --git a/src/cljc/auto_ap/entities/transaction_rule.cljc b/src/cljc/auto_ap/entities/transaction_rule.cljc index b8696546..1c46b6a2 100644 --- a/src/cljc/auto_ap/entities/transaction_rule.cljc +++ b/src/cljc/auto_ap/entities/transaction_rule.cljc @@ -1,5 +1,6 @@ (ns auto-ap.entities.transaction-rule - (:require [clojure.spec.alpha :as s])) + (:require [clojure.spec.alpha :as s] + [clojure.string :as str])) (s/def ::client (s/nilable map?)) (s/def ::description (s/nilable string?)) @@ -7,8 +8,22 @@ (s/def ::amount-lte (s/nilable double?)) (s/def ::dom-gte (s/nilable int?)) (s/def ::dom-lte (s/nilable int?)) -(s/def ::note (s/nilable string?)) +(s/def ::note (s/and string? + #(not (str/blank? %)))) (s/def ::bank-account (s/nilable map?)) (s/def ::vendor (s/nilable map?)) +(s/def ::yodlee-merchant (s/nilable map?)) -(s/def ::transaction-rule (s/keys :req-un [::client ::description ::amount-gte ::amount-lte ::dom-gte ::dom-lte ::note ::bank-account ::vendor])) +(s/def ::transaction-rule (s/and (s/keys :req-un [::client + ::description + ::amount-gte + ::amount-lte + ::dom-gte + ::dom-lte + ::note + ::bank-account + ::vendor + ::yodlee-merchant]) + + (s/or :description-required #(not (str/blank? (:description %))) + :description-required #(not (nil? (:yodlee-merchant %)))))) diff --git a/test/clj/auto_ap/graphql.clj b/test/clj/auto_ap/graphql.clj index 3554bd93..a437f37d 100644 --- a/test/clj/auto_ap/graphql.clj +++ b/test/clj/auto_ap/graphql.clj @@ -52,6 +52,15 @@ :location "B"}]}} [:id ]])}]})] (is (thrown? clojure.lang.ExceptionInfo (sut/query nil q))))) + (testing "It should reject rules that are missing both description and merchant" + (let [q (v/graphql-query {:venia/operation {:operation/type :mutation + :operation/name "UpsertTransactionRule"} + :venia/queries [{:query/data (sut/->graphql [:upsert-transaction-rule + {:transaction-rule {:accounts [{:account-id account-id + :percentage "1.0" + :location "B"}]}} + [:id ]])}]})] + (is (thrown? clojure.lang.ExceptionInfo (sut/query nil q))))) (testing "it should add rules" (let [q (v/graphql-query {:venia/operation {:operation/type :mutation :operation/name "UpsertTransactionRule"}