critical feature - you can now unset fields.
This commit is contained in:
@@ -30,7 +30,6 @@
|
||||
(update-in [:args] into (get-in query-part-2 [:args]))))
|
||||
|
||||
(defn remove-nils [m]
|
||||
|
||||
(let [result (reduce-kv
|
||||
(fn [m k v]
|
||||
(if (not (nil? v))
|
||||
@@ -43,6 +42,25 @@
|
||||
result
|
||||
nil)))
|
||||
|
||||
(defn replace-nils-with-retract [updated original]
|
||||
(let [result (reduce-kv
|
||||
(fn [[m & retractions] k v]
|
||||
(cond (and (nil? v)
|
||||
(not (nil? (get original k))))
|
||||
(into [m] (conj retractions [:db/retract (:db/id original) k (or (:db/id (get original k))
|
||||
(get original k))]))
|
||||
|
||||
(nil? v)
|
||||
(into [m] retractions)
|
||||
|
||||
:else
|
||||
(into [(assoc m k v)] retractions)))
|
||||
[{}]
|
||||
updated)]
|
||||
(if (seq result)
|
||||
result
|
||||
nil)))
|
||||
|
||||
|
||||
(def vendor-schema
|
||||
[{:db/ident :vendor/original-id
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
(:require [auto-ap.datomic.transaction-rules :as tr]
|
||||
[auto-ap.datomic.transactions :as t]
|
||||
[datomic.api :as d]
|
||||
[auto-ap.datomic :refer [remove-nils uri merge-query]]
|
||||
[auto-ap.datomic :refer [remove-nils uri merge-query replace-nils-with-retract]]
|
||||
[auto-ap.utils :refer [dollars=]]
|
||||
[auto-ap.graphql.utils :refer [->graphql <-graphql limited-clients assert-admin result->page snake->kebab]]
|
||||
[clj-time.coerce :as c]
|
||||
@@ -59,23 +59,24 @@
|
||||
(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")
|
||||
:description description
|
||||
:note note
|
||||
:client client_id
|
||||
:bank-account bank_account_id
|
||||
:yodlee-merchant yodlee_merchant_id
|
||||
:amount-lte amount_lte
|
||||
:amount-gte amount_gte
|
||||
:vendor vendor_id
|
||||
:transaction-approval-status
|
||||
(some->> transaction_approval_status
|
||||
name
|
||||
snake->kebab
|
||||
(keyword "transaction-approval-status"))
|
||||
:accounts (map transaction-rule-account->entity accounts)})]
|
||||
transaction (replace-nils-with-retract #:transaction-rule {:db/id (if id
|
||||
id
|
||||
"transaction-rule")
|
||||
:description description
|
||||
:note note
|
||||
:client client_id
|
||||
:bank-account bank_account_id
|
||||
:yodlee-merchant yodlee_merchant_id
|
||||
:amount-lte amount_lte
|
||||
:amount-gte amount_gte
|
||||
:vendor vendor_id
|
||||
:transaction-approval-status
|
||||
(some->> transaction_approval_status
|
||||
name
|
||||
snake->kebab
|
||||
(keyword "transaction-approval-status"))
|
||||
:accounts (map transaction-rule-account->entity accounts)}
|
||||
existing-transaction)
|
||||
|
||||
_ (println transaction)
|
||||
|
||||
|
||||
@@ -52,6 +52,8 @@
|
||||
: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"}
|
||||
@@ -90,6 +92,25 @@
|
||||
(is (= :approved (:transaction-approval-status result)))
|
||||
(is (= "hello" (-> result :accounts (get 0) :account :name )))
|
||||
(is (:id result))
|
||||
|
||||
(testing "it should unset removed fields"
|
||||
(let [q (v/graphql-query {:venia/operation {:operation/type :mutation
|
||||
:operation/name "UpsertTransactionRule"}
|
||||
:venia/queries [{:query/data (sut/->graphql [:upsert-transaction-rule
|
||||
{:transaction-rule {:id (:id result)
|
||||
:description "123"
|
||||
:vendor-id nil
|
||||
:accounts [{:id (-> result :accounts (get 0) :id)
|
||||
:account-id account-id
|
||||
:percentage "1.0"
|
||||
:location "B"}]}}
|
||||
[[:vendor [:name]]]])}]})
|
||||
result (-> (sut/query nil q)
|
||||
:data
|
||||
:upsert-transaction-rule)]
|
||||
|
||||
(is (nil? (:vendor result)))))
|
||||
|
||||
(testing "it should delete removed rules"
|
||||
(let [q (v/graphql-query {:venia/operation {:operation/type :mutation
|
||||
:operation/name "UpsertTransactionRule"}
|
||||
|
||||
Reference in New Issue
Block a user