enormous refactor but simplified much!
This commit is contained in:
74
src/clj/auto_ap/import/intuit.clj
Normal file
74
src/clj/auto_ap/import/intuit.clj
Normal file
@@ -0,0 +1,74 @@
|
||||
(ns auto-ap.import.intuit
|
||||
(:require [amazonica.aws.s3 :as s3]
|
||||
[auto-ap.datomic :refer [conn remove-nils]]
|
||||
[auto-ap.intuit.core :as i]
|
||||
[auto-ap.utils :refer [by allow-once]]
|
||||
[auto-ap.import.transactions :as t]
|
||||
[clj-time.coerce :as coerce]
|
||||
[clj-time.core :as time]
|
||||
[clj-time.format :as f]
|
||||
[clojure.string :as str]
|
||||
[clojure.tools.logging :as log]
|
||||
[datomic.api :as d]
|
||||
[mount.core :as mount]
|
||||
[unilog.context :as lc]
|
||||
[yang.scheduler :as scheduler]))
|
||||
|
||||
(defn get-intuit-bank-accounts [db]
|
||||
(d/q '[:find ?external-id ?ba ?c
|
||||
:in $
|
||||
:where
|
||||
[?c :client/bank-accounts ?ba]
|
||||
[?ba :bank-account/intuit-bank-account ?iab]
|
||||
[?iab :intuit-bank-account/external-id ?external-id]]
|
||||
db))
|
||||
|
||||
(defn intuit->transaction [transaction]
|
||||
{:transaction/description-original (:Memo/Description transaction)
|
||||
:transaction/amount (Double/parseDouble (:Amount transaction))
|
||||
:transaction/date (coerce/to-date (auto-ap.time/parse (:Date transaction) auto-ap.time/iso-date))
|
||||
:transaction/status "POSTED"})
|
||||
|
||||
(defn intuits->transactions [transactions bank-account-id client-id]
|
||||
(->> transactions
|
||||
(map intuit->transaction)
|
||||
(map #(assoc %
|
||||
:transaction/bank-account bank-account-id
|
||||
:transaction/client client-id))
|
||||
(t/apply-synthetic-ids)))
|
||||
|
||||
(defn import-intuit []
|
||||
(lc/with-context {:source "Import intuit transactions"}
|
||||
(let [import-batch (t/start-import-batch :import-source/intuit "Automated intuit user")
|
||||
db (d/db conn)
|
||||
end (auto-ap.time/local-now)
|
||||
start (time/plus end (time/days -30))]
|
||||
(try
|
||||
(doseq [[external-id bank-account-id client-id] (get-intuit-bank-accounts db)
|
||||
transaction (-> (i/get-transactions (auto-ap.time/unparse start auto-ap.time/iso-date)
|
||||
(auto-ap.time/unparse end auto-ap.time/iso-date)
|
||||
external-id)
|
||||
(intuits->transactions bank-account-id client-id))]
|
||||
(t/import-transaction! import-batch transaction))
|
||||
(t/finish! import-batch)
|
||||
(catch Exception e
|
||||
(t/fail! import-batch e))))))
|
||||
|
||||
(def upsert-transactions (allow-once upsert-transactions))
|
||||
|
||||
(defn upsert-accounts []
|
||||
(let [token (i/get-fresh-access-token)
|
||||
bank-accounts (i/get-bank-accounts token)]
|
||||
@(d/transact conn (mapv
|
||||
(fn [ba]
|
||||
{:intuit-bank-account/external-id (:name ba)
|
||||
:intuit-bank-account/name (:name ba)})
|
||||
bank-accounts))))
|
||||
|
||||
(mount/defstate import-worker
|
||||
:start (scheduler/every (* 1000 60 60 24) import-intuit)
|
||||
:stop (scheduler/stop import-worker))
|
||||
|
||||
(mount/defstate account-worker
|
||||
:start (scheduler/every (* 1000 60 60 24) upsert-accounts)
|
||||
:stop (scheduler/stop account-worker))
|
||||
Reference in New Issue
Block a user