more code trimming for datomic.
This commit is contained in:
@@ -678,8 +678,9 @@
|
|||||||
(defn load-users [users]
|
(defn load-users [users]
|
||||||
(->> users
|
(->> users
|
||||||
(map
|
(map
|
||||||
(fn [{:keys [id role provider-id provider companies]}]
|
(fn [{:keys [id role provider-id provider companies name]}]
|
||||||
(remove-nils #:user {:original-id id
|
(remove-nils #:user {:original-id id
|
||||||
|
:name name
|
||||||
:role (keyword "user-role" role)
|
:role (keyword "user-role" role)
|
||||||
:provider-id provider-id
|
:provider-id provider-id
|
||||||
:provider provider
|
:provider provider
|
||||||
|
|||||||
46
src/clj/auto_ap/datomic/users.clj
Normal file
46
src/clj/auto_ap/datomic/users.clj
Normal 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)))
|
||||||
@@ -13,6 +13,7 @@
|
|||||||
[auto-ap.db.companies :as companies]
|
[auto-ap.db.companies :as companies]
|
||||||
[auto-ap.datomic.clients :as d-clients]
|
[auto-ap.datomic.clients :as d-clients]
|
||||||
[auto-ap.datomic.checks :as d-checks]
|
[auto-ap.datomic.checks :as d-checks]
|
||||||
|
[auto-ap.datomic.users :as d-users]
|
||||||
[auto-ap.datomic.invoices :as d-invoices]
|
[auto-ap.datomic.invoices :as d-invoices]
|
||||||
[auto-ap.datomic.vendors :as d-vendors]
|
[auto-ap.datomic.vendors :as d-vendors]
|
||||||
[auto-ap.db.users :as users]
|
[auto-ap.db.users :as users]
|
||||||
@@ -89,8 +90,7 @@
|
|||||||
:body {:type 'String}
|
:body {:type 'String}
|
||||||
:scheduled {:type 'String}
|
:scheduled {:type 'String}
|
||||||
:sent {:type 'String}
|
:sent {:type 'String}
|
||||||
:vendor {:type :vendor
|
:vendor {:type :vendor}
|
||||||
:resolve :get-vendor-for-invoice}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -145,8 +145,7 @@
|
|||||||
{:fields {:id {:type 'Int}
|
{:fields {:id {:type 'Int}
|
||||||
:name {:type 'String}
|
:name {:type 'String}
|
||||||
:role {:type 'String}
|
:role {:type 'String}
|
||||||
:companies {:type '(list :company)
|
:clients {:type '(list :company)}}}
|
||||||
:resolve :get-user-companies}}}
|
|
||||||
|
|
||||||
:expense_account {:fields {:id {:type 'Int}
|
:expense_account {:fields {:id {:type 'Int}
|
||||||
:location {:type 'String}
|
:location {:type 'String}
|
||||||
@@ -407,29 +406,14 @@
|
|||||||
:start (:start args 0)
|
:start (:start args 0)
|
||||||
:end (+ (:start args 0) (count reminders))}] extra-context)))
|
: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]
|
(defn bank-account-for-check [context args value]
|
||||||
(->graphql
|
(->graphql
|
||||||
@@ -461,13 +445,8 @@
|
|||||||
(defn get-user [context args value]
|
(defn get-user [context args value]
|
||||||
(assert-admin (:id context))
|
(assert-admin (:id context))
|
||||||
|
|
||||||
(let [
|
(let [users (d-users/get-graphql args)]
|
||||||
users (users/get-all)
|
(->graphql users)))
|
||||||
|
|
||||||
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)))
|
|
||||||
|
|
||||||
(defn get-vendor [context args value]
|
(defn get-vendor [context args value]
|
||||||
(->graphql
|
(->graphql
|
||||||
@@ -495,13 +474,6 @@
|
|||||||
:get-payment-page gq-checks/get-payment-page
|
:get-payment-page gq-checks/get-payment-page
|
||||||
:get-transaction-page gq-transactions/get-transaction-page
|
:get-transaction-page gq-transactions/get-transaction-page
|
||||||
:get-reminder-page get-reminder-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-company get-company
|
||||||
:get-user get-user
|
:get-user get-user
|
||||||
:get-user-companies get-user-companies
|
:get-user-companies get-user-companies
|
||||||
|
|||||||
@@ -32,7 +32,7 @@
|
|||||||
[:name
|
[:name
|
||||||
:id
|
:id
|
||||||
:role
|
:role
|
||||||
[:companies [:id :name]]]]]}
|
[:clients [:id :name]]]]]}
|
||||||
:on-success [::received]}}))
|
:on-success [::received]}}))
|
||||||
|
|
||||||
(re-frame/reg-event-db
|
(re-frame/reg-event-db
|
||||||
@@ -79,7 +79,7 @@
|
|||||||
|
|
||||||
:venia/queries [{:query/data [:edit-user
|
:venia/queries [{:query/data [:edit-user
|
||||||
{:edit-user (update (get-in db [::editing :user]) :companies #(map :id %))}
|
{: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]}}))
|
:on-success [::saved]}}))
|
||||||
|
|
||||||
(re-frame/reg-event-fx
|
(re-frame/reg-event-fx
|
||||||
@@ -102,13 +102,13 @@
|
|||||||
[:th "User"]
|
[:th "User"]
|
||||||
[:th "Role"]
|
[:th "Role"]
|
||||||
[:th "Companies"]]]
|
[: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 )}
|
^{:key (str name "-" id )}
|
||||||
[:tr {:on-click (fn [] (re-frame/dispatch [::edit id]))
|
[:tr {:on-click (fn [] (re-frame/dispatch [::edit id]))
|
||||||
:style {"cursor" "pointer"}}
|
:style {"cursor" "pointer"}}
|
||||||
[:td name]
|
[:td name]
|
||||||
[:td role]
|
[:td role]
|
||||||
[:td (str/join ", " (map :name companies))]])]]))
|
[:td (str/join ", " (map :name clients))]])]]))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -152,7 +152,7 @@
|
|||||||
|
|
||||||
(when (= "user" (:role (:user editing)))
|
(when (= "user" (:role (:user editing)))
|
||||||
[horizontal-field
|
[horizontal-field
|
||||||
[:label.label "Companies"]
|
[:label.label "Clients"]
|
||||||
[:div.control
|
[:div.control
|
||||||
|
|
||||||
[:div.field.has-addons
|
[:div.field.has-addons
|
||||||
@@ -164,7 +164,7 @@
|
|||||||
:event ::change
|
:event ::change
|
||||||
:subscription editing}
|
:subscription editing}
|
||||||
[:option]
|
[: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])
|
(for [{:keys [id name]} @(re-frame/subscribe [::subs/companies])
|
||||||
:when (not (used-companies id))]
|
:when (not (used-companies id))]
|
||||||
^{:key id} [:option {:value id} name]))]]]]
|
^{:key id} [:option {:value id} name]))]]]]
|
||||||
@@ -172,7 +172,7 @@
|
|||||||
[:button.button.is-primary {:on-click (dispatch-event [::add-company])} "Add"]]]
|
[:button.button.is-primary {:on-click (dispatch-event [::add-company])} "Add"]]]
|
||||||
|
|
||||||
[:ul
|
[: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 ]]])]]])]])])
|
^{: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 {}]) }))
|
{:component-will-mount #(re-frame/dispatch-sync [::users-mounted {}]) }))
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user