Line data Source code
1 4 : (ns matchscore.application.event.class-participant-payment
2 : (:require
3 : [cats.core :as c]
4 : [cats.monad.either :as either]
5 : [matchscore.domain.class-participant.entity :as entity]
6 : [matchscore.domain.class-participant.repository.postgres :as class-participant-postgres-repo]
7 : [matchscore.domain.class.query :as class-query]
8 : [matchscore.domain.event.repository.postgres :as event-postgres-repo]
9 : [matchscore.utils.errors :as utils.errors])
10 : (:import
11 : (java.time Instant)))
12 :
13 :
14 4 : (defn- -find-class-participant-or-fail
15 : [repo class-id participant-id]
16 7 : (if-let [existing (class-participant-postgres-repo/find-by-class-and-participant
17 1 : repo
18 1 : class-id
19 1 : participant-id)]
20 3 : (either/right existing)
21 2 : (either/left
22 3 : (utils.errors/build-error
23 : :check
24 5 : {:code :error.class-participant/not-found
25 : :message "Class participant not found."}))))
26 :
27 :
28 4 : (defn- -resolve-amount
29 : [{:keys [repos]} class-id supplied-amount supplied-currency]
30 7 : (if (and supplied-amount supplied-currency)
31 2 : (either/right
32 4 : {:amount supplied-amount
33 1 : :currency supplied-currency})
34 4 : (let [class-repo (:class-postgres-repo repos)
35 4 : class (class-query/get-class class-repo class-id)]
36 5 : (if (nil? class)
37 0 : (either/left
38 0 : (utils.errors/build-error
39 : :check
40 0 : {:code :error.class/not-found
41 : :message "Class not found."}))
42 4 : (let [event-repo (:event-postgres-repo repos)
43 7 : event (event-postgres-repo/find-by-id event-repo (:event-id class) {})]
44 2 : (either/right
45 10 : {:amount (or supplied-amount (:fee event))
46 7 : :currency (or supplied-currency (:currency event))}))))))
47 :
48 :
49 4 : (defn- -check-payments-enabled
50 : "Returns Either<Error, nil>. Left if the event does not have payments-enabled in settings."
51 : [ctx class-id]
52 6 : (let [class-repo (:class-postgres-repo (:repos ctx))
53 4 : class (class-query/get-class class-repo class-id)]
54 5 : (if (nil? class)
55 0 : (either/left
56 0 : (utils.errors/build-error
57 : :check
58 0 : {:code :error.class/not-found
59 : :message "Class not found."}))
60 6 : (let [event-repo (:event-postgres-repo (:repos ctx))
61 7 : event (event-postgres-repo/find-by-id event-repo (:event-id class) {})]
62 9 : (if (true? (get-in event [:settings :payments-enabled]))
63 3 : (either/right nil)
64 2 : (either/left
65 3 : (utils.errors/build-error
66 : :check
67 5 : {:code :error.event/payments-not-enabled
68 : :message "Payments are not enabled for this event."})))))))
69 :
70 :
71 4 : (defn mark-paid!
72 : "Marks a class participant as paid. Defaults amount/currency to the class/event
73 : fee when not supplied. Returns Either<Error, ClassParticipant>."
74 : [{:keys [repos]
75 : :as ctx}
76 : {:keys [class-id participant-id amount currency marked-by]}]
77 4 : (let [repo (:class-participant-postgres-repo repos)]
78 25 : (c/mlet [_ (-check-payments-enabled ctx class-id)
79 6 : existing (-find-class-participant-or-fail repo class-id participant-id)
80 6 : fee-data (-resolve-amount ctx class-id amount currency)]
81 2 : (class-participant-postgres-repo/save!
82 1 : repo
83 2 : (entity/merge-updates
84 1 : existing
85 7 : {:payment-status "paid"
86 3 : :payment-amount (:amount fee-data)
87 3 : :payment-currency (:currency fee-data)
88 2 : :paid-at (Instant/now)
89 1 : :payment-marked-by marked-by})))))
90 :
91 :
92 4 : (defn revert!
93 : "Reverts a class participant payment back to unpaid. Returns Either<Error, ClassParticipant>."
94 : [{:keys [repos]
95 : :as ctx}
96 : {:keys [class-id participant-id]}]
97 4 : (let [repo (:class-participant-postgres-repo repos)]
98 18 : (c/mlet [_ (-check-payments-enabled ctx class-id)
99 5 : existing (-find-class-participant-or-fail repo class-id participant-id)]
100 2 : (class-participant-postgres-repo/save!
101 1 : repo
102 2 : (entity/merge-updates
103 1 : existing
104 11 : {:payment-status "unpaid"
105 : :payment-amount nil
106 : :payment-currency nil
107 : :paid-at nil
108 : :payment-marked-by nil})))))
|