about summary refs log tree commit diff
path: root/users/grfn/bbbg/src/bbbg/handlers/attendees.clj
blob: 3a5b49900476b2105bfca68f5c4b204e76aae46a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
(ns bbbg.handlers.attendees
  (:require
   [bbbg.attendee :as attendee]
   [bbbg.db :as db]
   [bbbg.db.attendee :as db.attendee]
   [bbbg.db.event :as db.event]
   [bbbg.event :as event]
   [bbbg.handlers.core :refer [page-response wrap-auth-required]]
   [bbbg.views.flash :as flash]
   [cheshire.core :as json]
   [compojure.coercions :refer [as-uuid]]
   [compojure.core :refer [GET POST routes]]
   [honeysql.helpers :refer [merge-where]]
   [ring.util.response :refer [content-type redirect response not-found]])
  (:import java.util.UUID))

(defn- attendees-page [{:keys [attendees q edit-notes]}]
  [:div
   [:form.search-form {:method :get :action "/attendees"}
    [:input {:type "search"
             :name "q"
             :value q}]
    [:input {:type "submit"
             :value "Search Attendees"}]]
   [:table.attendees
    [:thead
     [:tr
      [:th "Meetup Name"]
      [:th "Discord Name"]
      [:th "Events RSVPd"]
      [:th "Events Attended"]
      [:th "No-Shows"]
      [:th "Notes"]]]
    [:tbody
     (for [attendee attendees
           :let [id (::attendee/id attendee)]]
       [:tr
        [:td (::attendee/meetup-name attendee)]
        [:td (::attendee/discord-name attendee)]
        [:td (:events-rsvpd attendee)]
        [:td (:events-attended attendee)]
        [:td (:no-shows attendee)]
        (if (= edit-notes id)
          [:td
           [:form.organizer-notes {:method :post
                                   :action (str "/attendees/" id "/notes")}
            [:input {:type :text :name "notes"
                     :value (::attendee/organizer-notes attendee)}]
            [:input {:type "Submit" :value "Save Notes"}]]]
          [:td
           (::attendee/organizer-notes attendee)
           [:a {:href (str "/attendees?edit-notes=" id)}
            "Edit Notes"]])])]]])

(defn attendees-routes [{:keys [db]}]
  (routes
   (wrap-auth-required
    (routes
     (GET "/attendees" [q edit-notes]
       (let [attendees (db/list db (cond-> (db.attendee/with-stats)
                                     q (db.attendee/search q)))
             edit-notes (some-> edit-notes UUID/fromString)]
         (page-response (attendees-page {:attendees attendees
                                         :q q
                                         :edit-notes edit-notes}))))

     (POST "/attendees/:id/notes" [id :<< as-uuid notes]
       (if (seq (db/update! db
                            :attendee
                            {::attendee/organizer-notes notes}
                            [:= :id id]))
         (-> (redirect "/attendees")
             (flash/add-flash
              #:flash{:type :success
                      :message "Notes updated successfully"}))
         (not-found "Attendee not found")))))

   (GET "/attendees.json" [q event_id attended]
     (let [results
           (db/list
            db
            (cond->
                (if q
                  (db.attendee/search q)
                  {:select [:attendee.*] :from [:attendee]})
                event_id (db.attendee/for-event event_id)
                (some? attended)
                (merge-where
                 (case attended
                   "true" :attended
                   "false" [:or [:= :attended nil] [:not :attended]]))))]
       (-> {:results results}
           json/generate-string
           response
           (content-type "application/json"))))

   (POST "/event_attendees" [event_id attendee_id]
     (if (and (db/exists? db {:select [:id] :from [:event] :where [:= :id event_id]})
              (db/exists? db {:select [:id] :from [:attendee] :where [:= :id attendee_id]}))
       (do
         (db.event/attended! db {::event/id event_id
                                 ::attendee/id attendee_id})
         (-> (redirect (str "/signup-forms/" event_id))
             (assoc-in [:session :test] 1)
             (flash/add-flash
              #:flash{:type :success
                      :message "Thank you for signing in! Enjoy the event."})))
       (response "Something went wrong")))))

(comment
  (def db (:db bbbg.core/system))
  (db/list db :attendee)
  (db/list db
           (->
            (db.attendee/search "gr")
            (db.attendee/for-event #uuid "9f4f3eae-3317-41a7-843c-81bcae52aebf")))
  (honeysql.format/format
   (->
    (db.attendee/search "gr")
    (db.attendee/for-event #uuid "9f4f3eae-3317-41a7-843c-81bcae52aebf")))
  )