editing users uses datomic.
This commit is contained in:
@@ -517,10 +517,10 @@
|
||||
:db/valueType :db.type/string
|
||||
:db/cardinality :db.cardinality/one
|
||||
:db/doc "name of the user"}
|
||||
{:db/ident :user/companies
|
||||
{:db/ident :user/clients
|
||||
:db/valueType :db.type/ref
|
||||
:db/cardinality :db.cardinality/many
|
||||
:db/doc "The companies this user can view"}
|
||||
:db/doc "The clients this user can view"}
|
||||
|
||||
;;enums
|
||||
{:db/ident :user-role/admin}
|
||||
@@ -706,7 +706,7 @@
|
||||
:role (keyword "user-role" role)
|
||||
:provider-id provider-id
|
||||
:provider provider
|
||||
:companies (map (fn [c] [:client/original-id c]) companies)})))
|
||||
:clients (map (fn [c] [:client/original-id c]) companies)})))
|
||||
(d/transact (d/connect uri))))
|
||||
|
||||
(defn query-vendors []
|
||||
|
||||
@@ -11,6 +11,21 @@
|
||||
(update-in [:query :where] conj where))]
|
||||
(reduce #(update-in %1 [:query :where] conj %2) query rest)))
|
||||
|
||||
(defn get-by-id [id]
|
||||
(let [query (-> {:query {:find ['(pull ?e [*
|
||||
{:user/clients [*]}
|
||||
{:user/role [:db/ident]}])]
|
||||
:in ['$]
|
||||
:where []}
|
||||
:args [(d/db (d/connect uri))]}
|
||||
(add-arg '?e (cond-> id (string? id) Long/parseLong) ['?e])
|
||||
(doto println))]
|
||||
|
||||
(->> (d/query query)
|
||||
(map first)
|
||||
(map #(update % :user/role :db/ident))
|
||||
first)))
|
||||
|
||||
(defn raw-graphql [args]
|
||||
(let [query (cond-> {:query {:find ['(pull ?e [*
|
||||
{:user/clients [*]}
|
||||
|
||||
@@ -6,26 +6,18 @@
|
||||
[com.walmartlabs.lacinia.executor :as executor]
|
||||
[com.walmartlabs.lacinia.resolve :as resolve]
|
||||
[buddy.auth :refer [throw-unauthorized]]
|
||||
[auto-ap.db.invoices :as invoices]
|
||||
[auto-ap.utils :refer [by]]
|
||||
[auto-ap.graphql.utils :refer [assert-admin can-see-company? assert-can-see-company]]
|
||||
[auto-ap.db.vendors :as vendors]
|
||||
[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]
|
||||
[auto-ap.db.checks :as checks]
|
||||
[auto-ap.graphql.users :as gq-users]
|
||||
[auto-ap.graphql.checks :as gq-checks]
|
||||
[auto-ap.graphql.expense-accounts :as expense-accounts]
|
||||
[auto-ap.graphql.invoices :as gq-invoices]
|
||||
[auto-ap.graphql.transactions :as gq-transactions]
|
||||
[auto-ap.db.reminders :as reminders]
|
||||
[auto-ap.db.invoices-checks :as invoices-checks]
|
||||
[auto-ap.db.utils :as utils]
|
||||
[clojure.walk :as walk]
|
||||
[clojure.string :as str])
|
||||
(:import
|
||||
@@ -131,8 +123,7 @@
|
||||
:status {:type 'String}
|
||||
:client {:type :client}
|
||||
:payment {:type :payment}
|
||||
:bank_account {:type :bank_account
|
||||
:resolve :bank-account-for-check}
|
||||
:bank_account {:type :bank_account}
|
||||
:date {:type 'String}
|
||||
:post_date {:type 'String}}}
|
||||
:invoice_payment
|
||||
@@ -144,10 +135,10 @@
|
||||
:invoice {:type :invoice}}}
|
||||
|
||||
:user
|
||||
{:fields {:id {:type 'Int}
|
||||
{:fields {:id {:type 'String}
|
||||
:name {:type 'String}
|
||||
:role {:type 'String}
|
||||
:clients {:type '(list :company)}}}
|
||||
:clients {:type '(list :client)}}}
|
||||
|
||||
:expense_account {:fields {:id {:type 'Int}
|
||||
:location {:type 'String}
|
||||
@@ -245,12 +236,7 @@
|
||||
:asc {:type 'Boolean}}
|
||||
|
||||
:resolve :get-payment-page}
|
||||
:reminder_page {:type '(list :reminder_page)
|
||||
:args {:start {:type 'Int}
|
||||
:sort_by {:type 'String}
|
||||
:asc {:type 'Boolean}}
|
||||
|
||||
:resolve :get-reminder-page}
|
||||
|
||||
:company {:type '(list :company)
|
||||
:resolve :get-company}
|
||||
:client {:type '(list :company)
|
||||
@@ -266,10 +252,10 @@
|
||||
:amount {:type 'Float}}}
|
||||
|
||||
:edit_user
|
||||
{:fields {:id {:type 'Int}
|
||||
{:fields {:id {:type 'String}
|
||||
:name {:type 'String}
|
||||
:role {:type 'String}
|
||||
:companies {:type '(list Int)}}}
|
||||
:clients {:type '(list String)}}}
|
||||
|
||||
:edit_expense_account
|
||||
{:fields {:id {:type 'String}
|
||||
@@ -399,22 +385,6 @@
|
||||
(d-checks/get-graphql (assoc (<-graphql args)
|
||||
:limit Integer/MAX_VALUE))))
|
||||
|
||||
(defn get-reminder-page [context args value]
|
||||
(assert-admin (:id context))
|
||||
(let [extra-context
|
||||
(cond-> {}
|
||||
(executor/selects-field? context :reminder/vendor) (assoc :vendor-cache (by :id (vendors/get-all))))
|
||||
|
||||
reminders (map
|
||||
->graphql
|
||||
(reminders/get-graphql (<-graphql args)))
|
||||
reminder-count (reminders/count-graphql (<-graphql args))]
|
||||
(resolve/with-context
|
||||
[{:reminders reminders
|
||||
:total reminder-count
|
||||
:count (count reminders)
|
||||
:start (:start args 0)
|
||||
:end (+ (:start args 0) (count reminders))}] extra-context)))
|
||||
|
||||
|
||||
|
||||
@@ -425,18 +395,7 @@
|
||||
|
||||
|
||||
|
||||
(defn bank-account-for-check [context args value]
|
||||
(->graphql
|
||||
(let [company (if-let [company-cache (:company-cache context)]
|
||||
(company-cache (:company_id value))
|
||||
(companies/get-by-id (:company_id value)))]
|
||||
(first (filter #(= (:id %) (:bank_account_id value)) (:bank-accounts company))) )))
|
||||
|
||||
(defn get-user-companies [context args value]
|
||||
(->graphql
|
||||
(if-let [company-cache (:company-cache context)]
|
||||
(map company-cache (:companies value))
|
||||
(map companies/get-by-id (:companies value)))))
|
||||
|
||||
|
||||
|
||||
@@ -445,18 +404,12 @@
|
||||
(filter #(can-see-company? (:id context) %)
|
||||
(d-clients/get-all))))
|
||||
|
||||
(defn join-companies [users]
|
||||
(let [companies (by :id (companies/get-all))]
|
||||
(map
|
||||
(fn [u]
|
||||
(update u :companies #(map companies %)))
|
||||
users)))
|
||||
|
||||
(defn get-user [context args value]
|
||||
(assert-admin (:id context))
|
||||
|
||||
(let [users (d-users/get-graphql args)]
|
||||
(->graphql users)))
|
||||
(doto (let [users (d-users/get-graphql args)]
|
||||
(->graphql users))
|
||||
println))
|
||||
|
||||
(defn get-vendor [context args value]
|
||||
(->graphql
|
||||
@@ -480,13 +433,11 @@
|
||||
(attach-resolvers {:get-invoice-page get-invoice-page
|
||||
:get-all-invoices get-all-invoices
|
||||
:get-all-payments get-all-payments
|
||||
:bank-account-for-check bank-account-for-check
|
||||
:get-payment-page gq-checks/get-payment-page
|
||||
:get-transaction-page gq-transactions/get-transaction-page
|
||||
:get-reminder-page get-reminder-page
|
||||
|
||||
:get-company get-company
|
||||
:get-user get-user
|
||||
:get-user-companies get-user-companies
|
||||
:mutation/add-handwritten-check gq-checks/add-handwritten-check
|
||||
:mutation/print-checks print-checks
|
||||
:mutation/edit-user gq-users/edit-user
|
||||
|
||||
@@ -2,11 +2,9 @@
|
||||
(:require [auto-ap.graphql.utils :refer [->graphql <-graphql assert-can-see-company]]
|
||||
[datomic.api :as d]
|
||||
[clojure.edn :as edn]
|
||||
|
||||
[com.walmartlabs.lacinia :refer [execute]]
|
||||
[com.walmartlabs.lacinia.executor :as executor]
|
||||
[com.walmartlabs.lacinia.resolve :as resolve]
|
||||
|
||||
[auto-ap.datomic.checks :as d-checks]
|
||||
[auto-ap.datomic.invoices :as d-invoices]
|
||||
[auto-ap.datomic.vendors :as d-vendors]
|
||||
|
||||
@@ -1,11 +1,8 @@
|
||||
(ns auto-ap.graphql.invoices
|
||||
(:require [auto-ap.graphql.utils :refer [->graphql assert-can-see-company]]
|
||||
|
||||
[auto-ap.db.vendors :as vendors]
|
||||
[auto-ap.datomic.vendors :as d-vendors]
|
||||
[auto-ap.datomic.invoices :as d-invoices]
|
||||
[auto-ap.db.companies :as companies]
|
||||
[auto-ap.db.invoices-expense-accounts :as invoices-expense-accounts]
|
||||
[auto-ap.expense-accounts :as expense-accounts]
|
||||
[auto-ap.time :refer [parse iso-date]]
|
||||
[datomic.api :as d]
|
||||
@@ -13,11 +10,6 @@
|
||||
[clj-time.coerce :as coerce]
|
||||
[clojure.set :as set]))
|
||||
|
||||
(defn -create-or-get-vendor [vendor-id vendor-name]
|
||||
(if vendor-id
|
||||
(vendors/get-by-id vendor-id)
|
||||
(vendors/insert {:name vendor-name :default-expense-account 0})))
|
||||
|
||||
(defn add-invoice [context {{:keys [total invoice_number location client_id vendor_id vendor_name date] :as in} :invoice} value]
|
||||
(when (seq (d-invoices/find-conflicting {:invoice/invoice-number invoice_number
|
||||
:invoice/vendor (Long/parseLong vendor_id)
|
||||
|
||||
@@ -5,23 +5,11 @@
|
||||
[com.walmartlabs.lacinia :refer [execute]]
|
||||
[com.walmartlabs.lacinia.executor :as executor]
|
||||
[com.walmartlabs.lacinia.resolve :as resolve]
|
||||
[auto-ap.db.transactions :as transactions]
|
||||
[auto-ap.db.vendors :as vendors]
|
||||
[auto-ap.db.checks :as checks]
|
||||
[auto-ap.utils :refer [by]]
|
||||
[auto-ap.db.companies :as companies]
|
||||
[auto-ap.time :refer [parse normal-date]]))
|
||||
|
||||
(defn get-check-for-transaction [context args value]
|
||||
(->graphql
|
||||
(checks/get-by-id (:check_id value))))
|
||||
|
||||
(defn get-company-for-transaction [context args value]
|
||||
(->graphql
|
||||
(if-let [company-cache (:company-cache context)]
|
||||
(company-cache (:company_id value))
|
||||
(companies/get-by-id (:company_id value)))))
|
||||
|
||||
(defn get-transaction-page [context args value]
|
||||
(let [args (assoc args :id (:id context))
|
||||
transactions (map
|
||||
|
||||
@@ -1,10 +1,30 @@
|
||||
(ns auto-ap.graphql.users
|
||||
(:require [auto-ap.db.users :as users]
|
||||
(:require [auto-ap.datomic.users :as d-users]
|
||||
[datomic.api :as d]
|
||||
[auto-ap.datomic :refer [uri]]
|
||||
[auto-ap.graphql.utils :refer [->graphql assert-admin]]))
|
||||
|
||||
(defn edit-user [context args value]
|
||||
(assert-admin (:id context))
|
||||
(users/update! (:edit_user args))
|
||||
(->graphql
|
||||
(users/get-by-id (:id (:edit_user args)))))
|
||||
(def role->datomic-role {":none" :user-role/none
|
||||
":admin" :user-role/admin
|
||||
":user" :user-role/user})
|
||||
|
||||
(defn edit-user [context {:keys [edit_user] :as args} value]
|
||||
(println args edit_user)
|
||||
(assert-admin (:id context))
|
||||
(let [user (d-users/get-by-id (:id edit_user))
|
||||
new-clients (set (map #(Long/parseLong %) (:clients edit_user)))
|
||||
clients-to-remove (->> (:user/clients user)
|
||||
(map :db/id)
|
||||
(filter #(not (new-clients %)) ))]
|
||||
|
||||
|
||||
@(d/transact (d/connect uri)
|
||||
|
||||
(doto (-> [{:db/id (:db/id user)
|
||||
:user/role (role->datomic-role (:role edit_user))
|
||||
:user/clients new-clients}]
|
||||
(into (map (fn [c] [:db/retract (:db/id user) :user/clients c]) clients-to-remove)))
|
||||
clojure.pprint/pprint))
|
||||
(->graphql
|
||||
(d-users/get-by-id (:id edit_user)))))
|
||||
|
||||
|
||||
@@ -58,13 +58,13 @@
|
||||
::add-company
|
||||
(fn [db [_ d]]
|
||||
(let [company (get @(re-frame/subscribe [::subs/companies-by-id])
|
||||
(js/parseInt (get-in db [::editing :adding-company])))]
|
||||
(update-in db [::editing :user :companies] conj company))))
|
||||
(get-in db [::editing :adding-company]))]
|
||||
(update-in db [::editing :user :clients] conj company))))
|
||||
|
||||
(re-frame/reg-event-db
|
||||
::remove-company
|
||||
(fn [db [_ d]]
|
||||
(update-in db [::editing :user :companies] #(filter (fn [c] (not= (:id c) d)) %))))
|
||||
(update-in db [::editing :user :clients] #(filter (fn [c] (not= (:id c) d)) %))))
|
||||
|
||||
(re-frame/reg-event-fx
|
||||
::save
|
||||
@@ -78,7 +78,7 @@
|
||||
:operation/name "EditUser"}
|
||||
|
||||
: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]) :clients #(map :id %))}
|
||||
[:id :name :role [:clients [:id :name]]]]}]}
|
||||
:on-success [::saved]}}))
|
||||
|
||||
@@ -101,7 +101,7 @@
|
||||
[:tr
|
||||
[:th "User"]
|
||||
[:th "Role"]
|
||||
[:th "Companies"]]]
|
||||
[:th "Clients"]]]
|
||||
[:tbody (for [{:keys [id name role clients] :as c} @users]
|
||||
^{:key (str name "-" id )}
|
||||
[:tr {:on-click (fn [] (re-frame/dispatch [::edit id]))
|
||||
@@ -145,12 +145,12 @@
|
||||
:spec ::entity/name
|
||||
:event ::change
|
||||
:subscription editing}
|
||||
[:option {:value "none"} "None"]
|
||||
[:option {:value "user"} "User"]
|
||||
[:option {:value "admin"} "Admin"]]]]]]
|
||||
[:option {:value ":none"} "None"]
|
||||
[:option {:value ":user"} "User"]
|
||||
[:option {:value ":admin"} "Admin"]]]]]]
|
||||
|
||||
|
||||
(when (= "user" (:role (:user editing)))
|
||||
(when (= ":user" (:role (:user editing)))
|
||||
[horizontal-field
|
||||
[:label.label "Clients"]
|
||||
[:div.control
|
||||
|
||||
Reference in New Issue
Block a user