-
Notifications
You must be signed in to change notification settings - Fork 0
/
members.go
526 lines (449 loc) · 11.6 KB
/
members.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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
package main
import (
"bufio"
"encoding/csv"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"os"
"path/filepath"
"strings"
"github.com/johnamadeo/server"
)
const (
// MaxMemory : Max. amount of memory when parsing multipart form in the HTTP header
MaxMemory = 32 << 20
// CSVPath : Filepath for accessing local CSV files
CSVPath = "./csv/"
// FileFlag :
FileFlag = os.O_WRONLY | os.O_CREATE
// ReadWritePermissions :
ReadWritePermissions = 0666
)
// Member :
type Member struct {
Organization string
Email string `json:"email"`
Name string `json:"name"`
Metadata map[string]string
LastRoundWith map[string]int
}
// MemberResponse : Data structure for representing a member
type MemberResponse map[string]string
// CreateMembersResponse :
type CreateMembersResponse struct {
Members []MemberResponse `json:"members"`
Traits []string `json:"traits"`
}
// GetMembersResponse :
type GetMembersResponse struct {
Members []MemberResponse `json:"members"`
Traits []string `json:"traits"`
CrossMatchTrait string `json:"crossMatchTrait"`
}
// MembersHandler : Combined handlers for create and retrieving members
func MembersHandler(w http.ResponseWriter, r *http.Request) {
if r.Method == "POST" {
CreateMembersHandler(w, r)
} else if r.Method == "GET" {
GetMembersHandler(w, r)
}
}
// GetMembersHandler : HTTP handler for retrieving members
func GetMembersHandler(w http.ResponseWriter, r *http.Request) {
function := "GetMembersHandler"
if r.Method != "GET" {
LogAndWriteErr(
w,
errors.New("Only GET requests are allowed at this route"),
http.StatusMethodNotAllowed,
function,
)
return
}
orgname, err := getQueryParam(r, "org")
if err != nil {
LogAndWriteStatusBadRequest(w, err, function)
return
}
members, err := getActiveMembersFromDBAsMap(orgname)
if err != nil {
LogAndWriteStatusInternalServerError(w, err, function)
return
}
traits := []string{}
if len(members) > 0 {
for trait := range members[0] {
if trait != "name" && trait != "email" {
traits = append(traits, trait)
}
}
}
crossMatchTrait, err := GetCrossMatchTrait(orgname)
if err != nil {
LogAndWriteStatusInternalServerError(w, err, function)
return
}
resp := GetMembersResponse{
Members: members,
Traits: traits,
}
if crossMatchTrait != "" {
resp.CrossMatchTrait = crossMatchTrait
}
bytes, err := json.Marshal(resp)
if err != nil {
LogAndWriteStatusInternalServerError(w, err, function)
return
}
LogAndWrite(w, bytes, http.StatusOK, function)
}
// TODO: The problem is here!
// CreateMembersHandler : HTTP handler for creating members
func CreateMembersHandler(w http.ResponseWriter, r *http.Request) {
function := "CreateMembersHandler"
if r.Method != "POST" {
LogAndWriteErr(
w,
errors.New("Only POST requests are allowed at this route"),
http.StatusMethodNotAllowed,
function,
)
return
}
orgname, err := getQueryParam(r, "org")
if err != nil {
LogAndWriteStatusBadRequest(w, err, function)
return
}
err = r.ParseMultipartForm(MaxMemory)
if err != nil {
LogAndWriteStatusBadRequest(w, err, function)
return
}
formFile, handler, err := r.FormFile("members")
if err != nil {
LogAndWriteStatusBadRequest(w, err, function)
return
}
defer formFile.Close()
filename := filepath.Join(CSVPath, handler.Filename)
file, err := os.OpenFile(filename, FileFlag, ReadWritePermissions)
if err != nil {
LogAndWriteStatusInternalServerError(w, err, function)
return
}
defer file.Close()
_, err = io.Copy(file, formFile)
if err != nil {
LogAndWriteStatusInternalServerError(w, err, function)
return
}
members, err := createMembersFromCSV(orgname, filename)
if err != nil {
LogAndWriteStatusInternalServerError(w, err, function)
return
}
var traits []string
for trait := range members[0] {
if trait != "name" && trait != "email" {
traits = append(traits, trait)
}
}
resp := CreateMembersResponse{
Members: members,
Traits: traits,
}
bytes, err := json.Marshal(resp)
if err != nil {
LogAndWriteStatusInternalServerError(w, err, function)
return
}
LogAndWrite(w, bytes, http.StatusCreated, function)
}
// isValidFormatCSV :
func isValidFormatCSV(headers []string) bool {
for _, val := range headers {
if strings.ToLower(val) == "name" {
return true
}
}
return false
}
func createMembersFromCSV(orgname string, filename string) ([]MemberResponse, error) {
file, err := os.Open(filename)
if err != nil {
return []MemberResponse{}, err
}
reader := csv.NewReader(bufio.NewReader(file))
members := []Member{}
headers := []string{}
for {
row, err := reader.Read()
if err == io.EOF {
break
} else if err != nil {
return []MemberResponse{}, err
}
if len(headers) == 0 {
if isValidFormatCSV(row) {
for _, val := range row {
headers = append(headers, strings.ToLower(val))
}
continue
} else {
err = errors.New("CSV must have a column titled 'name'")
return []MemberResponse{}, err
}
}
name := ""
email := ""
metadata := map[string]string{}
for i, val := range row {
if headers[i] == "name" {
name = strings.Trim(val, " ")
} else if headers[i] == "email" {
email = strings.Trim(val, " ")
} else {
metadata[headers[i]] = val
}
}
members = append(members, Member{
Organization: orgname, // Need to grab from HTTP request
Email: email,
Name: name,
Metadata: metadata,
LastRoundWith: map[string]int{}, // fill in later
})
}
for i := range members {
for j := range members {
if i == j {
continue
}
members[i].LastRoundWith[members[j].Email] = -1
}
}
err = saveMembersInDB(orgname, members)
if err != nil {
return []MemberResponse{}, err
}
var membersJSON []MemberResponse
for _, member := range members {
memberJSON := member.Metadata
memberJSON["name"] = member.Name
memberJSON["email"] = member.Email
membersJSON = append(membersJSON, memberJSON)
}
return membersJSON, nil
}
// TODO: Use transactions!!! Current implementation is brittle since it does
// rollback if insertion of multiple members fails halfway
func saveMembersInDB(orgname string, newMembers []Member) error {
db, err := server.CreateDBConnection(LocalDBConnection)
defer db.Close()
if err != nil {
return err
}
newMembersMap := map[string]Member{}
for _, member := range newMembers {
newMembersMap[member.Email] = member
}
membersMap := map[string]Member{}
members, err := GetMembersFromDB(orgname, false)
if err != nil {
return err
}
for _, member := range members {
membersMap[member.Email] = member
}
// remove existing members that are not in new list
for email := range membersMap {
if _, ok := newMembersMap[email]; !ok {
delete(membersMap, email)
}
}
// update fields of existing member (if existing member changes name or metadata)
for email := range membersMap {
if _, ok := newMembersMap[email]; ok {
membersMap[email] = Member{
Organization: membersMap[email].Organization,
Email: membersMap[email].Email, // cannot be updated by user
LastRoundWith: membersMap[email].LastRoundWith, // update later
Name: newMembersMap[email].Name,
Metadata: newMembersMap[email].Metadata,
}
}
}
// update pair counts of existing members with new members
for email, member := range membersMap {
for newEmail := range newMembersMap {
// if new email is not existing member
if _, ok := membersMap[newEmail]; !ok {
member.LastRoundWith[newEmail] = -1
}
}
membersMap[email] = member
}
// save new member
for email := range newMembersMap {
if _, ok := membersMap[email]; !ok {
lastRoundWith := map[string]int{}
for otherEmail := range newMembersMap {
if otherEmail == email {
continue
}
lastRoundWith[otherEmail] = -1
}
membersMap[email] = Member{
Organization: orgname,
Name: newMembersMap[email].Name,
Email: newMembersMap[email].Email,
Metadata: newMembersMap[email].Metadata,
LastRoundWith: lastRoundWith,
}
}
}
existingMemberEmails := map[string]bool{}
for _, member := range members {
existingMemberEmails[member.Email] = true
}
for _, member := range membersMap {
metadataBytes, err := json.Marshal(member.Metadata)
if err != nil {
return err
}
lastRoundWithBytes, err := json.Marshal(member.LastRoundWith)
if err != nil {
return err
}
// Add new member
if _, ok := existingMemberEmails[member.Email]; !ok {
columns := "(organization, email, name, metadata, last_round_with, active)"
placeholders := "($1, $2, $3, $4, $5, $6)"
_, err = db.Exec(
fmt.Sprintf(
"INSERT INTO members %s VALUES %s",
columns,
placeholders,
),
member.Organization,
member.Email,
member.Name,
server.JSONB(metadataBytes),
server.JSONB(lastRoundWithBytes),
true,
)
if err != nil {
return err
}
// Update existing member
} else {
_, err = db.Exec(
"UPDATE members SET name = $1, metadata = $2, last_round_with = $3, active = $4 WHERE organization = $5 AND email = $6",
member.Name,
server.JSONB(metadataBytes),
server.JSONB(lastRoundWithBytes),
true,
orgname,
member.Email,
)
}
}
// Deactivate member (note we don't delete member from the DB)
for email := range existingMemberEmails {
if _, ok := membersMap[email]; !ok {
_, err = db.Exec(
"UPDATE members SET active = $1 WHERE organization = $2 AND email = $3",
false,
orgname,
email,
)
}
}
return nil
}
func getActiveMembersFromDBAsMap(orgname string) ([]MemberResponse, error) {
members, err := GetMembersFromDB(orgname, true)
if err != nil {
return []MemberResponse{}, err
}
mapMembers := []MemberResponse{}
for _, member := range members {
mapMember := member.Metadata
mapMember["name"] = member.Name
mapMember["email"] = member.Email
mapMembers = append(mapMembers, mapMember)
}
return mapMembers, nil
}
func getActiveMembersFromDBInPairFormat(orgname string) ([]Member, error) {
members, err := GetMembersFromDB(orgname, true)
if err != nil {
return []Member{}, err
}
pairMembers := []Member{}
for _, member := range members {
pairMembers = append(pairMembers, Member{
Name: member.Name,
Email: member.Email,
})
}
return pairMembers, nil
}
// GetMembersFromDB : Placeholder
func GetMembersFromDB(orgname string, onlyActive bool) ([]Member, error) {
db, err := server.CreateDBConnection(LocalDBConnection)
defer db.Close()
if err != nil {
return []Member{}, err
}
members := []Member{}
rows, err := db.Query(
"SELECT organization, name, email, metadata, last_round_with, active FROM members WHERE organization = $1 ORDER BY name",
orgname,
)
if err != nil {
return members, err
}
for rows.Next() {
var organization, name, email string
var metadataJSON, lastRoundWithJSON server.JSONB
var active bool
err := rows.Scan(&organization, &name, &email, &metadataJSON, &lastRoundWithJSON, &active)
if err != nil {
return members, err
}
if onlyActive && !active {
continue
}
bytes, err := metadataJSON.MarshalJSON()
if err != nil {
return members, err
}
var metadata map[string]string
err = json.Unmarshal(bytes, &metadata)
if err != nil {
return members, err
}
bytes, err = lastRoundWithJSON.MarshalJSON()
if err != nil {
return members, err
}
var lastRoundWith map[string]int
err = json.Unmarshal(bytes, &lastRoundWith)
if err != nil {
return members, err
}
members = append(members, Member{
Organization: organization,
Name: name,
Email: email,
Metadata: metadata,
LastRoundWith: lastRoundWith,
})
}
return members, nil
}