From 0e1261269bb54970abd27282ee35eccd6071606b Mon Sep 17 00:00:00 2001 From: Bryce Covert Date: Tue, 27 Oct 2020 21:03:47 -0700 Subject: [PATCH] allowing users to self service delete links to payments. --- src/clj/auto_ap/graphql.clj | 5 +++ src/clj/auto_ap/graphql/transactions.clj | 37 +++++++++++++++++++ .../views/pages/transactions/form.cljs | 21 +++++++++++ 3 files changed, 63 insertions(+) diff --git a/src/clj/auto_ap/graphql.clj b/src/clj/auto_ap/graphql.clj index fab231bd..c90607a4 100644 --- a/src/clj/auto_ap/graphql.clj +++ b/src/clj/auto_ap/graphql.clj @@ -911,6 +911,10 @@ :all {:type 'Boolean} :transaction_rule_id {:type :id}} :resolve :mutation/match-transaction-rules} + + :unlink_transaction {:type :transaction + :args {:transaction_id {:type :id}} + :resolve :mutation/unlink-transaction} :void_invoice {:type :invoice :args {:invoice_id {:type :id}} :resolve :mutation/void-invoice} @@ -1183,6 +1187,7 @@ :mutation/add-and-print-invoice gq-invoices/add-and-print-invoice :mutation/edit-invoice gq-invoices/edit-invoice :mutation/edit-transaction gq-transactions/edit-transaction + :mutation/unlink-transaction gq-transactions/unlink-transaction :mutation/unapprove-transactions gq-transactions/unapprove-transactions :mutation/delete-external-ledger gq-ledger/delete-external-ledger :mutation/delete-transactions gq-transactions/delete-transactions diff --git a/src/clj/auto_ap/graphql/transactions.clj b/src/clj/auto_ap/graphql/transactions.clj index 9a67e5e9..bf1eddd5 100644 --- a/src/clj/auto_ap/graphql/transactions.clj +++ b/src/clj/auto_ap/graphql/transactions.clj @@ -80,6 +80,43 @@ (:id context)) {:message (str "Succesfully deleted " (count all-ids) " transactions.")})) +(defn unlink-transaction [context args value] + (let [_ (assert-admin (:id context)) + args (assoc args :id (:id context)) + transaction-id (:transaction_id args) + transaction (d/pull (d/db conn) + [:transaction/approval-status + :transaction/status + :transaction/location + :transaction/vendor + :transaction/accounts + + {:transaction/payment [{:payment/status [:db/ident]} :db/id]} ] + transaction-id) + payment (-> transaction :transaction/payment ) + ] + + (log/info "Unlinking " transaction-id " from payment " payment) + (when (not= :payment-status/cleared (-> payment :payment/status :db/ident)) + (throw (ex-info "Payment can't be undone because it isn't cleared." {:validation-error "Payment can't be undone because it isn't cleared."}))) + (audit-transact + (into [{:db/id (:db/id payment) + :payment/status :payment-status/pending} + {:db/id transaction-id + :transaction/approval-status :transaction-approval-status/unapproved} + [:db/retract transaction-id :transaction/payment (:db/id payment)] + [:db/retract transaction-id :transaction/vendor (:db/id (:transaction/vendor transaction))] + [:db/retract transaction-id :transaction/location (:transaction/location transaction)]] + + (map (fn [a] + [:db/retract transaction-id :transaction/accounts (:db/id a)]) + (:transaction/accounts transaction))) + (:id context)) + (-> (d-transactions/get-by-id transaction-id) + approval-status->graphql + ->graphql))) + + (defn transaction-account->entity [{:keys [id account_id amount location]}] (remove-nils #:transaction-account {:amount (Double/parseDouble amount) :db/id id diff --git a/src/cljs/auto_ap/views/pages/transactions/form.cljs b/src/cljs/auto_ap/views/pages/transactions/form.cljs index be8ee2ae..dab015c7 100644 --- a/src/cljs/auto_ap/views/pages/transactions/form.cljs +++ b/src/cljs/auto_ap/views/pages/transactions/form.cljs @@ -122,6 +122,21 @@ [::edited (first (:match-transaction-rules result))]) :on-error [::forms/save-error ::form]}})) +(re-frame/reg-event-fx + ::unlink + [with-user (forms/triggers-loading ::form) (forms/in-form ::form)] + (fn [{{{:keys [id]} :data} :db user :user} [_]] + {:graphql + {:token user + :query-obj {:venia/operation {:operation/type :mutation + :operation/name "UnlinkTransaction"} + :venia/queries [{:query/data [:unlink-transaction + {:transaction-id id} + transaction-read]}]} + :on-success (fn [result] + [::edited (:unlink-transaction result)]) + :on-error [::forms/save-error ::form]}})) + (re-frame/reg-event-fx ::edited [(forms/triggers-stop ::form)] @@ -274,6 +289,12 @@ :type "typeahead-entity" :field [:forecast-match]}]) + (when (and (:payment data) + is-admin?) + [:p.notification.is-info.is-light>div.level>div.level-left + [:div.level-item "This transaction is linked to a payment "] + [:div.level-item [:button.button.is-warning {:on-click (dispatch-event [::unlink])} "Unlink"]]]) + (error-notification) (when-not should-disable-for-client?