From 14156e6ced25343b97dff258545d33e89c991f56 Mon Sep 17 00:00:00 2001 From: Bryce Covert Date: Thu, 4 May 2023 21:23:09 -0700 Subject: [PATCH] (cloud) Saving things now update the search index --- src/clj/auto_ap/graphql/checks.clj | 40 ++++++++++++++---------- src/clj/auto_ap/graphql/invoices.clj | 5 ++- src/clj/auto_ap/graphql/ledger.clj | 6 ++-- src/clj/auto_ap/graphql/transactions.clj | 13 ++++++-- src/clj/auto_ap/import/transactions.clj | 13 +++++--- src/clj/auto_ap/solr.clj | 32 +++++++++++++------ 6 files changed, 71 insertions(+), 38 deletions(-) diff --git a/src/clj/auto_ap/graphql/checks.clj b/src/clj/auto_ap/graphql/checks.clj index 845ffb44..df0e8f7c 100644 --- a/src/clj/auto_ap/graphql/checks.clj +++ b/src/clj/auto_ap/graphql/checks.clj @@ -35,7 +35,8 @@ [config.core :refer [env]] [clj-time.coerce :as coerce] [datomic.api :as dc] - [digest]) + [digest] + [auto-ap.solr :as solr]) (:import (java.io ByteArrayOutputStream) (java.text DecimalFormat) @@ -414,11 +415,12 @@ (make-pdfs (filter #(and (= :payment-type/check (:payment/type %)) (> (:payment/amount %) 0.0)) checks)))) - (audit-transact (map #(if (map? %) - (dissoc % :payment/pdf-data) - %) checks ) id) - - + (let [result (audit-transact (map #(if (map? %) + (dissoc % :payment/pdf-data) + %) checks ) id)] + + (doseq [i (:tempids result)] + (solr/touch-with-ledger i))) {:invoices (d-invoices/get-multi (map :invoice-id invoice-payments)) :pdf-url (if (= type :payment-type/check) (mu/trace ::merge-pdfs @@ -475,14 +477,16 @@ 0 invoice-payment-lookup)] - (audit-transact - (into [(assoc base-payment - :payment/type :payment-type/check - :payment/status :payment-status/pending - :payment/check-number (:check_number args) - :payment/date (c/to-date (parse (:date args) iso-date)))] - (invoice-payments invoices invoice-payment-lookup)) - (:id context)) + (let [result (audit-transact + (into [(assoc base-payment + :payment/type :payment-type/check + :payment/status :payment-status/pending + :payment/check-number (:check_number args) + :payment/date (c/to-date (parse (:date args) iso-date)))] + (invoice-payments invoices invoice-payment-lookup)) + (:id context))] + (doseq [i (:tempids result)] + (solr/touch-with-ledger i))) (->graphql {:s3-url nil :invoices (d-invoices/get-multi (map :invoice_id (:invoice_payments args)))}))) @@ -648,9 +652,11 @@ :payment/type :payment-type/balance-credit :payment/status :payment-status/cleared}] - (audit-transact (-> [] - (conj payment) - (into (invoice-payments invoices invoice-amounts))) (:id context)) + (let [result (audit-transact (-> [] + (conj payment) + (into (invoice-payments invoices invoice-amounts))) (:id context))] + (doseq [n (:tempids result)] + (solr/touch-with-ledger n))) (->graphql {:invoices (d-invoices/get-multi (map :db/id invoices))}))) (def objects diff --git a/src/clj/auto_ap/graphql/invoices.clj b/src/clj/auto_ap/graphql/invoices.clj index 1159413a..9f7705a3 100644 --- a/src/clj/auto_ap/graphql/invoices.clj +++ b/src/clj/auto_ap/graphql/invoices.clj @@ -22,7 +22,8 @@ [clj-time.core :as time] [clojure.tools.logging :as log] [com.brunobonacci.mulog :as mu] - [datomic.api :as dc])) + [datomic.api :as dc] + [auto-ap.solr :as solr])) (defn ->graphql [invoice user ] (if (= "admin" (:user/role user)) @@ -176,6 +177,7 @@ (assert-invoice-amounts-add-up in) (let [transaction-result (audit-transact [(add-invoice-transaction in)] (:id context))] + (solr/touch-with-ledger (get-in transaction-result [:tempids "invoice"])) (-> (d-invoices/get-by-id (get-in transaction-result [:tempids "invoice"])) (->graphql (:id context))))) @@ -231,6 +233,7 @@ :invoice/scheduled-payment (coerce/to-date scheduled_payment)}] (audit-transact [[:upsert-invoice updated-invoice]] (:id context)) + (solr/touch-with-ledger id) (-> (d-invoices/get-by-id id) (->graphql (:id context))))) diff --git a/src/clj/auto_ap/graphql/ledger.clj b/src/clj/auto_ap/graphql/ledger.clj index 693e2692..13fa5ad0 100644 --- a/src/clj/auto_ap/graphql/ledger.clj +++ b/src/clj/auto_ap/graphql/ledger.clj @@ -28,7 +28,8 @@ [clojure.tools.logging :as log] [com.brunobonacci.mulog :as mu] [datomic.api :as dc] - [iol-ion.tx :refer [random-tempid]]) + [iol-ion.tx :refer [random-tempid]] + [auto-ap.solr :as solr]) (:import (org.apache.commons.codec.binary Base64))) @@ -531,7 +532,8 @@ #_(log/info (map :tx success)) (mu/trace ::success-tx [:count (count success)] - (audit-transact-batch (map :tx success) (:id context))) + (doseq [n (:tempids (audit-transact-batch (map :tx success) (:id context)))] + (solr/touch n))) {:successful (map (fn [x] {:external_id (:external_id x)}) success) :ignored (map (fn [x] diff --git a/src/clj/auto_ap/graphql/transactions.clj b/src/clj/auto_ap/graphql/transactions.clj index 47fe74cc..f996ca75 100644 --- a/src/clj/auto_ap/graphql/transactions.clj +++ b/src/clj/auto_ap/graphql/transactions.clj @@ -27,7 +27,8 @@ [clojure.tools.logging :as log] [datomic.api :as dc] [iol-ion.tx :refer [random-tempid]] - [com.brunobonacci.mulog :as mu])) + [com.brunobonacci.mulog :as mu] + [auto-ap.solr :as solr])) (def approval-status->graphql (ident->enum-f :transaction/approval-status)) @@ -369,6 +370,7 @@ :transaction/accounts (map transaction-account->entity accounts) :transaction/forecast-match forecast_match}]] (:id context)) + (solr/touch-with-ledger id) (-> (d-transactions/get-by-id id) approval-status->graphql ->graphql))) @@ -403,6 +405,7 @@ :transaction-account/location "A" :transaction-account/amount (Math/abs (:transaction/amount transaction))}]}]]) (:id context))) + (solr/touch-with-ledger transaction_id) (-> (d-transactions/get-by-id transaction_id) approval-status->graphql ->graphql)) @@ -437,7 +440,7 @@ (:db/id (:transaction/client transaction)))] (log/info "Adding a new payment" payment-tx) (audit-transact payment-tx (:id context))) - + (solr/touch-with-ledger transaction_id) (-> (d-transactions/get-by-id transaction_id) approval-status->graphql ->graphql))) @@ -475,6 +478,7 @@ (:db/id (:transaction/bank-account transaction)) (:db/id (:transaction/client transaction)))] (audit-transact payment-tx (:id context))) + (solr/touch-with-ledger transaction_id) (-> (d-transactions/get-by-id transaction_id) approval-status->graphql @@ -505,7 +509,7 @@ (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}))) + :transaction transaction}))) (when (:transaction/payment transaction) @@ -520,6 +524,9 @@ (-> t :transaction/client :client/locations))))]) transactions) (:id context)) + + (doseq [n transactions] + (solr/touch-with-ledger (:db/id n))) ) (transduce (comp diff --git a/src/clj/auto_ap/import/transactions.clj b/src/clj/auto_ap/import/transactions.clj index d6b07563..de25f1ed 100644 --- a/src/clj/auto_ap/import/transactions.clj +++ b/src/clj/auto_ap/import/transactions.clj @@ -13,7 +13,8 @@ [clojure.core.cache :as cache] [clojure.tools.logging :as log] [datomic.api :as dc] - [digest :as di])) + [digest :as di] + [auto-ap.solr :as solr])) (defn rough-match [client-id bank-account-id amount] (if (and client-id bank-account-id amount) @@ -317,11 +318,13 @@ :error :import-batch/error :not-ready :import-batch/not-ready) inc)) (when (= :import action) - (audit-transact [[:upsert-transaction (transaction->txs transaction bank-account rule-applying-function)] - {:db/id import-id - :import-batch/entry (:db/id transaction)}] + (let [result (audit-transact [[:upsert-transaction (transaction->txs transaction bank-account rule-applying-function)] + {:db/id import-id + :import-batch/entry (:db/id transaction)}] {:user/name user - :user/role ":admin"})))) + :user/role ":admin"})] + (doseq [n (:tempids result)] + (solr/touch-with-ledger n)))))) (get-stats [_] @stats) diff --git a/src/clj/auto_ap/solr.clj b/src/clj/auto_ap/solr.clj index 1d7c7963..fdfcec51 100644 --- a/src/clj/auto_ap/solr.clj +++ b/src/clj/auto_ap/solr.clj @@ -1,13 +1,13 @@ (ns auto-ap.solr (:require - [auto-ap.datomic :refer [conn]] - [clj-http.client :as client] - [clojure.data.json :as json] - [config.core :refer [env]] - [datomic.api :as dc] - [clj-time.coerce :as c] + [auto-ap.datomic :refer [conn pull-id]] [auto-ap.time :as atime] - [clojure.string :as str])) + [clj-http.client :as client] + [clj-time.coerce :as c] + [clojure.data.json :as json] + [clojure.string :as str] + [config.core :refer [env]] + [datomic.api :as dc])) (def solr-uri (:solr-uri env)) @@ -34,8 +34,7 @@ "journal-entry" :else - (throw (ex-info "Trying to index unknown entity" - {:id d})))))) + nil)))) (defmethod datomic->solr "transaction" [d] (let [i (dc/pull (dc/db conn) '[:db/id :transaction/description-original @@ -54,6 +53,9 @@ "vendor_id" (-> i :transaction/vendor :db/id) "type" "transaction"})) +(defmethod datomic->solr nil [d] + nil) + (defmethod datomic->solr "journal-entry" [d] (let [i (dc/pull (dc/db conn) '[:db/id :journal-entry/amount @@ -123,7 +125,7 @@ :socket-timeout 30000 :connection-timeout 30000 :method "POST" - :body (json/write-str (map datomic->solr xs))})) + :body (json/write-str (filter identity (map datomic->solr xs)))})) (defn query [q] (-> (client/post (str solr-uri "/solr/invoices/query") @@ -145,3 +147,13 @@ :method "POST" :body (json/write-str {"delete" {"query" "*:*"}})}) ) + + +(defn touch-with-ledger [i] + (index-documents [i + (pull-id (dc/db conn) [:journal-entry/original-entity i])])) + +(defn touch [i] + (index-documents [i])) + +