Skip to content

Commit

Permalink
Merge pull request #77 from COS301-SE-2024/fix/backend/BookRoom(Occup…
Browse files Browse the repository at this point in the history
…iID-Generator)

Fix/backend/book room(occupi id generator)
  • Loading branch information
Rethakgetse-Manaka authored Jun 21, 2024
2 parents 1e3420d + 8503430 commit 7ef7c04
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 20 deletions.
2 changes: 1 addition & 1 deletion occupi-backend/pkg/database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,14 +110,14 @@ func GetUserBookings(ctx *gin.Context, db *mongo.Client, email string) ([]models
return nil, err
}
defer cursor.Close(ctx)

var bookings []models.Booking
for cursor.Next(ctx) {
var booking models.Booking
if err := cursor.Decode(&booking); err != nil {
logrus.Error(err)
return nil, err
}
fmt.Println("Here")
bookings = append(bookings, booking)
}
return bookings, nil
Expand Down
11 changes: 6 additions & 5 deletions occupi-backend/pkg/handlers/api_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func BookRoom(ctx *gin.Context, appsession *models.AppSession) {

// Generate a unique ID for the booking
booking.ID = primitive.NewObjectID().Hex()
booking.OccupiID = 1
booking.OccupiID = utils.GenerateBookingID()
booking.CheckedIn = false

// Save the booking to the database
Expand All @@ -70,7 +70,7 @@ func BookRoom(ctx *gin.Context, appsession *models.AppSession) {

// Prepare the email content
subject := "Booking Confirmation - Occupi"
body := mail.FormatBookingEmailBody(booking.ID, booking.RoomID, booking.Slot)
body := mail.FormatBookingEmailBodyForBooker(booking.ID, booking.RoomID, booking.Slot, booking.Emails)

// Send the confirmation email concurrently to all recipients
emailErrors := mail.SendMultipleEmailsConcurrently(booking.Emails, subject, body)
Expand All @@ -85,14 +85,15 @@ func BookRoom(ctx *gin.Context, appsession *models.AppSession) {

// ViewBookings handles the retrieval of all bookings for a user
func ViewBookings(ctx *gin.Context, appsession *models.AppSession) {
var userBooking models.User
if err := ctx.ShouldBindJSON(&userBooking); err != nil {
// Extract the email query parameter
email := ctx.Query("email")
if email == "" {
ctx.JSON(http.StatusBadRequest, utils.ErrorResponse(http.StatusBadRequest, "Invalid request payload", constants.InvalidRequestPayloadCode, "Expected Email Address", nil))
return
}

// Get all bookings for the userBooking
bookings, err := database.GetUserBookings(ctx, appsession.DB, userBooking.Email)
bookings, err := database.GetUserBookings(ctx, appsession.DB, email)
if err != nil {
ctx.JSON(http.StatusInternalServerError, utils.ErrorResponse(http.StatusInternalServerError, "Failed to get bookings", constants.InternalServerErrorCode, "Failed to get bookings", nil))
return
Expand Down
8 changes: 4 additions & 4 deletions occupi-backend/pkg/mail/email_format.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func FormatBookingEmailBody(bookingID string, roomID string, slot int) string {
}

// formats booking email body to send person who booked
func FormatBookingEmailBodyForBooker(bookingID int, roomID string, slot int, attendees map[string]string) string {
func FormatBookingEmailBodyForBooker(bookingID string, roomID string, slot int, attendees []string) string {
listOfAttendees := "<ul>"
for _, email := range attendees {
listOfAttendees += "<li>" + email + "</li>"
Expand All @@ -33,7 +33,7 @@ func FormatBookingEmailBodyForBooker(bookingID int, roomID string, slot int, att
<p>Dear booker,</p>
<p>
You have successfully booked an office space. Here are the booking details:<br><br>
<b>Booking ID:</b> ` + strconv.Itoa(bookingID) + `<br>
<b>Booking ID:</b> ` + bookingID + `<br>
<b>Room ID:</b> ` + roomID + `<br>
<b>Slot:</b> ` + strconv.Itoa(slot) + `<br><br>
<b>Attendees:</b>` + listOfAttendees + `<br><br>
Expand All @@ -45,13 +45,13 @@ func FormatBookingEmailBodyForBooker(bookingID int, roomID string, slot int, att
}

// formats booking email body to send attendees
func FormatBookingEmailBodyForAttendees(bookingID int, roomID string, slot int, email string) string {
func FormatBookingEmailBodyForAttendees(bookingID string, roomID string, slot int, email string) string {
return appendHeader("Booking") + `
<div class="content">
<p>Dear attendees,</p>
<p>
` + email + ` has booked an office space and invited you to join. Here are the booking details:<br><br>
<b>Booking ID:</b> ` + strconv.Itoa(bookingID) + `<br>
<b>Booking ID:</b> ` + bookingID + `<br>
<b>Room ID:</b> ` + roomID + `<br>
<b>Slot:</b> ` + strconv.Itoa(slot) + `<br><br>
If you have any questions, feel free to contact us.<br><br>
Expand Down
20 changes: 12 additions & 8 deletions occupi-backend/pkg/models/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,18 @@ type User struct {

// structure of booking
type Booking struct {
ID string `json:"_id" bson:"_id,omitempty"`
OccupiID int `json:"occupiId" bson:"occupiId,omitempty"`
RoomID string `json:"roomId" bson:"roomId"`
Slot int `json:"slot" bson:"slot"`
Emails []string `json:"emails" bson:"emails"`
CheckedIn bool `json:"checkedIn" bson:"checkedIn,omitempty"`
Creator string `json:"creator" bson:"creator"`
FloorNo int `json:"floorNo" bson:"floorNo"`
ID string `json:"_id" bson:"_id,omitempty"`
OccupiID string `json:"occupiId" bson:"occupiId,omitempty"`
RoomID string `json:"roomId" bson:"roomId"`
RoomName string `json:"roomName" bson:"roomName,omitempty"`
Slot int `json:"slot" bson:"slot"`
Emails []string `json:"emails" bson:"emails"`
CheckedIn bool `json:"checkedIn" bson:"checkedIn,omitempty"`
Creator string `json:"creator" bson:"creator"`
FloorNo int `json:"floorNo" bson:"floorNo"`
Date time.Time `json:"date" bson:"date,omitempty"`
Start time.Time `json:"start" bson:"start,omitempty"`
End time.Time `json:"end" bson:"end,omitempty"`
}

// structure of CheckIn
Expand Down
2 changes: 1 addition & 1 deletion occupi-backend/pkg/router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func OccupiRouter(router *gin.Engine, db *mongo.Client) {
api.POST("/book-room", middleware.ProtectedRoute, func(ctx *gin.Context) { handlers.BookRoom(ctx, appsession) })
api.POST("/check-in", middleware.ProtectedRoute, func(ctx *gin.Context) { handlers.CheckIn(ctx, appsession) })
api.POST("/cancel-booking", middleware.ProtectedRoute, func(ctx *gin.Context) { handlers.CancelBooking(ctx, appsession) })
api.GET(("/view-bookings"), middleware.ProtectedRoute, func(ctx *gin.Context) { handlers.ViewBookings(ctx, appsession) })
api.GET(("/view-bookings"), middleware.UnProtectedRoute, func(ctx *gin.Context) { handlers.ViewBookings(ctx, appsession) })
api.GET("/view-rooms", middleware.ProtectedRoute, func(ctx *gin.Context) { handlers.ViewRooms(ctx, appsession) })
}
auth := router.Group("/auth")
Expand Down
11 changes: 11 additions & 0 deletions occupi-backend/pkg/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,17 @@ func GenerateEmployeeID() string {
return employeeID
}

// Function to generate an employee ID with the structure OCCUPIYYYYXXXX
func GenerateBookingID() string {
currentYear := time.Now().Year()
randomNum, err := generateRandomNumber()
if err != nil {
return "BOOKOCCUPI00000000"
}
employeeID := fmt.Sprintf("BOOKOCCUPI%d%04d", currentYear, randomNum)
return employeeID
}

// generates a random auth0 state
func GenerateRandomState() (string, error) {
b := make([]byte, 32)
Expand Down
5 changes: 4 additions & 1 deletion occupi-backend/tests/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ func TestGenEmpID(t *testing.T) {
empID := utils.GenerateEmployeeID()
t.Logf("Generated Employee ID: %s", empID)
}

func TestGenBookID(t *testing.T) {
BookID := utils.GenerateBookingID()
t.Logf("Generated Booking ID: %s", BookID)
}
func TestSanitizeInput(t *testing.T) {
tests := []struct {
name string
Expand Down

0 comments on commit 7ef7c04

Please sign in to comment.