Line data Source code
1 4 : (ns schemas.domain.club
2 : (:require
3 : [malli.core :as m]
4 : [malli.error :as me]))
5 :
6 :
7 4 : (def ClubRecordSchema
8 28 : [:map {:swagger/title "ClubRecord"
9 : :swagger/description "Club record from database"}
10 20 : [:uuid [:ref :types/UuidString]]
11 8 : [:public-id
12 36 : [:string {:min 1
13 : :description "Public-facing nanoid identifier"
14 : :example "V1StGXR8_Z5j"}]]
15 28 : [:sl-uuid {:optional true
16 : :description "SportLevel UUID"}
17 12 : [:maybe :string]]
18 8 : [:name
19 44 : [:string {:min 1
20 : :max 255
21 : :description "Club name"
22 : :example "Grand-Prix"}]]
23 28 : [:network {:optional true
24 : :description "Club network name"}
25 12 : [:maybe :string]]
26 36 : [:country {:optional true
27 : :description "Country"
28 : :example "Ukraine"}
29 12 : [:maybe :string]]
30 36 : [:city {:optional true
31 : :description "City"
32 : :example "Kyiv"}
33 12 : [:maybe :string]]
34 28 : [:phone-number {:optional true
35 : :description "Contact phone number"}
36 12 : [:maybe :string]]
37 28 : [:location {:description "Location data (coordinates, address, etc.)"
38 4 : :example {}}
39 : :map]
40 24 : [:working-hours {:description "Working hours schedule"
41 4 : :example []}
42 12 : [:sequential :any]]
43 24 : [:images {:description "Club images"
44 4 : :example []}
45 12 : [:sequential :any]]
46 32 : [:is-active {:optional true
47 : :description "Whether the club is active (soft-delete flag)"}
48 : :boolean]
49 24 : [:created-datetime {:optional true}
50 : :any]
51 24 : [:updated-datetime {:optional true}
52 : :any]])
53 :
54 :
55 4 : (defrecord
56 : ClubRecord
57 : [uuid
58 : public-id
59 : sl-uuid
60 : name
61 : network
62 : country
63 : city
64 : phone-number
65 : location
66 : working-hours
67 : images
68 : is-active
69 : created-datetime
70 : updated-datetime])
71 :
72 :
73 4 : (defn make-club-record-from-qualified
74 : "Creates a Club record from a map with :club/* namespaced keys.
75 : The map may contain additional keys from other namespaces which are ignored."
76 : [{:clubs/keys [uuid
77 : public-id
78 : sl-uuid
79 : name
80 : network
81 : country
82 : city
83 : phone-number
84 : location
85 : working-hours
86 : images
87 : is-active
88 : created-datetime
89 : updated-datetime]}]
90 0 : (let [record {:uuid uuid
91 0 : :public-id public-id
92 0 : :sl-uuid sl-uuid
93 0 : :name name
94 0 : :network network
95 0 : :country country
96 0 : :city city
97 0 : :phone-number phone-number
98 0 : :location location
99 0 : :working-hours working-hours
100 0 : :images images
101 0 : :is-active (if (nil? is-active) true is-active)
102 0 : :created-datetime created-datetime
103 0 : :updated-datetime updated-datetime}]
104 0 : (if (m/validate ClubRecordSchema record)
105 0 : (map->ClubRecord record)
106 0 : (throw
107 0 : (ex-info
108 : "Invalid club data"
109 0 : {:type :internal-validation-error
110 0 : :schema ClubRecordSchema
111 0 : :data record
112 0 : :humanized-errors (me/humanize (m/explain ClubRecordSchema record))})))))
113 :
114 :
115 4 : (defn make-club-record
116 : "Creates a Club record with validation."
117 : {:malli/schema [:=> [:cat ClubRecordSchema] ClubRecordSchema]}
118 : [{:keys [uuid
119 : public-id
120 : sl-uuid
121 : name
122 : network
123 : country
124 : city
125 : phone-number
126 : location
127 : working-hours
128 : images
129 : is-active
130 : created-datetime
131 : updated-datetime]
132 : :as club-data}]
133 20 : (if (m/validate ClubRecordSchema club-data)
134 8 : (map->ClubRecord
135 64 : {:uuid uuid
136 4 : :public-id public-id
137 4 : :sl-uuid sl-uuid
138 4 : :name name
139 4 : :network network
140 4 : :country country
141 4 : :city city
142 4 : :phone-number phone-number
143 4 : :location location
144 4 : :working-hours working-hours
145 4 : :images images
146 26 : :is-active (if (nil? is-active) true is-active)
147 4 : :created-datetime created-datetime
148 4 : :updated-datetime updated-datetime})
149 0 : (throw
150 0 : (ex-info
151 : "Invalid club data"
152 0 : {:type :internal-validation-error
153 0 : :schema ClubRecordSchema
154 0 : :data club-data
155 0 : :humanized-errors (me/humanize (m/explain ClubRecordSchema club-data))}))))
|