more code trimming for datomic.

This commit is contained in:
Bryce Covert
2018-08-16 08:38:01 -07:00
parent a4e3fe2327
commit ad2dd386ee
4 changed files with 64 additions and 45 deletions

View File

@@ -678,8 +678,9 @@
(defn load-users [users]
(->> users
(map
(fn [{:keys [id role provider-id provider companies]}]
(fn [{:keys [id role provider-id provider companies name]}]
(remove-nils #:user {:original-id id
:name name
:role (keyword "user-role" role)
:provider-id provider-id
:provider provider

View File

@@ -0,0 +1,46 @@
(ns auto-ap.datomic.users
(:require [datomic.api :as d]
[auto-ap.datomic :refer [uri]]
[clojure.set :refer [rename-keys]]
[clj-time.coerce :as c]))
(defn add-arg [query name value where & rest]
(let [query (-> query
(update :args conj value)
(update-in [:query :in] conj name)
(update-in [:query :where] conj where))]
(reduce #(update-in %1 [:query :where] conj %2) query rest)))
(defn raw-graphql [args]
(let [query (cond-> {:query {:find ['(pull ?e [*
{:user/clients [*]}
{:user/role [:db/ident]}])]
:in ['$]
:where ['[?e :user/original-id]]}
:args [(d/db (d/connect uri))]})]
(->> (d/query query)
(map first)
(map #(update % :user/role :db/ident))
)))
(defn sort-fn [args]
(cond
(= "client" (:sort-by args))
#(-> % :payment/client :client/name)
:else
(keyword "payment" (:sort-by args))))
(defn get-graphql [args]
(let [results (raw-graphql args)]
(cond->> results
(:sort-by args) (sort-by (sort-fn args))
(= (:asc args) false) (reverse)
true (drop (:start args 0))
true (take (:count args 20)))))
(defn count-graphql [args]
(->> (raw-graphql args)
(count)))

View File

@@ -13,6 +13,7 @@
[auto-ap.db.companies :as companies]
[auto-ap.datomic.clients :as d-clients]
[auto-ap.datomic.checks :as d-checks]
[auto-ap.datomic.users :as d-users]
[auto-ap.datomic.invoices :as d-invoices]
[auto-ap.datomic.vendors :as d-vendors]
[auto-ap.db.users :as users]
@@ -89,8 +90,7 @@
:body {:type 'String}
:scheduled {:type 'String}
:sent {:type 'String}
:vendor {:type :vendor
:resolve :get-vendor-for-invoice}
:vendor {:type :vendor}
}
}
@@ -145,8 +145,7 @@
{:fields {:id {:type 'Int}
:name {:type 'String}
:role {:type 'String}
:companies {:type '(list :company)
:resolve :get-user-companies}}}
:clients {:type '(list :company)}}}
:expense_account {:fields {:id {:type 'Int}
:location {:type 'String}
@@ -407,29 +406,14 @@
:start (:start args 0)
:end (+ (:start args 0) (count reminders))}] extra-context)))
(defn get-vendor-for-invoice [context args value]
(->graphql
(if-let [vendor-cache (:vendor-cache context)]
(vendor-cache (:vendor_id value))
(vendors/get-by-id (:vendor_id value)))))
(defn get-check-by-id [context args value]
(->graphql
(checks/get-by-id (:check_id value))))
(defn get-invoices-checks [context args value]
(->graphql
(invoices-checks/get-for-invoice-id (:id value))))
(defn get-checks-invoices [context args value]
(->graphql
(invoices-checks/get-for-check-id (:id value))))
(defn get-company-for-invoice [context args value]
(->graphql
(if-let [company-cache (:company-cache context)]
(company-cache (:company_id value))
(companies/get-by-id (:company_id value)))))
(defn bank-account-for-check [context args value]
(->graphql
@@ -461,13 +445,8 @@
(defn get-user [context args value]
(assert-admin (:id context))
(let [
users (users/get-all)
extra-context (cond-> context
(executor/selects-field? context :user/companies) (assoc :company-cache (by :id (companies/get-all))))]
(resolve/with-context
(->graphql users) extra-context)))
(let [users (d-users/get-graphql args)]
(->graphql users)))
(defn get-vendor [context args value]
(->graphql
@@ -495,13 +474,6 @@
:get-payment-page gq-checks/get-payment-page
:get-transaction-page gq-transactions/get-transaction-page
:get-reminder-page get-reminder-page
:get-vendor-for-invoice get-vendor-for-invoice
: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-checks-invoices get-checks-invoices
:get-check-by-id get-check-by-id
:get-invoices-expense-accounts gq-invoices/get-invoices-expense-accounts
:get-company get-company
:get-user get-user
:get-user-companies get-user-companies

View File

@@ -32,7 +32,7 @@
[:name
:id
:role
[:companies [:id :name]]]]]}
[:clients [:id :name]]]]]}
:on-success [::received]}}))
(re-frame/reg-event-db
@@ -79,7 +79,7 @@
:venia/queries [{:query/data [:edit-user
{:edit-user (update (get-in db [::editing :user]) :companies #(map :id %))}
[:id :name :role [:companies [:id :name]]]]}]}
[:id :name :role [:clients [:id :name]]]]}]}
:on-success [::saved]}}))
(re-frame/reg-event-fx
@@ -102,13 +102,13 @@
[:th "User"]
[:th "Role"]
[:th "Companies"]]]
[:tbody (for [{:keys [id name role companies] :as c} @users]
[:tbody (for [{:keys [id name role clients] :as c} @users]
^{:key (str name "-" id )}
[:tr {:on-click (fn [] (re-frame/dispatch [::edit id]))
:style {"cursor" "pointer"}}
[:td name]
[:td role]
[:td (str/join ", " (map :name companies))]])]]))
[:td (str/join ", " (map :name clients))]])]]))
@@ -152,7 +152,7 @@
(when (= "user" (:role (:user editing)))
[horizontal-field
[:label.label "Companies"]
[:label.label "Clients"]
[:div.control
[:div.field.has-addons
@@ -164,7 +164,7 @@
:event ::change
:subscription editing}
[:option]
(let [used-companies (set (map :id (:companies (:user editing))))]
(let [used-companies (set (map :id (:clients (:user editing))))]
(for [{:keys [id name]} @(re-frame/subscribe [::subs/companies])
:when (not (used-companies id))]
^{:key id} [:option {:value id} name]))]]]]
@@ -172,7 +172,7 @@
[:button.button.is-primary {:on-click (dispatch-event [::add-company])} "Add"]]]
[:ul
(for [{:keys [id name]} (:companies (:user editing))]
(for [{:keys [id name]} (:clients (:user editing))]
^{:key id} [:li name [:a.icon {:on-click (dispatch-event [::remove-company id])} [:i.fa.fa-times ]]])]]])]])])
{:component-will-mount #(re-frame/dispatch-sync [::users-mounted {}]) }))