Line data Source code
1 4 : (ns schemas.domain.class-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 ClassParticipantRecordSchema
9 28 : [:map {:swagger/title "ClassParticipantRecord"
10 : :swagger/description "Complete class participant record from database"}
11 20 : [:class-id [:ref :types/UuidString]]
12 20 : [:participant-id [:ref :types/UuidString]]
13 20 : [:seed {:optional true}
14 12 : [:maybe :int]]
15 8 : [:status
16 28 : [:string {:description "Participation status"
17 : :example "registered"}]]
18 24 : [:registered-at {:optional true}
19 : :any]
20 24 : [:confirmed-at {:optional true}
21 : :any]
22 24 : [:extra {:optional true}
23 : :any]
24 24 : [:created-datetime {:optional true}
25 : :any]
26 24 : [:updated-datetime {:optional true}
27 : :any]
28 20 : [:source [:ref :matchscore/source]]
29 20 : [:payment-status {:optional true}
30 28 : [:maybe [:enum "unpaid" "claimed" "paid"]]]
31 20 : [:payment-amount {:optional true}
32 12 : [:maybe number?]]
33 20 : [:payment-currency {:optional true}
34 28 : [:maybe [:string {:max 10}]]]
35 20 : [:paid-at {:optional true}
36 36 : [:maybe [:or :string api-common/Datetime [:fn #(instance? java.time.OffsetDateTime %)]]]]
37 20 : [:payment-marked-by {:optional true}
38 20 : [:maybe [:ref :types/UuidString]]]])
39 :
40 :
41 4 : (defrecord
42 : ClassParticipantRecord
43 : [class-id
44 : participant-id
45 : seed
46 : status
47 : registered-at
48 : confirmed-at
49 : extra
50 : created-datetime
51 : updated-datetime
52 : source
53 : payment-status
54 : payment-amount
55 : payment-currency
56 : paid-at
57 : payment-marked-by])
58 :
59 :
60 4 : (defn make-class-participant-record
61 : "Creates a ClassParticipant record with validation.
62 : Note: This validation uses the default registry. Ensure schemas.registry/init-registry!
63 : has been called before using this function."
64 : {:malli/schema [:=> [:cat ClassParticipantRecordSchema] ClassParticipantRecordSchema]}
65 : [{:keys [class-id
66 : participant-id
67 : seed
68 : status
69 : registered-at
70 : confirmed-at
71 : extra
72 : created-datetime
73 : updated-datetime
74 : source
75 : payment-status
76 : payment-amount
77 : payment-currency
78 : paid-at
79 : payment-marked-by]
80 : :as class-participant-data}]
81 20 : (if (m/validate ClassParticipantRecordSchema class-participant-data)
82 8 : (map->ClassParticipantRecord
83 68 : {:class-id class-id
84 4 : :participant-id participant-id
85 4 : :seed seed
86 4 : :status status
87 4 : :registered-at registered-at
88 4 : :confirmed-at confirmed-at
89 4 : :extra extra
90 4 : :created-datetime created-datetime
91 4 : :updated-datetime updated-datetime
92 4 : :source source
93 20 : :payment-status (or payment-status "unpaid")
94 4 : :payment-amount payment-amount
95 4 : :payment-currency payment-currency
96 4 : :paid-at paid-at
97 4 : :payment-marked-by payment-marked-by})
98 0 : (throw
99 0 : (ex-info
100 : "Invalid class participant data"
101 0 : {:type :internal-validation-error
102 0 : :schema ClassParticipantRecordSchema
103 0 : :data class-participant-data
104 0 : :humanized-errors (me/humanize
105 0 : (m/explain ClassParticipantRecordSchema class-participant-data))}))))
|