Files
integreat/src/clj/auto_ap/db/transactions.clj
2018-07-10 21:55:13 -07:00

35 lines
1.4 KiB
Clojure

(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 limited-companies] :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 :check_id)))))))
(def base-query (sql/build :select :*
:from :transactions))
(defn base-graphql [{:keys [company-id id]}]
(println "ID" id)
(cond-> base-query
(limited-companies id) (helpers/merge-where [:in :company-id (limited-companies id)])
(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.*])))))