Files
integreat/src/clj/auto_ap/import/yodlee2.clj
2022-07-26 05:56:41 -07:00

66 lines
2.8 KiB
Clojure

(ns auto-ap.import.yodlee2
(:require
[auto-ap.datomic :refer [conn]]
[auto-ap.import.common :refer [wrap-integration]]
[auto-ap.import.transactions :as t]
[auto-ap.import.yodlee :as y]
[auto-ap.utils :refer [allow-once heartbeat]]
[auto-ap.yodlee.core2 :as client2]
[com.unbounce.dogstatsd.core :as statsd]
[datomic.api :as d]
[mount.core :as mount]
[unilog.context :as lc]
[yang.scheduler :as scheduler]))
(defn import-yodlee2-int []
(lc/with-context {:source "Import yodlee2 transactions"}
(statsd/event {:title "Yodlee2 import started"
:text "Starting"
:priority :low}
nil)
(let [import-batch (t/start-import-batch :import-source/yodlee2 "Automated yodlee2 user")]
(try
(let [account-lookup (d/q '[:find ?ya ?ba ?cd ?ud
:in $
:where
[?ba :bank-account/yodlee-account ?y]
[(get-else $ ?ba :bank-account/use-date-instead-of-post-date? false) ?ud]
[?c :client/bank-accounts ?ba]
[?c :client/code ?cd]
[?y :yodlee-account/id ?ya]
]
(d/db conn))]
(doseq [[yodlee-account bank-account client-code use-date-instead-of-post-date?] account-lookup
transaction (wrap-integration #(client2/get-specific-transactions client-code yodlee-account)
bank-account)]
(t/import-transaction! import-batch (assoc (y/yodlee->transaction transaction use-date-instead-of-post-date?)
:transaction/bank-account bank-account
:transaction/client [:client/code client-code])))
(t/finish! import-batch))
(statsd/event {:title "Yodlee2 import Finished"
:text (pr-str (t/get-stats import-batch))
:priority :low}
nil)
(catch Exception e
(t/fail! import-batch e)
(statsd/event {:title "Yodlee2 import failed"
:text (str e)
:alert-type :warning
:priority :normal}
nil))))))
(def import-yodlee2 (allow-once import-yodlee2-int))
(mount/defstate import-worker
:start (scheduler/every (* 1000 60 60 4) (heartbeat import-yodlee2 "import-yodlee"))
:stop (scheduler/stop import-worker))
(mount/defstate account-worker
:start (scheduler/every (* 5 60 1000) (heartbeat client2/upsert-accounts "upsert-yodlee2-accounts"))
:stop (scheduler/stop account-worker))