Line data Source code
1 4 : (ns schemas.domain.templates
2 : (:require
3 : [malli.core :as m]
4 : [malli.error :as me]
5 : [schemas.api.common :as api-common]
6 : [schemas.domain.club :as club-schemas]))
7 :
8 :
9 4 : (def PaymentReminderSchema
10 8 : [:and
11 8 : [:map
12 12 : [:enabled :boolean]
13 20 : [:timing {:optional true}
14 16 : [:enum "before-start" "after-start"]]
15 20 : [:hours {:optional true}
16 28 : [:int {:min 0
17 : :max 168}]]
18 20 : [:payment-method-ids {:optional true}
19 20 : [:vector [:ref :types/UuidString]]]
20 20 : [:payment-purpose {:optional true}
21 28 : [:maybe [:string {:max 160}]]]]
22 20 : [:fn {:error/message
23 : "Enabled payment reminders require timing, hours, and at least one payment method"}
24 4 : (fn [{:keys [enabled timing hours payment-method-ids]}]
25 57 : (or (not enabled) (and timing (some? hours) (seq payment-method-ids))))]])
26 :
27 :
28 4 : (def TemplateRecordSchema
29 28 : [:map {:swagger/title "TemplateRecord"
30 : :swagger/description "Complete event template record from database"}
31 20 : [:uuid [:ref :types/UuidString]]
32 8 : [:public-id
33 36 : [:string {:min 1
34 : :description "Public-facing nanoid identifier"
35 : :example "V1StGXR8_Z5j"}]]
36 28 : [:sport-code (into [:enum] api-common/sport-options)]
37 28 : [:activity-type-code (into [:enum] api-common/activity-type-options)]
38 20 : [:name {:optional true}
39 28 : [:maybe [:string {:max 255}]]]
40 20 : [:description {:optional true}
41 12 : [:maybe :string]]
42 8 : [:state
43 28 : [:string {:min 1
44 : :max 50}]]
45 28 : [:visibility (into [:enum] api-common/visibility-options)]
46 20 : [:club-id {:optional true}
47 20 : [:maybe [:ref :types/UuidString]]]
48 12 : [:fee number?]
49 24 : [:fee-type [:enum "total" "per-person"]]
50 8 : [:currency
51 8 : (into
52 20 : [:enum {:error/message "Currency must be one of the supported options"}]
53 4 : api-common/currency-options)]
54 8 : [:timezone
55 8 : (into
56 20 : [:enum {:error/message "Timezone must be one of the supported options"}]
57 4 : api-common/timezone-options)]
58 20 : [:language {:optional true}
59 24 : [:maybe [:enum "en" "uk"]]]
60 20 : [:duration {:optional true}
61 28 : [:maybe [:int {:min 1}]]]
62 28 : [:score-entry-mode (into [:enum] api-common/score-entry-mode-options)]
63 28 : [:score-entry-default (into [:enum] api-common/score-entry-default-options)]
64 12 : [:created-datetime api-common/Datetime]
65 12 : [:updated-datetime api-common/Datetime]
66 20 : [:source [:ref :matchscore/source]]
67 20 : [:host-user-id {:optional true}
68 20 : [:maybe [:ref :types/UuidString]]]
69 20 : [:created-by {:optional true}
70 20 : [:maybe [:ref :types/UuidString]]]
71 24 : [:automatic-event-creation {:optional true}
72 : :boolean]
73 20 : [:automatic-event-creation-delay-minutes {:optional true}
74 28 : [:int {:min 1
75 : :max 525600}]]
76 20 : [:rrule {:optional true}
77 28 : [:maybe [:string {:max 512}]]]
78 20 : [:rrule-json {:optional true}
79 12 : [:maybe :map]]
80 20 : [:ruleset-id {:optional true}
81 20 : [:maybe [:ref :types/UuidString]]]
82 20 : [:clubs {:optional true}
83 20 : [:maybe [:sequential club-schemas/ClubRecordSchema]]]
84 20 : [:club-ids {:optional true}
85 28 : [:maybe [:sequential [:ref :types/UuidString]]]]
86 20 : [:settings {:optional true}
87 8 : [:maybe
88 8 : [:map
89 20 : [:payments-enabled {:optional true}
90 12 : [:maybe :boolean]]
91 20 : [:payment-reminder {:optional true}
92 4 : PaymentReminderSchema]]]]
93 20 : [:images-version {:optional true}
94 20 : [:int {:min 0}]]
95 20 : [:images {:optional true}
96 20 : [:maybe [:vector :map]]]
97 20 : [:cover-image {:optional true}
98 12 : [:maybe :map]]])
99 :
100 :
101 4 : (defrecord
102 : TemplateRecord
103 : [uuid
104 : public-id
105 : sport-code
106 : activity-type-code
107 : name
108 : description
109 : state
110 : visibility
111 : club-id
112 : fee
113 : fee-type
114 : currency
115 : timezone
116 : language
117 : duration
118 : score-entry-mode
119 : score-entry-default
120 : automatic-event-creation
121 : automatic-event-creation-delay-minutes
122 : created-datetime
123 : updated-datetime
124 : source
125 : host-user-id
126 : created-by
127 : rrule
128 : rrule-json
129 : ruleset-id
130 : clubs
131 : club-ids
132 : settings
133 : images-version
134 : images
135 : cover-image])
136 :
137 :
138 4 : (defn make-template-record
139 : "Creates an EventTemplate record with validation.
140 : Note: This validation uses the default registry. Ensure schemas.registry/init-registry!
141 : has been called before using this function."
142 : {:malli/schema [:=> [:cat :map] TemplateRecordSchema]}
143 : [{:keys [uuid
144 : public-id
145 : sport-code
146 : activity-type-code
147 : name
148 : description
149 : state
150 : visibility
151 : club-id
152 : fee
153 : fee-type
154 : currency
155 : timezone
156 : language
157 : duration
158 : score-entry-mode
159 : score-entry-default
160 : automatic-event-creation
161 : automatic-event-creation-delay-minutes
162 : created-datetime
163 : updated-datetime
164 : source
165 : host-user-id
166 : created-by
167 : rrule
168 : rrule-json
169 : ruleset-id
170 : clubs
171 : club-ids
172 : settings
173 : images-version
174 : images
175 : cover-image]
176 : :as template-data}]
177 0 : (if (m/validate TemplateRecordSchema template-data)
178 0 : (map->TemplateRecord
179 0 : {:uuid uuid
180 0 : :public-id public-id
181 0 : :sport-code sport-code
182 0 : :activity-type-code activity-type-code
183 0 : :name name
184 0 : :description description
185 0 : :state state
186 0 : :visibility visibility
187 0 : :club-id club-id
188 0 : :fee fee
189 0 : :fee-type fee-type
190 0 : :currency currency
191 0 : :timezone timezone
192 0 : :language (or language "en")
193 0 : :duration duration
194 0 : :score-entry-mode (or score-entry-mode "either")
195 0 : :score-entry-default (or score-entry-default "game")
196 0 : :automatic-event-creation (boolean automatic-event-creation)
197 0 : :automatic-event-creation-delay-minutes (or automatic-event-creation-delay-minutes 5)
198 0 : :created-datetime created-datetime
199 0 : :updated-datetime updated-datetime
200 0 : :source (or source "matchscore")
201 0 : :host-user-id host-user-id
202 0 : :created-by created-by
203 0 : :rrule rrule
204 0 : :rrule-json rrule-json
205 0 : :ruleset-id ruleset-id
206 0 : :clubs clubs
207 0 : :club-ids club-ids
208 0 : :settings settings
209 0 : :images-version (or images-version 0)
210 0 : :images images
211 0 : :cover-image cover-image})
212 0 : (throw
213 0 : (ex-info
214 : "Invalid template data"
215 0 : {:type :internal-validation-error
216 0 : :schema TemplateRecordSchema
217 0 : :data template-data
218 0 : :humanized-errors (me/humanize (m/explain TemplateRecordSchema template-data))}))))
219 :
220 :
221 4 : (defn make-template-record-from-qualified
222 : "Creates an EventTemplate record from a map with :event-template/* namespaced keys."
223 : [{:templates/keys [uuid
224 : public-id
225 : sport-code
226 : activity-type-code
227 : name
228 : description
229 : state
230 : visibility
231 : club-id
232 : fee
233 : fee-type
234 : currency
235 : timezone
236 : language
237 : duration
238 : score-entry-mode
239 : score-entry-default
240 : automatic-event-creation
241 : automatic-event-creation-delay-minutes
242 : created-datetime
243 : updated-datetime
244 : source
245 : host-user-id
246 : created-by
247 : rrule
248 : rrule-json
249 : ruleset-id
250 : settings
251 : images-version]}]
252 128 : (let [record {:uuid uuid
253 4 : :public-id public-id
254 4 : :sport-code sport-code
255 4 : :activity-type-code activity-type-code
256 4 : :name name
257 4 : :description description
258 4 : :state state
259 4 : :visibility visibility
260 4 : :club-id club-id
261 4 : :fee fee
262 4 : :fee-type fee-type
263 4 : :currency currency
264 4 : :timezone timezone
265 20 : :language (or language "en")
266 4 : :duration duration
267 20 : :score-entry-mode (or score-entry-mode "either")
268 20 : :score-entry-default (or score-entry-default "game")
269 8 : :automatic-event-creation (boolean automatic-event-creation)
270 20 : :automatic-event-creation-delay-minutes (or automatic-event-creation-delay-minutes 5)
271 4 : :created-datetime created-datetime
272 4 : :updated-datetime updated-datetime
273 4 : :source source
274 4 : :host-user-id host-user-id
275 4 : :created-by created-by
276 4 : :rrule rrule
277 4 : :rrule-json rrule-json
278 4 : :ruleset-id ruleset-id
279 4 : :settings settings
280 20 : :images-version (or images-version 0)}]
281 20 : (if (m/validate TemplateRecordSchema record)
282 12 : (map->TemplateRecord record)
283 0 : (throw
284 0 : (ex-info
285 : "Invalid template data"
286 0 : {:type :internal-validation-error
287 0 : :schema TemplateRecordSchema
288 0 : :data record
289 0 : :humanized-errors (me/humanize (m/explain TemplateRecordSchema record))})))))
|