automated imports.

This commit is contained in:
BC
2018-06-14 23:16:10 -07:00
parent a17b6b9688
commit 80112d08a7
19 changed files with 458 additions and 71 deletions

View File

@@ -68,9 +68,10 @@
:else
q)))
(defn base-graphql [{:keys [company-id]}]
(defn base-graphql [{:keys [company-id vendor-id]}]
(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])))
(defn get-graphql [{:keys [start sort-by asc] :as args}]
(query

View File

@@ -0,0 +1,32 @@
(ns auto-ap.db.transactions
(:require [clojure.java.jdbc :as j]
[honeysql.core :as sql]
[honeysql.helpers :as helpers]
[honeysql-postgres.format :as postgres-format]
[honeysql-postgres.helpers :as postgres-helpers]
[auto-ap.db.utils :refer [clj->db kebab->snake db->clj get-conn query] :as utils]))
(defn upsert! [row]
(j/db-do-prepared (get-conn)
(sql/format (-> (helpers/insert-into :transactions)
(helpers/values [row])
(postgres-helpers/upsert (-> (postgres-helpers/on-conflict :id)
(postgres-helpers/do-update-set :post_date :status)))))))
(def base-query (sql/build :select :*
:from :transactions))
(defn base-graphql [{:keys [company-id]}]
(cond-> base-query
(not (nil? company-id)) (helpers/merge-where [:= :company-id company-id])))
(defn get-graphql [{:keys [start sort-by asc] :as args}]
(query
(cond-> (base-graphql args)
#_#_(not (nil? sort-by) ) (add-sort-by sort-by asc)
true (assoc :limit 20)
start (assoc :offset start))))
(defn count-graphql [args]
(:count (first (query
(assoc (base-graphql args) :select [:%count.*])))))

View File

@@ -1,14 +0,0 @@
(ns auto-ap.db.yodlee-imports
(:require [clojure.java.jdbc :as j]
[honeysql.core :as sql]
[honeysql.helpers :as helpers]
[honeysql-postgres.format :as postgres-format]
[honeysql-postgres.helpers :as postgres-helpers]
[auto-ap.db.utils :refer [clj->db kebab->snake db->clj get-conn query] :as utils]))
(defn upsert! [row]
(j/db-do-prepared (get-conn)
(sql/format (-> (helpers/insert-into :yodlee-imports)
(helpers/values [row])
(postgres-helpers/upsert (-> (postgres-helpers/on-conflict :id)
(postgres-helpers/do-update-set :amount :status)))))))

View File

@@ -16,6 +16,7 @@
[auto-ap.graphql.checks :as gq-checks]
[auto-ap.graphql.expense-accounts :as expense-accounts]
[auto-ap.graphql.invoices :as gq-invoices]
[auto-ap.graphql.transactions :as gq-transactions]
[auto-ap.db.reminders :as reminders]
[auto-ap.db.invoices-checks :as invoices-checks]
[auto-ap.db.utils :as utils]
@@ -69,6 +70,19 @@
:s3_url {:type 'String}
:check_number {:type 'Int}}}
:transaction {:fields {:id {:type 'Int}
:amount {:type 'String}
:description_original {:type 'String}
:description_simple {:type 'String}
:status {:type 'String}
:vendor {:type :vendor
:resolve :get-vendor-for-transaction}
:company {:type :company
:resolve :get-company-for-transaction}
:check {:type :check
:resolve :get-check-for-transaction}
:date {:type 'String}
:post_date {:type 'String}}}
:invoice_check
{:fields {:id {:type 'Int}
:amount {:type 'String}
@@ -126,6 +140,12 @@
:start {:type 'Int}
:end {:type 'Int}}}
:transaction_page {:fields {:transactions {:type '(list :transaction)}
:count {:type 'Int}
:total {:type 'Int}
:start {:type 'Int}
:end {:type 'Int}}}
:reminder_page {:fields {:reminders {:type '(list :reminder)}
:count {:type 'Int}
:total {:type 'Int}
@@ -150,6 +170,14 @@
:resolve :get-invoice-page}
:transaction_page {:type '(list :transaction_page)
:args {:company_id {:type 'Int}
:start {:type 'Int}
:sort_by {:type 'String}
:asc {:type 'Boolean}}
:resolve :get-transaction-page}
:check_page {:type '(list :check_page)
:args {:company_id {:type 'Int}
:start {:type 'Int}
@@ -352,10 +380,14 @@
(-> integreat-schema
(attach-resolvers {:get-invoice-page get-invoice-page
:get-check-page gq-checks/get-check-page
:get-transaction-page gq-transactions/get-transaction-page
:get-reminder-page get-reminder-page
:get-vendor-for-invoice get-vendor-for-invoice
:get-vendor-for-check gq-checks/get-vendor-for-check
:get-company-for-check gq-checks/get-company-for-check
:get-company-for-transaction gq-transactions/get-company-for-transaction
:get-vendor-for-transaction gq-transactions/get-vendor-for-transaction
:get-check-for-transaction gq-transactions/get-check-for-transaction
:get-company-for-invoice get-company-for-invoice
:get-invoices-checks get-invoices-checks
:get-check-by-id get-check-by-id

View File

@@ -8,7 +8,7 @@
(defn -create-vendor-if-necessary [vendor-id vendor-name]
(if vendor-id
vendor-id
(:id (doto (vendors/insert {:name vendor-name :default-expense-account 0}) println))))
(:id (vendors/insert {:name vendor-name :default-expense-account 0}))))
(defn add-invoice [context {{:keys [total invoice_number company_id vendor_id vendor_name date] :as in} :invoice} value]
(let [vendor_id (-create-vendor-if-necessary vendor_id vendor_name)]

View File

@@ -6,6 +6,7 @@
[auto-ap.routes.reminders :as reminders]
[auto-ap.routes.graphql :as graphql]
[auto-ap.routes.vendors :as vendors]
[auto-ap.routes.events :as events]
[auto-ap.routes.checks :as checks]
[auto-ap.db.utils :as u]
[buddy.auth.backends.token :refer [jws-backend]]
@@ -41,11 +42,6 @@
(def auth-backend (jws-backend {:secret (:jwt-secret env) :options {:alg :hs512}}))
(def app-routes
(routes
api-routes
static-routes))
(defn wrap-transaction [handler]
(fn [request]
(jdbc/with-db-transaction [t (u/get-conn)]
@@ -56,9 +52,18 @@
(jdbc/db-set-rollback-only! t)
(throw e)))))))
(def app-routes
(routes
(wrap-transaction api-routes)
(context "/api" [] events/routes)
static-routes))
(def app
(-> #'app-routes
(wrap-transaction)
(wrap-authorization auth-backend)
(wrap-authentication auth-backend)
(wrap-reload)

View File

@@ -7,5 +7,5 @@
(defn -main [& args]
(let [port (Integer/parseInt (or (env :port) "3000"))]
(future (always-process-sqs))
#_(future (always-process-sqs))
(run-jetty app {:port port :join? false})))

View File

@@ -1,30 +1,60 @@
(ns auto-ap.yodlee.import
(:require [auto-ap.yodlee.core :as client]
[auto-ap.db.yodlee-imports :as yodlee-imports]
[auto-ap.db.transactions :as transactions]
[auto-ap.db.vendors :as vendors]
[auto-ap.db.companies :as companies]
[auto-ap.db.checks :as checks]
[auto-ap.time :as time]))
(defn account->company-id [account-id]
(-> (companies/get-all)
first
:id))
(defn transaction->vendor-id [_]
(-> (vendors/get-all)
first
:id))
(defn transaction->check-id [_ company-id vendor-id]
(when (= 0 (rand-int 2))
(-> (checks/get-graphql {:company-id company-id
:vendor-id vendor-id})
first
:id)))
(defn do-import []
(doseq [transaction (client/get-transactions)
:let [{post-date :postDate
account-id :accountId
date :date
id :id
{amount :amount} :amount
{description-original :original
description-simple :simple} :description
{merchant-id :i
merchant-name :name} :merchant
type :type
status :status
}
transaction]]
(yodlee-imports/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})))
:let [{post-date :postDate
account-id :accountId
date :date
id :id
{amount :amount} :amount
{description-original :original
description-simple :simple} :description
{merchant-id :i
merchant-name :name} :merchant
type :type
status :status}
transaction
company-id (account->company-id account-id)
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-id (transaction->check-id transaction company-id vendor-id)
})
(catch Exception e
(println e)))))