Skip to content
Snippets Groups Projects
Unverified Commit 26369824 authored by Cal Herries's avatar Cal Herries Committed by GitHub
Browse files

Assure that common_name is assoc'd on a User correctly (#34056)

parent c47e63c3
No related branches found
No related tags found
No related merge requests found
......@@ -170,13 +170,17 @@
email (update :email u/lower-case-en))))
(defn add-common-name
"Add a `:common_name` key to `user` by combining their first and last names, or using their email if names are `nil`."
"Conditionally add a `:common_name` key to `user` by combining their first and last names, or using their email if names are `nil`.
The key will only be added if `user` contains the required keys to derive it correctly."
[{:keys [first_name last_name email], :as user}]
(let [common-name (if (or first_name last_name)
(str/trim (str first_name " " last_name))
email)]
(cond-> user
common-name (assoc :common_name common-name))))
(and (contains? user :first_name)
(contains? user :last_name)
common-name)
(assoc :common_name common-name))))
(t2/define-after-select :model/User
[user]
......
......@@ -534,3 +534,22 @@
(t2.with-temp/with-temp [User {user-id :id} {}]
(mw.session/with-current-user user-id
(is (= "v0.47.1" (setting/get :last-acknowledged-version))))))))
(deftest common-name-test
(testing "common_name should be present depending on what is selected"
(mt/with-temp [User user {:first_name "John"
:last_name "Smith"
:email "john.smith@gmail.com"}]
(is (= "John Smith"
(:common_name (t2/select-one [User :first_name :last_name] (:id user)))))
(is (= "John Smith"
(:common_name (t2/select-one User (:id user)))))
(is (nil? (:common_name (t2/select-one [User :first_name :email] (:id user)))))
(is (nil? (:common_name (t2/select-one [User :email] (:id user)))))))
(testing "common_name should be present if first_name and last_name are selected but nil and email is also selected"
(mt/with-temp [User user {:first_name nil
:last_name nil
:email "john.smith@gmail.com"}]
(is (= "john.smith@gmail.com"
(:common_name (t2/select-one [User :email :first_name :last_name] (:id user)))))
(is (nil? (:common_name (t2/select-one [User :first_name :last_name] (:id user))))))))
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment