New import approach to capture the batch.

This commit is contained in:
2021-12-15 20:35:37 -08:00
parent b7324af538
commit 26a949e63f
12 changed files with 335 additions and 218 deletions

View File

@@ -11,7 +11,7 @@
[auto-ap.routes.utils :refer [wrap-secure]]
[auto-ap.time-utils :refer [next-dom]]
[auto-ap.utils :refer [by]]
[auto-ap.yodlee.import :refer [manual-import]]
[auto-ap.yodlee.import :refer [grouped-import] :as y]
[clj-time.coerce :as coerce :refer [to-date]]
[clj-time.core :as time]
[clojure.data.csv :as csv]
@@ -439,36 +439,41 @@
(try
#_(throw (Exception. "Unexpected error"))
(let [columns [:status :raw-date :description-original :high-level-category nil nil :amount nil nil nil nil nil :bank-account-code :client-code]
all-clients (d-clients/get-all)
all-bank-accounts (mapcat :client/bank-accounts all-clients)
all-clients (merge (by :client/code all-clients) (by :client/name all-clients))
all-bank-accounts (merge (by :bank-account/code all-bank-accounts))
rows (->> (str/split data #"\n" )
(drop 1)
(map #(str/split % #"\t"))
(map #(into {} (filter identity (map (fn [c k] [k c] ) % columns))))
(map (parse-or-error :amount parse-amount))
(map (parse-or-error :date parse-date))
(map (fn [{:keys [bank-account-code] :as row}]
(if-let [bank-account-id (:db/id (all-bank-accounts bank-account-code))]
(assoc row :bank-account-id bank-account-id)
(update row :errors conj {:info (str "Cannot find bank by code " bank-account-code)
:details (str "Cannot find bank by code " bank-account-code)}))))
(map (fn [{:keys [client-code] :as row}]
(if-let [client-id (:db/id (all-clients client-code))]
(assoc row :client-id client-id)
(update row :errors conj {:info (str "Cannot find client by code " client-code)
:details (str "Cannot find client by code " client-code)})))))
error-rows (filter :errors rows)
all-clients (d-clients/get-all)
all-bank-accounts (mapcat :client/bank-accounts all-clients)
all-clients (merge (by :client/code all-clients) (by :client/name all-clients))
all-bank-accounts (merge (by :bank-account/code all-bank-accounts))
rows (->> (str/split data #"\n" )
(drop 1)
(map #(str/split % #"\t"))
(map #(into {} (filter identity (map (fn [c k] [k c] ) % columns))))
(map (parse-or-error :amount parse-amount))
(map (parse-or-error :date parse-date))
(map (fn [{:keys [bank-account-code] :as row}]
(if-let [bank-account-id (:db/id (all-bank-accounts bank-account-code))]
(assoc row :bank-account-id bank-account-id)
(update row :errors conj {:info (str "Cannot find bank by code " bank-account-code)
:details (str "Cannot find bank by code " bank-account-code)}))))
(map (fn [{:keys [client-code] :as row}]
(if-let [client-id (:db/id (all-clients client-code))]
(assoc row :client-id client-id)
(update row :errors conj {:info (str "Cannot find client by code " client-code)
:details (str "Cannot find client by code " client-code)})))))
error-rows (filter :errors rows)
_ (log/info "Importing " (count rows) "raw transactions")
raw-transactions (vec (->> rows
(filter #(not (seq (:errors %))) )
(map (fn [row]
(select-keys row [:description-original :client-code :status :high-level-category :amount :bank-account-id :date :client-id])))))]
(manual-import raw-transactions)
raw-transactions (vec (->> rows
(filter #(not (seq (:errors %))) )
(map (fn [row]
(select-keys row [:description-original :client-code :status :high-level-category :amount :bank-account-id :date :client-id])))))
import-id (y/start-import :import-source/manual (:user/name user))
result (grouped-import raw-transactions import-id)]
(y/finish-import {:db/id import-id
:import-batch/imported (count (:import result))
:import-batch/suppressed (count (:suppressed result))
:import-batch/extant (count (:extant result))})
{:status 200
:body (pr-str {:imported (count raw-transactions)
:body (pr-str {:imported (count (:import result))
:errors (map #(dissoc % :date) error-rows)})
:headers {"Content-Type" "application/edn"}})
(catch Exception e