Line data Source code
1 4 : (ns matchscore.domain.user.entity
2 : (:require
3 : [schemas.domain.user :as user-schemas]))
4 :
5 :
6 20 : (def updatable-fields [:name :username :photo-url])
7 :
8 :
9 24 : (def optional-updatable-fields [:settings :name-localized :name-confirmed-at :country-code])
10 :
11 :
12 4 : (defn make-user
13 : "Creates a validated User map. Delegates to existing schema validation."
14 : [data]
15 : ;; Cached JSON (Redis) comes back keywordized; language keys are strings.
16 8 : (user-schemas/make-user-record
17 24 : (cond-> data
18 20 : (map? (:name-localized data))
19 34 : (update :name-localized update-keys #(if (keyword? %) (name %) %)))))
20 :
21 :
22 4 : (defn has-changes?
23 : "Returns true if any updatable field differs between existing and incoming data.
24 : Optional fields (like settings) are only compared when present in updates."
25 : [existing updates]
26 15 : (or
27 30 : (not= (select-keys existing updatable-fields) (select-keys updates updatable-fields))
28 3 : (some
29 3 : (fn [k]
30 48 : (and (contains? updates k) (not= (get existing k) (get updates k))))
31 3 : optional-updatable-fields)))
32 :
33 :
34 4 : (defn username-changed?
35 : "Returns true if username differs between existing and incoming data.
36 : Returns false if incoming username is nil (no change intended)."
37 : [existing updates]
38 12 : (let [new-username (:username updates)]
39 37 : (and (some? new-username) (not= (:username existing) new-username))))
40 :
41 :
42 4 : (defn merge-updates
43 : "Merges updatable fields from updates into existing account.
44 : Optional fields are only merged when present in updates."
45 : [existing updates]
46 24 : (let [base (merge existing (select-keys updates updatable-fields))
47 33 : present (select-keys updates (filter #(contains? updates %) optional-updatable-fields))]
48 12 : (merge base present)))
|