Skip to content

Commit

Permalink
Merge pull request #60 from srm-kzilla/as/feat/event-register-counter
Browse files Browse the repository at this point in the history
add route secret
  • Loading branch information
meltedhyperion authored Aug 12, 2023
2 parents 25c327b + b6086df commit 162c43f
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
2 changes: 1 addition & 1 deletion api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func SetupApp(app *fiber.App) {
api.Get("/events", eventController.GetAllEvents)
api.Post("/register", userController.RegisterForEvent)
api.Get("/rsvp", userController.RsvpForEvent)
api.Get("/event/:slug/registrations", eventController.GetEventRegistrationsCount)
api.Get("/event/:slug/registrations/:secret", eventController.GetEventRegistrationsCount)
protected := api.Use(authController.AuthenticateAdmin)
protected.Get("/users", eventController.GetEventUsers)
protected.Get("/user/:userId", userController.GetUserById)
Expand Down
9 changes: 9 additions & 0 deletions api/events/controller/eventController.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
userModel "github.com/srm-kzilla/events/api/users/model"
"github.com/srm-kzilla/events/database"
helpers "github.com/srm-kzilla/events/utils/helpers"
"github.com/srm-kzilla/events/utils/services"
S3 "github.com/srm-kzilla/events/utils/services/s3"
"github.com/srm-kzilla/events/validators"
"go.mongodb.org/mongo-driver/bson"
Expand Down Expand Up @@ -512,6 +513,14 @@ Get event totaal registrations count by event slug.
*/
func GetEventRegistrationsCount(c *fiber.Ctx) error {
slug := c.Params("slug")

secret := c.Params("secret")
if !services.VerifyRouteSecret(secret) {
return c.Status(fiber.StatusBadGateway).JSON(fiber.Map{
"error": "Invalid route secret",
})
}

usersCollection, e := database.GetCollection(os.Getenv("DB_NAME"), "Users")
if e != nil {
fmt.Println("Error: ", e)
Expand Down
5 changes: 5 additions & 0 deletions utils/services/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,8 @@ func GenerateCollegeYearRegistrationPrefix() (firstYearPrefix, secondYearPrefix,
fourthYearPrefix = "RA" + strconv.Itoa(currentFirstYear-3)
return firstYearPrefix, secondYearPrefix, thirdYearPrefix, fourthYearPrefix, nil
}

func VerifyRouteSecret(secret string) bool {
routeSecret := os.Getenv("ROUTE_SECRET")
return secret == routeSecret
}

0 comments on commit 162c43f

Please sign in to comment.