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

@@ -10,10 +10,10 @@ delete from companies;
INSERT INTO companies (code, name, data) INSERT INTO companies (code, name, data)
VALUES 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,58 +2,80 @@
(: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)
:let [{post-date :postDate all-bank-accounts (mapcat (fn [c] (map
account-id :accountId (fn [{:keys [id yodlee-account-id]}]
date :date (when (and id yodlee-account-id)
id :id {:bank-account-id id
{amount :amount} :amount :company-id (:id c)
{description-original :original :yodlee-account-id yodlee-account-id}))
description-simple :simple} :description (:bank-accounts c)))
{merchant-id :i (companies/get-all))
merchant-name :name} :merchant account->company (by :yodlee-account-id :company-id all-bank-accounts)
type :type yodlee-account-id->bank-account-id (by :yodlee-account-id :bank-account-id all-bank-accounts)]
status :status} (println "importing " (count transactions) "transactions")
transaction (println "yodlee account->company" account->company)
company-id (account->company-id account-id) (println "yodlee account->bank-account-id" yodlee-account-id->bank-account-id)
vendor-id (transaction->vendor-id transaction)]] (doseq [transaction transactions
:let [{post-date :postDate
(try account-id :accountId
(transactions/upsert! date :date
{:post-date (time/parse post-date "YYYY-MM-dd") id :id
:id id {amount :amount} :amount
:account-id account-id {description-original :original
:date (time/parse date "YYYY-MM-dd") description-simple :simple} :description
:amount amount {merchant-id :i
:description-original description-original merchant-name :name} :merchant
:description-simple description-simple type :type
:type type status :status}
:status status transaction
:company-id company-id transaction (if (= 0 (rand-int 3))
:vendor-id vendor-id (assoc-in transaction [:description :original] (str "check xxx" (rand-nth ["6789" "1234" "6790"])))
:check-id (transaction->check-id transaction company-id vendor-id) transaction)
}) check-number (extract-check-number transaction)
(catch Exception e company-id (account->company account-id)
(println e))))) vendor-id (transaction->vendor-id transaction)]]
(try
(transactions/upsert!
{:post-date (time/parse post-date "YYYY-MM-dd")
:id id
:account-id account-id
:date (time/parse date "YYYY-MM-dd")
:amount amount
:description-original description-original
:description-simple description-simple
:type type
:status status
:company-id company-id
:vendor-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
(println e))))))