graphql used for invoices

This commit is contained in:
Bryce Covert
2018-04-12 10:17:15 -07:00
parent 4165c7d180
commit 7425f7f393
15 changed files with 266 additions and 115 deletions

View File

@@ -1,20 +1,33 @@
(ns auto-ap.db.companies
(:require [auto-ap.db.utils :refer [clj->db db->clj get-conn]]
(:require [auto-ap.db.utils :refer [clj->db db->clj get-conn query execute!]]
[auto-ap.entities.companies :as entity]
[clojure.edn :as edn]
[clojure.java.jdbc :as j]))
[clojure.java.jdbc :as j]
[honeysql.core :as sql]
[honeysql.helpers :as helpers]))
(defn parse [x]
(db->clj x))
(def base-query (sql/build :select :*
:from :companies))
(defn get-all []
(->> (j/query (get-conn) "SELECT * FROM companies")
(map parse)))
(defn upsert [id data]
(j/update! (get-conn) :companies (clj->db data) ["id = ?" (Integer/parseInt id)] )
(parse (first (j/query (get-conn) ["SELECT * FROM companies WHERE id = ?" (Integer/parseInt id)]))))
(query base-query))
(defn get-by-id [id]
(parse (first (j/query (get-conn) ["SELECT * FROM companies WHERE id = ?" id]))))
(first (query (-> base-query
(helpers/merge-where [:= :id id])))))
(defn upsert [id data]
(prn (clj->db (select-keys data entity/all-keys)))
(-> (sql/build
:update :companies
:set (clj->db (select-keys data entity/all-keys ))
:where [:= :id (if (int? id)
id
(Integer/parseInt id))])
execute!)
(get-by-id (if (int? id)
id
(Integer/parseInt id))))

View File

@@ -1,66 +1,56 @@
(ns auto-ap.db.invoices
(:require [auto-ap.db.utils :refer [clj->db db->clj get-conn]]
(:require [auto-ap.db.utils :refer [clj->db db->clj get-conn query] :as utils]
[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.string :as str]))
[clojure.string :as str]
[honeysql.core :as sql]
[honeysql.helpers :as helpers]))
(defn insert-multi! [rows]
(j/insert-multi! (get-conn)
:invoices
(map clj->db rows)))
(defn with-relations [results]
(let [companies (reduce
#(assoc %1 (:id %2) %2)
{}
(companies/get-all))
vendors (reduce
#(assoc %1 (:id %2) %2)
{}
(vendors/get-all))]
(println companies vendors)
(->> results
(map #(assoc % :vendor (vendors (:vendor-id %))))
(map #(assoc % :company (companies (:company-id %)))))))
(def base-query (sql/build :select :*
:from :invoices))
(defn get-all []
(->> (j/query (get-conn)
(str " SELECT invoices.* "
" FROM invoices "))
(map db->clj)
with-relations
))
(query base-query))
(defn approve []
(map db->clj (j/update! (get-conn) :invoices {:imported true} [] )))
(j/update! (get-conn) :invoices {:imported true} [] ))
(defn reject []
(j/delete! (get-conn) :invoices ["imported = false"]))
(defn get-unpaid [company]
(if company
(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")))))
(query
(if company
(-> base-query
(helpers/merge-where [:= :imported true])
(helpers/merge-where [:= :company-id company]))
(-> base-query
(helpers/merge-where [:= :imported true])))))
(defn get-pending [company]
(if company
(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")))))
(query
(if company
(-> base-query
(helpers/merge-where [:= :imported false])
(helpers/merge-where [:= :company-id company]))
(-> base-query
(helpers/merge-where [:= :imported false])))))
(defn query [params]
(let [ks (keys params)
sql (str " SELECT * FROM invoices "
(when (seq params)
" WHERE ")
(str/join " AND " (map (fn [k] (str (name k) " = ?")) ks)))
vs (map params ks)]
(j/query (get-conn) (into [sql] vs))))
(defn get-graphql [{:keys [imported company-id]}]
(query
(cond-> base-query
(not (nil? imported)) (helpers/merge-where [:= :imported imported])
(not (nil? company-id)) (helpers/merge-where [:= :company-id company-id]))))
(defn import [parsed-invoices companies vendors]
(insert-multi!
@@ -71,4 +61,4 @@
:vendor-id (:id (first (filter #(= (:code %) vendor-code) vendors)))
:imported false
:potential-duplicate false)
:vendor-code)))))
:vendor-code)))))

View File

@@ -1,7 +1,9 @@
(ns auto-ap.db.utils
(ns auto-ap.db.utils
(:require [clojure.string :as str]
[clojure.edn :as edn]
[config.core :refer [env]]))
[clojure.java.jdbc :as j]
[config.core :refer [env]]
[honeysql.core :as sql]))
(defn snake->kebab [s]
(str/replace s #"_" "-"))
@@ -52,3 +54,13 @@
:user "ap"
:password "fifteen-invoices-imported!"}))
(defn query [q]
(let [formatted (sql/format q)]
(println "Executing query " q " SQL: " formatted)
(map db->clj (j/query (get-conn) formatted))))
(defn execute! [q]
(let [formatted (sql/format q)]
(println "Executing query " q " SQL: " formatted)
(j/execute! (get-conn) formatted)))

View File

@@ -1,33 +1,46 @@
(ns auto-ap.db.vendors
(:require [auto-ap.db.utils :refer [clj->db db->clj get-conn]]
(:require [auto-ap.db.utils :refer [clj->db db->clj get-conn query execute!]]
[auto-ap.entities.vendors :as entities]
[clojure.edn :as edn]
[clojure.java.jdbc :as j]))
[clojure.java.jdbc :as j]
[honeysql.core :as sql]
[honeysql.helpers :as helpers]
[honeysql.format :as f]))
(defn parse [x]
(db->clj x))
(defn unparse [x]
(-> x
(select-keys entities/all-keys)
clj->db))
(select-keys entities/all-keys)))
(def base-query (sql/build :select :*
:from :vendors))
(defn get-all []
(->> (j/query (get-conn) "SELECT * FROM vendors")
(map parse)))
(query base-query))
(defn get-by-id [id]
(parse (first (j/query (get-conn) ["SELECT * FROM vendors WHERE id = ?" id]))))
(first (query (-> base-query
(helpers/merge-where [:= :id id])))))
(defn upsert [id data]
(j/update! (get-conn) :vendors (unparse data) ["id = ?" (Integer/parseInt id)] )
(parse (first (j/query (get-conn) ["SELECT * FROM vendors WHERE id = ?" (Integer/parseInt id)]))))
(-> (sql/build
:update :vendors
:set (unparse data)
:where [:= :id (if (int? id)
id
(Integer/parseInt id))])
execute!)
(get-by-id (if (int? id)
id
(Integer/parseInt id))))
(defn insert [data]
(parse (first (j/insert! (get-conn)
:vendors
(unparse data)))))
(let [[id] (-> (sql/build :insert-into :vendors
:values [(unparse data)])
execute!)]
(get-by-id id)))
(defn find-with-reminders []
(map parse (j/query (get-conn) ["SELECT * FROM vendors WHERE invoice_reminder_schedule = ?" "Weekly"])))
(query (-> base-query
(helpers/merge-where [:= :invoice-reminder-schedule "Weekly"]))))

View File

@@ -1,15 +1,16 @@
(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]
[auto-ap.db.invoices :as invoices]
[auto-ap.db.vendors :as vendors]
[auto-ap.db.companies :as companies]
[auto-ap.db.utils :as utils]
[clojure.walk :as walk])
[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]
[auto-ap.db.invoices :as invoices]
[auto-ap.db.vendors :as vendors]
[auto-ap.db.companies :as companies]
[auto-ap.db.utils :as utils]
[clojure.walk :as walk]
[clojure.string :as str])
(:import
(clojure.lang IPersistentMap)))
@@ -19,14 +20,20 @@
{
:company
{:fields {:id {:type 'Int}
:name {:type 'String}}}
:name {:type 'String}
:email {:type 'String}}}
:vendor
{:fields {:id {:type 'Int}
:name {:type 'String}}}
:name {:type 'String}
:invoice_reminder_schedule {:type 'String}}}
:invoice
{:fields {:id {:type 'Int}
:total {:type 'String}
:invoice_number {:type 'String}
:date {:type 'String}
:company_id {:type 'Int}
:vendor {:type :vendor
:resolve :get-vendor}
:company {:type :company
:resolve :get-company}}}}
@@ -44,22 +51,65 @@
{}
x))
(defn snake->kebab [s]
(str/replace s #"_" "-"))
(defn kebab [x]
(keyword (snake->kebab (name x))))
(defn kebab->snake [s]
(str/replace s #"-" "_"))
(defn snake [x]
(keyword (kebab->snake (name x))))
(defn ->graphql [m]
(walk/postwalk
(fn [node]
(cond
(keyword? node)
(snake node)
:else
node))
m))
(defn <-graphql [m]
(walk/postwalk
(fn [node]
(cond
(keyword? node)
(kebab node)
:else
node))
m))
(defn get-invoice [context args value]
(println (<-graphql args))
(let [extra-context
(cond-> {}
(executor/selects-field? context :invoice/vendor) (assoc :vendor-cache (by (vendors/get-all) :id ))
(executor/selects-field? context :invoice/company) (assoc :company-cache (by (companies/get-all) :id )))]
(resolve/with-context (invoices/query args) extra-context)))
(resolve/with-context
(map
->graphql
(invoices/get-graphql (<-graphql args))) extra-context)))
(defn get-vendor [context args value]
(if-let [vendor-cache (:vendor-cache context)]
(vendor-cache (:vendor_id value))
(vendors/get-by-id (:vendor_id value))))
(->graphql
(if-let [vendor-cache (:vendor-cache context)]
(vendor-cache (:vendor_id value))
(vendors/get-by-id (:vendor_id value)))))
(defn get-company [context args value]
(if-let [company-cache (:company-cache context)]
(company-cache (:company_id value))
(companies/get-by-id (:company_id value))))
(->graphql
(if-let [company-cache (:company-cache context)]
(company-cache (:company_id value))
(companies/get-by-id (:company_id value)))))
(def schema
(-> integreat-schema
@@ -68,6 +118,8 @@
:get-company get-company})
schema/compile))
(defn simplify
"Converts all ordered maps nested within the map into standard hash maps, and
sequences into vectors, which makes for easier constants in the tests, and eliminates ordering problems."
@@ -81,9 +133,15 @@
(seq? node)
(vec node)
(keyword? node)
(kebab node)
:else
node))
m))
(defn query [q]
(simplify (execute schema q nil nil)))
(defn query
([q]
(simplify (execute schema q nil nil)))
([q v]
(simplify (execute schema q v nil))))

View File

@@ -3,6 +3,7 @@
[auto-ap.routes.utils :refer [wrap-secure wrap-spec]]
[auto-ap.entities.companies :as entity]
[auto-ap.graphql :as ql]
[clojure.edn :as edn]
[compojure.core :refer [GET PUT context defroutes
wrap-routes]]))
@@ -11,7 +12,10 @@
(wrap-routes
(context "/graphql" []
(GET "/" {:keys [query-params]}
{:status 200
:body (pr-str (ql/query (query-params "query")))
:headers {"Content-Type" "application/edn"}}))
(let [variables (some-> (query-params "variables")
edn/read-string)]
(println variables)
{:status 200
:body (pr-str (ql/query (query-params "query") variables))
:headers {"Content-Type" "application/edn"}})))
wrap-secure))

View File

@@ -2,6 +2,7 @@
(:require [auto-ap.db.companies :as companies]
[auto-ap.db.vendors :as vendors]
[auto-ap.db.invoices :as invoices]
[auto-ap.db.utils :refer [query]]
[auto-ap.parse :as parse]
[auto-ap.routes.utils :refer [wrap-secure]]
[compojure.core :refer [GET POST context defroutes