Makes multiple client selection somewhat possible via graphql.

This commit is contained in:
2023-08-31 13:05:06 -07:00
parent 9c472dfc8e
commit 1d82ec29e0
29 changed files with 274 additions and 139 deletions

View File

@@ -1,10 +1,47 @@
(ns auto-ap.routes.graphql
(:require [auto-ap.routes.utils :refer [wrap-secure]]
[auto-ap.graphql :as ql]
[auto-ap.logging :refer [warn-event]]
[buddy.auth :refer [throw-unauthorized]]
[clojure.edn :as edn]
[clojure.tools.logging :as log]))
(:require
[auto-ap.datomic :refer [conn pull-many]]
[auto-ap.datomic.clients :as d-clients]
[auto-ap.graphql :as ql]
[auto-ap.graphql.utils :refer [limited-clients]]
[auto-ap.logging :refer [warn-event]]
[auto-ap.routes.utils :refer [wrap-secure]]
[buddy.auth :refer [throw-unauthorized]]
[clojure.edn :as edn]
[clojure.set :as set]
[clojure.tools.logging :as log]
[datomic.api :as dc]))
(defn headers->clients [identity headers]
(let [x-clients (clojure.edn/read-string (get headers "x-clients"))
ideal-ids (set (cond
(or (= :all x-clients)
(nil? x-clients))
(->> (dc/q '[:find ?c
:where [?c :client/code]]
(dc/db conn))
(map first))
(= :mine x-clients)
(map :db/id (:user/clients identity))
(seq x-clients)
x-clients))
_ (println "X-CLIENSTT" x-clients)
_ (println "IDEAL" ideal-ids)
_ (println "LIMITS" (limited-clients identity))
limited-clients (some->> (limited-clients identity)
(map :db/id )
set)
client-ids (if (set? limited-clients)
(set/intersection ideal-ids
limited-clients)
ideal-ids)]
(pull-many (dc/db conn)
d-clients/full-read
client-ids)))
(defn handle-graphql [{:keys [request-method query-params] :as r}]
(when (= "none" (:user/role (:identity r)))
(throw-unauthorized))
@@ -13,8 +50,11 @@
(let [variables (some-> (query-params "variables")
edn/read-string)
body (some-> r :body slurp)]
{:status 200
:body (pr-str (ql/query (:identity r) (if (= request-method :get) (query-params "query") body) variables ))
:body (pr-str (ql/query (:identity r) (if (= request-method :get) (query-params "query") body) (assoc variables
:clients
(headers->clients (:identity r) (:headers r))) ))
:headers {"Content-Type" "application/edn"}})
(catch Throwable e
(log/info "here we are " (.getCause e))