Files
integreat/src/clj/auto_ap/graphql/transactions.clj
2020-04-28 20:43:20 -07:00

179 lines
9.0 KiB
Clojure

(ns auto-ap.graphql.transactions
(:require [auto-ap.graphql.utils :refer [->graphql <-graphql assert-can-see-client assert-admin ident->enum-f snake->kebab enum->keyword]]
[auto-ap.datomic.transactions :as d-transactions]
[auto-ap.datomic.vendors :as d-vendors]
[auto-ap.datomic.checks :as d-checks]
[auto-ap.graphql.transaction-rules :as g-tr]
[datomic.api :as d]
[auto-ap.datomic :refer [uri remove-nils]]
[com.walmartlabs.lacinia :refer [execute]]
[com.walmartlabs.lacinia.executor :as executor]
[com.walmartlabs.lacinia.resolve :as resolve]
[auto-ap.utils :refer [by dollars=]]
[auto-ap.time :refer [parse normal-date]]
[auto-ap.datomic.clients :as d-clients]
[clojure.set :as set]
[clojure.string :as str]
[auto-ap.datomic.accounts :as a]
[auto-ap.datomic.transaction-rules :as tr]
[auto-ap.rule-matching :as rm]
[clj-time.coerce :as coerce]))
(defn prn-each [xs]
(doseq [x xs]
(println x))
xs)
(def approval-status->graphql (ident->enum-f :transaction/approval-status))
(defn get-transaction-page [context args value]
(let [args (assoc args :id (:id context))
[transactions transactions-count] (d-transactions/get-graphql (update (<-graphql args) :approval-status enum->keyword "transaction-approval-status"))
transactions (map ->graphql (map approval-status->graphql transactions))]
[{:transactions transactions
:total transactions-count
:count (count transactions)
:start (:start args 0)
:end (+ (:start args 0) (count transactions))}]))
(defn unapprove-transactions [context args value]
(let [args (assoc args :id (:id context))
ids (:ids (d-transactions/raw-graphql-ids (update (<-graphql args) :approval-status enum->keyword "transaction-approval-status")))]
(d-transactions/unapprove ids)
(get-transaction-page context args value)))
(defn transaction-account->entity [{:keys [id account_id amount location]}]
(remove-nils #:transaction-account {:amount (Double/parseDouble amount)
:db/id id
:account account_id
:location location}))
(defn deleted-accounts [transaction accounts]
(let [current-accounts (:transaction/accounts transaction)
specified-ids (->> accounts
(map :id)
set)
existing-ids (->> current-accounts
(map :db/id)
set)]
(set/difference existing-ids specified-ids)))
(defn edit-transaction [context {{:keys [id accounts vendor_id approval_status] :as transaction} :transaction} value]
(let [existing-transaction (d-transactions/get-by-id id)
_ (assert-can-see-client (:id context) (:transaction/client existing-transaction) )
deleted (deleted-accounts existing-transaction accounts)
account-total (reduce + 0 (map (fn [x] (Double/parseDouble (:amount x))) accounts))
missing-locations (seq (set/difference
(->> accounts
(map :location)
set)
(-> (:transaction/client existing-transaction)
:client/locations
set
(conj "A")
(conj "HQ"))))]
(when-not (dollars= (Math/abs (:transaction/amount existing-transaction)) account-total)
(let [error (str "Expense account total (" account-total ") does not equal transaction total (" (Math/abs (:transaction/amount existing-transaction)) ")")]
(throw (ex-info error {:validation-error error}))))
(when missing-locations
(throw (ex-info (str "Location '" (str/join ", " missing-locations) "' not found on client.") {})) )
@(d/transact (d/connect uri)
(concat [(remove-nils {:db/id id
:transaction/vendor vendor_id
:transaction/approval-status (some->> approval_status
name
snake->kebab
(keyword "transaction-approval-status"))
:transaction/accounts (map transaction-account->entity accounts)
})]
(map (fn [d]
[:db/retract id :transaction/accounts d])
deleted)))
(-> (d-transactions/get-by-id id)
approval-status->graphql
->graphql)))
(defn match-transaction [context {:keys [transaction_id payment_id]} value]
(let [transaction (d-transactions/get-by-id transaction_id)
payment (d-checks/get-by-id payment_id)
_ (assert-can-see-client (:id context) (:transaction/client transaction) )
_ (assert-can-see-client (:id context) (:payment/client payment) )]
(when (not= (:db/id (:transaction/client transaction))
(:db/id (:payment/client payment)))
(throw (ex-info "Clients don't match" {:validation-error "Payment and client do not match."})))
(when-not (dollars= (- (:transaction/amount transaction))
(:payment/amount payment))
(throw (ex-info "Amounts don't match" {:validation-error "Amounts don't match"})))
@(d/transact (d/connect uri)
(into
[{:db/id (:db/id payment)
:payment/status :payment-status/cleared}
{:db/id (:db/id transaction)
:transaction/payment (:db/id payment)
:transaction/vendor (:db/id (:payment/vendor payment))
:transaction/location "A"
:transaction/accounts [{:transaction-account/account (:db/id (a/get-account-by-numeric-code-and-sets 2110 ["default"]))
:transaction-account/location "A"
:transaction-account/amount (Math/abs (:transaction/amount transaction))}]}]
(map (fn [x] [:db/retractEntity (:db/id x)] )
(:transaction/accounts transaction)))))
(-> (d-transactions/get-by-id transaction_id)
approval-status->graphql
->graphql))
(defn match-transaction-rules [context {:keys [transaction_ids transaction_rule_id all]} value]
(let [_ (assert-admin (:id context))
transaction_ids (if all
(->> (g-tr/run-transaction-rule context {:transaction_rule_id transaction_rule_id
:count Integer/MAX_VALUE} nil)
(filter #(not (:transaction/payment %)))
(map :id ))
transaction_ids)
transactions (transduce
(comp
(map d-transactions/get-by-id)
(map #(update % :transaction/date coerce/to-date)))
conj
[]
transaction_ids)
transaction-rule (update (tr/get-by-id transaction_rule_id) :transaction-rule/description #(some-> % re-pattern))]
(doseq [transaction transactions]
(when (not (rm/rule-applies? transaction transaction-rule))
(throw (ex-info "Transaction rule does not apply" {:validation-error "Transaction rule does not apply"
:transaction-rule transaction-rule
:transaction transaction})))
(when (:transaction/payment transaction)
(throw (ex-info "Transaction already associated with a payment" {:validation-error "Transaction already associated with a payment"}))))
@(d/transact (d/connect uri)
(transduce
(map #(into
[(remove-nils (rm/apply-rule {:db/id (:db/id %)
:transaction/amount (:transaction/amount %)}
transaction-rule
(or (-> % :transaction/bank-account :bank-account/locations)
(-> % :transaction/client :client/locations))))]
(map (fn [x] [:db/retractEntity (:db/id x)] )
(:transaction/accounts %))))
into
[]
transactions))
)
(transduce
(comp
(map d-transactions/get-by-id)
(map approval-status->graphql)
(map ->graphql))
conj
[]
transaction_ids ))