When plaid merchant is linked to vendor, automatically code to that vendor.

This commit is contained in:
Bryce
2023-07-24 09:10:28 -07:00
parent dfd1af77c4
commit 445375d15e
54 changed files with 9155 additions and 68 deletions

View File

@@ -141,20 +141,7 @@
:name (first name)}))
[]))
(def single-thread (ex/fixed-thread-executor 1))
(defn rebuild-search-index []
(de/future-with
single-thread
(auto-ap.solr/index-documents-raw
auto-ap.solr/impl
"plaid_merchants"
(for [[result] (dc/qseq {:query '[:find (pull ?v [:plaid-merchant/name :db/id])
:in $
:where [?v :plaid-merchant/name]]
:args [(dc/db conn)]})]
{"id" (:db/id result)
"name" (:plaid-merchant/name result)}))))
(defn attach [schema]
(->

View File

@@ -3,13 +3,17 @@
[auto-ap.datomic :refer [conn random-tempid]]
[auto-ap.import.common :refer [wrap-integration]]
[auto-ap.import.transactions :as t]
[auto-ap.logging :as alog]
[auto-ap.plaid.core :as p]
[auto-ap.solr]
[auto-ap.time :as atime]
[auto-ap.utils :refer [allow-once by]]
[clj-time.coerce :as coerce]
[clj-time.core :as time]
[datomic.api :as dc]
[digest :as di]
[manifold.deferred :as de]
[manifold.executor :as ex]
[unilog.context :as lc]))
(defn get-plaid-accounts [db]
@@ -24,7 +28,7 @@
db )))
(defn plaid->transaction [t]
(defn plaid->transaction [t plaid-merchant->vendor-id]
(cond-> #:transaction {:description-original (:name t)
:raw-id (:transaction_id t)
:db/id (random-tempid)
@@ -36,14 +40,41 @@
:date (coerce/to-date (atime/parse (:authorized_date t) atime/iso-date))
:status "POSTED"}
(:merchant_name t) (assoc :transaction/plaid-merchant {:plaid-merchant/name (:merchant_name t)
:db/id (random-tempid)})))
:db/id (random-tempid)})
(plaid-merchant->vendor-id (:merchant_name t)) (assoc :transaction/default-vendor
(plaid-merchant->vendor-id (:merchant_name t)))))
(defn build-plaid-merchant->vendor-id []
(into {}
(dc/q '[:find ?pmn ?v
:in $
:where
[?v :vendor/plaid-merchant ?pm]
[?pm :plaid-merchant/name ?pmn]]
(dc/db conn ))))
(def single-thread (ex/fixed-thread-executor 1))
(defn rebuild-search-index []
(de/future-with
single-thread
(auto-ap.solr/index-documents-raw
auto-ap.solr/impl
"plaid_merchants"
(for [[result] (dc/qseq {:query '[:find (pull ?v [:plaid-merchant/name :db/id])
:in $
:where [?v :plaid-merchant/name]]
:args [(dc/db conn)]})]
{"id" (:db/id result)
"name" (:plaid-merchant/name result)}))))
(defn import-plaid-int []
(lc/with-context {:source "Import plaid transactions"}
(let [import-batch (t/start-import-batch :import-source/plaid "Automated plaid user")
end (atime/local-now)
start (time/plus end (time/days -30))]
start (time/plus end (time/days -30))
plaid-merchant->vendor-id (build-plaid-merchant->vendor-id)]
(try
(doseq [[bank-account-id client-id external-id access-token] (get-plaid-accounts (dc/db conn))
:let [transaction-result (wrap-integration #(p/get-transactions access-token external-id start end)
@@ -53,13 +84,19 @@
(when (not (:pending transaction))
(t/import-transaction! import-batch (assoc (plaid->transaction (assoc transaction
:account
(accounts-by-id (:account_id transaction))))
(accounts-by-id (:account_id transaction)))
plaid-merchant->vendor-id)
:transaction/bank-account bank-account-id
:transaction/client client-id))))
(try
(rebuild-search-index)
(catch Exception e
(alog/error ::cant-index-plaid
:error e)))
(t/finish! import-batch)
(catch Exception e
(t/fail! import-batch e))))))
(def import-plaid (allow-once import-plaid-int))

View File

@@ -247,6 +247,15 @@
(when-not (seq (match-transaction-to-unpaid-invoices amount client))
(apply-rules transaction valid-locations)))
(defn maybe-apply-default-vendor [t]
(cond-> t
(and (not (:transaction/vendor t))
(:transaction/default-vendor t))
(assoc :transaction/vendor (:transaction/default-vendor t))
true
(dissoc :transaction/default-vendor)))
(defn transaction->txs [transaction bank-account apply-rules]
(let [bank-account-id (:db/id bank-account)
client (:client/_bank-accounts bank-account)
@@ -263,6 +272,7 @@
(assoc :transaction/approval-status :transaction-approval-status/unapproved)
maybe-assoc-check-number
code-fn
(maybe-apply-default-vendor)
remove-nils)))

View File

@@ -28,7 +28,7 @@
:legal-entity-first-name :legal-entity-middle-name :legal-entity-last-name
:legal-entity-tin :legal-entity-tin-type
:legal-entity-1099-type
[:address [:street1 :street2 :city :state :zip]]])
[:address [:id :street1 :street2 :city :state :zip]]])
(re-frame/reg-event-fx
::params-change

View File

@@ -14,4 +14,4 @@
:legal-entity-first-name :legal-entity-middle-name :legal-entity-last-name
:legal-entity-tin :legal-entity-tin-type
:legal-entity-1099-type
[:address [:street1 :street2 :city :state :zip]]])
[:address [:id :street1 :street2 :city :state :zip]]])