From de7fecc364d782062d20473624267268efe52fe3 Mon Sep 17 00:00:00 2001 From: Bryce Covert Date: Thu, 16 May 2019 22:08:10 -0700 Subject: [PATCH] critical feature - you can now unset fields. --- src/clj/auto_ap/datomic.clj | 20 +++++++++- src/clj/auto_ap/graphql/transaction_rules.clj | 37 ++++++++++--------- test/clj/auto_ap/graphql.clj | 21 +++++++++++ 3 files changed, 59 insertions(+), 19 deletions(-) diff --git a/src/clj/auto_ap/datomic.clj b/src/clj/auto_ap/datomic.clj index 224d2390..f987e052 100644 --- a/src/clj/auto_ap/datomic.clj +++ b/src/clj/auto_ap/datomic.clj @@ -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 diff --git a/src/clj/auto_ap/graphql/transaction_rules.clj b/src/clj/auto_ap/graphql/transaction_rules.clj index dc93664f..344eec45 100644 --- a/src/clj/auto_ap/graphql/transaction_rules.clj +++ b/src/clj/auto_ap/graphql/transaction_rules.clj @@ -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) diff --git a/test/clj/auto_ap/graphql.clj b/test/clj/auto_ap/graphql.clj index a437f37d..02bbbc6e 100644 --- a/test/clj/auto_ap/graphql.clj +++ b/test/clj/auto_ap/graphql.clj @@ -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"}