Fix bulk code vendor pre-population for single vs multi-client contexts

- vendor-default-account now uses raw vendor default account (not client-specific override)
- Account name is clientized via d-accounts/clientize only for single-client contexts
- Added single-client-id helper that returns client ID only when user has exactly one client
- Added multi-client e2e test verifying no pre-population across multiple clients
- Updated test server to support multi-client mode switching via /test-set-client-mode
- Test server now seeds a second client for multi-client scenarios
This commit is contained in:
2026-05-23 11:21:22 -07:00
parent ba87805d4c
commit 03bfca35cb
3 changed files with 113 additions and 18 deletions

View File

@@ -2,6 +2,7 @@
(:require
[auto-ap.datomic
:refer [audit-transact-batch conn pull-attr pull-many]]
[auto-ap.datomic.accounts :as d-accounts]
[auto-ap.logging :as alog]
[auto-ap.permissions :refer [wrap-must]]
[auto-ap.query-params :refer [wrap-copy-qp-pqp]]
@@ -324,14 +325,16 @@
[:p (str "Successfully coded " (count all-ids) " transactions.")])
:headers {"hx-trigger" "refreshTable"})))))
(defn- get-client-id [request]
(-> request :clients first :db/id))
(defn- vendor-default-account [vendor-id client-id]
"Returns the vendor's standard default account. For single-client contexts,
the account name is clientized (tailored to the customer). For multi-client
contexts, the raw account name is used."
(when vendor-id
(let [vendor (edit/get-vendor vendor-id)
clientized (edit/clientize-vendor vendor client-id)]
(:vendor/default-account clientized))))
account (:vendor/default-account vendor)]
(if client-id
(d-accounts/clientize account client-id)
account))))
(defn- build-default-account-row [account]
{:db/id (str (java.util.UUID/randomUUID))
@@ -360,11 +363,17 @@
:index (count (fc/field-value))}
"New account")))))])))
(defn- single-client-id [request]
"Returns the client ID if the user has access to exactly one client, nil otherwise."
(when (= 1 (count (:clients request)))
(-> request :clients first :db/id)))
(defn vendor-changed-handler [request]
(let [snapshot (:snapshot (:multi-form-state request))
step-params (:step-params (:multi-form-state request))
client-id (get-client-id request)
client-id (single-client-id request)
vendor-id (or (:vendor step-params) (:vendor snapshot))
_ (println ::VENDOR-CHANGED :client-id client-id :vendor-id vendor-id :accounts-empty (empty? (:accounts step-params)))
updated-step-params (if (and (empty? (:accounts step-params))
vendor-id
client-id)