-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroutes.go
1563 lines (1347 loc) · 41.1 KB
/
routes.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
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package main
import (
"crypto/hmac"
"crypto/sha256"
"crypto/subtle"
"encoding/base64"
"encoding/hex"
"encoding/json"
"fmt"
"net/http"
"net/url"
"os"
"strconv"
"strings"
"github.com/vibecamp/myvibecamp/db"
"github.com/vibecamp/myvibecamp/fields"
"github.com/cockroachdb/errors"
"github.com/gin-gonic/gin"
log "github.com/sirupsen/logrus"
"github.com/skip2/go-qrcode"
)
func IndexHandler(c *gin.Context) {
session := GetSession(c)
if !session.SignedIn() {
c.HTML(http.StatusOK, "index.html.tmpl", nil)
return
}
findUser(c, session.UserName, false)
}
func VC2Welcome(c *gin.Context) {
session := GetSession(c)
if c.Request.Method == http.MethodGet {
if !session.SignedIn() {
c.HTML(http.StatusOK, "vc2welcome.html.tmpl", nil)
return
}
findUser(c, session.UserName, false)
return
}
// I need to setup session stuff for this - oauth email?
// or just get user by email & handle the rest like their
// email is their twitter
// magic email links? not hard really but need a way to send emails
emailAddr := c.PostForm("email-address")
if !localDevMode && !strings.Contains(emailAddr, "@") {
// throw an error if it's not a valid email on prod
c.AbortWithError(http.StatusBadRequest, errors.New("must enter a valid email address"))
return
}
findUser(c, emailAddr, true)
}
func findUser(c *gin.Context, username string, isEmailSignIn bool) {
user, err := db.GetUser(username)
if err == nil && user != nil {
if isEmailSignIn {
makeEmailSession(c, user.UserName, user.AirtableID)
}
// if they have an order ID, check the order
if len(user.OrderID) > 0 {
order, err := db.GetOrder(user.OrderID)
// if it exists and is not blank payment status, direct them by payment status
if err == nil && order != nil && order.PaymentStatus != "" {
switch order.PaymentStatus {
case "failed":
c.Redirect(http.StatusFound, "/checkout-failed")
return
case "success":
c.Redirect(http.StatusFound, "/vc2-ticket")
return
case "processing":
c.Redirect(http.StatusFound, "/checkout-complete")
return
}
} else {
// otherwise send them based on their ticket path to the cart
if user.TicketPath == "Sponsorship" {
c.Redirect(http.StatusFound, "/sponsorship-cart")
return
} else if user.TicketPath == "FCFS" || user.TicketPath == "Lottery" || user.TicketPath == "Application" || user.TicketPath == fields.LateApp {
c.Redirect(http.StatusFound, "/chaos-cart")
return
} else {
c.Redirect(http.StatusFound, "/ticket-cart")
return
}
}
} else if user.TicketPath == "Sponsorship" {
// if they dont have an order ID, check if they're in sponsorship table. if not, they're a full sponsor
sponsoredUser, err := db.GetSponsorshipUser(username)
if sponsoredUser == nil && err != nil {
c.Redirect(http.StatusFound, "/vc2-ticket")
return
}
} else if user.AdmissionLevel == fields.Staff || user.TicketPath == fields.Volunteer || user.TicketPath == fields.TicketSwap || user.TicketPath == fields.Comped {
// if they don't have an order id check if they're staff
c.Redirect(http.StatusFound, "/vc2-ticket")
return
}
}
// check for sponsorship first, in case they're both on sponsorship and e.g. soft launch
sponsoredUser, err := db.GetSponsorshipUser(username)
if err == nil && sponsoredUser != nil {
if isEmailSignIn {
makeEmailSession(c, sponsoredUser.UserName, sponsoredUser.AirtableID)
}
c.Redirect(http.StatusFound, "/sponsorship-cart")
return
}
// check if they're a soft launch
softLaunchUser, err := db.GetSoftLaunchUser(username)
if err == nil && softLaunchUser != nil {
if isEmailSignIn {
makeEmailSession(c, softLaunchUser.UserName, softLaunchUser.AirtableID)
}
c.Redirect(http.StatusFound, "/vc2-sl")
return
}
// check if they're chaos user
chaosUser, err := db.GetChaosUser(username)
if err == nil && chaosUser != nil {
if isEmailSignIn {
makeEmailSession(c, chaosUser.UserName, chaosUser.AirtableID)
}
c.Redirect(http.StatusFound, "/chaos-mode")
return
}
// abort
c.AbortWithError(http.StatusBadRequest, err)
}
func makeEmailSession(c *gin.Context, username string, id string) {
session := GetSession(c)
session.UserName = username
session.TwitterName = username
session.TwitterID = id
session.Oauth = nil
SaveSession(c, session)
}
func CalendarHandler(c *gin.Context) {
session := GetSession(c)
if !session.SignedIn() {
c.HTML(http.StatusOK, "calendar.html.tmpl", nil)
return
}
user, err := db.GetUser(session.TwitterName)
if err != nil {
c.AbortWithError(http.StatusBadRequest, err)
return
}
c.HTML(http.StatusOK, "calendar.html.tmpl", user)
}
func VC2TicketHandler(c *gin.Context) {
session := GetSession(c)
if !session.SignedIn() {
c.Redirect(http.StatusFound, "/")
return
}
user, err := db.GetUser(session.TwitterName)
if err != nil {
c.AbortWithError(http.StatusBadRequest, err)
return
}
if user.TicketID != "" && !strings.Contains(user.TicketID, "manual") {
qr, err := qrcode.Encode(fmt.Sprintf(`%s/checkin/%s`, externalURL, user.TicketID), qrcode.Medium, 256)
if err != nil {
c.AbortWithError(http.StatusInternalServerError, err)
return
}
c.HTML(http.StatusOK, "vibecamp2.html.tmpl", gin.H{
"QR": base64.StdEncoding.EncodeToString(qr),
"user": user,
})
} else {
c.HTML(http.StatusOK, "vibecamp2.html.tmpl", gin.H{
"user": user,
})
}
}
func Transport2023Handler(c *gin.Context) {
session := GetSession(c)
if !session.SignedIn() {
c.Redirect(http.StatusFound, "/")
return
}
_, err := db.GetUser(session.UserName)
if err != nil {
c.AbortWithError(http.StatusBadRequest, err)
return
}
if c.Request.Method == "POST" {
busQuantity, _ := strconv.Atoi(c.PostForm("busQuantity"))
busToVibecamp := c.PostForm("busArriveTime")
busFromVibecamp := c.PostForm("busDepartTime")
sleepingBags := 0
pillows := 0
sheetSets := 0
if busQuantity > 0 {
if busToVibecamp == "" && busFromVibecamp == "" {
c.AbortWithError(http.StatusBadRequest, errors.New("must select a bus slot"))
return
}
if busToVibecamp != "" {
bs, err := db.GetSlot(busToVibecamp)
if err != nil {
c.AbortWithError(http.StatusBadRequest, err)
return
}
if bs.Purchased+busQuantity > bs.Cap {
c.AbortWithError(http.StatusBadRequest, errors.New("bus slot is full"))
return
}
}
if busFromVibecamp != "" {
bs, err := db.GetSlot(busFromVibecamp)
if err != nil {
c.AbortWithError(http.StatusBadRequest, err)
return
}
if bs.Purchased+busQuantity > bs.Cap {
c.AbortWithError(http.StatusBadRequest, errors.New("bus slot is full"))
return
}
}
} else {
busToVibecamp = ""
busFromVibecamp = ""
}
params := url.Values{}
params.Set("busQuantity", strconv.Itoa(busQuantity))
params.Set("sleepingBags", strconv.Itoa(sleepingBags))
params.Set("pillows", strconv.Itoa(pillows))
params.Set("sheetSets", strconv.Itoa(sheetSets))
params.Set("busToVibecamp", busToVibecamp)
params.Set("busFromVibecamp", busFromVibecamp)
c.Redirect(http.StatusFound, "/transport-checkout"+"?"+params.Encode())
return
}
c.HTML(http.StatusOK, "transport2023.html.tmpl", gin.H{})
}
func TransportCheckoutHandler(c *gin.Context) {
session := GetSession(c)
if !session.SignedIn() {
c.Redirect(http.StatusFound, "/")
return
}
user, err := db.GetUser(session.UserName)
if err != nil {
c.AbortWithError(http.StatusBadRequest, err)
return
}
busSpots, _ := strconv.Atoi(c.Query("busQuantity"))
busToVibecamp := c.Query("busToVibecamp")
busFromVibecamp := c.Query("busFromVibecamp")
sleepingBags, _ := strconv.Atoi(c.Query("sleepingBags"))
pillows, _ := strconv.Atoi(c.Query("pillows"))
sheetSets, _ := strconv.Atoi(c.Query("sheetSets"))
items := struct {
BusSpots int `json:"busSpots"`
BusToVibecamp string `json:"busToVibecamp"`
BusFromVibecamp string `json:"busFromVibecamp"`
SleepingBags int `json:"sleepingBags"`
SheetSets int `json:"sheetSets"`
Pillows int `json:"pillows"`
}{
BusSpots: busSpots,
BusToVibecamp: busToVibecamp,
BusFromVibecamp: busFromVibecamp,
SleepingBags: sleepingBags,
SheetSets: sheetSets,
Pillows: pillows,
}
// log.Debugf("%v", itemMap)
itemJson, err := json.Marshal(items)
if err != nil {
log.Errorf("%v", err)
c.AbortWithError(http.StatusInternalServerError, err)
return
}
// log.Debugf(string(itemJson))
c.HTML(http.StatusOK, "transportCheckout.html.tmpl", gin.H{
"User": user,
"Items": string(itemJson),
})
}
func TicketCartHandler(c *gin.Context) {
session := GetSession(c)
if !session.SignedIn() {
c.Redirect(http.StatusFound, "/")
return
}
user, err := db.GetSoftLaunchUser(session.UserName)
if err != nil {
c.AbortWithError(http.StatusBadRequest, err)
return
}
if user.TicketLimit < 1 {
c.HTML(http.StatusOK, "ticketSalesClosed.html.tmpl", gin.H{
"flashes": GetFlashes(c),
})
return
}
attendee, err := db.GetUser(session.UserName)
if err == nil && attendee != nil {
if attendee.OrderID != "" {
order, err := db.GetOrder(attendee.OrderID)
if err == nil && order != nil && order.PaymentStatus == "success" {
c.Redirect(http.StatusFound, "/2023-logistics")
return
}
}
}
if c.Request.Method == http.MethodGet {
c.HTML(http.StatusOK, "ticketCart.html.tmpl", gin.H{
"flashes": GetFlashes(c),
"User": user,
})
return
}
ticketType := c.PostForm("ticket-type")
adultTix, _ := strconv.Atoi(c.PostForm("adult-tickets"))
childTix, _ := strconv.Atoi(c.PostForm("child-tickets"))
toddlerTix, _ := strconv.Atoi(c.PostForm("toddler-tickets"))
donationAmount, _ := strconv.Atoi(c.PostForm("donation-amount"))
if adultTix > user.TicketLimit {
ErrorFlash(c, fmt.Sprintf("You're limited to %d ticket in the soft launch", user.TicketLimit))
return
}
totalTix := adultTix + childTix + toddlerTix
dbTicketType := ""
if adultTix > 0 {
dbTicketType = "Adult"
} else if childTix > 0 {
dbTicketType = "Child"
} else if toddlerTix > 0 {
dbTicketType = "Toddler"
}
var admissionLevel string
if ticketType == "cabin" {
admissionLevel = "Cabin"
cabinCap, err := db.GetConstant(fields.SoftCabinCap)
if err != nil {
c.AbortWithError(http.StatusInternalServerError, err)
return
}
cabinSold, err := db.GetAggregation(fields.CabinSold)
if err != nil {
c.AbortWithError(http.StatusInternalServerError, err)
return
}
if totalTix+cabinSold.Quantity > cabinCap.Value {
ErrorFlash(c, fmt.Sprintf("Sorry, buying that many cabin tickets exceeds our cap! %d cabin tickets left.", cabinCap.Value-cabinSold.Quantity))
return
}
} else if ticketType == "tent" {
admissionLevel = "Tent"
} else if ticketType == "sat" {
admissionLevel = "Saturday Night"
}
if ticketType != "sat" {
salesCap, err := db.GetConstant(fields.SalesCap)
if err != nil {
c.AbortWithError(http.StatusInternalServerError, err)
return
}
fullTixSold, err := db.GetAggregation(fields.FullSold)
if err != nil {
c.AbortWithError(http.StatusInternalServerError, err)
return
}
if totalTix+fullTixSold.Quantity > salesCap.Value {
ErrorFlash(c, fmt.Sprintf("Sorry, buying that many tickets exceeds our cap! %d tickets left.", salesCap.Value-fullTixSold.Quantity))
return
}
} else {
satCap, err := db.GetConstant(fields.SatCap)
if err != nil {
c.AbortWithError(http.StatusInternalServerError, err)
return
}
satSold, err := db.GetAggregation(fields.SatSold)
if err != nil {
c.AbortWithError(http.StatusInternalServerError, err)
return
}
if totalTix+satSold.Quantity > satCap.Value {
ErrorFlash(c, fmt.Sprintf("Sorry, buying that many tickets exceeds our Saturday night cap! %d Saturday tickets left.", satCap.Value-satSold.Quantity))
return
}
}
newUser := &db.User{
AirtableID: "",
UserName: user.UserName,
TwitterName: user.TwitterName,
Name: user.Name,
Email: user.Email,
Cabin2022: user.Cabin2022,
AdmissionLevel: admissionLevel,
TicketType: dbTicketType,
CheckedIn: false,
Barcode: "",
OrderNotes: "",
OrderID: "",
TicketID: "",
Badge: c.PostForm("badge-checkbox") == "on",
Vegetarian: c.PostForm("vegetarian") == "on",
GlutenFree: c.PostForm("glutenfree") == "on",
LactoseIntolerant: c.PostForm("lactose") == "on",
FoodComments: c.PostForm("comments"),
DiscordName: c.PostForm("discord-name"),
TicketPath: "2022 Attendee",
SponsorshipConfirm: false,
}
if attendee != nil {
newUser.OrderID = attendee.OrderID
newUser.AirtableID = attendee.AirtableID
err = newUser.UpdateUser()
if err != nil {
c.AbortWithError(http.StatusBadRequest, err)
return
}
} else {
err = newUser.CreateUser()
if err != nil {
c.AbortWithError(http.StatusBadRequest, err)
return
}
}
params := url.Values{}
params.Set("ticketType", ticketType)
params.Set("adult", strconv.Itoa(adultTix))
params.Set("child", strconv.Itoa(childTix))
params.Set("toddler", strconv.Itoa(toddlerTix))
params.Set("donation", strconv.Itoa(donationAmount))
c.Redirect(http.StatusFound, "/checkout"+"?"+params.Encode())
}
func SponsorshipCartHandler(c *gin.Context) {
session := GetSession(c)
if !session.SignedIn() {
c.Redirect(http.StatusFound, "/")
return
}
user, err := db.GetSponsorshipUser(session.UserName)
if err != nil {
c.AbortWithError(http.StatusBadRequest, err)
return
}
if user.TicketLimit < 1 {
c.HTML(http.StatusOK, "ticketSalesClosed.html.tmpl", gin.H{
"flashes": GetFlashes(c),
})
return
}
attendee, err := db.GetUser(session.UserName)
if err == nil && attendee != nil {
if attendee.OrderID != "" {
order, err := db.GetOrder(attendee.OrderID)
if err == nil && order != nil && order.PaymentStatus == "success" {
c.Redirect(http.StatusFound, "/checkout-complete")
return
}
// add pending status page?
}
}
adultTix := 1
dbTicketType := "Adult"
admissionLevel := user.AdmissionLevel
var basePrice float64
var ticketType string
if admissionLevel == "Tent" {
basePrice = float64(420.69)
ticketType = "tent"
salesCap, err := db.GetConstant(fields.SalesCap)
if err != nil {
c.AbortWithError(http.StatusInternalServerError, err)
return
}
fullTixSold, err := db.GetAggregation(fields.FullSold)
if err != nil {
c.AbortWithError(http.StatusInternalServerError, err)
return
}
if adultTix+fullTixSold.Quantity > salesCap.Value {
ErrorFlash(c, fmt.Sprintf("Sorry, buying that many tickets exceeds our cap! %d tickets left.", salesCap.Value-fullTixSold.Quantity))
return
}
} else {
basePrice = float64(140)
ticketType = "sat"
satCap, err := db.GetConstant(fields.SatCap)
if err != nil {
c.AbortWithError(http.StatusInternalServerError, err)
return
}
satTixSold, err := db.GetAggregation(fields.SatSold)
if err != nil {
c.AbortWithError(http.StatusInternalServerError, err)
return
}
if adultTix+satTixSold.Quantity > satCap.Value {
ErrorFlash(c, fmt.Sprintf("Sorry, buying that many tickets exceeds our cap! %d tickets left.", satCap.Value-satTixSold.Quantity))
return
}
}
subtotalFloat := basePrice - user.Discount.ToFloat()
feeFloat := subtotalFloat * float64(0.03)
subtotal := db.CurrencyFromFloat(subtotalFloat)
total := db.CurrencyFromFloat(subtotalFloat + feeFloat)
fee := db.CurrencyFromFloat(feeFloat)
if c.Request.Method == http.MethodGet {
c.HTML(http.StatusOK, "sponsorshipCart.html.tmpl", gin.H{
"flashes": GetFlashes(c),
"User": user,
"Total": total,
"Fee": fee,
"Subtotal": subtotal,
})
return
}
newUser := &db.User{
AirtableID: "",
UserName: user.UserName,
TwitterName: user.TwitterName,
Name: user.Name,
Email: user.Email,
AdmissionLevel: admissionLevel,
TicketType: dbTicketType,
CheckedIn: false,
Barcode: "",
OrderNotes: "",
OrderID: "",
TicketID: "",
SponsorshipConfirm: false,
Badge: c.PostForm("badge-checkbox") == "on",
Vegetarian: c.PostForm("vegetarian") == "on",
GlutenFree: c.PostForm("glutenfree") == "on",
LactoseIntolerant: c.PostForm("lactose") == "on",
FoodComments: c.PostForm("comments"),
DiscordName: c.PostForm("discord-name"),
TicketPath: "Sponsorship",
}
if attendee != nil {
newUser.OrderID = attendee.OrderID
newUser.AirtableID = attendee.AirtableID
err = newUser.UpdateUser()
if err != nil {
c.AbortWithError(http.StatusBadRequest, err)
return
}
} else {
err = newUser.CreateUser()
if err != nil {
c.AbortWithError(http.StatusBadRequest, err)
return
}
}
params := url.Values{}
params.Set("ticketType", ticketType)
params.Set("adult", strconv.Itoa(adultTix))
params.Set("child", "0")
params.Set("toddler", "0")
params.Set("donation", "0")
c.Redirect(http.StatusFound, "/checkout"+"?"+params.Encode())
}
func SoftLaunchSignIn(c *gin.Context) {
session := GetSession(c)
if c.Request.Method == http.MethodGet {
if !session.SignedIn() {
c.HTML(http.StatusOK, "softLaunchSignIn.html.tmpl", nil)
return
}
user, err := db.GetSoftLaunchUser(session.UserName)
if err != nil {
c.AbortWithError(http.StatusBadRequest, err)
return
}
if user.TicketLimit < 1 {
c.HTML(http.StatusOK, "ticketSalesClosed.html.tmpl", gin.H{
"flashes": GetFlashes(c),
})
return
}
c.HTML(http.StatusOK, "softLaunchSignIn.html.tmpl", user)
return
}
// I need to setup session stuff for this - oauth email?
// or just get user by email & handle the rest like their
// email is their twitter
// magic email links? not hard really but need a way to send emails
emailAddr := c.PostForm("email-address")
if !localDevMode && !strings.Contains(emailAddr, "@") {
// throw an error if it's not a valid email on prod
c.AbortWithError(http.StatusBadRequest, errors.New("must enter a valid email address"))
return
}
// get user by email somehow
user, err := db.GetSoftLaunchUser(emailAddr)
// then return the same page
if err != nil {
c.AbortWithError(http.StatusBadRequest, err)
return
}
session.UserName = user.UserName
session.TwitterName = user.UserName
session.TwitterID = user.AirtableID
session.Oauth = nil
SaveSession(c, session)
c.HTML(http.StatusOK, "softLaunchSignIn.html.tmpl", user)
}
func ChaosModeSignIn(c *gin.Context) {
session := GetSession(c)
if c.Request.Method == http.MethodGet {
if !session.SignedIn() {
c.HTML(http.StatusOK, "chaosSignIn.html.tmpl", nil)
return
}
user, err := db.GetChaosUser(session.UserName)
if err != nil {
c.AbortWithError(http.StatusBadRequest, err)
return
}
c.HTML(http.StatusOK, "chaosSignIn.html.tmpl", user)
return
}
emailAddr := c.PostForm("email-address")
if !localDevMode && !strings.Contains(emailAddr, "@") {
// throw an error if it's not a valid email on prod
c.AbortWithError(http.StatusBadRequest, errors.New("must enter a valid email address"))
return
}
// get user by email somehow
user, err := db.GetChaosUser(emailAddr)
// then return the same page
if err != nil {
c.AbortWithError(http.StatusBadRequest, err)
return
}
session.UserName = user.UserName
session.TwitterName = user.UserName
session.TwitterID = user.AirtableID
session.Oauth = nil
SaveSession(c, session)
if user.TicketLimit < 1 {
c.HTML(http.StatusOK, "ticketSalesClosed.html.tmpl", gin.H{
"flashes": GetFlashes(c),
})
return
}
c.HTML(http.StatusOK, "chaosSignIn.html.tmpl", user)
}
func ChaosModeCartHandler(c *gin.Context) {
session := GetSession(c)
if !session.SignedIn() {
c.Redirect(http.StatusFound, "/")
return
}
user, err := db.GetChaosUser(session.UserName)
if err != nil {
c.AbortWithError(http.StatusBadRequest, err)
return
}
if user.TicketLimit < 1 {
c.HTML(http.StatusOK, "ticketSalesClosed.html.tmpl", gin.H{
"flashes": GetFlashes(c),
})
return
}
attendee, err := db.GetUser(session.UserName)
if err == nil && attendee != nil {
if attendee.OrderID != "" {
order, err := db.GetOrder(attendee.OrderID)
if err == nil && order != nil && order.PaymentStatus == "success" {
c.Redirect(http.StatusFound, "/checkout-complete")
return
}
}
}
if c.Request.Method == http.MethodGet {
c.HTML(http.StatusOK, "hardLaunchCart.html.tmpl", gin.H{
"flashes": GetFlashes(c),
"User": user,
})
return
}
ticketType := c.PostForm("ticket-type")
adultTix, _ := strconv.Atoi(c.PostForm("adult-tickets"))
childTix, _ := strconv.Atoi(c.PostForm("child-tickets"))
toddlerTix, _ := strconv.Atoi(c.PostForm("toddler-tickets"))
donationAmount, _ := strconv.Atoi(c.PostForm("donation-amount"))
if adultTix > user.TicketLimit {
log.Errorf("user ticket limit exceeded %d", adultTix)
ErrorFlash(c, fmt.Sprintf("You're limited to %d adult tickets", user.TicketLimit))
return
}
totalTix := adultTix + childTix + toddlerTix
dbTicketType := ""
if adultTix > 0 {
dbTicketType = "Adult"
} else if childTix > 0 {
dbTicketType = "Child"
} else if toddlerTix > 0 {
dbTicketType = "Toddler"
}
var admissionLevel string
if ticketType == "cabin" {
admissionLevel = "Cabin"
cabinCap, err := db.GetConstant(fields.CabinCap)
if err != nil {
c.AbortWithError(http.StatusInternalServerError, err)
return
}
cabinSold, err := db.GetAggregation(fields.CabinSold)
if err != nil {
c.AbortWithError(http.StatusInternalServerError, err)
return
}
if totalTix+cabinSold.Quantity > cabinCap.Value {
log.Errorf("cabin ticket limit exceeded %d", totalTix+cabinSold.Quantity)
ErrorFlash(c, fmt.Sprintf("Sorry, buying that many cabin tickets exceeds our cap! %d cabin tickets left.", cabinCap.Value-cabinSold.Quantity))
return
}
} else if ticketType == "tent" {
admissionLevel = "Tent"
} else if ticketType == "sat" {
admissionLevel = "Saturday Night"
}
if ticketType != "sat" {
salesCap, err := db.GetConstant(fields.SalesCap)
if err != nil {
c.AbortWithError(http.StatusInternalServerError, err)
return
}
fullTixSold, err := db.GetAggregation(fields.FullSold)
if err != nil {
c.AbortWithError(http.StatusInternalServerError, err)
return
}
if totalTix+fullTixSold.Quantity > salesCap.Value {
log.Errorf("total ticket limit exceeded %d", totalTix+fullTixSold.Quantity)
ErrorFlash(c, fmt.Sprintf("Sorry, buying that many tickets exceeds our cap! %d tickets left.", salesCap.Value-fullTixSold.Quantity))
return
}
} else {
satCap, err := db.GetConstant(fields.SatCap)
if err != nil {
c.AbortWithError(http.StatusInternalServerError, err)
return
}
satSold, err := db.GetAggregation(fields.SatSold)
if err != nil {
c.AbortWithError(http.StatusInternalServerError, err)
return
}
if totalTix+satSold.Quantity > satCap.Value {
log.Errorf("saturday ticket limit exceeded %d", totalTix+satSold.Quantity)
ErrorFlash(c, fmt.Sprintf("Sorry, buying that many tickets exceeds our Saturday night cap! %d Saturday tickets left.", satCap.Value-satSold.Quantity))
return
}
}
newUser := &db.User{
AirtableID: "",
UserName: user.UserName,
TwitterName: user.TwitterName,
Name: user.Name,
Email: user.Email,
AdmissionLevel: admissionLevel,
TicketType: dbTicketType,
CheckedIn: false,
Barcode: "",
OrderNotes: "",
OrderID: "",
TicketID: "",
SponsorshipConfirm: false,
Badge: c.PostForm("badge-checkbox") == "on",
Vegetarian: c.PostForm("vegetarian") == "on",
GlutenFree: c.PostForm("glutenfree") == "on",
LactoseIntolerant: c.PostForm("lactose") == "on",
FoodComments: c.PostForm("comments"),
DiscordName: c.PostForm("discord-name"),
TicketPath: user.Phase,
}
if attendee != nil {
newUser.OrderID = attendee.OrderID
newUser.AirtableID = attendee.AirtableID
err = newUser.UpdateUser()
if err != nil {
c.AbortWithError(http.StatusBadRequest, err)
return
}
} else {
err = newUser.CreateUser()
if err != nil {
c.AbortWithError(http.StatusBadRequest, err)
return
}
}
params := url.Values{}
params.Set("ticketType", ticketType)
params.Set("adult", strconv.Itoa(adultTix))
params.Set("child", strconv.Itoa(childTix))
params.Set("toddler", strconv.Itoa(toddlerTix))
params.Set("donation", strconv.Itoa(donationAmount))
c.Redirect(http.StatusFound, "/checkout"+"?"+params.Encode())
}
func SignInRedirect(c *gin.Context) {
session := GetSession(c)
if !session.SignedIn() {
c.Redirect(http.StatusFound, "/")
return
}
findUser(c, session.UserName, false)
}
func StripeCheckoutHandler(c *gin.Context) {
session := GetSession(c)
if !session.SignedIn() {
c.Redirect(http.StatusFound, "/")
return
}
user, err := db.GetUser(session.UserName)
if err != nil {
c.AbortWithError(http.StatusBadRequest, err)
return
}
ticketIds := []string{"adult", "child", "toddler", "donation"}
ticketType := c.Query("ticketType")
var items []db.Item
for ind, element := range ticketIds {
amt, _ := strconv.Atoi(c.Query(element))
if amt > 0 {
if ind < 3 {
items = append(items, db.Item{
Id: element + "-" + ticketType,
Quantity: amt,
Amount: 0,
})
} else {
items = append(items, db.Item{
Id: ticketIds[ind],
Quantity: 1,
Amount: amt,
})
}
}
}
// log.Debugf("%v", items)
itemMap := struct {
Items []db.Item `json:"items"`
}{
Items: items,
}
// log.Debugf("%v", itemMap)
itemJson, err := json.Marshal(itemMap)
if err != nil {
log.Errorf("%v", err)
c.AbortWithError(http.StatusInternalServerError, err)
return
}
// log.Debugf(string(itemJson))
c.HTML(http.StatusOK, "checkout.html.tmpl", gin.H{
"User": user,
"Items": string(itemJson),
"OrderType": "Purchase",
"UserType": user.TicketPath,
})
}
func PurchaseCompleteHandler(c *gin.Context) {
session := GetSession(c)
if !session.SignedIn() {
c.Redirect(http.StatusFound, "/")
return
}