Line data Source code
1 4 : (ns matchscore.application.template.create
2 : "Application service for event template creation.
3 : Orchestrates across user, participant, and event template domains."
4 : (:require
5 : [cats.core :as c]
6 : [cats.monad.either :as either]
7 : [matchscore.application.template.common :as template-common]
8 : [matchscore.domain.class-template.command :as class-template-command]
9 : [matchscore.domain.class-template.repository.postgres :as class-template-postgres-repo]
10 : [matchscore.domain.participant.repository.postgres :as participant-postgres-repo]
11 : [matchscore.domain.template-participant.command :as template-participant-command]
12 : [matchscore.domain.template-participant.repository.postgres
13 : :as
14 : template-participant-postgres-repo]
15 : [matchscore.domain.template.command :as template-command]
16 : [matchscore.domain.template.repository.postgres :as template-postgres-repo]
17 : [matchscore.domain.user.repository.postgres :as user-postgres-repo]
18 : [matchscore.media.db :as media-db]
19 : [matchscore.utils.errors :as utils.errors]
20 : [matchscore.utils.nanoid :as nanoid]
21 : [matchscore.utils.uuid :as uuid-utils]
22 : [next.jdbc :as jdbc]
23 : [next.jdbc.result-set :as rs]))
24 :
25 :
26 4 : (defn- find-current-user
27 : [repos current-user-id]
28 11 : (if-let [user (user-postgres-repo/find-by-id (:user-postgres-repo repos) current-user-id)]
29 3 : (either/right user)
30 0 : (either/left
31 0 : (utils.errors/build-error
32 : :check
33 0 : {:code :error.template/user-not-found
34 : :message "Current user not found."}))))
35 :
36 :
37 4 : (defn- find-or-create-participant!
38 : [repos user-id]
39 4 : (let [repo (:participant-postgres-repo repos)
40 6 : existing-participant (first (participant-postgres-repo/find-by-user-id repo user-id))]
41 2 : (if existing-participant
42 0 : (either/right existing-participant)
43 2 : (participant-postgres-repo/save!
44 1 : repo
45 10 : {:uuid (uuid-utils/generate-uuid7-str)
46 3 : :public-id (nanoid/generate 15)
47 : :source "matchscore"
48 1 : :user-id user-id
49 : :type "person"}))))
50 :
51 :
52 4 : (defn- make-tx-repos
53 : [repos tx-opts]
54 2 : (merge
55 1 : repos
56 9 : {:template-postgres-repo (template-postgres-repo/make-repository tx-opts)
57 3 : :template-participant-postgres-repo (template-participant-postgres-repo/make-repository tx-opts)
58 3 : :class-template-postgres-repo (class-template-postgres-repo/make-repository tx-opts)
59 3 : :participant-postgres-repo (participant-postgres-repo/make-repository tx-opts)
60 3 : :user-postgres-repo (user-postgres-repo/make-repository tx-opts)}))
61 :
62 :
63 4 : (defn execute!
64 : "Creates an event template, ensures the creator has a participant record,
65 : and registers that participant in the created template."
66 : [{:keys [repos pg]
67 : :as ctx}
68 : template-data
69 : current-user-id]
70 1 : (try
71 4 : (jdbc/with-transaction
72 1 : [tx pg]
73 5 : (let [default-class-max-players (or
74 3 : (:default-class-max-players template-data)
75 : ;; Compatibility for clients deployed before the
76 : ;; template-level capacity was removed.
77 0 : (:max-players template-data))
78 5 : template-data (dissoc template-data :default-class-max-players :max-players)
79 6 : tx-opts (jdbc/with-options tx {:builder-fn rs/as-kebab-maps})
80 4 : tx-repos (make-tx-repos repos tx-opts)
81 5 : tx-ctx (assoc ctx :repos tx-repos)
82 46 : result (c/mlet [user (find-current-user tx-repos current-user-id)
83 6 : template (template-command/create-template!
84 1 : tx-ctx
85 2 : (->
86 1 : template-data
87 1 : template-common/with-rrule-json
88 5 : (assoc :created-by (:uuid user))))
89 4 : _ (do
90 3 : (media-db/attach-created-gallery!
91 1 : tx
92 : :template
93 3 : (:uuid template)
94 3 : (:uuid user)
95 1 : template-data)
96 3 : (either/right nil))
97 4 : _ (class-template-command/create-class-template!
98 1 : tx-ctx
99 10 : {:uuid (uuid-utils/generate-uuid7-str)
100 3 : :public-id (nanoid/generate 15)
101 3 : :template-id (:uuid template)
102 : :name "Default"
103 1 : :max-players default-class-max-players
104 3 : :created-by (:created-by template)})
105 7 : participant (find-or-create-participant! tx-repos (:uuid user))
106 2 : _ (template-participant-command/register-participant!
107 1 : tx-ctx
108 12 : {:template-id (:uuid template)
109 3 : :participant-id (:uuid participant)
110 : :status "registered"
111 2 : :roles ["admin"]
112 1 : :extra {}
113 : :source "matchscore"})]
114 3 : (c/return template))]
115 5 : (if (either/left? result) (throw (ex-info "tx-rollback" {::result result})) result)))
116 : (catch clojure.lang.ExceptionInfo e
117 0 : (if (contains? (ex-data e) ::result) (::result (ex-data e)) (throw e)))))
|