Line data Source code
1 4 : (ns matchscore.domain.club.command
2 : (:require
3 : [cats.core :as c]
4 : [cats.monad.either :as either]
5 : [matchscore.domain.club.entity :as entity]
6 : [matchscore.domain.club.repository.postgres :as club-postgres-repo]
7 : [matchscore.domain.club.repository.redis :as redis-repo]
8 : [matchscore.utils.errors :as utils.errors]
9 : [matchscore.utils.nanoid :as nanoid]
10 : [matchscore.utils.uuid :as uuid-utils]))
11 :
12 :
13 4 : (defn- check-id-not-taken
14 : [repo club-data]
15 21 : (if (club-postgres-repo/find-by-id repo (:uuid club-data))
16 0 : (either/left
17 0 : (utils.errors/build-error
18 : :check
19 0 : {:code :error.club/conflict-id
20 : :message "Club with such id already exists"
21 0 : :context {:input-data club-data}}))
22 9 : (either/right club-data)))
23 :
24 :
25 4 : (defn create-club!
26 : "Creates a new club. Validates data and checks for ID conflicts.
27 : Returns Either<Error, Club>."
28 : [{:keys [repos]} club-data]
29 12 : (let [repo (:club-postgres-repo repos)
30 9 : club-redis (:club-redis-repo repos)
31 59 : club-data (cond-> club-data
32 25 : (not (:uuid club-data)) (assoc :uuid (uuid-utils/generate-uuid7-str))
33 30 : (not (:public-id club-data)) (assoc :public-id (nanoid/generate))
34 27 : (nil? (:is-active club-data)) (assoc :is-active true)
35 26 : (nil? (:location club-data)) (assoc :location {})
36 26 : (nil? (:working-hours club-data)) (assoc :working-hours [])
37 22 : (nil? (:images club-data)) (assoc :images []))
38 9 : club (entity/make-club club-data)
39 45 : result (c/mlet [_ (check-id-not-taken repo club)] (club-postgres-repo/save! repo club))]
40 36 : (when (and (either/right? result) club-redis) (redis-repo/invalidate-clubs! club-redis))
41 3 : result))
42 :
43 :
44 4 : (defn update-club!
45 : "Updates an existing club. Returns
46 : Either<Error, {:code :success.club/ok|:no-changes :data Club}>."
47 : [{:keys [repos]} club-data]
48 8 : (let [repo (:club-postgres-repo repos)
49 6 : club-redis (:club-redis-repo repos)
50 12 : existing (club-postgres-repo/find-by-id repo (:uuid club-data))]
51 8 : (if-not existing
52 2 : (either/left
53 3 : (utils.errors/build-error
54 : :not-found
55 6 : {:code :error.club/not-found
56 : :message "Club not found"
57 5 : :context {:uuid (:uuid club-data)}}))
58 14 : (if-not (entity/has-changes? existing club-data)
59 2 : (either/right
60 4 : {:code :success.club/no-changes
61 1 : :data existing})
62 14 : (let [merged (entity/make-club (entity/merge-updates existing club-data))
63 8 : result (club-postgres-repo/save! repo merged)]
64 24 : (when (and (either/right? result) club-redis) (redis-repo/invalidate-clubs! club-redis))
65 4 : (c/fmap
66 2 : (fn [saved]
67 8 : {:code :success.club/ok
68 2 : :data saved})
69 2 : result))))))
70 :
71 :
72 4 : (defn deactivate-club!
73 : "Deactivates a club (soft delete). Returns Either<Error, Club>."
74 : [{:keys [repos]} club-uuid]
75 8 : (let [repo (:club-postgres-repo repos)
76 6 : club-redis (:club-redis-repo repos)
77 8 : result (club-postgres-repo/deactivate! repo club-uuid)]
78 24 : (when (and (either/right? result) club-redis) (redis-repo/invalidate-clubs! club-redis))
79 2 : result))
|