Line data Source code
1 4 : (ns schemas.domain.user
2 : (:require
3 : [malli.core :as m]
4 : [malli.error :as me]))
5 :
6 :
7 4 : (def CountryCodeSchema
8 32 : [:re {:description "ISO 3166-1 alpha-2 country code, uppercase"
9 : :example "UA"}
10 : "^[A-Z]{2}$"])
11 :
12 :
13 4 : (def UserSettingsUiSchema
14 8 : [:map
15 8 : [:theme
16 28 : [:string {:description "UI theme name"
17 : :example "default"}]]
18 28 : [:mode [:enum "light" "dark" "auto"]]])
19 :
20 :
21 4 : (def UserLanguagePromptSchema
22 20 : [:map {:description "One-time UI language suggestion record"}
23 28 : [:status [:enum "shown" "accepted" "dismissed"]]
24 20 : [:suggested {:optional true}
25 28 : [:string {:description "Suggested language code"
26 : :example "uk"}]]
27 20 : [:at {:optional true}
28 28 : [:string {:description "ISO-8601 time of the last status change"
29 : :example "2026-10-30T12:00:00.000Z"}]]])
30 :
31 :
32 4 : (def NotificationSettingsSchema
33 28 : [:map {:closed true
34 : :description "Per-user notification preferences; missing keys use defaults"}
35 24 : [:telegram {:optional true}
36 : :boolean]
37 32 : [:in-app {:optional true
38 : :description "Unread badge on the in-app bell; the feed page itself always works"}
39 : :boolean]
40 20 : [:results {:optional true}
41 20 : [:enum "show" "spoiler" "off"]]
42 24 : [:followed-pairings {:optional true}
43 : :boolean]
44 24 : [:followed-results {:optional true}
45 : :boolean]
46 20 : [:video {:optional true}
47 20 : [:enum "new" "edit" "off"]]])
48 :
49 :
50 4 : (def UserSettingsSchema
51 8 : [:map
52 12 : [:ui UserSettingsUiSchema]
53 20 : [:language {:optional true}
54 28 : [:string {:description "Language code"
55 : :example "en"}]]
56 20 : [:language-prompt {:optional true}
57 4 : UserLanguagePromptSchema]
58 24 : [:telegram-match-notifications-enabled {:optional true}
59 : :boolean]
60 20 : [:notifications {:optional true}
61 4 : NotificationSettingsSchema]
62 20 : [:timezone {:optional true}
63 36 : [:string {:max 64
64 : :description "IANA time zone used to display match times"
65 : :example "Europe/Kyiv"}]]])
66 :
67 :
68 16 : (def supported-languages ["en" "uk"])
69 :
70 :
71 32 : (def localized-languages (vec (remove #{"en"} supported-languages)))
72 :
73 :
74 8 : (def LatinNameRegex #"^[\p{IsLatin}\p{M}][\p{IsLatin}\p{M}\s'.-]*$")
75 :
76 :
77 : ;; A string, not a Pattern: it ends up in the generated OpenAPI JSON.
78 8 : (def LocalizedNameRegex "^[\\p{L}\\p{M}][\\p{L}\\p{M}\\s'.-]*$")
79 :
80 :
81 4 : (def NameLocksSchema
82 24 : [:sequential {:description
83 : "Languages whose name only an admin can change; \"en\" is the Latin name"
84 12 : :example ["en" "uk"]}
85 20 : (into [:enum] supported-languages)])
86 :
87 :
88 4 : (def NameLocalizedSchema
89 8 : [:map-of
90 20 : (into [:enum] localized-languages)
91 8 : [:and
92 28 : [:string {:min 1
93 : :max 120}]
94 12 : [:re LocalizedNameRegex]]])
95 :
96 :
97 4 : (def UserRecordSchema
98 28 : [:map {:swagger/title "UserRecord"
99 : :swagger/description "Complete user account record from database"}
100 20 : [:uuid [:ref :types/UuidString]]
101 8 : [:public-id
102 36 : [:string {:min 1
103 : :description "Public-facing nanoid identifier"
104 : :example "V1StGXR8_Z5j"}]]
105 8 : [:name
106 44 : [:string {:min 1
107 : :max 120
108 : :description "User's display name"
109 : :example "John Doe"}]]
110 20 : [:name-localized {:optional true}
111 4 : NameLocalizedSchema]
112 20 : [:name-confirmed-at {:optional true}
113 12 : [:maybe :any]]
114 20 : [:name-locks {:optional true}
115 4 : NameLocksSchema]
116 20 : [:username {:optional true}
117 8 : [:maybe
118 44 : [:string {:min 4
119 : :max 100
120 : :description "Unique username"
121 : :example "johndoe"}]]]
122 28 : [:photo-url {:description "URL to user's profile photo"
123 : :example "https://example.com/photo.jpg"}
124 12 : [:maybe :string]]
125 20 : [:country-code {:optional true}
126 12 : [:maybe CountryCodeSchema]]
127 28 : [:telegram-id {:description "Telegram user ID if linked"
128 : :example 123456789}
129 12 : [:maybe :int]]
130 20 : [:telegram-bot-access-status {:optional true}
131 28 : [:enum "unknown" "requested" "assumed" "confirmed" "denied"]]
132 20 : [:telegram-bot-access-updated-at {:optional true}
133 12 : [:maybe :any]]
134 20 : [:is-fake {:optional true}
135 20 : [:boolean {:description "Whether this account is fake/test data"}]]
136 24 : [:created-datetime {:optional true}
137 : :any]
138 24 : [:updated-datetime {:optional true}
139 : :any]
140 20 : [:roles {:optional true}
141 28 : [:sequential [:enum "root" "admin" "player"]]]
142 12 : [:settings UserSettingsSchema]])
143 :
144 :
145 4 : (defrecord
146 : UserRecord
147 : [uuid
148 : public-id
149 : name
150 : name-localized
151 : name-confirmed-at
152 : name-locks
153 : username
154 : photo-url
155 : country-code
156 : telegram-id
157 : telegram-bot-access-status
158 : telegram-bot-access-updated-at
159 : is-fake
160 : created-datetime
161 : updated-datetime
162 : roles
163 : settings])
164 :
165 :
166 4 : (defn make-user-record
167 : "Creates a User record with validation.
168 : Note: This validation uses the default registry. Ensure schemas.registry/init-registry!
169 : has been called before using this function."
170 : {:malli/schema [:=> [:cat UserRecordSchema] UserRecordSchema]}
171 : [{:keys [uuid
172 : public-id
173 : name
174 : name-localized
175 : name-confirmed-at
176 : name-locks
177 : username
178 : photo-url
179 : country-code
180 : telegram-id
181 : telegram-bot-access-status
182 : telegram-bot-access-updated-at
183 : is-fake
184 : created-datetime
185 : updated-datetime
186 : roles
187 : settings]
188 : :as user-data}]
189 20 : (if (m/validate UserRecordSchema user-data)
190 8 : (map->UserRecord
191 76 : {:uuid uuid
192 4 : :public-id public-id
193 4 : :name name
194 20 : :name-localized (or name-localized {})
195 4 : :name-confirmed-at name-confirmed-at
196 29 : :name-locks (vec (or name-locks []))
197 4 : :username username
198 4 : :photo-url photo-url
199 4 : :country-code country-code
200 4 : :telegram-id telegram-id
201 21 : :telegram-bot-access-status (or telegram-bot-access-status "unknown")
202 4 : :telegram-bot-access-updated-at telegram-bot-access-updated-at
203 8 : :is-fake (boolean is-fake)
204 4 : :created-datetime created-datetime
205 4 : :updated-datetime updated-datetime
206 22 : :roles (or roles ["player"])
207 4 : :settings settings})
208 1 : (throw
209 3 : (ex-info
210 : "Invalid user data"
211 6 : {:type :internal-validation-error
212 1 : :schema UserRecordSchema
213 1 : :data user-data
214 6 : :humanized-errors (me/humanize (m/explain UserRecordSchema user-data))}))))
|