Line data Source code
1 4 : (ns matchscore.db.postgres.template-occurrence-exceptions
2 : (:require
3 : [clojure.string :as str]
4 : [next.jdbc :as jdbc]
5 : [next.jdbc.result-set :as rs])
6 : (:import
7 : (java.sql Timestamp)
8 : (java.time Instant)
9 : (java.util Date)))
10 :
11 :
12 4 : (defn instant
13 : [value]
14 15 : (cond (nil? value) nil
15 10 : (instance? Instant value) value
16 10 : (instance? Date value) (.toInstant ^Date value)
17 6 : (string? value) (Instant/parse value)
18 0 : :else (.toInstant value)))
19 :
20 :
21 14 : (defn- timestamp [value] (Timestamp/from (instant value)))
22 :
23 :
24 4 : (defn- row
25 : [value]
26 6 : (when value
27 10 : (let [value (into {} value)]
28 32 : (cond-> value
29 14 : (:uuid value) (update :uuid str)
30 14 : (:template-id value) (update :template-id str)
31 14 : (:created-by value) (update :created-by str)
32 24 : (:occurrence-start value) (update :occurrence-start #(str (instant %)))
33 24 : (:created-datetime value) (update :created-datetime #(str (instant %)))
34 20 : (:updated-datetime value) (update :updated-datetime #(str (instant %)))))))
35 :
36 :
37 4 : (defn list-for-template
38 : [ds template-id]
39 1 : (->>
40 2 : (jdbc/execute!
41 1 : ds
42 2 : ["SELECT * FROM template_occurrence_exceptions
43 : WHERE template_id = ?::uuid
44 : ORDER BY occurrence_start"
45 3 : (str template-id)]
46 3 : {:builder-fn rs/as-unqualified-kebab-maps})
47 2 : (mapv row)))
48 :
49 :
50 4 : (defn find-by-id
51 : [ds template-id exception-id]
52 8 : (some->
53 2 : (jdbc/execute-one!
54 1 : ds
55 2 : ["SELECT * FROM template_occurrence_exceptions
56 : WHERE template_id = ?::uuid AND uuid = ?::uuid"
57 3 : (str template-id)
58 3 : (str exception-id)]
59 3 : {:builder-fn rs/as-unqualified-kebab-maps})
60 1 : row))
61 :
62 :
63 4 : (defn find-by-start
64 : [ds template-id occurrence-start]
65 0 : (some->
66 0 : (jdbc/execute-one!
67 0 : ds
68 0 : ["SELECT * FROM template_occurrence_exceptions
69 : WHERE template_id = ?::uuid AND occurrence_start = ?"
70 0 : (str template-id)
71 0 : (timestamp occurrence-start)]
72 0 : {:builder-fn rs/as-unqualified-kebab-maps})
73 0 : row))
74 :
75 :
76 4 : (defn starts-on-or-after
77 : [ds template-id from]
78 1 : (->>
79 2 : (jdbc/execute!
80 1 : ds
81 2 : ["SELECT occurrence_start FROM template_occurrence_exceptions
82 : WHERE template_id = ?::uuid AND occurrence_start >= ?"
83 3 : (str template-id)
84 3 : (timestamp from)]
85 3 : {:builder-fn rs/as-unqualified-kebab-maps})
86 8 : (keep #(instant (:occurrence-start %)))
87 1 : set))
88 :
89 :
90 4 : (defn event-exists?
91 : [ds template-id occurrence-start]
92 1 : (boolean
93 2 : (jdbc/execute-one!
94 1 : ds
95 2 : ["SELECT 1 FROM events
96 : WHERE template_id = ?::uuid AND datetime_start = ?
97 : LIMIT 1"
98 3 : (str template-id)
99 3 : (timestamp occurrence-start)])))
100 :
101 :
102 4 : (defn save!
103 : [ds {:keys [template-id occurrence-start note notify-linked-chats notify-participants created-by]}]
104 16 : (some->
105 4 : (jdbc/execute-one!
106 2 : ds
107 4 : ["INSERT INTO template_occurrence_exceptions
108 : (template_id, occurrence_start, note, notify_linked_chats,
109 : notify_participants, created_by)
110 : VALUES (?::uuid, ?, ?, ?, ?, ?::uuid)
111 : ON CONFLICT (template_id, occurrence_start) DO UPDATE SET
112 : note = EXCLUDED.note,
113 : notify_linked_chats = EXCLUDED.notify_linked_chats,
114 : notify_participants = EXCLUDED.notify_participants,
115 : created_by = EXCLUDED.created_by,
116 : updated_datetime = now()
117 : RETURNING *"
118 6 : (str template-id)
119 6 : (timestamp occurrence-start)
120 44 : (some->
121 2 : note
122 2 : str
123 2 : str/trim
124 2 : not-empty)
125 4 : (boolean notify-linked-chats)
126 4 : (boolean notify-participants)
127 16 : (some->
128 2 : created-by
129 2 : str)]
130 6 : {:builder-fn rs/as-unqualified-kebab-maps})
131 2 : row))
132 :
133 :
134 4 : (defn delete!
135 : [ds template-id exception-id]
136 8 : (some->
137 2 : (jdbc/execute-one!
138 1 : ds
139 2 : ["DELETE FROM template_occurrence_exceptions
140 : WHERE template_id = ?::uuid AND uuid = ?::uuid
141 : RETURNING *"
142 3 : (str template-id)
143 3 : (str exception-id)]
144 3 : {:builder-fn rs/as-unqualified-kebab-maps})
145 1 : row))
|