Skip to content

Commit

Permalink
Refactor CreateBookingMatchFilter function to include date filtering …
Browse files Browse the repository at this point in the history
…in tests
  • Loading branch information
waveyboym committed Sep 17, 2024
1 parent 3513119 commit dfaa7f5
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 8 deletions.
5 changes: 5 additions & 0 deletions occupi-backend/pkg/analytics/analytics.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package analytics

import (
"fmt"

"github.com/COS301-SE-2024/occupi/occupi-backend/pkg/models"
"go.mongodb.org/mongo-driver/bson"
)
Expand Down Expand Up @@ -42,6 +44,9 @@ func CreateBookingMatchFilter(creatorEmail string, attendeesEmail []string, filt

// Conditionally add the attendees filter if emails is not of length 0
if len(attendeesEmail) > 0 {
fmt.Println(attendeesEmail)
// print len of attendeesEmail
fmt.Println(len(attendeesEmail))
matchFilter = append(matchFilter, bson.E{Key: "emails", Value: bson.D{{Key: "$in", Value: attendeesEmail}}})
}

Expand Down
9 changes: 5 additions & 4 deletions occupi-backend/pkg/database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -2065,17 +2065,18 @@ func GetAnalyticsOnBookings(ctx *gin.Context, appsession *models.AppSession, cre
switch calculate {
case "top3":
dateFilter = "date"
filter.Filter["timeTo"] = ""
pipeline = analytics.GetTop3MostBookedRooms(creatorEmail, attendeeEmails, filter, dateFilter)
case "historical":
// add or overwrite "timeTo" with time.Now and delete "timeFrom" if present
filter.Filter["timeTo"] = time.Now().Format(time.RFC3339)
delete(filter.Filter, "timeFrom")
filter.Filter["timeTo"] = time.Now()
filter.Filter["timeFrom"] = ""
dateFilter = "end"
pipeline = analytics.AggregateBookings(creatorEmail, attendeeEmails, filter, dateFilter)
case "upcoming":
// add or overwrite "timeFrom" with time.Now and delete "timeTo" if present
filter.Filter["timeFrom"] = time.Now().Format(time.RFC3339)
delete(filter.Filter, "timeTo")
filter.Filter["timeFrom"] = time.Now()
filter.Filter["timeTo"] = ""
dateFilter = "end"
pipeline = analytics.AggregateBookings(creatorEmail, attendeeEmails, filter, dateFilter)
default:
Expand Down
3 changes: 3 additions & 0 deletions occupi-backend/pkg/database/database_helpers.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package database

import (
"fmt"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -290,6 +291,8 @@ func MakeEmailAndEmailsAndTimeFilter(creatorEmail string, attendeeEmails []strin

// filter attendeeEmails in emails array
if len(attendeeEmails) > 0 {
fmt.Println(attendeeEmails)
fmt.Println(len(attendeeEmails))
mongoFilter["emails"] = bson.M{"$in": attendeeEmails}
}

Expand Down
6 changes: 5 additions & 1 deletion occupi-backend/pkg/handlers/api_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -1486,7 +1486,11 @@ func GetAnalyticsOnBookings(ctx *gin.Context, appsession *models.AppSession, cal
if err := ctx.ShouldBindJSON(&request); err != nil {
request.Creator = ctx.DefaultQuery("creator", "")
attendees := ctx.DefaultQuery("attendeeEmails", "")
request.Attendees = strings.Split(attendees, ",")
if attendees == "" {
request.Attendees = []string{}
} else {
request.Attendees = strings.Split(attendees, ",")
}

// default time is since 1970
timeFromStr := ctx.DefaultQuery("timeFrom", "1970-01-01T00:00:00Z")
Expand Down
6 changes: 3 additions & 3 deletions occupi-backend/pkg/router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,9 @@ func OccupiRouter(router *gin.Engine, appsession *models.AppSession) {
analytics.GET("/arrival-departure-average", middleware.ProtectedRoute, func(ctx *gin.Context) { handlers.GetAnalyticsOnHours(ctx, appsession, "arrivaldeparture", true) })
analytics.GET("/in-office", middleware.ProtectedRoute, func(ctx *gin.Context) { handlers.GetAnalyticsOnHours(ctx, appsession, "inofficehours", true) })

analytics.GET("/top-bookings", middleware.ProtectedRoute, func(ctx *gin.Context) { handlers.GetAnalyticsOnBookings(ctx, appsession, "top3") })
analytics.GET("/bookings-historical", middleware.ProtectedRoute, func(ctx *gin.Context) { handlers.GetAnalyticsOnBookings(ctx, appsession, "historical") })
analytics.GET("/bookings-current", middleware.ProtectedRoute, func(ctx *gin.Context) { handlers.GetAnalyticsOnBookings(ctx, appsession, "upcoming") })
analytics.GET("/top-bookings", func(ctx *gin.Context) { handlers.GetAnalyticsOnBookings(ctx, appsession, "top3") })
analytics.GET("/bookings-historical", func(ctx *gin.Context) { handlers.GetAnalyticsOnBookings(ctx, appsession, "historical") })
analytics.GET("/bookings-current", func(ctx *gin.Context) { handlers.GetAnalyticsOnBookings(ctx, appsession, "upcoming") })
}
auth := router.Group("/auth")
{
Expand Down

0 comments on commit dfaa7f5

Please sign in to comment.