Skip to content

Commit

Permalink
Merge branch 'main' into 848-feat-implement-rsvp-and-waitlist
Browse files Browse the repository at this point in the history
  • Loading branch information
garrettladley authored Jun 13, 2024
2 parents 3cc32f2 + 83ced17 commit 7ea15ec
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 94 deletions.
20 changes: 1 addition & 19 deletions backend/entities/auth/base/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import (
"context"
"log/slog"
"net/http"
"net/url"
"time"

"github.com/GenerateNU/sac/backend/integrations/oauth/soth"
"github.com/GenerateNU/sac/backend/integrations/oauth/soth/sothic"
Expand Down Expand Up @@ -72,17 +70,6 @@ func (h *Handler) Provider(c *fiber.Ctx) error {
}

func (h *Handler) ProviderCallback(c *fiber.Ctx) error {
defer func() {
c.Cookie(&fiber.Cookie{
Name: "redirect",
Value: "",
Expires: time.Now().Add(-1 * time.Hour), // expire the cookie immediately
// MARK: secure should be true in prod
// use go build tags to do this
HTTPOnly: true,
})
}()

gfUser, err := sothic.CompleteUserAuth(c)
if err != nil {
return err
Expand All @@ -102,12 +89,7 @@ func (h *Handler) ProviderCallback(c *fiber.Ctx) error {
return err
}

redirect, err := url.PathUnescape(c.Cookies("redirect", "/"))
if err != nil {
return err
}

return c.Redirect(redirect)
return c.SendStatus(http.StatusOK)
}

func (h *Handler) ProviderLogout(c *fiber.Ctx) error {
Expand Down
2 changes: 1 addition & 1 deletion backend/entities/events/previews/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func NewService(serviceParams types.ServiceParams) Service {

func (h *Handler) GetEventPreviews(pageInfo *fiberpaginate.PageInfo, start string, end string) ([]EventPreview, error) {
if start == "" || end == "" {
return GetEventPreivews(h.DB, pageInfo)
return GetEventPreviews(h.DB, pageInfo)
}

startTime, err := utilities.ParseTime(start, utilities.YYYY_dash_MM_dash_DD)
Expand Down
66 changes: 46 additions & 20 deletions backend/entities/events/previews/transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@ import (
"time"

"github.com/garrettladley/fiberpaginate"
"github.com/google/uuid"

"github.com/GenerateNU/sac/backend/database/cache"
"github.com/GenerateNU/sac/backend/entities/clubs"
"github.com/GenerateNU/sac/backend/entities/events/tags"
"github.com/GenerateNU/sac/backend/entities/models"
"github.com/GenerateNU/sac/backend/utilities"

"gorm.io/gorm"
)

func GetEventPreivews(db *gorm.DB, pageInfo *fiberpaginate.PageInfo) ([]EventPreview, error) {
func GetEventPreviews(db *gorm.DB, pageInfo *fiberpaginate.PageInfo) ([]EventPreview, error) {
db = cache.SetUseCache(db, true)

var events []EventStub
Expand All @@ -24,19 +24,16 @@ func GetEventPreivews(db *gorm.DB, pageInfo *fiberpaginate.PageInfo) ([]EventPre
return processEventStubs(db, events)
}

func GetEventPreviewsByTime(db *gorm.DB, pageInfo *fiberpaginate.PageInfo, startTime *time.Time, endTime *time.Time) ([]EventPreview, error) {
var events []EventStub
func GetEventPreviewsByTime(db *gorm.DB, pageInfo *fiberpaginate.PageInfo, startTime, endTime *time.Time) ([]EventPreview, error) {
db = cache.SetUseCache(db, true)

query := cache.SetUseCache(db, true).Model(&events)
var events []EventStub
query := db.Model(&events).Scopes(utilities.IntoScope(*pageInfo, db))

if startTime != nil && endTime != nil {
query = query.Where("start_time >= ? AND end_time <= ?", *startTime, *endTime)
}

if pageInfo != nil {
query = query.Scopes(utilities.IntoScope(*pageInfo, db))
}

if err := query.Find(&events).Error; err != nil {
return nil, err
}
Expand All @@ -45,19 +42,48 @@ func GetEventPreviewsByTime(db *gorm.DB, pageInfo *fiberpaginate.PageInfo, start
}

func processEventStubs(db *gorm.DB, events []EventStub) ([]EventPreview, error) {
eventPreviews := make([]EventPreview, len(events))
eventIDs := make([]uuid.UUID, len(events))
for index, event := range events {
eventTags, err := tags.GetEventTags(db, event.ID)
if err != nil {
return nil, err
}
eventIDs[index] = event.ID
}

var eventTags []struct {
EventID uuid.UUID `gorm:"column:event_id"`
TagID uuid.UUID `gorm:"column:tag_id"`
Name string `gorm:"column:name"`
}
if err := db.Table("tags").Select("tags.id as tag_id, tags.name, event_tags.event_id").
Joins("JOIN event_tags ON event_tags.tag_id = tags.id").
Where("event_tags.event_id IN ?", eventIDs).
Scan(&eventTags).Error; err != nil {
return nil, err
}

eventTagsMap := make(map[uuid.UUID][]models.Tag)
for _, et := range eventTags {
eventTagsMap[et.EventID] = append(eventTagsMap[et.EventID],
models.Tag{
Model: models.Model{
ID: et.TagID,
},
Name: et.Name,
})
}

var hosts []models.Club
if err := db.Where("id IN ?", eventIDs).Find(&hosts).Error; err != nil {
return nil, err
}

host, err := clubs.GetClub(db, event.Host)
if err != nil {
return nil, err
}
hostsMap := make(map[uuid.UUID]models.Club)
for _, host := range hosts {
hostsMap[host.ID] = host
}

eventPreviews[index] = *event.Into(eventTags, host.Name, host.Logo)
eventPreviews := make([]EventPreview, len(events))
for index, event := range events {
eventPreviews[index] = *event.Into(eventTagsMap[event.ID], hostsMap[event.Host].Name, hostsMap[event.Host].Logo)
}

return eventPreviews, nil
}
12 changes: 3 additions & 9 deletions backend/entities/events/tags/transactions.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package tags

import (
"github.com/GenerateNU/sac/backend/database/cache"
"github.com/GenerateNU/sac/backend/entities/events"

"github.com/GenerateNU/sac/backend/entities/models"
Expand All @@ -26,15 +25,10 @@ func CreateEventTags(db *gorm.DB, id uuid.UUID, tags []models.Tag) ([]models.Tag
}

func GetEventTags(db *gorm.DB, id uuid.UUID) ([]models.Tag, error) {
db = cache.SetUseCache(db, true)

var tags []models.Tag
event, err := events.GetEvent(db, id, transactions.PreloadTag())
if err != nil {
return nil, err
}

if err := db.Model(&event).Association("Tag").Find(&tags); err != nil {
if err := db.Joins("JOIN event_tags ON event_tags.tag_id = tags.id").
Where("event_tags.event_id = ?", id).
Find(&tags).Error; err != nil {
return nil, err
}
return tags, nil
Expand Down
4 changes: 3 additions & 1 deletion backend/integrations/oauth/soth/sothic/sothic.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"fmt"
"io"
"log/slog"
"net/http"
"net/url"
"strings"

Expand Down Expand Up @@ -72,7 +73,8 @@ func BeginAuthHandler(c *fiber.Ctx) error {
return c.Status(fiber.StatusBadRequest).SendString(err.Error())
}

return c.Redirect(url, fiber.StatusTemporaryRedirect)
c.Set("redirect", url)
return c.SendStatus(http.StatusOK)
}

// SetState sets the state string associated with the given request.
Expand Down
15 changes: 3 additions & 12 deletions backend/middleware/auth/authorize.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package auth

import (
"net/url"
"net/http"
"slices"
"time"

"github.com/GenerateNU/sac/backend/entities/models"
"github.com/GenerateNU/sac/backend/integrations/oauth/soth/sothic"
Expand All @@ -18,16 +17,8 @@ func (m *AuthMiddlewareHandler) Authorize(requiredPermissions ...permission.Perm
return func(c *fiber.Ctx) error {
strUser, err := sothic.GetFromSession("user", c)
if err != nil {
c.Cookie(&fiber.Cookie{
Name: "redirect",
Value: url.PathEscape(c.OriginalURL()),
Expires: time.Now().Add(5 * time.Minute),
// MARK: secure should be true in prod
// use go build tags to do this
HTTPOnly: true,
})

return c.Redirect("/api/v1/auth/login")
c.Set("redirect", "/api/v1/auth/login")
return c.SendStatus(http.StatusUnauthorized)
}

user := models.UnmarshalUser(strUser)
Expand Down
13 changes: 3 additions & 10 deletions backend/middleware/auth/club.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package auth

import (
"net/http"
"slices"
"time"

"github.com/GenerateNU/sac/backend/entities/clubs"
"github.com/GenerateNU/sac/backend/entities/models"
Expand All @@ -16,15 +16,8 @@ import (
func (m *AuthMiddlewareHandler) ClubAuthorizeById(c *fiber.Ctx, extractor ExtractID) error {
strUser, err := sothic.GetFromSession("user", c)
if err != nil {
c.Cookie(&fiber.Cookie{
Name: "redirect",
Value: c.OriginalURL(),
Expires: time.Now().Add(5 * time.Minute),
// MARK: secure should be true in prod
// use go build tags to do this
HTTPOnly: true,
})
return c.Redirect("/api/v1/auth/login")
c.Set("redirect", "/api/v1/auth/login")
return c.SendStatus(http.StatusUnauthorized)
}

user := models.UnmarshalUser(strUser)
Expand Down
14 changes: 3 additions & 11 deletions backend/middleware/auth/event.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package auth

import (
"net/http"
"slices"
"time"

"github.com/GenerateNU/sac/backend/entities/events"
"github.com/GenerateNU/sac/backend/entities/models"
Expand All @@ -17,16 +17,8 @@ import (
func (m *AuthMiddlewareHandler) EventAuthorizeById(c *fiber.Ctx, extractor ExtractID) error {
strUser, err := sothic.GetFromSession("user", c)
if err != nil {
c.Cookie(&fiber.Cookie{
Name: "redirect",
Value: c.OriginalURL(),
Expires: time.Now().Add(5 * time.Minute),
// MARK: secure should be true in prod
// use go build tags to do this
HTTPOnly: true,
})

return c.Redirect("/api/v1/auth/login")
c.Set("redirect", "/api/v1/auth/login")
return c.SendStatus(http.StatusUnauthorized)
}

user := models.UnmarshalUser(strUser)
Expand Down
14 changes: 3 additions & 11 deletions backend/middleware/auth/user.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package auth

import (
"time"
"net/http"

"github.com/GenerateNU/sac/backend/entities/models"
"github.com/GenerateNU/sac/backend/integrations/oauth/soth/sothic"
Expand All @@ -13,16 +13,8 @@ import (
func (m *AuthMiddlewareHandler) UserAuthorizeById(c *fiber.Ctx) error {
strUser, err := sothic.GetFromSession("user", c)
if err != nil {
c.Cookie(&fiber.Cookie{
Name: "redirect",
Value: c.OriginalURL(),
Expires: time.Now().Add(5 * time.Minute),
// MARK: secure should be true in prod
// use go build tags to do this
HTTPOnly: true,
})

return c.Redirect("/api/v1/auth/login")
c.Set("redirect", "/api/v1/auth/login")
return c.SendStatus(http.StatusUnauthorized)
}

user := models.UnmarshalUser(strUser)
Expand Down

0 comments on commit 7ea15ec

Please sign in to comment.