loading relations

This commit is contained in:
Bryce Covert
2018-04-10 09:52:35 -07:00
parent 03f3df8643
commit 4a0275f024
5 changed files with 52 additions and 27 deletions

View File

@@ -1,17 +1,38 @@
(ns auto-ap.db.invoices
(:require [auto-ap.db.utils :refer [clj->db db->clj get-conn]]
[auto-ap.parse :as parse]
[auto-ap.db.companies :as companies]
[auto-ap.db.vendors :as vendors]
[auto-ap.entities.companies :as company]
[auto-ap.entities.vendors :as vendor]
[clojure.java.jdbc :as j]))
[clojure.java.jdbc :as j]
[aggregate.core :as agg]))
(defn insert-multi! [rows]
(j/insert-multi! (get-conn)
:invoices
(map clj->db rows)))
(defn with-relations [results]
(let [companies (reduce
#(assoc %1 (::company/id %2) %2)
{}
(companies/get-all))
vendors (reduce
#(assoc %1 (::vendor/id %2) %2)
{}
(vendors/get-all))]
(->> results
(map #(assoc % :vendor (vendors (:vendor-id %))))
(map #(assoc % :company (companies (:company-id %)))))))
(defn get-all []
(map db->clj (j/query (get-conn) "SELECT * FROM invoices")))
(->> (j/query (get-conn)
(str " SELECT invoices.* "
" FROM invoices "))
(map db->clj)
with-relations
))
(defn approve []
@@ -22,21 +43,18 @@
(defn get-unpaid [company]
(if company
(map db->clj (j/query (get-conn) ["SELECT * FROM invoices WHERE imported=true AND company_id = ?" (Integer/parseInt company)]))
(map db->clj (j/query (get-conn) "SELECT * FROM invoices WHERE imported=true"))))
(with-relations (map db->clj (j/query (get-conn) ["SELECT * FROM invoices WHERE imported=true AND company_id = ?" (Integer/parseInt company)])))
(with-relations (map db->clj (j/query (get-conn) "SELECT * FROM invoices WHERE imported=true")))))
(defn get-pending [company]
(if company
(map db->clj (j/query (get-conn) ["SELECT * FROM invoices WHERE (imported=false or imported is null) AND company_id = ?" (Integer/parseInt company)]))
(map db->clj (j/query (get-conn) "SELECT * FROM invoices WHERE imported=false or imported is null"))))
(with-relations (map db->clj (j/query (get-conn) ["SELECT * FROM invoices WHERE (imported=false or imported is null) AND company_id = ?" (Integer/parseInt company)])))
(with-relations (map db->clj (j/query (get-conn) "SELECT * FROM invoices WHERE imported=false or imported is null")))))
(defn import [parsed-invoices companies vendors]
(insert-multi!
(for [{:keys [total date invoice-number customer-identifier vendor-code] :as row} parsed-invoices]
(do
(println "VENDORS" vendors)
(println "VENDOR" (::vendor-id (first (filter #(= (::vendor/code %) vendor-code) vendors))))
(dissoc (assoc row
:company-id (::company/id (parse/best-match companies customer-identifier))
:vendor-id (::vendor/id (first (filter #(= (::vendor/code %) vendor-code) vendors)))

View File

@@ -1,5 +1,6 @@
(ns auto-ap.db.utils
(:require [clojure.string :as str]
[clojure.edn :as edn]
[config.core :refer [env]]))
(defn snake->kebab [s]
@@ -8,12 +9,18 @@
(defn kebab->snake [s]
(str/replace s #"-" "_"))
(defn db->clj [x]
(into {}
(map
(fn [[k v]]
[(keyword (snake->kebab (name k))) v])
x)))
(defn db->clj
([x]
(let [kebabed (into {}
(map
(fn [[k v]]
[(keyword (snake->kebab (name k))) v])
x))
merged (merge kebabed (edn/read-string (:data kebabed)))]
merged))
([x namespace]
(let [x (db->clj x)]
(assign-namespace x namespace))))
(defn clj->db [x]
(into {}
@@ -22,6 +29,9 @@
[(keyword (kebab->snake (name k))) v])
x)))
(defn merge-data [{:keys [data] :as x}]
(merge x (edn/read-string data)))
(defn get-conn []
(let [db-host (:server (:db env))
db-port 5432

View File

@@ -5,15 +5,9 @@
[clojure.edn :as edn]
[clojure.java.jdbc :as j]))
(defn merge-data [{:keys [data] :as x}]
(merge x (edn/read-string data)))
(defn parse [x]
(-> x
(db->clj)
merge-data
(assign-namespace "auto-ap.entities.vendors")
))
(db->clj x "auto-ap.entities.vendors"))
(defn unparse [x]
(-> x

View File

@@ -4,6 +4,7 @@
[auto-ap.events :as events]
[auto-ap.subs :as subs]
[auto-ap.entities.companies :as company]
[auto-ap.entities.vendors :as vendor]
[cljsjs.dropzone :as dropzone]
[cljs.reader :as edn]))
(def dropzone
@@ -57,12 +58,12 @@
[:th "Date"]
[:th "Amount"]
[:th]]]
[:tbody (for [{:keys [vendor-id potential-duplicate company-id customer-identifier invoice-number date total id] :as i} @invoices]
[:tbody (for [{:keys [vendor vendor-id potential-duplicate company-id customer-identifier invoice-number date total id] :as i} @invoices]
^{:key (str company-id "-" invoice-number "-" date "-" total "-" id)}
[:tr
[:td vendor-id]
[:td (::vendor/name (:vendor i))]
(if company-id
[:td company-id]
[:td (::company/name (:company i))]
[:td [:i.icon.fa.fa-warning {:title "potential duplicate"}]
(str "'" customer-identifier "' doesn't match any known company")])
[:td invoice-number]

View File

@@ -1,5 +1,7 @@
(ns auto-ap.views.pages.unpaid-invoices
(:require [re-frame.core :as re-frame]
[auto-ap.entities.companies :as company]
[auto-ap.entities.vendors :as vendor]
[auto-ap.subs :as subs]
[auto-ap.events :as events]
))
@@ -27,8 +29,8 @@
[:tbody (for [{:keys [company invoice-number date total id vendor] :as i} @invoices]
^{:key (str company "-" invoice-number "-" date "-" total "-" id)}
[:tr
[:td vendor]
[:td company]
[:td (::vendor/name vendor)]
[:td (::company/name company)]
[:td invoice-number]
[:td date]
[:td total]])]])]))