-
Notifications
You must be signed in to change notification settings - Fork 0
/
info_session.go
338 lines (292 loc) · 9.64 KB
/
info_session.go
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
package notify
import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"strconv"
"strings"
"time"
"github.com/operationspark/service-signup/greenlight"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"golang.org/x/sync/errgroup"
)
type (
Participant struct {
NameFirst string `bson:"nameFirst"`
NameLast string `bson:"nameLast"`
FullName string `bson:"fullName"`
Cell string `bson:"cell"`
Email string `bson:"email"`
ZoomJoinURL string `bson:"zoomJoinUrl"`
SessionDate time.Time
SessionLocationType string
SessionLocation Location
}
UpcomingSession struct {
ID string `bson:"_id"`
ProgramID string `bson:"programId"`
Times greenlight.Times `bson:"times"`
Participants []Participant
LocationID string `bson:"locationId"`
LocationType string `bson:"locationType"`
Location Location
}
Location struct {
Name string `json:"name"`
Line1 string `json:"line1"`
CityStateZip string `json:"cityStateZip"`
MapURL string `json:"mapUrl"`
}
MongoService struct {
dbName string
client *mongo.Client
}
OSRenderer interface {
// CreateMessageURL creates a URL to the Operation Spark Message Template Renderer with URL encoded data.
CreateMessageURL(Participant) (string, error)
}
Store interface {
GetUpcomingSessions(context.Context, time.Duration) ([]*UpcomingSession, error)
}
Shortener interface {
ShortenURL(ctx context.Context, url string) (string, error)
}
ServerOpts struct {
OSRendererService OSRenderer
ShortLinkService Shortener
SMSService SMSSender
Store Store
}
SMSSender interface {
Send(ctx context.Context, toNum, msg string) error
FormatCell(string) string
}
Server struct {
osMsSvc OSRenderer
shortySrv Shortener
store Store
twilioService SMSSender
}
Request struct {
JobName string `json:"jobName"`
JobArgs JobArgs `json:"jobArgs"`
}
JobArgs struct {
Period Period `json:"period"`
DryRun bool `json:"dryRun"`
}
Period string
contextKey int
)
const (
RECIPIENT_TZ contextKey = iota
)
const (
INFO_SESSION_PROGRAM_ID = "5sTmB97DzcqCwEZFR"
CENTRAL_TZ_NAME = "America/Chicago"
)
func NewServer(o ServerOpts) *Server {
return &Server{
osMsSvc: o.OSRendererService,
shortySrv: o.ShortLinkService,
store: o.Store,
twilioService: o.SMSService,
}
}
func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
http.Error(w, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed)
}
// Add timezone to the request context.
// TODO: Base the TZ on some location information somewhere
tz, err := time.LoadLocation(CENTRAL_TZ_NAME)
if err != nil {
fmt.Printf("loadLocation: %v, ", err)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
ctx := context.WithValue(r.Context(), RECIPIENT_TZ, tz)
var reqBody Request
err = reqBody.fromJSON(r.Body)
if err != nil {
fmt.Printf("fromJSON: %v, bodyL%+v\n", err, reqBody)
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
// Remind attendees for some period in the future.
// (1 hour, 2 days, etc)
inFuture, err := reqBody.JobArgs.Period.Parse()
if err != nil {
fmt.Printf("parse: %v, bodyL%+v\n", err, reqBody)
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
sessions, err := s.store.GetUpcomingSessions(ctx, inFuture)
if err != nil {
fmt.Printf("getUpcomingSessions: %v", err)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
if len(sessions) == 0 {
fmt.Printf("No upcoming sessions in the next %s\n", reqBody.JobArgs.Period)
return
}
for _, sess := range sessions {
fmt.Printf("upcoming info session:\nSessionID: %s, Time: %s\n",
sess.ID,
sess.Times.Start.DateTime.Format(time.RubyDate))
}
err = s.sendSMSReminders(ctx, sessions, reqBody.JobArgs.DryRun)
if err != nil {
fmt.Println("One or more messages failed to send", err)
http.Error(w, "One or more messages failed to send", http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusOK)
}
func NewMongoService(dbClient *mongo.Client, dbName string) *MongoService {
return &MongoService{
dbName: dbName,
client: dbClient,
}
}
// GetUpcomingSessions queries the database for Info Sessions starting between not and some time in the future. Returns the upcoming Info Sessions and the email addresses of each session's prospective participants.
func (m *MongoService) GetUpcomingSessions(ctx context.Context, inFuture time.Duration) ([]*UpcomingSession, error) {
sessions := m.client.Database(m.dbName).Collection("sessions")
infoSessionProgID := "5sTmB97DzcqCwEZFR"
filterDate := time.Now().Add(inFuture)
var upcomingSessions []*UpcomingSession
sessCursor, err := sessions.Find(ctx, bson.M{
"programId": infoSessionProgID,
"times.start.dateTime": bson.M{
"$gte": time.Now(),
"$lte": filterDate,
},
})
if err != nil {
return upcomingSessions, fmt.Errorf("sessions.Find: %w", err)
}
if err = sessCursor.All(ctx, &upcomingSessions); err != nil {
return upcomingSessions, fmt.Errorf("sessions cursor.All(): %w", err)
}
// Fetch the attendees
signups := m.client.Database(m.dbName).Collection("signups")
locations := m.client.Database(m.dbName).Collection("locations")
for _, session := range upcomingSessions {
suCur, err := signups.Find(ctx, bson.M{"sessionId": session.ID})
if err != nil {
return upcomingSessions, fmt.Errorf("signups.Find: %w", err)
}
// Get associated Location data
var loc greenlight.Location
res := locations.FindOne(ctx, bson.M{"_id": session.LocationID})
if res.Err() != nil {
return upcomingSessions, fmt.Errorf("locations.findOne: %w\nlocationId: %q", err, session.LocationID)
}
err = res.Decode(&loc)
if err != nil {
return upcomingSessions, fmt.Errorf("decode location: %w", err)
}
var attendees []Participant
if err = suCur.All(ctx, &attendees); err != nil {
return upcomingSessions, fmt.Errorf("signups.cursor.All(): %w", err)
}
for _, p := range attendees {
p.SessionDate = session.Times.Start.DateTime
p.SessionLocationType = session.LocationType
p.SessionLocation = transformLocation(loc)
session.Participants = append(session.Participants, p)
}
}
return upcomingSessions, nil
}
// SendSMSReminders sends an SMS message to each of the attendees in each of the given sessions. Each SMS is sent in it's own goroutine.
func (s *Server) sendSMSReminders(ctx context.Context, sessions []*UpcomingSession, dryRun bool) error {
errs, ctx := errgroup.WithContext(ctx)
for _, session := range sessions {
for _, p := range session.Participants {
// https://stackoverflow.com/questions/40326723/go-vet-range-variable-captured-by-func-literal-when-using-go-routine-inside-of-f
errs.Go(func(p Participant) func() error {
return func() error {
msg, err := reminderMsg(ctx, *session)
if err != nil {
return fmt.Errorf("reminderMsg: %w", err)
}
infoURL, err := s.osMsSvc.CreateMessageURL(p)
if err != nil {
return err
}
// The link will be a long URL even if there is an error
link, err := s.shortySrv.ShortenURL(ctx, infoURL)
if err != nil {
fmt.Printf("Failed to shorten URL: %q\nError: %v\n", infoURL, err)
}
msg = fmt.Sprintf("%s\nMore details: %s", msg, link)
toNum := s.twilioService.FormatCell(p.Cell)
if dryRun {
fmt.Printf("Dry Run Mode: (not sending SMS)\ntoNum: %s\n,msg: %s\n", toNum, msg)
return nil
}
return s.twilioService.Send(ctx, toNum, msg)
}
}(p))
}
}
return errs.Wait()
}
func reminderMsg(ctx context.Context, session UpcomingSession) (string, error) {
tz, ok := ctx.Value(RECIPIENT_TZ).(*time.Location)
if !ok {
return "", errors.New("could not retrieve local timezone from context")
}
day := session.Times.Start.DateTime.In(tz).Format("Monday")
time := session.Times.Start.DateTime.In(tz).Format("3:04PM MST")
date := session.Times.Start.DateTime.In(tz).Format(" 1/2")
if isToday(session.Times.Start.DateTime) {
day = "today"
date = ""
}
return fmt.Sprintf("Hi from Operation Spark! A friendly reminder that you have an Info Session %s%s at %s.", day, date, time), nil
}
// IsToday is checks if the given time is today.
func isToday(date time.Time) bool {
return time.Now().Before(date) && date.Before(time.Now().Add(time.Hour*13))
}
func (r *Request) fromJSON(body io.Reader) error {
d := json.NewDecoder(body)
return d.Decode(r)
}
// Parse parses the Period string ("3 hours") into a time.Duration.
// Acceptable periods are "day(s)", "hour(s)", "minute(s)", "min(s)".
func (p Period) Parse() (time.Duration, error) {
parts := strings.Fields(string(p))
rawVal := parts[0]
val, err := strconv.Atoi(rawVal)
if err != nil {
return time.Duration(0), err
}
if strings.Contains(string(p), "day") {
return time.Hour * 24 * time.Duration(val), nil
}
if strings.Contains(string(p), "hour") {
return time.Hour * time.Duration(val), nil
}
if strings.Contains(string(p), "min") {
return time.Minute * time.Duration(val), nil
}
return time.Duration(0), fmt.Errorf(`invalid period type: %s\nacceptable types are "day(s)", "hour(s)", "minute(s)", "min(s)"`, parts[1])
}
func transformLocation(loc greenlight.Location) Location {
line1, cityStateZip := greenlight.ParseAddress(loc.GooglePlace.Address)
mapURL := greenlight.GoogleLocationLink(loc.GooglePlace.Address)
return Location{
Name: loc.GooglePlace.Name,
Line1: line1,
CityStateZip: cityStateZip,
MapURL: mapURL,
}
}