checks are actually matched

This commit is contained in:
BC
2018-06-28 21:20:51 -07:00
parent e738349cec
commit 216429bfdb
6 changed files with 79 additions and 50 deletions

View File

@@ -13,7 +13,7 @@ VALUES
('BCBC', 'Brown Chicken Brown Cow', '{:locations ["CB"] }'), ('BCBC', 'Brown Chicken Brown Cow', '{:locations ["CB"] }'),
('BSG', 'Bella Saratoga', '{:locations ["SG"] }'), ('BSG', 'Bella Saratoga', '{:locations ["SG"] }'),
('CBC', 'Campbell Brewing Co', '{:locations ["CB"]}'), ('CBC', 'Campbell Brewing Co', '{:locations ["CB"]}'),
('IBC', 'Iguanas Burritozilla', '{:locations ["DT" "EG" "SC" "EG" "SG" "CB" "BH"]}'), ('IBC', 'Iguanas Burritozilla', '{:locations ["DT" "EG" "SC" "EG" "SG" "CB" "BH"] :bank-accounts [{:number "123456789" :id 1 :check-number 6789 :bank-name "Bank of America" :bank-code "90-4149/1211" :routing "12345678" :name "BOA-6789" :yodlee-account-id 11703936} {:number "987654321" :id 2 :check-number 1234 :bank-name "Bank of America" :bank-code "90-4149/1211" :routing "123456" :name "BOA-4321"}]}'),
('IBCBC', 'Shared CBC-IBC Expenses', '{:locations ["CB"]}'), ('IBCBC', 'Shared CBC-IBC Expenses', '{:locations ["CB"]}'),
('KOG', 'Knock Out Grill & Bar', '{:locations ["SV"]}'), ('KOG', 'Knock Out Grill & Bar', '{:locations ["SV"]}'),
('LFT', 'Lefty''s East Coast Pizzeria', '{:locations ["SB"]}'), ('LFT', 'Lefty''s East Coast Pizzeria', '{:locations ["SB"]}'),

View File

@@ -0,0 +1,3 @@
-- 1530242355 DOWN add-check-number
alter table transactions drop column check_number;
alter table transactions drop column bank_account_id;

View File

@@ -0,0 +1,3 @@
-- 1530242355 UP add-check-number
alter table transactions add column check_number INT;
alter table transactions add column bank_account_id int;

View File

@@ -68,10 +68,11 @@
:else :else
q))) q)))
(defn base-graphql [{:keys [company-id vendor-id]}] (defn base-graphql [{:keys [company-id vendor-id check-number]}]
(cond-> base-query (cond-> base-query
(not (nil? company-id)) (helpers/merge-where [:= :company-id company-id]) (not (nil? company-id)) (helpers/merge-where [:= :company-id company-id])
(not (nil? vendor-id)) (helpers/merge-where [:= :vendor-id vendor-id]))) (not (nil? vendor-id)) (helpers/merge-where [:= :vendor-id vendor-id])
(not (nil? check-number)) (helpers/merge-where [:= :check-number check-number])))
(defn get-graphql [{:keys [start sort-by asc] :as args}] (defn get-graphql [{:keys [start sort-by asc] :as args}]
(query (query

View File

@@ -55,7 +55,7 @@
(defn get-transactions [] (defn get-transactions []
(let [cob-session (login-cobrand) (let [cob-session (login-cobrand)
user-session (login-user cob-session) user-session (login-user cob-session)
batch-size 500 batch-size 100
get-transaction-batch (fn [skip] get-transaction-batch (fn [skip]
(-> (str "https://developer.api.yodlee.com/ysl/transactions?top=" batch-size "&skip=" skip) (-> (str "https://developer.api.yodlee.com/ysl/transactions?top=" batch-size "&skip=" skip)
(client/get {:headers (merge base-headers {"Authorization" (auth-header cob-session user-session)}) (client/get {:headers (merge base-headers {"Authorization" (auth-header cob-session user-session)})
@@ -71,7 +71,7 @@
(recur (concat transactions transaction-batch) (+ batch-size skip)) (recur (concat transactions transaction-batch) (+ batch-size skip))
transactions))))) transactions)))))
#_(defn create-user [] (defn create-user []
(let [cob-session (login-cobrand)] (let [cob-session (login-cobrand)]
(-> "https://developer.api.yodlee.com/ysl/user/register" (-> "https://developer.api.yodlee.com/ysl/user/register"
(client/post {:headers (merge base-headers {"Authorization" (auth-header cob-session)}) (client/post {:headers (merge base-headers {"Authorization" (auth-header cob-session)})

View File

@@ -2,29 +2,45 @@
(:require [auto-ap.yodlee.core :as client] (:require [auto-ap.yodlee.core :as client]
[auto-ap.db.transactions :as transactions] [auto-ap.db.transactions :as transactions]
[auto-ap.db.vendors :as vendors] [auto-ap.db.vendors :as vendors]
[auto-ap.utils :refer [by]]
[auto-ap.db.companies :as companies] [auto-ap.db.companies :as companies]
[auto-ap.db.checks :as checks] [auto-ap.db.checks :as checks]
[auto-ap.time :as time])) [auto-ap.time :as time]))
(defn account->company-id [account-id]
(-> (companies/get-all)
first
:id))
(defn transaction->vendor-id [_] (defn transaction->vendor-id [_]
(-> (vendors/get-all) (-> (vendors/get-all)
first first
:id)) :id))
(defn transaction->check-id [_ company-id vendor-id] (defn transaction->check-id [_ check-number company-id vendor-id]
(when (= 0 (rand-int 2)) (when check-number
(-> (checks/get-graphql {:company-id company-id (-> (checks/get-graphql {:company-id company-id
:vendor-id vendor-id}) :check-number check-number})
first first
:id))) :id)))
(defn extract-check-number [{{description-original :original} :description}]
(if-let [[_ check-number] (re-find #"(?i)check[^0-9]+([0-9]*)" description-original)]
(Integer/parseInt check-number)
nil))
(defn do-import [] (defn do-import []
(doseq [transaction (client/get-transactions) (let [transactions (client/get-transactions)
all-bank-accounts (mapcat (fn [c] (map
(fn [{:keys [id yodlee-account-id]}]
(when (and id yodlee-account-id)
{:bank-account-id id
:company-id (:id c)
:yodlee-account-id yodlee-account-id}))
(:bank-accounts c)))
(companies/get-all))
account->company (by :yodlee-account-id :company-id all-bank-accounts)
yodlee-account-id->bank-account-id (by :yodlee-account-id :bank-account-id all-bank-accounts)]
(println "importing " (count transactions) "transactions")
(println "yodlee account->company" account->company)
(println "yodlee account->bank-account-id" yodlee-account-id->bank-account-id)
(doseq [transaction transactions
:let [{post-date :postDate :let [{post-date :postDate
account-id :accountId account-id :accountId
date :date date :date
@@ -37,7 +53,11 @@
type :type type :type
status :status} status :status}
transaction transaction
company-id (account->company-id account-id) transaction (if (= 0 (rand-int 3))
(assoc-in transaction [:description :original] (str "check xxx" (rand-nth ["6789" "1234" "6790"])))
transaction)
check-number (extract-check-number transaction)
company-id (account->company account-id)
vendor-id (transaction->vendor-id transaction)]] vendor-id (transaction->vendor-id transaction)]]
(try (try
@@ -53,7 +73,9 @@
:status status :status status
:company-id company-id :company-id company-id
:vendor-id vendor-id :vendor-id vendor-id
:check-id (transaction->check-id transaction company-id vendor-id) :check-number check-number
:bank-account-id (yodlee-account-id->bank-account-id account-id)
:check-id (transaction->check-id transaction check-number company-id vendor-id)
}) })
(catch Exception e (catch Exception e
(println e))))) (println e))))))