Line data Source code
1 4 : (ns matchscore.domain.template.query
2 : (:require
3 : [cats.monad.either :as either]
4 : [matchscore.domain.template.repository.postgres :as template-postgres-repo]))
5 :
6 :
7 4 : (defn- with-default-sort-dir
8 : "Defaults sort-dir to :desc when not provided.
9 : `qh/build-search-query` defaults to :asc; for templates we want newest-first."
10 : [pagination]
11 31 : (update pagination :sort-dir #(or % :desc)))
12 :
13 :
14 4 : (defn get-template
15 : "Retrieves an event template by ID. Returns EventTemplate map or nil."
16 : [repo id ctx]
17 15 : (template-postgres-repo/find-by-id repo id ctx))
18 :
19 :
20 4 : (defn get-templates-by-ids
21 : "Retrieves event templates by IDs. Returns a vector of EventTemplate maps."
22 : [repo ids]
23 4 : (template-postgres-repo/find-by-ids repo ids))
24 :
25 :
26 4 : (defn get-template-by-public-id
27 : "Retrieves an event template by public_id. Returns EventTemplate map or nil."
28 : [repo public-id ctx]
29 15 : (template-postgres-repo/find-by-public-id repo public-id ctx))
30 :
31 :
32 4 : (defn get-templates-by-creator-id
33 : "Retrieves event templates by creator ID. Returns a list of EventTemplate maps."
34 : [repo creator-id ctx]
35 5 : (template-postgres-repo/find-by-creator-id repo creator-id ctx))
36 :
37 :
38 4 : (defn search-templates
39 : "Search event templates by filters and pagination. Returns Either<Error, Result>."
40 : [repo filters pagination]
41 18 : (either/right (template-postgres-repo/search repo filters (with-default-sort-dir pagination))))
42 :
43 :
44 4 : (defn search-user-templates
45 : "Search the authenticated user's event templates by filters and pagination.
46 : Forces a `createdBy = user-id` filter on top of the supplied filters. With
47 : `include-admin-role?`, templates where the user holds the admin participant role match too.
48 : Returns Either<Error, Result>."
49 7 : ([repo user-id filters pagination] (search-user-templates repo user-id filters pagination false))
50 : ([repo user-id filters pagination include-admin-role?]
51 6 : (either/right
52 6 : (template-postgres-repo/search
53 3 : repo
54 18 : (cond (:hosted-user-id filters) filters
55 30 : (and include-admin-role? (seq user-id)) (assoc filters :managing-user-id user-id)
56 14 : :else (assoc filters :createdBy {:eq user-id}))
57 9 : (with-default-sort-dir pagination)))))
58 :
59 :
60 4 : (defn search-user-participating-templates
61 : "Search templates where the authenticated user participates.
62 : Forces participation through template_participants -> participants.user_id. With
63 : `player-only?`, only templates where the user holds the player role match.
64 : Returns Either<Error, Result>."
65 : ([repo user-id filters pagination]
66 0 : (search-user-participating-templates repo user-id filters pagination false))
67 : ([repo user-id filters pagination player-only?]
68 4 : (if (seq user-id)
69 2 : (either/right
70 2 : (template-postgres-repo/search
71 1 : repo
72 16 : (cond-> (assoc (or filters {}) :participating-user-id user-id)
73 2 : player-only? (assoc :participating-player-only? true))
74 3 : (with-default-sort-dir pagination)))
75 5 : (either/left {:message "Authentication required."}))))
|