diff --git a/src/clj/auto_ap/graphql/checks.clj b/src/clj/auto_ap/graphql/checks.clj index ee1b631f..c430e9dc 100644 --- a/src/clj/auto_ap/graphql/checks.clj +++ b/src/clj/auto_ap/graphql/checks.clj @@ -31,6 +31,7 @@ [clojure.set :as set] [clojure.string :as str] [clojure.tools.logging :as log] + [com.brunobonacci.mulog :as mu] [com.walmartlabs.lacinia.util :refer [attach-resolvers]] [config.core :refer [env]] [datomic.api :as d] @@ -390,14 +391,11 @@ (let [type (keyword "payment-type" (name type)) invoices (d-invoices/get-multi (map :invoice-id invoice-payments)) client (d-clients/get-by-id client-id) - vendors (->> (d/q '[:find [?e ...] - :in $ - :where [?e :vendor/name]] - (d/db conn)) - (d/pull-many (d/db conn) d-vendors/default-read) - (by :db/id)) + invoice-amounts (by :invoice-id :amount invoice-payments) invoices-grouped-by-vendor (group-by (comp :db/id :invoice/vendor) invoices) + vendors (->> (d/pull-many (d/db conn) d-vendors/default-read (keys invoices-grouped-by-vendor)) + (by :db/id)) bank-account (d-bank-accounts/get-by-id bank-account-id) _ (validate-belonging client-id invoices bank-account) _ (when (and (nil? (:bank-account/check-number bank-account)) @@ -405,23 +403,27 @@ (let [message (str "The bank account " (:bank-account/name bank-account) " does not have a starting check number. Please ask the integreat staff to initialize it.")] (throw (ex-info message {:validation-error message})))) - checks (->> (for [[[vendor-id invoices] index] (map vector invoices-grouped-by-vendor (range))] - (invoices->entities invoices (vendors vendor-id) client bank-account type index invoice-amounts)) - (reduce into []) - doall) + checks (mu/trace ::build-checks [] + (->> (for [[[vendor-id invoices] index] (map vector invoices-grouped-by-vendor (range))] + (invoices->entities invoices (vendors vendor-id) client bank-account type index invoice-amounts)) + (reduce into []) + doall)) checks (if (= type :payment-type/check) (conj checks [:inc (:db/id bank-account) :bank-account/check-number (count invoices-grouped-by-vendor)]) checks)] (when (= type :payment-type/check) - (make-pdfs (filter #(and (= :payment-type/check (:payment/type %)) - (> (:payment/amount %) 0.0)) - checks))) + (mu/trace ::making-pdfs [:checks checks] + (make-pdfs (filter #(and (= :payment-type/check (:payment/type %)) + (> (:payment/amount %) 0.0)) + checks)))) (transact-with-ledger checks id) {:invoices (d-invoices/get-multi (map :invoice-id invoice-payments)) :pdf-url (if (= type :payment-type/check) - (merge-pdfs (filter identity (map :payment/s3-key checks))) + (mu/trace ::merge-pdfs + [] + (merge-pdfs (filter identity (map :payment/s3-key checks)))) nil)})) (defn get-payment-page [context args _] diff --git a/src/clj/auto_ap/graphql/invoices.clj b/src/clj/auto_ap/graphql/invoices.clj index 95045818..bd909491 100644 --- a/src/clj/auto_ap/graphql/invoices.clj +++ b/src/clj/auto_ap/graphql/invoices.clj @@ -24,7 +24,8 @@ [clojure.set :as set] [clojure.tools.logging :as log] [datomic.api :as d] - [manifold.deferred :as de])) + [manifold.deferred :as de] + [com.brunobonacci.mulog :as mu])) (defn ->graphql [invoice user ] (if (= "admin" (:user/role user)) @@ -188,20 +189,24 @@ (defn add-and-print-invoice [context {{:keys [total client_id vendor_id] :as in} :invoice bank-account-id :bank_account_id type :type} _] - (assert-no-conflicting in) - (assert-can-see-client (:id context) client_id) - (assert-bank-account-belongs client_id bank-account-id) - (assert-not-locked client_id (:date in)) - (assert-valid-expense-accounts (:expense_accounts in) vendor_id) - (assert-invoice-amounts-add-up in) + (mu/trace ::validating-invoice [:invoice in] + (do + (assert-no-conflicting in) + (assert-can-see-client (:id context) client_id) + (assert-bank-account-belongs client_id bank-account-id) + (assert-not-locked client_id (:date in)) + (assert-valid-expense-accounts (:expense_accounts in) vendor_id) + (assert-invoice-amounts-add-up in))) (let [transaction-result (transact-with-ledger [(add-invoice-transaction in)] (:id context))] - (-> (gq-checks/print-checks-internal [{:invoice-id (get-in transaction-result [:tempids "invoice"]) - :amount total}] - client_id - bank-account-id - type - (:id context)) - u/->graphql))) + (mu/trace ::printing-checks + [] + (-> (gq-checks/print-checks-internal [{:invoice-id (get-in transaction-result [:tempids "invoice"]) + :amount total}] + client_id + bank-account-id + type + (:id context)) + u/->graphql)))) (defn edit-invoice [context {{:keys [id due invoice_number vendor_id total date expense_accounts scheduled_payment] :as in} :invoice} _] (let [invoice (d-invoices/get-by-id id)