makes client selection not contanimate other tabs

This commit is contained in:
Bryce
2024-04-09 23:27:35 -07:00
parent 10a60dcee2
commit ff2bf4c2b3
20 changed files with 228 additions and 183 deletions

View File

@@ -2,7 +2,6 @@
(:require [amazonica.core :refer [defcredential]]
[auto-ap.client-routes :as client-routes]
[auto-ap.datomic :refer [conn pull-many]]
[auto-ap.datomic.clients :as d-clients]
[auto-ap.graphql.utils :refer [assert-can-see-client
limited-clients]]
[auto-ap.logging :as alog]
@@ -16,6 +15,7 @@
[auto-ap.routes.yodlee2 :as yodlee2]
[auto-ap.ssr-routes :as ssr-routes]
[auto-ap.ssr.core :as ssr]
[auto-ap.ssr.utils :refer [entity-id main-transformer]]
[bidi.bidi :as bidi]
[bidi.ring :refer [->ResourcesMaybe make-handler]]
[buddy.auth.backends.session :refer [session-backend]]
@@ -26,13 +26,14 @@
[cheshire.core :as cheshire]
[clj-time.coerce :as coerce]
[clj-time.core :as time]
[clojure.edn :as edn]
[clojure.data.json :as json]
[clojure.set :as set]
[clojure.string :as str]
[com.brunobonacci.mulog :as mu]
[config.core :refer [env]]
[datomic.api :as dc]
[hiccup2.core :as hiccup]
[malli.core :as mc]
[ring.middleware.edn :refer [wrap-edn-params]]
[ring.middleware.multipart-params :as mp]
[ring.middleware.params :refer [wrap-params]]
@@ -110,10 +111,10 @@
uri
:request-method request-method))
matched-hx-current-url-route (some->> (get headers "hx-current-url")
url/url
:path
(bidi/match-route ssr-routes/only-routes)
:handler)]
url/url
:path
(bidi/match-route ssr-routes/only-routes)
:handler)]
(handler (assoc request
:matched-route
matched-route
@@ -135,6 +136,7 @@
(:uri request)
:request-method (:request-method request)))
:client-selection (:client-selection request)
:source "request"
:query (:uri request)
:request-method (:request-method request)
@@ -185,10 +187,17 @@
request (assoc request :hx-query-params query-params)]
(handler request))))
(def client-selection-schema
(mc/schema
[:orn
[:global [:enum :all :mine]]
[:group-name [:map [:group :string]]]
[:specific [:map [:selected [:vector entity-id]]]]]))
(defn wrap-hydrate-clients
[handler]
(fn [request]
(let [x-clients (-> request :session :client-selection)
(let [x-clients (-> request :client-selection)
identity (or (-> request :identity)
(-> request :session :identity))
ideal-ids (set (cond
@@ -202,28 +211,21 @@
(= :mine x-clients)
(map :db/id (:user/clients identity))
(= :group (first x-clients))
(:group x-clients)
(->>
(dc/q '[:find ?c
:in $ ?g
:where [?c :client/groups ?g]]
(dc/db conn)
(str/upper-case (or (second x-clients) "INVALID")))
(str/upper-case (or (:group x-clients) "INVALID")))
(map first)
set)
(seq x-clients)
(seq (:selected x-clients))
(->> x-clients
(map (fn [c]
(if (string? c)
(try
(Long/parseLong c)
(catch Exception e
nil))
c)))
:selected
(filter #(not (nil? %)))
set)))
limited-clients (some->> (limited-clients identity)
(map :db/id)
set)
@@ -236,12 +238,11 @@
(pull-many (dc/db conn)
'[:db/id :client/name :client/code :client/locations
:client/matches :client/feature-flags
{:client/bank-accounts [:db/id
{:bank-account/type [:db/ident]}
{:client/bank-accounts [:db/id
{:bank-account/type [:db/ident]}
:bank-account/number
:bank-account/name
:bank-account/code]}]))]
(mu/with-context {:clients (take 10 (map :client/code clients))}
(handler (assoc request
:clients clients
@@ -251,33 +252,22 @@
(defn wrap-store-client-in-session
[handler]
(fn [{:keys [headers identity] :as request}]
(let [x-clients (edn/read-string (get headers "x-clients"))
x-clients (try (if-let [client-id (and x-clients
(sequential? x-clients)
(first x-clients)
(not= :group (first x-clients))
(first x-clients))]
(do
(assert-can-see-client identity (cond-> client-id
(string? client-id) (Long/parseLong)))
[(if (string? client-id)
(Long/parseLong client-id)
client-id)])
x-clients)
(catch Exception e
(alog/warn ::cant-access :error e
:identity identity
:x-clients (pr-str x-clients))
:all))
new-request (if x-clients
(assoc-in request [:session :client-selection] x-clients)
request)]
(let [client-selection (try (mc/decode client-selection-schema (some-> (get headers "x-clients") not-empty json/read-str) main-transformer)
(catch Exception e
(alog/warn ::cant-access :error e
:identity identity
:x-clients (pr-str (get headers "x-clients")))
nil))
new-request (if client-selection
(assoc-in request [:client-selection] client-selection)
(assoc-in request [:client-selection] (get-in request [:session :client-selection] :all)))]
(cond-> (handler new-request)
x-clients (update :session
(fn [new-session]
(-> (:session request)
(into new-session)
(assoc :client-selection x-clients))))))))
client-selection (update :session
(fn [new-session]
(-> (:session request)
(into new-session)
(assoc :client-selection client-selection))))))))
(defn wrap-gunzip-jwt
[handler]
@@ -317,9 +307,9 @@
(-> route-handler
(wrap-hx-current-url-params)
(wrap-guess-route)
(wrap-logging)
(wrap-hydrate-clients)
(wrap-store-client-in-session)
(wrap-logging)
(wrap-gunzip-jwt)
(wrap-authorization auth-backend)
(wrap-authentication auth-backend