so many bug fixes.
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
(ns auto-ap.datomic.ledger
|
||||
(:require [datomic.api :as d]
|
||||
[auto-ap.graphql.utils :refer [->graphql limited-clients]]
|
||||
[auto-ap.utils :refer [dollars-0?]]
|
||||
[auto-ap.datomic :refer [merge-query apply-sort-3 apply-pagination add-sorter-fields]]
|
||||
[auto-ap.datomic :refer [uri]]
|
||||
[clj-time.coerce :as c]
|
||||
@@ -88,7 +89,9 @@
|
||||
:args [(:location args)]})
|
||||
|
||||
true
|
||||
(merge-query {:query {:find ['?base-date '?e] :where ['[?e :journal-entry/date ?base-date]]}}))]
|
||||
(merge-query {:query {:find ['?base-date '?e] :where ['[?e :journal-entry/date ?base-date]
|
||||
'[?e :journal-entry/amount ?ja2]
|
||||
'[(not= 0.0 ?ja2)]]}}))]
|
||||
(->> query
|
||||
(d/query)
|
||||
(apply-sort-3 args)
|
||||
|
||||
@@ -1,37 +1,37 @@
|
||||
(ns auto-ap.graphql
|
||||
(:require
|
||||
[com.walmartlabs.lacinia.util :refer [attach-resolvers]]
|
||||
[com.walmartlabs.lacinia.schema :as schema]
|
||||
[com.walmartlabs.lacinia :refer [execute]]
|
||||
[com.walmartlabs.lacinia.executor :as executor]
|
||||
[com.walmartlabs.lacinia.resolve :as resolve]
|
||||
[buddy.auth :refer [throw-unauthorized]]
|
||||
[auto-ap.utils :refer [by]]
|
||||
[auto-ap.graphql.utils :refer [assert-admin can-see-client? assert-can-see-client]]
|
||||
[auto-ap.datomic :refer [uri merge-query]]
|
||||
[datomic.api :as d]
|
||||
[clj-time.coerce :as coerce]
|
||||
[clj-time.core :as t]
|
||||
[auto-ap.datomic.clients :as d-clients]
|
||||
[auto-ap.datomic.checks :as d-checks]
|
||||
[auto-ap.datomic.users :as d-users]
|
||||
[auto-ap.datomic.invoices :as d-invoices]
|
||||
[auto-ap.datomic.vendors :as d-vendors]
|
||||
[auto-ap.graphql.users :as gq-users]
|
||||
[auto-ap.graphql.yodlee-merchants :as ym]
|
||||
[auto-ap.graphql.ledger :as gq-ledger]
|
||||
[auto-ap.graphql.accounts :as gq-accounts]
|
||||
[auto-ap.graphql.clients :as gq-clients]
|
||||
[auto-ap.graphql.vendors :as gq-vendors]
|
||||
[auto-ap.graphql.checks :as gq-checks]
|
||||
[auto-ap.graphql.invoices :as gq-invoices]
|
||||
[auto-ap.graphql.transactions :as gq-transactions]
|
||||
[auto-ap.graphql.transaction-rules :as gq-transaction-rules]
|
||||
[auto-ap.time :as time]
|
||||
[clojure.walk :as walk]
|
||||
[clojure.string :as str])
|
||||
[com.walmartlabs.lacinia.util :refer [attach-resolvers]]
|
||||
[com.walmartlabs.lacinia.schema :as schema]
|
||||
[com.walmartlabs.lacinia :refer [execute]]
|
||||
[com.walmartlabs.lacinia.executor :as executor]
|
||||
[com.walmartlabs.lacinia.resolve :as resolve]
|
||||
[buddy.auth :refer [throw-unauthorized]]
|
||||
[auto-ap.utils :refer [by]]
|
||||
[auto-ap.graphql.utils :refer [assert-admin can-see-client? assert-can-see-client]]
|
||||
[auto-ap.datomic :refer [uri merge-query]]
|
||||
[datomic.api :as d]
|
||||
[clj-time.coerce :as coerce]
|
||||
[clj-time.core :as t]
|
||||
[auto-ap.datomic.clients :as d-clients]
|
||||
[auto-ap.datomic.checks :as d-checks]
|
||||
[auto-ap.datomic.users :as d-users]
|
||||
[auto-ap.datomic.invoices :as d-invoices]
|
||||
[auto-ap.datomic.vendors :as d-vendors]
|
||||
[auto-ap.graphql.users :as gq-users]
|
||||
[auto-ap.graphql.yodlee-merchants :as ym]
|
||||
[auto-ap.graphql.ledger :as gq-ledger]
|
||||
[auto-ap.graphql.accounts :as gq-accounts]
|
||||
[auto-ap.graphql.clients :as gq-clients]
|
||||
[auto-ap.graphql.vendors :as gq-vendors]
|
||||
[auto-ap.graphql.checks :as gq-checks]
|
||||
[auto-ap.graphql.invoices :as gq-invoices]
|
||||
[auto-ap.graphql.transactions :as gq-transactions]
|
||||
[auto-ap.graphql.transaction-rules :as gq-transaction-rules]
|
||||
[auto-ap.time :as time]
|
||||
[clojure.walk :as walk]
|
||||
[clojure.string :as str])
|
||||
(:import
|
||||
(clojure.lang IPersistentMap)))
|
||||
(clojure.lang IPersistentMap)))
|
||||
|
||||
|
||||
(def integreat-schema
|
||||
@@ -922,6 +922,20 @@
|
||||
0
|
||||
(- total outstanding-balance))})))
|
||||
|
||||
(defn has-fulfilled? [id date recent-fulfillments]
|
||||
|
||||
(seq (transduce
|
||||
(filter (fn [[potential-id potential-date]]
|
||||
(let [date (coerce/to-date-time date)
|
||||
potential-date (coerce/to-date-time potential-date)]
|
||||
#_(println "HERE" id potential-id potential-date date)
|
||||
(and (= id potential-id)
|
||||
(<= (t/in-days (apply t/interval (sort [date potential-date]))) 10)))))
|
||||
conj
|
||||
[]
|
||||
recent-fulfillments))
|
||||
)
|
||||
|
||||
(defn get-cash-flow [context {:keys [client_id]} value]
|
||||
(when client_id
|
||||
(let [{:client/keys [weekly-credits weekly-debits forecasted-transactions ]} (d/pull (d/db (d/connect uri)) '[*] client_id )
|
||||
@@ -959,14 +973,25 @@
|
||||
[?p :payment/type :payment-type/debit]
|
||||
[?p :payment/type :payment-type/check])]}
|
||||
:args [(d/db (d/connect uri)) client_id (coerce/to-date (t/plus (time/local-now) (t/days 31)))]})))
|
||||
forecasted-transactions (->> forecasted-transactions
|
||||
(filter (fn [{:forecasted-transaction/keys [day-of-month]}]
|
||||
(>= day-of-month (t/day (time/local-now)))))
|
||||
(mapv (fn [{:forecasted-transaction/keys [amount identifier day-of-month]}]
|
||||
{:identifier identifier
|
||||
:amount amount
|
||||
:date (coerce/to-date-time (t/local-date (t/year (time/local-now)) (t/month (time/local-now)) day-of-month ))}
|
||||
)))]
|
||||
recent-fulfillments (d/query {:query {:find '[?f ?d]
|
||||
:in '[$ ?client ?min-date]
|
||||
:where ['[?t :transaction/forecast-match ?f]
|
||||
'[?t :transaction/date ?d]
|
||||
'[?t :transaction/client ?client]
|
||||
'[(>= ?d ?min-date)]]}
|
||||
:args [(d/db (d/connect uri)) client_id (coerce/to-date (t/plus (time/local-now) (t/months -2)))]})
|
||||
forecasted-transactions (for [{:forecasted-transaction/keys [amount identifier day-of-month]
|
||||
:db/keys [id]} forecasted-transactions
|
||||
month (range -1 2)
|
||||
:let [next (t/plus (t/local-date (t/year (time/local-now))
|
||||
(t/month (time/local-now))
|
||||
day-of-month )
|
||||
(t/months month))]
|
||||
:when (not (has-fulfilled? id next recent-fulfillments))]
|
||||
{:identifier identifier
|
||||
:amount amount
|
||||
:date (coerce/to-date-time next)})]
|
||||
(println "RECENT" forecasted-transactions)
|
||||
{:beginning_balance total-cash
|
||||
:outstanding_payments outstanding-checks
|
||||
:invoices_due_soon (mapv (fn [[due outstanding]]
|
||||
@@ -979,13 +1004,12 @@
|
||||
:date (coerce/to-date-time date)})
|
||||
(take 5 (time/day-of-week-seq 1)))
|
||||
(filter #(>= (:amount %) 0) forecasted-transactions))
|
||||
:upcoming_debits (doto (into (mapv
|
||||
(fn [date]
|
||||
{:amount (- (or weekly-debits 0))
|
||||
:date (coerce/to-date-time date)})
|
||||
(take 5 (time/day-of-week-seq 1)))
|
||||
(filter #(< (:amount %) 0) forecasted-transactions))
|
||||
println)
|
||||
:upcoming_debits (into (mapv
|
||||
(fn [date]
|
||||
{:amount (- (or weekly-debits 0))
|
||||
:date (coerce/to-date-time date)})
|
||||
(take 5 (time/day-of-week-seq 1)))
|
||||
(filter #(< (:amount %) 0) forecasted-transactions))
|
||||
})))
|
||||
|
||||
(def schema
|
||||
|
||||
@@ -184,7 +184,6 @@
|
||||
(:entries args))
|
||||
all-vendors (into all-vendors new-hidden-vendors)
|
||||
all-accounts (transduce (map (comp str :account/numeric-code)) conj #{} (a/get-accounts))
|
||||
_ (println all-accounts)
|
||||
transaction (doall (map
|
||||
(assoc-error (fn [entry]
|
||||
(let [entry (-> entry
|
||||
@@ -202,6 +201,8 @@
|
||||
(throw (Exception. (str "Client '" (:client_code entry )"' not found.")) ))
|
||||
(when-not vendor
|
||||
(throw (Exception. (str "Vendor '" (:vendor_name entry) "' not found."))))
|
||||
(when-not (re-find #"\d{1,2}/\d{1,2}/\d{4}" (:date entry))
|
||||
(throw (Exception. (str "Date must be MM/dd/yyyy"))))
|
||||
(when-not (dollars= (doto (reduce + 0.0 (map :debit (:line_items entry))))
|
||||
(reduce + 0.0 (map :credit (:line_items entry))))
|
||||
(throw (Exception. (str "Debits '"
|
||||
@@ -249,6 +250,7 @@
|
||||
success (filter (comp not :error) transaction)
|
||||
retraction (mapv (fn [x] [:db/retractEntity [:journal-entry/external-id (:journal-entry/external-id x)]])
|
||||
success)]
|
||||
(println (take 4 success))
|
||||
(run! (fn [batch] (println "transacting retraction batch") @(d/transact (d/connect uri) batch)) (partition-all 100 retraction))
|
||||
(run! (fn [batch] (println "transacting success batch") @(d/transact (d/connect uri) batch)) (partition-all 100 success))
|
||||
{:successful (map (fn [x] {:external_id (:journal-entry/external-id x)}) success)
|
||||
|
||||
@@ -81,33 +81,36 @@
|
||||
[file filename]
|
||||
(excel/parse-file file filename))
|
||||
|
||||
(defn best-match [clients invoice-client-name]
|
||||
(let [fuzzy-match (->> clients
|
||||
(mapcat (fn [{:keys [:db/id :client/matches :client/name] :as client :or {matches []}}]
|
||||
(map (fn [m]
|
||||
[client (m/jaccard (.toLowerCase invoice-client-name) (.toLowerCase m))])
|
||||
(conj matches name))))
|
||||
(filter #(< (second %) 0.25))
|
||||
(sort-by second)
|
||||
ffirst)
|
||||
(defn best-match
|
||||
([clients invoice-client-name]
|
||||
(best-match clients invoice-client-name 0.25))
|
||||
([clients invoice-client-name threshold]
|
||||
(let [fuzzy-match (->> clients
|
||||
(mapcat (fn [{:keys [:db/id :client/matches :client/name] :as client :or {matches []}}]
|
||||
(map (fn [m]
|
||||
[client (m/jaccard (.toLowerCase invoice-client-name) (.toLowerCase m))])
|
||||
(conj matches name))))
|
||||
(filter #(<= (second %) threshold))
|
||||
(sort-by second)
|
||||
ffirst)
|
||||
|
||||
word-set (set (filter (complement str/blank?) (str/split (.toLowerCase invoice-client-name) #"[\s:\-]" )))
|
||||
client-word-match (->> clients
|
||||
(map
|
||||
(fn [{:keys [:db/id :client/matches :client/name] :as client :or {matches []}}]
|
||||
(let [client-words (-> #{}
|
||||
(into
|
||||
(mapcat
|
||||
(fn [match] (str/split (.toLowerCase match) #"\s" ))
|
||||
matches))
|
||||
(into
|
||||
(str/split (.toLowerCase name) #"\s" )))]
|
||||
[client (count (set/intersection client-words word-set))])))
|
||||
(filter (fn [[_ c]] (> c 0)))
|
||||
(sort-by (fn [[_ c]] c))
|
||||
reverse
|
||||
ffirst)]
|
||||
(or fuzzy-match client-word-match)))
|
||||
word-set (set (filter (complement str/blank?) (str/split (.toLowerCase invoice-client-name) #"[\s:\-]" )))
|
||||
client-word-match (->> clients
|
||||
(map
|
||||
(fn [{:keys [:db/id :client/matches :client/name] :as client :or {matches []}}]
|
||||
(let [client-words (-> #{}
|
||||
(into
|
||||
(mapcat
|
||||
(fn [match] (str/split (.toLowerCase match) #"\s" ))
|
||||
matches))
|
||||
(into
|
||||
(str/split (.toLowerCase name) #"\s" )))]
|
||||
[client (count (set/intersection client-words word-set))])))
|
||||
(filter (fn [[_ c]] (> c 0)))
|
||||
(sort-by (fn [[_ c]] c))
|
||||
reverse
|
||||
ffirst)]
|
||||
(or fuzzy-match client-word-match))))
|
||||
|
||||
(defn best-location-match [client text full-text]
|
||||
(or (->> client
|
||||
|
||||
@@ -42,7 +42,8 @@
|
||||
:extract {:invoice-number #"Invoice #\s*\n\s*[\w\.]+\s+[\w\./]+(.*)\s*\n"
|
||||
:customer-identifier #"Bill To[^\n]+\n[^\n]*\n([\w ]+)\s{2,}"
|
||||
:date #"Invoice #\s*\n\s*[\w\.]+\s+([\w\./]+)"
|
||||
:total #"Total\s+\$([0-9.,]+)"}
|
||||
:total #"Total\s+\$([0-9.,]+)"
|
||||
:account-number #"Account #\s+(\d+)"}
|
||||
:parser {:date [:clj-time "MM/dd/yy"]
|
||||
:total [:trim-commas nil]}}
|
||||
|
||||
@@ -211,7 +212,8 @@
|
||||
:extract {:date #"INVOICE DATE\s+([0-9]+/[0-9]+/[0-9]+)"
|
||||
:customer-identifier #"SHIP-TO-PARTY.*\n(.*?)(?=\s{2,})"
|
||||
:invoice-number #"INV #\s+(\d+)"
|
||||
:total #"PLEASE PAY THIS AMOUNT\s+([0-9]+\.[0-9]{2})"}
|
||||
:total #"PLEASE PAY THIS AMOUNT\s+([0-9]+\.[0-9]{2})"
|
||||
:account-number #"CUSTOMER NUMBER\s+(\d+)"}
|
||||
:parser {:date [:clj-time "MM/dd/yyyy"]
|
||||
:total [:trim-commas nil]}}
|
||||
|
||||
|
||||
@@ -48,7 +48,7 @@
|
||||
(let [jwt (jwt/sign (doto {:user (:name profile)
|
||||
:exp (time/plus (time/now) (time/days 30))
|
||||
:user/clients (map (fn [c]
|
||||
(dissoc c :client/bank-accounts :client/location-matches))
|
||||
(dissoc c :client/bank-accounts :client/location-matches :client/forecasted-transactions :client/matches :client/weekly-debits :client/weekly-credits :client/signature-file :client/address))
|
||||
(:user/clients user))
|
||||
:user/role (name (:user/role user))
|
||||
:user/name (:name profile)}
|
||||
|
||||
@@ -93,7 +93,11 @@
|
||||
|
||||
|
||||
(defn parse-date [{:keys [raw-date]}]
|
||||
(when-not
|
||||
(re-find #"\d{1,2}/\d{1,2}/\d{4}" raw-date)
|
||||
(throw (Exception. (str "Date " raw-date " must match MM/dd/yyyy"))))
|
||||
(try
|
||||
|
||||
(parse-u/parse-value :clj-time "MM/dd/yyyy" raw-date)
|
||||
(catch Exception e
|
||||
(throw (Exception. (str "Could not parse date from '" raw-date "'") e)))))
|
||||
@@ -197,10 +201,12 @@
|
||||
|
||||
(defn import-uploaded-invoice [client forced-location forced-vendor imports]
|
||||
(let [clients (d-clients/get-all)
|
||||
transactions (reduce (fn [result {:keys [invoice-number customer-identifier total date vendor-code text full-text] :as info}]
|
||||
transactions (reduce (fn [result {:keys [invoice-number customer-identifier account-number total date vendor-code text full-text] :as info}]
|
||||
(println "searching for" vendor-code)
|
||||
(let [ _ (println "matching" customer-identifier)
|
||||
matching-client (or (and customer-identifier
|
||||
matching-client (or (and account-number
|
||||
(parse/best-match clients account-number 0.0))
|
||||
(and customer-identifier
|
||||
(parse/best-match clients customer-identifier))
|
||||
(if client
|
||||
(first (filter (fn [c]
|
||||
@@ -274,8 +280,113 @@
|
||||
(throw (ex-info "No invoices found."
|
||||
{:imports (str imports)})))
|
||||
@(d/transact (d/connect uri) (vec (set transactions)))))
|
||||
|
||||
|
||||
(defn validate-account-rows [rows code->existing-account]
|
||||
(when-let [bad-types (seq (->> rows
|
||||
(filter (fn [[account _ _ type :as row]]
|
||||
(and (not (code->existing-account (Integer/parseInt account)))
|
||||
(not (#{"Asset" "Liability" "Revenue" "Expense" "Equity" "Dividend"} type)))
|
||||
))))]
|
||||
(throw (ex-info (str "You are adding accounts without a valid type" )
|
||||
{:rows bad-types})))
|
||||
|
||||
(when-let [duplicate-rows (seq (->> rows
|
||||
(filter (fn [[account]]
|
||||
(not-empty account)))
|
||||
(group-by (fn [[account]]
|
||||
account))
|
||||
vals
|
||||
(filter #(> (count %) 1))
|
||||
(filter (fn [duplicates]
|
||||
(apply not= duplicates)))
|
||||
#_(map (fn [[[_ account]]]
|
||||
account))
|
||||
))]
|
||||
(throw (ex-info (str "You have duplicated rows with different values." )
|
||||
{:rows duplicate-rows}))))
|
||||
|
||||
(defn import-account-overrides [customer filename]
|
||||
(let [conn (d/connect uri)
|
||||
[header & rows] (-> filename (io/reader) csv/read-csv)
|
||||
[client-id] (first (d/query (-> {:query {:find ['?e]
|
||||
:in ['$ '?z]
|
||||
:where [['?e :client/code '?z]]}
|
||||
:args [(d/db (d/connect uri)) customer]})))
|
||||
_ (println client-id)
|
||||
headers (map read-string header)
|
||||
code->existing-account (by :account/numeric-code (map first (d/query {:query {:find ['(pull ?e [:account/numeric-code
|
||||
{:account/applicability [:db/ident]}
|
||||
:db/id])]
|
||||
:in ['$]
|
||||
:where ['[?e :account/name]]}
|
||||
:args [(d/db conn)]})))
|
||||
|
||||
existing-account-overrides (d/query (-> {:query {:find ['?e]
|
||||
:in ['$ '?client-id]
|
||||
:where [['?e :account-client-override/client '?client-id]]}
|
||||
:args [(d/db (d/connect uri)) client-id]}))
|
||||
|
||||
|
||||
rows (transduce (comp
|
||||
(map (fn [[_ account account-name override-name _ type]]
|
||||
[account account-name override-name type]))
|
||||
(filter (fn [[account]]
|
||||
(not-empty account))))
|
||||
conj
|
||||
[]
|
||||
rows)
|
||||
|
||||
_ (validate-account-rows rows code->existing-account)
|
||||
rows (vec (set rows))
|
||||
|
||||
txes (transduce
|
||||
(comp
|
||||
(mapcat (fn parse-map [[account account-name override-name type]]
|
||||
(let [code (some-> account
|
||||
not-empty
|
||||
Integer/parseInt)
|
||||
existing (code->existing-account code)]
|
||||
(cond (not code)
|
||||
[]
|
||||
|
||||
(and existing (or (#{:account-applicability/optional :account-applicability/customized}
|
||||
(:db/ident (:account/applicability existing)))
|
||||
(and (not-empty override-name)
|
||||
(not-empty account-name)
|
||||
(not= override-name account-name)
|
||||
)))
|
||||
[{:db/id (:db/id existing)
|
||||
:account/client-overrides [{:account-client-override/client client-id
|
||||
:account-client-override/name (or (not-empty override-name)
|
||||
(not-empty account-name))}]}]
|
||||
|
||||
(not existing)
|
||||
[{:account/applicability :account-applicability/customized
|
||||
:account/name account-name
|
||||
:account/account-set "default"
|
||||
:account/numeric-code code
|
||||
:account/code (str code)
|
||||
:account/type (if (str/blank? type)
|
||||
:account-type/expense
|
||||
(keyword "account-type" (str/lower-case type)))
|
||||
:account/client-overrides [{:account-client-override/client client-id
|
||||
:account-client-override/name (or (not-empty override-name)
|
||||
(not-empty account-name))}]}]
|
||||
:else
|
||||
[])))))
|
||||
|
||||
conj
|
||||
(mapv
|
||||
(fn [[x]]
|
||||
[:db/retractEntity x])
|
||||
existing-account-overrides)
|
||||
rows)]
|
||||
|
||||
@(d/transact conn txes)
|
||||
txes))
|
||||
|
||||
|
||||
|
||||
|
||||
(defn import-transactions-cleared-against [file]
|
||||
(let [[header & rows] (-> file (io/reader) csv/read-csv)
|
||||
@@ -418,5 +529,25 @@
|
||||
:body (pr-str {:message (.getMessage e)
|
||||
:error (.toString e)
|
||||
:data (ex-data e)})
|
||||
:headers {"Content-Type" "application/edn"}})))))
|
||||
:headers {"Content-Type" "application/edn"}}))))
|
||||
(wrap-json-response (POST "/account-overrides"
|
||||
{{files :file
|
||||
files-2 "file"
|
||||
client :client
|
||||
client-2 "client"} :params :as params
|
||||
user :identity}
|
||||
(let [files (or files files-2)
|
||||
client (or client client-2)
|
||||
{:keys [filename tempfile]} files]
|
||||
(assert-admin user)
|
||||
(try
|
||||
{:status 200
|
||||
:body (import-account-overrides client (.getPath tempfile))
|
||||
:headers {"Content-Type" "application/json"}}
|
||||
(catch Exception e
|
||||
(println e)
|
||||
{:status 500
|
||||
:body {:message (.getMessage e)
|
||||
:data (ex-data e)}
|
||||
:headers {"Content-Type" "application/json"}}))))))
|
||||
wrap-secure))
|
||||
|
||||
@@ -36,10 +36,36 @@
|
||||
{:status 200
|
||||
:headers {"Content-Type" "application/edn"}
|
||||
:body (pr-str (yodlee/get-accounts)) }))
|
||||
(POST "/accounts/:id" {:keys [query-params identity] {:keys [id]} :route-params :as request}
|
||||
|
||||
(GET "/provider-accounts" {:keys [query-params identity] :as request}
|
||||
(assert-admin identity)
|
||||
(let [[session token] (yodlee/get-access-token)]
|
||||
{:status 200
|
||||
:headers {"Content-Type" "application/edn"}
|
||||
:body (pr-str (yodlee/update-yodlee (Long/parseLong id))) })))
|
||||
:body (pr-str (yodlee/get-provider-accounts-with-accounts)) }))
|
||||
(POST "/reauthenticate/:id" {:keys [query-params identity] {:keys [id]} :route-params
|
||||
data :edn-params
|
||||
:as request}
|
||||
(assert-admin identity)
|
||||
(try
|
||||
(let [[session token] (yodlee/get-access-token)]
|
||||
{:status 200
|
||||
:headers {"Content-Type" "application/edn"}
|
||||
:body (pr-str (yodlee/reauthenticate (Long/parseLong id) data)) })
|
||||
(catch Exception e
|
||||
{:status 500
|
||||
:headers {"Content-Type" "application/edn"}
|
||||
:body (pr-str {:message (.getMessage e)
|
||||
:error (.toString e)})})))
|
||||
(POST "/provider-accounts/:id" {:keys [query-params identity] {:keys [id]} :route-params :as request}
|
||||
(assert-admin identity)
|
||||
(try
|
||||
(let [[session token] (yodlee/get-access-token)]
|
||||
{:status 200
|
||||
:headers {"Content-Type" "application/edn"}
|
||||
:body (pr-str (yodlee/update-yodlee (Long/parseLong id))) })
|
||||
(catch Exception e
|
||||
{:status 400
|
||||
:headers {"Content-Type" "application/edn"}
|
||||
:body (pr-str e)}))))
|
||||
wrap-secure))
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
(ns auto-ap.yodlee.core
|
||||
(:require [clj-http.client :as client]
|
||||
[auto-ap.utils :refer [by]]
|
||||
[cemerick.url :as u]
|
||||
[clojure.data.json :as json]
|
||||
[config.core :refer [env]]))
|
||||
@@ -65,10 +66,12 @@
|
||||
user-session (login-user cob-session)]
|
||||
(-> (str (:yodlee-base-url env) "/providerAccounts")
|
||||
(-> (client/get {:headers (merge base-headers {"Authorization" (auth-header cob-session user-session)})
|
||||
:query-params {"include" "credentials,questions,preferences"}
|
||||
:as :json})
|
||||
:body
|
||||
:providerAccount)
|
||||
)))
|
||||
:providerAccount))))
|
||||
|
||||
|
||||
|
||||
(defn get-transactions []
|
||||
(let [cob-session (login-cobrand)
|
||||
@@ -124,6 +127,22 @@
|
||||
:body
|
||||
:providerAccount)))
|
||||
|
||||
(defn get-provider-account-detail [id]
|
||||
(let [cob-session (login-cobrand)
|
||||
user-session (login-user cob-session)
|
||||
batch-size 100]
|
||||
|
||||
(-> (str (:yodlee-base-url env) "/providerAccounts/" id )
|
||||
|
||||
(client/get {:headers (doto
|
||||
(merge base-headers {"Authorization" (auth-header cob-session user-session)})
|
||||
println)
|
||||
:query-params {"include" "credentials,questions,preferences"}
|
||||
:as :json})
|
||||
:body
|
||||
:providerAccount
|
||||
first)))
|
||||
|
||||
(defn update-provider-account [pa]
|
||||
(let [cob-session (login-cobrand)
|
||||
user-session (login-user cob-session)
|
||||
@@ -137,9 +156,21 @@
|
||||
:body "{\"dataSetName\": [\"BASIC_AGG_DATA\"]}"
|
||||
:as :json}))))
|
||||
|
||||
|
||||
(defn reauthenticate [pa data]
|
||||
(let [cob-session (login-cobrand)
|
||||
user-session (login-user cob-session)
|
||||
batch-size 100]
|
||||
|
||||
(-> (str (:yodlee-base-url env) "/providerAccounts?providerAccountIds=" pa)
|
||||
|
||||
(client/put {:headers (merge base-headers {"Authorization" (auth-header cob-session user-session)})
|
||||
:body (json/write-str data)
|
||||
:as :json}))))
|
||||
|
||||
(defn update-yodlee [id]
|
||||
(update-provider-account (:providerAccountId (first (filter #(= (:id %) id) (get-accounts)))))
|
||||
(get-provider-account (:providerAccountId (first (filter #(= (:id %) id) (get-accounts))))))
|
||||
(update-provider-account id)
|
||||
(get-provider-account id))
|
||||
|
||||
(defn get-specific-transactions [account]
|
||||
(let [cob-session (login-cobrand)
|
||||
@@ -205,6 +236,24 @@
|
||||
:as :json})
|
||||
:body)))
|
||||
|
||||
(defn get-provider-accounts-with-details []
|
||||
(let [provider-accounts (get-provider-accounts)]
|
||||
(reduce
|
||||
(fn [pas pa]
|
||||
(conj pas (try (get-provider-account-detail (:id pa))
|
||||
(catch Exception e
|
||||
pa))))
|
||||
[]
|
||||
provider-accounts)))
|
||||
|
||||
(defn get-provider-accounts-with-accounts []
|
||||
(let [provider-accounts (by :id (get-provider-accounts-with-details))
|
||||
accounts (get-accounts)]
|
||||
(->> accounts
|
||||
(reduce
|
||||
(fn [provider-accounts a]
|
||||
(update-in provider-accounts [(:providerAccountId a) :accounts] conj a)) provider-accounts)
|
||||
vals)))
|
||||
|
||||
#_(defn get-users []
|
||||
(let [cob-session (login-cobrand)]
|
||||
|
||||
@@ -90,12 +90,9 @@
|
||||
:id (sha-256 (str id))
|
||||
:account-id account-id
|
||||
:date (coerce/to-date (time/parse date "YYYY-MM-dd"))
|
||||
:yodlee-merchant (when (and merchant-id merchant-name (not (str/blank? merchant-id)))
|
||||
{:yodlee-merchant/yodlee-id merchant-id
|
||||
:yodlee-merchant/name merchant-name})
|
||||
:amount (double amount)
|
||||
:description-original description-original
|
||||
:description-simple description-simple
|
||||
:description-original (some-> description-original (str/replace #"\s+" " "))
|
||||
:description-simple (some-> description-simple (str/replace #"\s+" " "))
|
||||
:approval-status (if check
|
||||
:transaction-approval-status/approved
|
||||
:transaction-approval-status/unapproved)
|
||||
|
||||
@@ -196,11 +196,17 @@
|
||||
_ (println client-id)
|
||||
headers (map read-string header)
|
||||
code->existing-account (by :account/numeric-code (map first (d/query {:query {:find ['(pull ?e [:account/numeric-code
|
||||
{:account/applicability [:db/ident]}
|
||||
:db/id])]
|
||||
:in ['$]
|
||||
:where ['[?e :account/name]]}
|
||||
:args [(d/db conn)]})))
|
||||
|
||||
existing-account-overrides (d/query (-> {:query {:find ['?e]
|
||||
:in ['$ '?client-id]
|
||||
:where [['?e :account-client-override/client '?client-id]]}
|
||||
:args [(d/db (d/connect uri)) client-id]}))
|
||||
|
||||
|
||||
|
||||
_ (if-let [bad-rows (seq (->> rows
|
||||
@@ -219,27 +225,46 @@
|
||||
txes (transduce
|
||||
(comp
|
||||
|
||||
(filter (fn [[account _ override-name]]
|
||||
(and
|
||||
(not (str/blank? override-name))
|
||||
(not (str/blank? account)))))
|
||||
(map (fn parse-map [[account account-name override-name _ type]]
|
||||
(let [code (Integer/parseInt account)
|
||||
existing-id (:db/id (code->existing-account code))]
|
||||
(cond-> {:account/client-overrides [{:account-client-override/client client-id
|
||||
:account-client-override/name override-name}]}
|
||||
existing-id (assoc :db/id existing-id)
|
||||
(not existing-id) (assoc :account/applicability :account-applicability/customized
|
||||
:account/name account-name
|
||||
:account/account-set "default"
|
||||
:account/numeric-code code
|
||||
:account/code (str code)
|
||||
:account/type (if (str/blank? type)
|
||||
:account-type/expense
|
||||
(keyword "account-type" (str/lower-case type)))))))))
|
||||
|
||||
(mapcat (fn parse-map [[account account-name override-name _ type]]
|
||||
(let [code (some-> account
|
||||
not-empty
|
||||
Integer/parseInt)
|
||||
existing (code->existing-account code)]
|
||||
(cond (not code)
|
||||
[]
|
||||
|
||||
(and existing (or (#{:account-applicability/optional :account-applicability/customized}
|
||||
(:db/ident (:account/applicability existing)))
|
||||
(and (not-empty override-name)
|
||||
(not-empty account-name)
|
||||
(not= override-name account-name)
|
||||
)))
|
||||
[{:db/id (:db/id existing)
|
||||
:account/client-overrides [{:account-client-override/client client-id
|
||||
:account-client-override/name (or (not-empty override-name)
|
||||
(not-empty account-name))}]}]
|
||||
|
||||
(not existing)
|
||||
[{:account/applicability :account-applicability/customized
|
||||
:account/name account-name
|
||||
:account/account-set "default"
|
||||
:account/numeric-code code
|
||||
:account/code (str code)
|
||||
:account/type (if (str/blank? type)
|
||||
:account-type/expense
|
||||
(keyword "account-type" (str/lower-case type)))
|
||||
:account/client-overrides [{:account-client-override/client client-id
|
||||
:account-client-override/name (or (not-empty override-name)
|
||||
(not-empty account-name))}]}]
|
||||
:else
|
||||
[])))))
|
||||
|
||||
conj
|
||||
[]
|
||||
(mapv
|
||||
(fn [[x]]
|
||||
[:db/retractEntity x])
|
||||
existing-account-overrides)
|
||||
rows)]
|
||||
|
||||
txes
|
||||
|
||||
Reference in New Issue
Block a user