Line data Source code
1 4 : (ns schemas.domain.template-participants
2 : (:require
3 : [malli.core :as m]
4 : [malli.error :as me]
5 : [schemas.api.common :as api-common]))
6 :
7 :
8 4 : (def TemplateParticipantRecordSchema
9 28 : [:map {:swagger/title "TemplateParticipantRecord"
10 : :swagger/description "Complete event template participant record from database"}
11 20 : [:template-id [:ref :types/UuidString]]
12 20 : [:participant-id [:ref :types/UuidString]]
13 20 : [:seed {:optional true}
14 28 : [:maybe [:int {:min 1}]]]
15 20 : [:status {:optional true}
16 20 : [:enum "registered" "confirmed" "withdrawn"]]
17 20 : [:registered-at {:optional true}
18 12 : [:maybe api-common/Datetime]]
19 20 : [:confirmed-at {:optional true}
20 12 : [:maybe api-common/Datetime]]
21 20 : [:roles {:optional true}
22 4 : sequential?]
23 20 : [:extra {:optional true}
24 4 : map?]
25 20 : [:source {:optional true}
26 12 : [:ref :matchscore/source]]
27 20 : [:created-datetime {:optional true}
28 12 : [:maybe api-common/Datetime]]
29 12 : [:updated-datetime api-common/Datetime]])
30 :
31 :
32 4 : (defrecord
33 : TemplateParticipantRecord
34 : [template-id
35 : participant-id
36 : seed
37 : status
38 : registered-at
39 : confirmed-at
40 : roles
41 : extra
42 : source
43 : created-datetime
44 : updated-datetime])
45 :
46 :
47 4 : (defn make-template-participant-record
48 : "Creates an EventTemplateParticipant record with validation.
49 : Note: This validation uses the default registry. Ensure schemas.registry/init-registry!
50 : has been called before using this function."
51 : {:malli/schema [:=> [:cat :map] TemplateParticipantRecordSchema]}
52 : [{:keys [template-id
53 : participant-id
54 : seed
55 : status
56 : registered-at
57 : confirmed-at
58 : roles
59 : extra
60 : source
61 : created-datetime
62 : updated-datetime]
63 : :as participant-data}]
64 15 : (if (m/validate TemplateParticipantRecordSchema participant-data)
65 6 : (map->TemplateParticipantRecord
66 39 : {:template-id template-id
67 3 : :participant-id participant-id
68 3 : :seed seed
69 15 : :status (or status "registered")
70 3 : :registered-at registered-at
71 3 : :confirmed-at confirmed-at
72 15 : :roles (or roles [])
73 15 : :extra (or extra {})
74 15 : :source (or source "matchscore")
75 3 : :created-datetime created-datetime
76 3 : :updated-datetime updated-datetime})
77 0 : (throw
78 0 : (ex-info
79 : "Invalid template participant data"
80 0 : {:type :internal-validation-error
81 0 : :schema TemplateParticipantRecordSchema
82 0 : :data participant-data
83 0 : :humanized-errors (me/humanize
84 0 : (m/explain TemplateParticipantRecordSchema participant-data))}))))
85 :
86 :
87 4 : (defn make-template-participant-record-from-qualified
88 : "Creates an EventTemplateParticipant record from a map with :event-template-participants/* namespaced keys."
89 : [{:template-participants/keys [template-id
90 : participant-id
91 : seed
92 : status
93 : registered-at
94 : confirmed-at
95 : roles
96 : extra
97 : source
98 : created-datetime
99 : updated-datetime]}]
100 0 : (let [record {:template-id template-id
101 0 : :participant-id participant-id
102 0 : :seed seed
103 0 : :status status
104 0 : :registered-at registered-at
105 0 : :confirmed-at confirmed-at
106 0 : :roles roles
107 0 : :extra extra
108 0 : :source source
109 0 : :created-datetime created-datetime
110 0 : :updated-datetime updated-datetime}]
111 0 : (if (m/validate TemplateParticipantRecordSchema record)
112 0 : (map->TemplateParticipantRecord record)
113 0 : (throw
114 0 : (ex-info
115 : "Invalid template participant data"
116 0 : {:type :internal-validation-error
117 0 : :schema TemplateParticipantRecordSchema
118 0 : :data record
119 0 : :humanized-errors (me/humanize (m/explain TemplateParticipantRecordSchema record))})))))
|