about summary refs log tree commit diff
path: root/users/grfn/bbbg/src/bbbg/meetup/import.clj
blob: 29aaf53e50b8754b2eebc3c01f2514909697f960 (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
122
123
124
(ns bbbg.meetup.import
  (:require
   [bbbg.attendee :as attendee]
   [bbbg.db.attendee :as db.attendee]
   [bbbg.db.event-attendee :as db.event-attendee]
   [bbbg.event :as event]
   [bbbg.event-attendee :as event-attendee]
   [bbbg.meetup-user :as meetup-user]
   [bbbg.util.core :as u]
   [bbbg.util.spec :as u.s]
   [clojure.data.csv :as csv]
   [clojure.java.io :as io]
   [clojure.spec.alpha :as s]
   [clojure.string :as str]
   [expound.alpha :as exp]))

(def spreadsheet-column->key
  {"Name" :name
   "User ID" :user-id
   "Title" :title
   "Event Host" :event-host
   "RSVP" :rsvp
   "Guests" :guests
   "RSVPed on" :rsvped-on
   "Joined Group on" :joined-group-on
   "URL of Member Profile" :member-profile-url})

(defn read-attendees [f]
  (with-open [reader (io/reader f)]
    (let [[headers & rows] (-> reader (csv/read-csv :separator \tab))
          keys (map spreadsheet-column->key headers)]
      (doall
       (->> rows
            (map (partial zipmap keys))
            (map (partial u/filter-kv (fn [k _] (some? k))))
            (filter (partial some (comp seq val))))))))

;;;

(s/def ::imported-attendee
  (s/keys :req [::attendee/meetup-name
                ::meetup-user/id]))

(def key->attendee-col
  {:name ::attendee/meetup-name
   :user-id ::meetup-user/id})

(defn row-user-id->user-id [row-id]
  (str/replace-first row-id "user " ""))

(defn check-attendee [attendee]
  ()
  (if (s/valid? ::imported-attendee attendee)
    attendee
    (throw (ex-info
            (str "Invalid imported attendee\n"
                 (exp/expound-str ::imported-attendee attendee))
            (assoc (s/explain-data ::imported-attendee attendee)
                   ::s/failure
                   ::s/assertion-failed)))))

(defn row->attendee [r]
  (u.s/assert!
   ::imported-attendee
   (update (u/keep-keys key->attendee-col r)
           ::meetup-user/id row-user-id->user-id)))

;;;

(s/def ::imported-event-attendee
  (s/keys :req [::event-attendee/rsvpd-attending?
                ::attendee/id
                ::event/id]))

(def key->event-attendee-col
  {:rsvp ::event-attendee/rsvpd-attending?})

(defn row->event-attendee
  [{event-id ::event/id :keys [meetup-id->attendee-id]} r]
  (let [attendee-id (-> r :user-id row-user-id->user-id meetup-id->attendee-id)]
    (u.s/assert!
     ::imported-event-attendee
     (-> (u/keep-keys key->event-attendee-col r)
         (update ::event-attendee/rsvpd-attending?
                 (partial = "Yes"))
         (assoc ::event/id event-id
                ::attendee/id attendee-id)))))

;;;

(defn import-data! [db event-id f]
  (let [rows (read-attendees f)
        attendees (db.attendee/upsert-all! db (map row->attendee rows))
        meetup-id->attendee-id (into {}
                                     (map (juxt ::meetup-user/id ::attendee/id))
                                     attendees)]
    (db.event-attendee/upsert-all!
     db
     (map (partial row->event-attendee
                   {::event/id event-id
                    :meetup-id->attendee-id meetup-id->attendee-id})
          rows))))

;;; Spreadsheet columns:
;;;
;;; Name
;;; User ID
;;; Title
;;; Event Host
;;; RSVP
;;; Guests
;;; RSVPed on
;;; Joined Group on
;;; URL of Member Profile
;;; Have you been to one of our events before? Note, attendance at all events will require proof of vaccination until further notice.

(comment
  (def -filename- "/home/grfn/code/depot/users/grfn/bbbg/sample-data.tsv")
  (def event-id #uuid "09f8fed6-7480-451b-89a2-bb4edaeae657")

  (read-attendees -filename-)
  (import-data! (:db bbbg.core/system) event-id -filename-)

  )