71 lines
2.7 KiB
Clojure
71 lines
2.7 KiB
Clojure
(ns auto-ap.import.yodlee
|
|
(:require [auto-ap.datomic :refer [conn]]
|
|
[auto-ap.import.transactions :as t]
|
|
[auto-ap.time :as atime]
|
|
[auto-ap.utils :refer [allow-once]]
|
|
[auto-ap.yodlee.core :as client]
|
|
[clj-time.coerce :as coerce]
|
|
[clojure.string :as str]
|
|
[datomic.api :as d]
|
|
[digest :refer [sha-256]]
|
|
[mount.core :as mount]
|
|
[unilog.context :as lc]
|
|
[yang.scheduler :as scheduler]
|
|
[clojure.tools.logging :as log]))
|
|
|
|
(defn yodlee->transaction [transaction]
|
|
(let [{post-date :postDate
|
|
account-id :accountId
|
|
date :date
|
|
id :id
|
|
{amount :amount} :amount
|
|
{description-original :original
|
|
description-simple :simple} :description
|
|
{merchant-id :id
|
|
merchant-name :name} :merchant
|
|
base-type :baseType
|
|
type :type
|
|
status :status} transaction
|
|
amount (if (= "DEBIT" base-type)
|
|
(- amount)
|
|
amount)
|
|
date (atime/parse date "YYYY-MM-dd")]
|
|
#:transaction
|
|
{:post-date (coerce/to-date (atime/parse post-date "YYYY-MM-dd"))
|
|
:id (sha-256 (str id))
|
|
:raw-id (str id)
|
|
:account-id account-id
|
|
:date (coerce/to-date date)
|
|
:amount (double amount)
|
|
:description-original (some-> description-original (str/replace #"\s+" " "))
|
|
:description-simple (some-> description-simple (str/replace #"\s+" " "))
|
|
:type type
|
|
:status status}))
|
|
|
|
(defn import-yodlee []
|
|
(lc/with-context {:source "Import yodlee transactions"}
|
|
(let [import-batch (t/start-import-batch :import-source/yodlee "Automated yodlee user")]
|
|
(try
|
|
(let [account-lookup (d/q '[:find ?ya ?ba ?c
|
|
:in $
|
|
:where [?ba :bank-account/yodlee-account-id ?ya]
|
|
[?c :client/bank-accounts ?ba]]
|
|
(d/db conn))]
|
|
(doseq [[yodlee-account bank-account client-id] account-lookup
|
|
transaction (client/get-specific-transactions yodlee-account)]
|
|
(log/info "importing")
|
|
(t/import-transaction! import-batch (assoc (yodlee->transaction transaction)
|
|
:transaction/bank-account bank-account
|
|
:transaction/client client-id)))
|
|
|
|
(t/finish! import-batch))
|
|
(catch Exception e
|
|
(t/fail! import-batch e))))))
|
|
|
|
(def import-yodlee (allow-once import-yodlee))
|
|
|
|
|
|
(mount/defstate import-worker
|
|
:start (scheduler/every (* 1000 60 60 4) import-yodlee)
|
|
:stop (scheduler/stop import-worker))
|