Yodlee is manually refreshable
This commit is contained in:
@@ -1,8 +1,8 @@
|
||||
(ns auto-ap.datomic.clients
|
||||
(:require [datomic.api :as d]
|
||||
[auto-ap.datomic :refer [uri]]
|
||||
(:require [auto-ap.datomic :refer [conn uri]]
|
||||
[clj-time.coerce :as coerce]
|
||||
[clojure.tools.logging :as log]
|
||||
[clj-time.coerce :as coerce]))
|
||||
[datomic.api :as d]))
|
||||
|
||||
(defn cleanse [e]
|
||||
(-> e
|
||||
@@ -31,21 +31,14 @@
|
||||
|
||||
))
|
||||
(defn get-by-id [id]
|
||||
|
||||
(->>
|
||||
(d/query (-> {:query {:find ['(pull ?e [*
|
||||
{:client/bank-accounts [* {:bank-account/type [*]
|
||||
:bank-account/yodlee-account [:yodlee-account/name :yodlee-account/id :yodlee-account/number]}]}
|
||||
{:yodlee-provider-account/_client [*]}])]
|
||||
:in ['$ '?e]
|
||||
:where [['?e]]}
|
||||
:args [(d/db (d/connect uri)) id]}
|
||||
))
|
||||
(first)
|
||||
(first)
|
||||
(cleanse)
|
||||
#_(map first)
|
||||
|
||||
#_(first)))
|
||||
(d/pull (d/db conn )
|
||||
'[* {:client/bank-accounts [* {:bank-account/type [*]
|
||||
:bank-account/yodlee-account [:yodlee-account/name :yodlee-account/id :yodlee-account/number]}]}
|
||||
{:yodlee-provider-account/_client [*]}]
|
||||
id)
|
||||
(cleanse)))
|
||||
|
||||
(defn code->id [code]
|
||||
(->>
|
||||
|
||||
@@ -80,8 +80,6 @@
|
||||
:address/city (:city (:address edit_client))
|
||||
:address/state (:state (:address edit_client))
|
||||
:address/zip (:zip (:address edit_client))})
|
||||
|
||||
|
||||
:client/bank-accounts (map #(remove-nils
|
||||
(cond-> {:db/id (:id %)
|
||||
:bank-account/code (:code %)
|
||||
@@ -99,11 +97,9 @@
|
||||
:bank-account/locations (:locations %)
|
||||
|
||||
:bank-account/yodlee-account-id (:yodlee_account_id %)
|
||||
|
||||
:bank-account/type (keyword "bank-account-type" (name (:type %)))
|
||||
}
|
||||
(:yodlee_account %) (assoc :bank-account/yodlee-account [:yodlee-account/id (:yodlee_account %)]))
|
||||
) (:bank_accounts edit_client))
|
||||
:bank-account/type (keyword "bank-account-type" (name (:type %)))}
|
||||
(:yodlee_account %) (assoc :bank-account/yodlee-account [:yodlee-account/id (:yodlee_account %)])))
|
||||
(:bank_accounts edit_client))
|
||||
|
||||
})
|
||||
[:reset id :client/forecasted-transactions (map #(remove-nils
|
||||
|
||||
@@ -12,7 +12,8 @@
|
||||
[clojure.string :as str]
|
||||
[config.core :refer [env]]
|
||||
|
||||
[clojure.tools.logging :as log]))
|
||||
[clojure.tools.logging :as log]
|
||||
[auto-ap.datomic.clients :as d-clients]))
|
||||
|
||||
(defroutes routes
|
||||
(wrap-routes
|
||||
@@ -52,15 +53,21 @@
|
||||
:headers {"Content-Type" "application/edn"}
|
||||
:body (pr-str {:message (.getMessage e)
|
||||
:error (.toString e)})})))
|
||||
(POST "/provider-accounts/refresh/:id" {:keys [query-params identity] {:keys [id]} :route-params :as request}
|
||||
(POST "/provider-accounts/refresh/" {:keys [query-params identity edn-params]
|
||||
{:keys [id]} :route-params
|
||||
:as request}
|
||||
(assert-admin identity)
|
||||
(try
|
||||
(let [[session token] (yodlee/get-access-token)]
|
||||
(yodlee/refresh-provider-account (Long/parseLong id))
|
||||
{:status 200
|
||||
:headers {"Content-Type" "application/edn"}
|
||||
:body (pr-str @yodlee/in-memory-cache) })
|
||||
(yodlee/refresh-provider-account (-> (:client-id edn-params)
|
||||
Long/parseLong
|
||||
d-clients/get-by-id
|
||||
:client/code)
|
||||
(:provider-account-id edn-params))
|
||||
{:status 200
|
||||
:headers {"Content-Type" "application/edn"}
|
||||
:body "{}" }
|
||||
(catch Exception e
|
||||
(log/error e)
|
||||
{:status 400
|
||||
:headers {"Content-Type" "application/edn"}
|
||||
:body (pr-str {:message (.getMessage e)
|
||||
|
||||
@@ -14,6 +14,8 @@
|
||||
[datomic.api :as d]
|
||||
[auto-ap.datomic :refer [conn]]
|
||||
[auto-ap.datomic.clients :as d-clients]))
|
||||
;; switch all of this to use tokens instead of passing around client codes, particularly because the codes
|
||||
;; need to be tweaked for repeats
|
||||
(defn client-code->login [client-code]
|
||||
(if (< (count client-code) 3)
|
||||
(str client-code "_" client-code)
|
||||
@@ -131,7 +133,8 @@
|
||||
:as :json}
|
||||
other-config))
|
||||
:body
|
||||
:providerAccount)))
|
||||
:providerAccount
|
||||
first)))
|
||||
|
||||
(defn get-provider-account-detail [client-code id]
|
||||
(let [cob-session (login-user client-code)]
|
||||
@@ -232,7 +235,7 @@
|
||||
(async/<!! (async/into [] output-chan)))))
|
||||
|
||||
(defn get-accounts-for-providers [client-code provider-account-ids]
|
||||
(log/info "looking up " (count provider-account-ids) " provider accounts for client " client-code ".")
|
||||
(log/info "looking up " (count provider-account-ids) " provider accounts's accounts for client " client-code ".")
|
||||
(into {}
|
||||
(mapv (fn [provider-account-id]
|
||||
(lc/with-context {:provider-account-id provider-account-id}
|
||||
@@ -269,29 +272,32 @@
|
||||
first))
|
||||
@(d/transact conn [:db/retractEntity [:yodlee-provider-account/id id]]))
|
||||
|
||||
(defn upsert-accounts-tx [client-code]
|
||||
(let [provider-accounts (get-provider-accounts client-code)
|
||||
accounts (get-accounts-for-providers client-code (map :id provider-accounts))]
|
||||
(map (fn [pa]
|
||||
{:yodlee-provider-account/id (:id pa)
|
||||
:yodlee-provider-account/status (:status pa)
|
||||
:yodlee-provider-account/detailed-status (-> pa :dataset first :additionalStatus)
|
||||
:yodlee-provider-account/client [:client/code client-code]
|
||||
:yodlee-provider-account/last-updated (-> pa :dataset first :lastUpdated coerce/to-date)
|
||||
:yodlee-provider-account/accounts (mapv
|
||||
(fn [a]
|
||||
{:yodlee-account/id (:id a)
|
||||
:yodlee-account/name (str (:providerName a) " (" (:accountName a) ")")
|
||||
:yodlee-account/number (:accountNumber a)
|
||||
:yodlee-account/status (-> a :dataset first :additionalStatus)
|
||||
:yodlee-account/available-balance (or (-> a :currentBalance :amount)
|
||||
0.0)})
|
||||
(get accounts (:id pa)))})
|
||||
provider-accounts)))
|
||||
(defn upsert-accounts-tx
|
||||
([client-code]
|
||||
(upsert-accounts-tx (get-provider-accounts (client-code->login client-code))))
|
||||
([client-code provider-accounts]
|
||||
(let [accounts (get-accounts-for-providers (client-code->login client-code) (map :id provider-accounts))]
|
||||
(map (fn [pa]
|
||||
{:yodlee-provider-account/id (:id pa)
|
||||
:yodlee-provider-account/status (:status pa)
|
||||
:yodlee-provider-account/detailed-status (-> pa :dataset first :additionalStatus)
|
||||
:yodlee-provider-account/client [:client/code client-code]
|
||||
:yodlee-provider-account/last-updated (-> pa :dataset first :lastUpdated coerce/to-date)
|
||||
:yodlee-provider-account/accounts (mapv
|
||||
(fn [a]
|
||||
{:yodlee-account/id (:id a)
|
||||
:yodlee-account/name (str (:providerName a) " (" (:accountName a) ")")
|
||||
:yodlee-account/number (:accountNumber a)
|
||||
:yodlee-account/status (-> a :dataset first :additionalStatus)
|
||||
:yodlee-account/available-balance (or (-> a :currentBalance :amount)
|
||||
0.0)})
|
||||
(get accounts (:id pa)))})
|
||||
provider-accounts))))
|
||||
|
||||
(defn refresh-provider-account [client-code id]
|
||||
@(d/transact conn (upsert-accounts-tx (client-code->login (client-code->login client-code)
|
||||
id))))
|
||||
(log/info "refreshing yodlee provider account id" id)
|
||||
@(d/transact conn (upsert-accounts-tx client-code
|
||||
[(get-provider-account (client-code->login client-code) id)])))
|
||||
|
||||
(defn upsert-accounts []
|
||||
(let [concurrent 20
|
||||
@@ -301,7 +307,7 @@
|
||||
(mapcat (fn [client]
|
||||
(log/info "Upserting Yodlee Accounts for " (:client/code client))
|
||||
(lc/with-context {:client-code (:client/code client)}
|
||||
(upsert-accounts-tx (client-code->login (:client/code client))))))
|
||||
(upsert-accounts-tx (:client/code client)))))
|
||||
(async/to-chan (d-clients/get-all)))
|
||||
(let [result (async/<!! (async/into [] output-chan))]
|
||||
(log/info "Current yodlee state is " result)
|
||||
|
||||
Reference in New Issue
Block a user