Line data Source code
1 4 : (ns matchscore.domain.class-participant.entity
2 : (:require
3 : [schemas.domain.class-participants :as class-participant-schemas]
4 : [schemas.domain.event-participants :as event-participant-schemas]))
5 :
6 :
7 4 : (def updatable-fields
8 28 : [:status :payment-status :payment-amount :payment-currency :paid-at :payment-marked-by])
9 :
10 :
11 4 : (def roster-statuses
12 : "Statuses that hold a place in a class. Other entries neither fill it nor queue."
13 16 : #{"registered" "confirmed" "checked_in"})
14 :
15 :
16 4 : (defn valid-status?
17 : "Returns true when status is one a participant can hold."
18 : [status]
19 6 : (contains? (set event-participant-schemas/participant-statuses) status))
20 :
21 :
22 4 : (defn make-class-participant
23 : "Creates a validated ClassParticipant map."
24 : [data]
25 12 : (class-participant-schemas/make-class-participant-record data))
26 :
27 :
28 4 : (defn has-changes?
29 : "Returns true if any updatable field present in updates differs from existing."
30 : [existing updates]
31 2 : (not=
32 4 : (select-keys existing updatable-fields)
33 7 : (select-keys (merge existing updates) updatable-fields)))
34 :
35 :
36 4 : (defn merge-updates
37 : "Merges updatable fields from updates into existing class participant."
38 : [existing updates]
39 14 : (merge existing (select-keys updates updatable-fields)))
40 :
41 :
42 4 : (defn- registration-order-key
43 : [participant]
44 14 : [(nil? (:registered-at participant))
45 6 : (:registered-at participant)
46 10 : (str (:participant-id participant))])
47 :
48 :
49 4 : (defn with-queue-positions
50 : "Marks which class participants are on the waitlist. Entries holding a place are ranked
51 : by registration time (unknown times last, participant id as tie-break); the ones ranked
52 : past `max-players` get `:queued true` and a 1-based `:queue-position`. Nobody is queued
53 : without a positive `max-players`. Returns the ranked entries followed by the entries
54 : that hold no place."
55 : [max-players participants]
56 16 : (let [holds-place? #(contains? roster-statuses (:status %))
57 20 : capacity (when (and max-players (pos? max-players)) max-players)]
58 4 : (concat
59 4 : (map-indexed
60 2 : (fn [idx participant]
61 16 : (let [queue-position (when capacity (- (inc idx) capacity))]
62 16 : (if (and queue-position (pos? queue-position))
63 12 : (assoc participant
64 : :queued true
65 2 : :queue-position queue-position)
66 10 : (assoc participant :queued false))))
67 14 : (sort-by registration-order-key (filter holds-place? participants)))
68 19 : (map #(assoc % :queued false) (remove holds-place? participants)))))
|