From 91c09e202572a8fc3937588f7aac9533048af53d Mon Sep 17 00:00:00 2001 From: Jareth Gomes Date: Tue, 16 Nov 2021 00:28:34 -0600 Subject: [PATCH 01/30] Added virtual and in person pt values to event, added virtual prop to codes --- services/event/controller/controller.go | 13 +++++++--- services/event/models/event.go | 19 +++++++------- services/event/models/event_code.go | 1 + services/event/service/event_service.go | 6 ++--- services/event/tests/event_test.go | 33 ++++++++++++++++--------- 5 files changed, 46 insertions(+), 26 deletions(-) diff --git a/services/event/controller/controller.go b/services/event/controller/controller.go index 34e55e7c..9a8ef2ef 100644 --- a/services/event/controller/controller.go +++ b/services/event/controller/controller.go @@ -219,7 +219,7 @@ func Checkin(w http.ResponseWriter, r *http.Request) { var checkin_request models.CheckinRequest json.NewDecoder(r.Body).Decode(&checkin_request) - valid, event_id, err := service.CanRedeemPoints(checkin_request.Code) + valid, is_virtual, event_id, err := service.CanRedeemPoints(checkin_request.Code) result := models.CheckinResult{ NewPoints: -1, @@ -264,10 +264,17 @@ func Checkin(w http.ResponseWriter, r *http.Request) { return } - result.NewPoints = event.Points + points_to_award := 0 + if is_virtual { + points_to_award = event.VirtualPoints + } else { + points_to_award = event.InPersonPoints + } + + result.NewPoints = points_to_award // Add this point value to given profile - profile, err := service.AwardPoints(id, event.Points) + profile, err := service.AwardPoints(id, points_to_award) if err != nil { errors.WriteError(w, r, errors.UnknownError(err.Error(), "Failed to award user with points")) diff --git a/services/event/models/event.go b/services/event/models/event.go index 7a311ab9..7b8798fc 100644 --- a/services/event/models/event.go +++ b/services/event/models/event.go @@ -1,15 +1,16 @@ package models type Event struct { - ID string `json:"id" validate:"required"` - Name string `json:"name" validate:"required"` - Description string `json:"description" validate:"required"` - StartTime int64 `json:"startTime" validate:"required"` - EndTime int64 `json:"endTime" validate:"required"` - Locations []EventLocation `json:"locations" validate:"required,dive,required"` - Sponsor string `json:"sponsor"` - EventType string `json:"eventType" validate:"required,oneof=MEAL SPEAKER WORKSHOP MINIEVENT QNA OTHER"` - Points int `json:"points"` + ID string `json:"id" validate:"required"` + Name string `json:"name" validate:"required"` + Description string `json:"description" validate:"required"` + StartTime int64 `json:"startTime" validate:"required"` + EndTime int64 `json:"endTime" validate:"required"` + Locations []EventLocation `json:"locations" validate:"required,dive,required"` + Sponsor string `json:"sponsor"` + EventType string `json:"eventType" validate:"required,oneof=MEAL SPEAKER WORKSHOP MINIEVENT QNA OTHER"` + InPersonPoints int `json:"inPersonPoints"` + VirtualPoints int `json:"virtualPoints"` } type EventLocation struct { diff --git a/services/event/models/event_code.go b/services/event/models/event_code.go index a9d0680a..c2269cd2 100644 --- a/services/event/models/event_code.go +++ b/services/event/models/event_code.go @@ -3,5 +3,6 @@ package models type EventCode struct { ID string `json:"id"` Code string `json:"code"` + IsVirtual bool `json:"isVirtual"` Expiration int64 `json:"expiration"` } diff --git a/services/event/service/event_service.go b/services/event/service/event_service.go index 0c6b4392..6ac701b8 100644 --- a/services/event/service/event_service.go +++ b/services/event/service/event_service.go @@ -478,7 +478,7 @@ func GetStats() (map[string]interface{}, error) { Check if an event can be redeemed for points, i.e., that the point timeout has not been reached Returns true if the current time is between `PreEventCheckinIntervalInMinutes` number of minutes before the event, and the end of event. */ -func CanRedeemPoints(event_code string) (bool, string, error) { +func CanRedeemPoints(event_code string) (bool, bool, string, error) { query := database.QuerySelector{ "code": event_code, } @@ -487,13 +487,13 @@ func CanRedeemPoints(event_code string) (bool, string, error) { err := db.FindOne("eventcodes", query, &eventCode) if err != nil { - return false, "invalid", err + return false, false, "invalid", err } expiration_time := eventCode.Expiration current_time := time.Now().Unix() - return current_time < expiration_time, eventCode.ID, nil + return current_time < expiration_time, eventCode.IsVirtual, eventCode.ID, nil } /* diff --git a/services/event/tests/event_test.go b/services/event/tests/event_test.go index 4b2e12f6..8e10cac3 100644 --- a/services/event/tests/event_test.go +++ b/services/event/tests/event_test.go @@ -66,7 +66,8 @@ func SetupTestDB(t *testing.T) { Longitude: 123.456, }, }, - Points: 10, + InPersonPoints: 10, + VirtualPoints: 5, } err := db.Insert("events", &event) @@ -153,7 +154,8 @@ func TestGetAllEventsService(t *testing.T) { Longitude: 123.456, }, }, - Points: 10, + InPersonPoints: 10, + VirtualPoints: 5, }, { ID: "testid2", @@ -172,7 +174,8 @@ func TestGetAllEventsService(t *testing.T) { Longitude: 123.456, }, }, - Points: 0, + InPersonPoints: 0, + VirtualPoints: 0, }, }, } @@ -224,7 +227,8 @@ func TestGetFilteredEventsService(t *testing.T) { Longitude: 123.456, }, }, - Points: 0, + InPersonPoints: 0, + VirtualPoints: 0, } err := db.Insert("events", &event) @@ -262,7 +266,8 @@ func TestGetFilteredEventsService(t *testing.T) { Longitude: 123.456, }, }, - Points: 0, + InPersonPoints: 0, + VirtualPoints: 0, }, }, } @@ -300,7 +305,8 @@ func TestGetFilteredEventsService(t *testing.T) { Longitude: 123.456, }, }, - Points: 10, + InPersonPoints: 0, + VirtualPoints: 0, }, { ID: "testid2", @@ -319,7 +325,8 @@ func TestGetFilteredEventsService(t *testing.T) { Longitude: 123.456, }, }, - Points: 0, + InPersonPoints: 0, + VirtualPoints: 0, }, }, } @@ -377,7 +384,8 @@ func TestGetEventService(t *testing.T) { Longitude: 123.456, }, }, - Points: 10, + InPersonPoints: 10, + VirtualPoints: 5, } if !reflect.DeepEqual(event, &expected_event) { @@ -439,7 +447,8 @@ func TestCreateEventService(t *testing.T) { Longitude: 123.456, }, }, - Points: 0, + InPersonPoints: 0, + VirtualPoints: 0, } if !reflect.DeepEqual(event, &expected_event) { @@ -536,7 +545,8 @@ func TestUpdateEventService(t *testing.T) { Longitude: 123.456, }, }, - Points: 100, + InPersonPoints: 100, + VirtualPoints: 50, } err := service.UpdateEvent("testid", event) @@ -567,7 +577,8 @@ func TestUpdateEventService(t *testing.T) { Longitude: 123.456, }, }, - Points: 100, + InPersonPoints: 100, + VirtualPoints: 50, } if !reflect.DeepEqual(updated_event, &expected_event) { From 1d97a962bec4bd928fcdcf3fccd3ca59ff79f911 Mon Sep 17 00:00:00 2001 From: Jareth Gomes Date: Tue, 16 Nov 2021 01:33:46 -0600 Subject: [PATCH 02/30] Changed PUT /event/code/ to POST, changed GET /code/{id}/ to return a list of codes --- gateway/services/event.go | 6 +++--- services/event/controller/controller.go | 21 ++++++--------------- services/event/service/event_service.go | 16 +++++++++------- 3 files changed, 18 insertions(+), 25 deletions(-) diff --git a/gateway/services/event.go b/gateway/services/event.go index 8b2317b6..e1e72908 100644 --- a/gateway/services/event.go +++ b/gateway/services/event.go @@ -93,8 +93,8 @@ var EventRoutes = arbor.RouteCollection{ }, arbor.Route{ "UpdateEventCode", - "PUT", - "/event/code/{id}/", + "POST", + "/event/code/", alice.New(middleware.AuthMiddleware([]models.Role{models.AdminRole, models.StaffRole}), middleware.IdentificationMiddleware).ThenFunc(PutEventCode).ServeHTTP, }, arbor.Route{ @@ -122,7 +122,7 @@ func CreateEvent(w http.ResponseWriter, r *http.Request) { } func UpdateEvent(w http.ResponseWriter, r *http.Request) { - arbor.PUT(w, config.EVENT_SERVICE+r.URL.String(), EventFormat, "", r) + arbor.POST(w, config.EVENT_SERVICE+r.URL.String(), EventFormat, "", r) } func GetEventCode(w http.ResponseWriter, r *http.Request) { diff --git a/services/event/controller/controller.go b/services/event/controller/controller.go index 9a8ef2ef..e2fb35c5 100644 --- a/services/event/controller/controller.go +++ b/services/event/controller/controller.go @@ -26,7 +26,7 @@ func SetupController(route *mux.Route) { router.HandleFunc("/", UpdateEvent).Methods("PUT") router.HandleFunc("/", GetAllEvents).Methods("GET") router.HandleFunc("/code/{id}/", GetEventCode).Methods("GET") - router.HandleFunc("/code/{id}/", UpdateEventCode).Methods("PUT") + router.HandleFunc("/code/", UpdateEventCode).Methods("POST") router.HandleFunc("/checkin/", Checkin).Methods("POST") @@ -162,47 +162,38 @@ func GetEventCode(w http.ResponseWriter, r *http.Request) { return } - code, err := service.GetEventCode(id) + codes, err := service.GetEventCode(id) if err != nil { errors.WriteError(w, r, errors.DatabaseError(err.Error(), "Failed to receive event code information from database")) return } - json.NewEncoder(w).Encode(code) + json.NewEncoder(w).Encode(codes) } /* Endpoint to update an event code and end time */ func UpdateEventCode(w http.ResponseWriter, r *http.Request) { - id := mux.Vars(r)["id"] - - if id == "" { - errors.WriteError(w, r, errors.MalformedRequestError("Must provide event id in request url.", "Must provide event id in request url.")) - return - } - var eventCode models.EventCode json.NewDecoder(r.Body).Decode(&eventCode) - eventCode.ID = id - - err := service.UpdateEventCode(id, eventCode) + err := service.UpdateEventCode(eventCode.Code, eventCode) if err != nil { errors.WriteError(w, r, errors.DatabaseError(err.Error(), "Could not update the code and timestamp of the event.")) return } - updated_event, err := service.GetEventCode(id) + updated_codes, err := service.GetEventCode(eventCode.ID) if err != nil { errors.WriteError(w, r, errors.DatabaseError(err.Error(), "Could not get updated event code and timestamp details.")) return } - json.NewEncoder(w).Encode(updated_event) + json.NewEncoder(w).Encode(updated_codes) } /* diff --git a/services/event/service/event_service.go b/services/event/service/event_service.go index 6ac701b8..d8801ad6 100644 --- a/services/event/service/event_service.go +++ b/services/event/service/event_service.go @@ -499,30 +499,32 @@ func CanRedeemPoints(event_code string) (bool, bool, string, error) { /* Returns the eventcode struct for the event with the given id */ -func GetEventCode(id string) (*models.EventCode, error) { +func GetEventCode(id string) (*[]models.EventCode, error) { query := database.QuerySelector{ "id": id, } - var eventCode models.EventCode - err := db.FindOne("eventcodes", query, &eventCode) + eventCodes := []models.EventCode{} + + err := db.FindAll("eventcodes", query, &eventCodes) if err != nil { return nil, err } - return &eventCode, nil + return &eventCodes, nil } /* Updates the event code and end time with the given id + If no matching code is not found, then it is a new code and add it */ -func UpdateEventCode(id string, eventCode models.EventCode) error { +func UpdateEventCode(code string, eventCode models.EventCode) error { selector := database.QuerySelector{ - "id": id, + "code": code, } - err := db.Update("eventcodes", selector, &eventCode) + _, err := db.Upsert("eventcodes", selector, &eventCode) return err } From 537d2ee66d5a73c73b7312322b0974f9a3c3686b Mon Sep 17 00:00:00 2001 From: Jareth Gomes Date: Fri, 24 Dec 2021 14:44:42 -0600 Subject: [PATCH 03/30] Added internal GET /profile/user/{id}/ route --- gateway/services/profile.go | 10 +++++++++ services/profile/controller/controller.go | 25 +++++++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/gateway/services/profile.go b/gateway/services/profile.go index ad8f9ed4..efa90395 100644 --- a/gateway/services/profile.go +++ b/gateway/services/profile.go @@ -85,6 +85,12 @@ var ProfileRoutes = arbor.RouteCollection{ "/profile/favorite/", alice.New(middleware.AuthMiddleware([]models.Role{models.AdminRole, models.AttendeeRole, models.ApplicantRole, models.StaffRole, models.MentorRole}), middleware.IdentificationMiddleware).ThenFunc(RemoveProfileFavorite).ServeHTTP, }, + arbor.Route{ + "GetUserProfileByUserId", + "GET", + "/profile/user/{id}/", + alice.New(middleware.AuthMiddleware([]models.Role{models.AdminRole}), middleware.IdentificationMiddleware).ThenFunc(GetProfileByUserId).ServeHTTP, + }, // This needs to be the last route in order to prevent endpoints like "search", "leaderboard" from accidentally being routed as the {id} variable. arbor.Route{ "GetUserProfileById", @@ -102,6 +108,10 @@ func GetProfileById(w http.ResponseWriter, r *http.Request) { arbor.GET(w, config.PROFILE_SERVICE+r.URL.String(), ProfileFormat, "", r) } +func GetProfileByUserId(w http.ResponseWriter, r *http.Request) { + arbor.GET(w, config.PROFILE_SERVICE+r.URL.String(), ProfileFormat, "", r) +} + func GetValidFilteredProfiles(w http.ResponseWriter, r *http.Request) { arbor.GET(w, config.PROFILE_SERVICE+r.URL.String(), ProfileFormat, "", r) } diff --git a/services/profile/controller/controller.go b/services/profile/controller/controller.go index 7e16060b..73d601ae 100644 --- a/services/profile/controller/controller.go +++ b/services/profile/controller/controller.go @@ -30,6 +30,8 @@ func SetupController(route *mux.Route) { router.HandleFunc("/favorite/", AddProfileFavorite).Methods("POST") router.HandleFunc("/favorite/", RemoveProfileFavorite).Methods("DELETE") + router.HandleFunc("/user/{id}/", GetProfileByUserId).Methods("GET") + router.HandleFunc("/{id}/", GetProfileById).Methods("GET") } @@ -72,6 +74,29 @@ func GetProfileById(w http.ResponseWriter, r *http.Request) { json.NewEncoder(w).Encode(user_profile) } +/* + GetProfileByUserId is used to get a profile for a provided user id. +*/ +func GetProfileByUserId(w http.ResponseWriter, r *http.Request) { + user_id := mux.Vars(r)["id"] + + profile_id, err := service.GetProfileIdFromUserId(user_id) + + if err != nil { + errors.WriteError(w, r, errors.DatabaseError(err.Error(), "Could not get profile id associated with the user")) + return + } + + user_profile, err := service.GetProfile(profile_id) + + if err != nil { + errors.WriteError(w, r, errors.DatabaseError(err.Error(), "Could not get profile for user id "+user_id)) + return + } + + json.NewEncoder(w).Encode(user_profile) +} + /* CreateProfile is the endpoint to create the profile for the current user. */ From b1223ec2bd45e70d07938f00033d6a2aceb86ba3 Mon Sep 17 00:00:00 2001 From: Jareth Gomes Date: Fri, 24 Dec 2021 14:46:52 -0600 Subject: [PATCH 04/30] Fixed bug causing new rows in profileattendance not including an id value --- services/profile/service/profile_service.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/services/profile/service/profile_service.go b/services/profile/service/profile_service.go index 38b685c2..0c2ec4df 100644 --- a/services/profile/service/profile_service.go +++ b/services/profile/service/profile_service.go @@ -323,10 +323,11 @@ func RedeemEvent(profile_id string, event_id string) (*models.RedeemEventRespons if err != nil { if err == database.ErrNotFound { - err = db.Insert("profileattendance", &models.AttendanceTracker{ + attended_events = models.AttendanceTracker{ ID: profile_id, Events: []string{}, - }) + } + err = db.Insert("profileattendance", &attended_events) if err != nil { redemption_status.Status = "Could not add tracker to db" From 3fafc1f106e3d969926f9854728e0500de2aaea5 Mon Sep 17 00:00:00 2001 From: Jareth Gomes Date: Fri, 24 Dec 2021 14:48:29 -0600 Subject: [PATCH 05/30] Removed unused profile info, added isVirtual bool field to Profile model --- services/profile/models/profile.go | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/services/profile/models/profile.go b/services/profile/models/profile.go index 9db48172..520de24b 100644 --- a/services/profile/models/profile.go +++ b/services/profile/models/profile.go @@ -1,14 +1,9 @@ package models type Profile struct { - ID string `json:"id"` - FirstName string `json:"firstName"` - LastName string `json:"lastName"` - Points int `json:"points"` - Timezone string `json:"timezone"` - Description string `json:"description"` - Discord string `json:"discord"` - AvatarUrl string `json:"avatarUrl"` - TeamStatus string `json:"teamStatus"` - Interests []string `json:"interests"` + ID string `json:"id"` + FirstName string `json:"firstName"` + LastName string `json:"lastName"` + Points int `json:"points"` + IsVirtual bool `json:"isVirtual"` } From ec0ed0de6efc448e332b92b7b4768307e22c0dbe Mon Sep 17 00:00:00 2001 From: Jareth Gomes Date: Fri, 24 Dec 2021 14:56:36 -0600 Subject: [PATCH 06/30] Reimplemented event redemption for 3-varient point awarding --- services/event/controller/controller.go | 13 +++++++++++-- services/event/models/event.go | 21 +++++++++++---------- services/event/service/profile_service.go | 16 ++++++++++++++++ 3 files changed, 38 insertions(+), 12 deletions(-) diff --git a/services/event/controller/controller.go b/services/event/controller/controller.go index e2fb35c5..4722c7fa 100644 --- a/services/event/controller/controller.go +++ b/services/event/controller/controller.go @@ -210,7 +210,7 @@ func Checkin(w http.ResponseWriter, r *http.Request) { var checkin_request models.CheckinRequest json.NewDecoder(r.Body).Decode(&checkin_request) - valid, is_virtual, event_id, err := service.CanRedeemPoints(checkin_request.Code) + valid, is_code_virtual, event_id, err := service.CanRedeemPoints(checkin_request.Code) result := models.CheckinResult{ NewPoints: -1, @@ -228,6 +228,13 @@ func Checkin(w http.ResponseWriter, r *http.Request) { return } + is_user_virtual, err := service.GetIsUserVirtual(id) + + if err != nil { + errors.WriteError(w, r, errors.UnknownError(err.Error(), "Failed to retreive if user is virtual or in-person")) + return + } + if !valid { result.Status = "InvalidTime" json.NewEncoder(w).Encode(result) @@ -256,8 +263,10 @@ func Checkin(w http.ResponseWriter, r *http.Request) { } points_to_award := 0 - if is_virtual { + if is_user_virtual { points_to_award = event.VirtualPoints + } else if is_code_virtual { + points_to_award = event.InPersonVirtPoints } else { points_to_award = event.InPersonPoints } diff --git a/services/event/models/event.go b/services/event/models/event.go index 7b8798fc..15d0bd4a 100644 --- a/services/event/models/event.go +++ b/services/event/models/event.go @@ -1,16 +1,17 @@ package models type Event struct { - ID string `json:"id" validate:"required"` - Name string `json:"name" validate:"required"` - Description string `json:"description" validate:"required"` - StartTime int64 `json:"startTime" validate:"required"` - EndTime int64 `json:"endTime" validate:"required"` - Locations []EventLocation `json:"locations" validate:"required,dive,required"` - Sponsor string `json:"sponsor"` - EventType string `json:"eventType" validate:"required,oneof=MEAL SPEAKER WORKSHOP MINIEVENT QNA OTHER"` - InPersonPoints int `json:"inPersonPoints"` - VirtualPoints int `json:"virtualPoints"` + ID string `json:"id" validate:"required"` + Name string `json:"name" validate:"required"` + Description string `json:"description" validate:"required"` + StartTime int64 `json:"startTime" validate:"required"` + EndTime int64 `json:"endTime" validate:"required"` + Locations []EventLocation `json:"locations" validate:"required,dive,required"` + Sponsor string `json:"sponsor"` + EventType string `json:"eventType" validate:"required,oneof=MEAL SPEAKER WORKSHOP MINIEVENT QNA OTHER"` + InPersonPoints int `json:"inPersonPoints"` + InPersonVirtPoints int `json:"inPersonVirtPoints"` + VirtualPoints int `json:"virtualPoints"` } type EventLocation struct { diff --git a/services/event/service/profile_service.go b/services/event/service/profile_service.go index 0333c762..0f7adab5 100644 --- a/services/event/service/profile_service.go +++ b/services/event/service/profile_service.go @@ -53,3 +53,19 @@ func AwardPoints(id string, points int) (*models.Profile, error) { return &profile, nil } + +func GetIsUserVirtual(id string) (bool, error) { + var profile models.Profile + + status, err := apirequest.Get(config.PROFILE_SERVICE+"/profile/user/"+id+"/", &profile) + + if err != nil { + return false, err + } + + if status != http.StatusOK { + return false, errors.New("Unable to retrieve if user is virtual or in-person") + } + + return profile.IsVirtual, nil +} From f8ce10a91a2d0afad83f15be02d64c97606d2cba Mon Sep 17 00:00:00 2001 From: Jareth Gomes Date: Fri, 24 Dec 2021 15:29:35 -0600 Subject: [PATCH 07/30] Modified event models in tests --- services/event/tests/event_test.go | 55 ++++++++++++++++++------------ 1 file changed, 33 insertions(+), 22 deletions(-) diff --git a/services/event/tests/event_test.go b/services/event/tests/event_test.go index 8e10cac3..ae5af2a1 100644 --- a/services/event/tests/event_test.go +++ b/services/event/tests/event_test.go @@ -66,8 +66,9 @@ func SetupTestDB(t *testing.T) { Longitude: 123.456, }, }, - InPersonPoints: 10, - VirtualPoints: 5, + InPersonPoints: 10, + InPersonVirtPoints: 7, + VirtualPoints: 5, } err := db.Insert("events", &event) @@ -154,8 +155,9 @@ func TestGetAllEventsService(t *testing.T) { Longitude: 123.456, }, }, - InPersonPoints: 10, - VirtualPoints: 5, + InPersonPoints: 10, + InPersonVirtPoints: 7, + VirtualPoints: 5, }, { ID: "testid2", @@ -174,8 +176,9 @@ func TestGetAllEventsService(t *testing.T) { Longitude: 123.456, }, }, - InPersonPoints: 0, - VirtualPoints: 0, + InPersonPoints: 0, + InPersonVirtPoints: 0, + VirtualPoints: 0, }, }, } @@ -227,8 +230,9 @@ func TestGetFilteredEventsService(t *testing.T) { Longitude: 123.456, }, }, - InPersonPoints: 0, - VirtualPoints: 0, + InPersonPoints: 0, + InPersonVirtPoints: 0, + VirtualPoints: 0, } err := db.Insert("events", &event) @@ -266,8 +270,9 @@ func TestGetFilteredEventsService(t *testing.T) { Longitude: 123.456, }, }, - InPersonPoints: 0, - VirtualPoints: 0, + InPersonPoints: 0, + InPersonVirtPoints: 0, + VirtualPoints: 0, }, }, } @@ -305,8 +310,9 @@ func TestGetFilteredEventsService(t *testing.T) { Longitude: 123.456, }, }, - InPersonPoints: 0, - VirtualPoints: 0, + InPersonPoints: 10, + InPersonVirtPoints: 7, + VirtualPoints: 5, }, { ID: "testid2", @@ -325,8 +331,9 @@ func TestGetFilteredEventsService(t *testing.T) { Longitude: 123.456, }, }, - InPersonPoints: 0, - VirtualPoints: 0, + InPersonPoints: 0, + InPersonVirtPoints: 0, + VirtualPoints: 0, }, }, } @@ -384,8 +391,9 @@ func TestGetEventService(t *testing.T) { Longitude: 123.456, }, }, - InPersonPoints: 10, - VirtualPoints: 5, + InPersonPoints: 10, + InPersonVirtPoints: 7, + VirtualPoints: 5, } if !reflect.DeepEqual(event, &expected_event) { @@ -447,8 +455,9 @@ func TestCreateEventService(t *testing.T) { Longitude: 123.456, }, }, - InPersonPoints: 0, - VirtualPoints: 0, + InPersonPoints: 0, + InPersonVirtPoints: 0, + VirtualPoints: 0, } if !reflect.DeepEqual(event, &expected_event) { @@ -545,8 +554,9 @@ func TestUpdateEventService(t *testing.T) { Longitude: 123.456, }, }, - InPersonPoints: 100, - VirtualPoints: 50, + InPersonPoints: 100, + InPersonVirtPoints: 70, + VirtualPoints: 50, } err := service.UpdateEvent("testid", event) @@ -577,8 +587,9 @@ func TestUpdateEventService(t *testing.T) { Longitude: 123.456, }, }, - InPersonPoints: 100, - VirtualPoints: 50, + InPersonPoints: 100, + InPersonVirtPoints: 70, + VirtualPoints: 50, } if !reflect.DeepEqual(updated_event, &expected_event) { From 3980c733d5e8fb2fd90c76e1f0661df4f5a7ab20 Mon Sep 17 00:00:00 2001 From: Jareth Gomes Date: Fri, 24 Dec 2021 16:07:25 -0600 Subject: [PATCH 08/30] Modified profile tests --- services/profile/tests/profile_test.go | 555 ++++++++----------------- 1 file changed, 166 insertions(+), 389 deletions(-) diff --git a/services/profile/tests/profile_test.go b/services/profile/tests/profile_test.go index fe933518..df8be867 100644 --- a/services/profile/tests/profile_test.go +++ b/services/profile/tests/profile_test.go @@ -53,16 +53,11 @@ func SetupTestDB(t *testing.T) { profile_id := "testid" profile := models.Profile{ - ID: profile_id, - FirstName: "testfirstname", - LastName: "testlastname", - Points: 0, - Timezone: "America/Chicago", - Description: "Hi", - Discord: "testdiscordusername", - AvatarUrl: "https://imgs.smoothradio.com/images/191589?crop=16_9&width=660&relax=1&signature=Rz93ikqcAz7BcX6SKiEC94zJnqo=", - TeamStatus: "LOOKING_FOR_TEAM", - Interests: []string{"testinterest1", "testinterest2"}, + ID: profile_id, + FirstName: "testfirstname", + LastName: "testlastname", + Points: 0, + IsVirtual: false, } id_map := models.IdMap{ @@ -121,18 +116,14 @@ func CleanupTestDB(t *testing.T) { */ func TestGetAllProfilesService(t *testing.T) { SetupTestDB(t) + defer CleanupTestDB(t) profile := models.Profile{ - ID: "testid2", - FirstName: "testfirstname2", - LastName: "testlastname2", - Points: 340, - Timezone: "America/New York", - Description: "Hello", - Discord: "testdiscordusername2", - AvatarUrl: "https://yt3.ggpht.com/ytc/AAUvwniHNhQyp4hWj3nrADnils-6N3jNREP8rWKGDTp0Lg=s900-c-k-c0x00ffffff-no-rj", - TeamStatus: "NOT_LOOKING", - Interests: []string{"testinterest2"}, + ID: "testid2", + FirstName: "testfirstname2", + LastName: "testlastname2", + Points: 340, + IsVirtual: true, } err := db.Insert("profiles", &profile) @@ -152,28 +143,18 @@ func TestGetAllProfilesService(t *testing.T) { expected_profile_list := models.ProfileList{ Profiles: []models.Profile{ { - ID: "testid", - FirstName: "testfirstname", - LastName: "testlastname", - Points: 0, - Timezone: "America/Chicago", - Description: "Hi", - Discord: "testdiscordusername", - AvatarUrl: "https://imgs.smoothradio.com/images/191589?crop=16_9&width=660&relax=1&signature=Rz93ikqcAz7BcX6SKiEC94zJnqo=", - TeamStatus: "LOOKING_FOR_TEAM", - Interests: []string{"testinterest1", "testinterest2"}, + ID: "testid", + FirstName: "testfirstname", + LastName: "testlastname", + Points: 0, + IsVirtual: false, }, { - ID: "testid2", - FirstName: "testfirstname2", - LastName: "testlastname2", - Points: 340, - Timezone: "America/New York", - Description: "Hello", - Discord: "testdiscordusername2", - AvatarUrl: "https://yt3.ggpht.com/ytc/AAUvwniHNhQyp4hWj3nrADnils-6N3jNREP8rWKGDTp0Lg=s900-c-k-c0x00ffffff-no-rj", - TeamStatus: "NOT_LOOKING", - Interests: []string{"testinterest2"}, + ID: "testid2", + FirstName: "testfirstname2", + LastName: "testlastname2", + Points: 340, + IsVirtual: true, }, }, } @@ -197,9 +178,6 @@ func TestGetAllProfilesService(t *testing.T) { if !reflect.DeepEqual(actual_profile_list, &expected_profile_list) { t.Errorf("Wrong profile list. Expected %v, got %v", expected_profile_list, actual_profile_list) } - - CleanupTestDB(t) - } /* @@ -207,6 +185,7 @@ func TestGetAllProfilesService(t *testing.T) { */ func TestGetProfileService(t *testing.T) { SetupTestDB(t) + defer CleanupTestDB(t) profile, err := service.GetProfile("testid") @@ -215,23 +194,16 @@ func TestGetProfileService(t *testing.T) { } expected_profile := models.Profile{ - ID: "testid", - FirstName: "testfirstname", - LastName: "testlastname", - Points: 0, - Timezone: "America/Chicago", - Description: "Hi", - Discord: "testdiscordusername", - AvatarUrl: "https://imgs.smoothradio.com/images/191589?crop=16_9&width=660&relax=1&signature=Rz93ikqcAz7BcX6SKiEC94zJnqo=", - TeamStatus: "LOOKING_FOR_TEAM", - Interests: []string{"testinterest1", "testinterest2"}, + ID: "testid", + FirstName: "testfirstname", + LastName: "testlastname", + Points: 0, + IsVirtual: false, } if !reflect.DeepEqual(profile, &expected_profile) { t.Errorf("Wrong profile info. Expected %v, got %v", &expected_profile, profile) } - - CleanupTestDB(t) } /* @@ -239,18 +211,14 @@ func TestGetProfileService(t *testing.T) { */ func TestCreateProfileService(t *testing.T) { SetupTestDB(t) + defer CleanupTestDB(t) new_profile := models.Profile{ - ID: "testid2", - FirstName: "testfirstname2", - LastName: "testlastname2", - Points: 340, - Timezone: "America/New York", - Description: "Hello", - Discord: "testdiscordusername2", - AvatarUrl: "https://yt3.ggpht.com/ytc/AAUvwniHNhQyp4hWj3nrADnils-6N3jNREP8rWKGDTp0Lg=s900-c-k-c0x00ffffff-no-rj", - TeamStatus: "NOT_LOOKING", - Interests: []string{"testinterest2"}, + ID: "testid2", + FirstName: "testfirstname2", + LastName: "testlastname2", + Points: 340, + IsVirtual: true, } err := service.CreateProfile("testuserid2", "testid2", new_profile) @@ -266,16 +234,11 @@ func TestCreateProfileService(t *testing.T) { } expected_profile := models.Profile{ - ID: "testid2", - FirstName: "testfirstname2", - LastName: "testlastname2", - Points: 340, - Timezone: "America/New York", - Description: "Hello", - Discord: "testdiscordusername2", - AvatarUrl: "https://yt3.ggpht.com/ytc/AAUvwniHNhQyp4hWj3nrADnils-6N3jNREP8rWKGDTp0Lg=s900-c-k-c0x00ffffff-no-rj", - TeamStatus: "NOT_LOOKING", - Interests: []string{"testinterest2"}, + ID: "testid2", + FirstName: "testfirstname2", + LastName: "testlastname2", + Points: 340, + IsVirtual: true, } if !reflect.DeepEqual(profile, &expected_profile) { @@ -292,8 +255,6 @@ func TestCreateProfileService(t *testing.T) { if profile_id1 != "testid2" { t.Errorf("Wrong profile mapping found for user %s. Expected %s but got %s", "testuserid2", "testid2", profile_id1) } - - CleanupTestDB(t) } /* @@ -301,6 +262,7 @@ func TestCreateProfileService(t *testing.T) { */ func TestDeleteProfileService(t *testing.T) { SetupTestDB(t) + defer CleanupTestDB(t) profile_id := "testid" @@ -318,8 +280,6 @@ func TestDeleteProfileService(t *testing.T) { if err == nil { t.Errorf("Found profile %v in profiles database.", profile) } - - CleanupTestDB(t) } /* @@ -327,18 +287,14 @@ func TestDeleteProfileService(t *testing.T) { */ func TestUpdateProfileService(t *testing.T) { SetupTestDB(t) + defer CleanupTestDB(t) profile := models.Profile{ - ID: "testid", - FirstName: "testfirstname2", - LastName: "testlastname2", - Points: 340, - Timezone: "America/New York", - Description: "Hello", - Discord: "testdiscordusername2", - AvatarUrl: "https://yt3.ggpht.com/ytc/AAUvwniHNhQyp4hWj3nrADnils-6N3jNREP8rWKGDTp0Lg=s900-c-k-c0x00ffffff-no-rj", - TeamStatus: "NOT_LOOKING", - Interests: []string{"testinterest2"}, + ID: "testid", + FirstName: "testfirstname2", + LastName: "testlastname2", + Points: 340, + IsVirtual: true, } err := service.UpdateProfile("testid", profile) @@ -354,61 +310,44 @@ func TestUpdateProfileService(t *testing.T) { } expected_profile := models.Profile{ - ID: "testid", - FirstName: "testfirstname2", - LastName: "testlastname2", - Points: 340, - Timezone: "America/New York", - Description: "Hello", - Discord: "testdiscordusername2", - AvatarUrl: "https://yt3.ggpht.com/ytc/AAUvwniHNhQyp4hWj3nrADnils-6N3jNREP8rWKGDTp0Lg=s900-c-k-c0x00ffffff-no-rj", - TeamStatus: "NOT_LOOKING", - Interests: []string{"testinterest2"}, + ID: "testid", + FirstName: "testfirstname2", + LastName: "testlastname2", + Points: 340, + IsVirtual: true, } if !reflect.DeepEqual(updated_profile, &expected_profile) { t.Errorf("Wrong profile info. Expected %v, got %v", expected_profile, updated_profile) } - - CleanupTestDB(t) } func TestGetFilteredProfiles(t *testing.T) { SetupTestDB(t) + defer CleanupTestDB(t) profile := models.Profile{ - ID: "testid2", - FirstName: "testfirstname2", - LastName: "testlastname2", - Points: 340, - Timezone: "America/New York", - Description: "Hello", - Discord: "testdiscordusername2", - AvatarUrl: "https://yt3.ggpht.com/ytc/AAUvwniHNhQyp4hWj3nrADnils-6N3jNREP8rWKGDTp0Lg=s900-c-k-c0x00ffffff-no-rj", - TeamStatus: "NOT_LOOKING", - Interests: []string{"Cpp", "Machine Learning", "Additional Interest"}, + ID: "testid2", + FirstName: "testfirstname2", + LastName: "testlastname2", + Points: 340, + IsVirtual: true, } err := db.Insert("profiles", &profile) profile = models.Profile{ - ID: "testid3", - FirstName: "testfirstname3", - LastName: "testlastname3", - Points: 342, - Timezone: "America/New York", - Description: "Hello", - Discord: "testdiscordusername3", - AvatarUrl: "https://yt3.ggpht.com/ytc/AAUvwniHNhQyp4hWj3nrADnils-6N3jNREP8rWKGDTp0Lg=s900-c-k-c0x00ffffff-no-rj", - TeamStatus: "NOT_LOOKING", - Interests: []string{"Cpp", "Machine Learning"}, + ID: "testid3", + FirstName: "testfirstname3", + LastName: "testlastname3", + Points: 342, + IsVirtual: true, } err = db.Insert("profiles", &profile) parameters := map[string][]string{ - "teamStatus": {"NOT_LOOKING"}, - "interests": {"Cpp,Machine Learning"}, - "limit": {"0"}, + "IsVirtual": {"true"}, + "limit": {"0"}, } filtered_profile_list, err := service.GetFilteredProfiles(parameters) @@ -420,28 +359,18 @@ func TestGetFilteredProfiles(t *testing.T) { expected_filtered_profile_list := models.ProfileList{ Profiles: []models.Profile{ { - ID: "testid2", - FirstName: "testfirstname2", - LastName: "testlastname2", - Points: 340, - Timezone: "America/New York", - Description: "Hello", - Discord: "testdiscordusername2", - AvatarUrl: "https://yt3.ggpht.com/ytc/AAUvwniHNhQyp4hWj3nrADnils-6N3jNREP8rWKGDTp0Lg=s900-c-k-c0x00ffffff-no-rj", - TeamStatus: "NOT_LOOKING", - Interests: []string{"Cpp", "Machine Learning", "Additional Interest"}, + ID: "testid2", + FirstName: "testfirstname2", + LastName: "testlastname2", + Points: 340, + IsVirtual: true, }, { - ID: "testid3", - FirstName: "testfirstname3", - LastName: "testlastname3", - Points: 342, - Timezone: "America/New York", - Description: "Hello", - Discord: "testdiscordusername3", - AvatarUrl: "https://yt3.ggpht.com/ytc/AAUvwniHNhQyp4hWj3nrADnils-6N3jNREP8rWKGDTp0Lg=s900-c-k-c0x00ffffff-no-rj", - TeamStatus: "NOT_LOOKING", - Interests: []string{"Cpp", "Machine Learning"}, + ID: "testid3", + FirstName: "testfirstname3", + LastName: "testlastname3", + Points: 342, + IsVirtual: true, }, }, } @@ -452,9 +381,8 @@ func TestGetFilteredProfiles(t *testing.T) { // Add a limit and test that parameters = map[string][]string{ - "teamStatus": {"NOT_LOOKING"}, - "interests": {"Cpp,Machine Learning"}, - "limit": {"1"}, + "IsVirtual": {"true"}, + "limit": {"1"}, } filtered_profile_list, err = service.GetFilteredProfiles(parameters) @@ -462,46 +390,11 @@ func TestGetFilteredProfiles(t *testing.T) { expected_filtered_profile_list = models.ProfileList{ Profiles: []models.Profile{ { - ID: "testid2", - FirstName: "testfirstname2", - LastName: "testlastname2", - Points: 340, - Timezone: "America/New York", - Description: "Hello", - Discord: "testdiscordusername2", - AvatarUrl: "https://yt3.ggpht.com/ytc/AAUvwniHNhQyp4hWj3nrADnils-6N3jNREP8rWKGDTp0Lg=s900-c-k-c0x00ffffff-no-rj", - TeamStatus: "NOT_LOOKING", - Interests: []string{"Cpp", "Machine Learning", "Additional Interest"}, - }, - }, - } - - if !reflect.DeepEqual(filtered_profile_list, &expected_filtered_profile_list) { - t.Errorf("Wrong profile list. Expected %v, got %v", expected_filtered_profile_list, filtered_profile_list) - } - - // Change the interests to be off by one - parameters = map[string][]string{ - "teamStatus": {"NOT_LOOKING"}, - "interests": {"Cpp,Machine Learning,Additional Interest"}, - "limit": {"0"}, - } - - filtered_profile_list, err = service.GetFilteredProfiles(parameters) - - expected_filtered_profile_list = models.ProfileList{ - Profiles: []models.Profile{ - { - ID: "testid2", - FirstName: "testfirstname2", - LastName: "testlastname2", - Points: 340, - Timezone: "America/New York", - Description: "Hello", - Discord: "testdiscordusername2", - AvatarUrl: "https://yt3.ggpht.com/ytc/AAUvwniHNhQyp4hWj3nrADnils-6N3jNREP8rWKGDTp0Lg=s900-c-k-c0x00ffffff-no-rj", - TeamStatus: "NOT_LOOKING", - Interests: []string{"Cpp", "Machine Learning", "Additional Interest"}, + ID: "testid2", + FirstName: "testfirstname2", + LastName: "testlastname2", + Points: 340, + IsVirtual: true, }, }, } @@ -510,10 +403,9 @@ func TestGetFilteredProfiles(t *testing.T) { t.Errorf("Wrong profile list. Expected %v, got %v", expected_filtered_profile_list, filtered_profile_list) } - // Remove filter by interests + // Remove filter by virtual status parameters = map[string][]string{ - "teamStatus": {"NOT_LOOKING"}, - "limit": {"0"}, + "limit": {"0"}, } filtered_profile_list, err = service.GetFilteredProfiles(parameters) @@ -521,57 +413,25 @@ func TestGetFilteredProfiles(t *testing.T) { expected_filtered_profile_list = models.ProfileList{ Profiles: []models.Profile{ { - ID: "testid2", - FirstName: "testfirstname2", - LastName: "testlastname2", - Points: 340, - Timezone: "America/New York", - Description: "Hello", - Discord: "testdiscordusername2", - AvatarUrl: "https://yt3.ggpht.com/ytc/AAUvwniHNhQyp4hWj3nrADnils-6N3jNREP8rWKGDTp0Lg=s900-c-k-c0x00ffffff-no-rj", - TeamStatus: "NOT_LOOKING", - Interests: []string{"Cpp", "Machine Learning", "Additional Interest"}, + ID: "testid", + FirstName: "testfirstname", + LastName: "testlastname", + Points: 0, + IsVirtual: false, }, { - ID: "testid3", - FirstName: "testfirstname3", - LastName: "testlastname3", - Points: 342, - Timezone: "America/New York", - Description: "Hello", - Discord: "testdiscordusername3", - AvatarUrl: "https://yt3.ggpht.com/ytc/AAUvwniHNhQyp4hWj3nrADnils-6N3jNREP8rWKGDTp0Lg=s900-c-k-c0x00ffffff-no-rj", - TeamStatus: "NOT_LOOKING", - Interests: []string{"Cpp", "Machine Learning"}, + ID: "testid2", + FirstName: "testfirstname2", + LastName: "testlastname2", + Points: 340, + IsVirtual: true, }, - }, - } - - if !reflect.DeepEqual(filtered_profile_list, &expected_filtered_profile_list) { - t.Errorf("Wrong profile list. Expected %v, got %v", expected_filtered_profile_list, filtered_profile_list) - } - - // Remove filter by teamStatus - parameters = map[string][]string{ - "interests": {"Cpp,Machine Learning,Additional Interest"}, - "limit": {"0"}, - } - - filtered_profile_list, err = service.GetFilteredProfiles(parameters) - - expected_filtered_profile_list = models.ProfileList{ - Profiles: []models.Profile{ { - ID: "testid2", - FirstName: "testfirstname2", - LastName: "testlastname2", - Points: 340, - Timezone: "America/New York", - Description: "Hello", - Discord: "testdiscordusername2", - AvatarUrl: "https://yt3.ggpht.com/ytc/AAUvwniHNhQyp4hWj3nrADnils-6N3jNREP8rWKGDTp0Lg=s900-c-k-c0x00ffffff-no-rj", - TeamStatus: "NOT_LOOKING", - Interests: []string{"Cpp", "Machine Learning", "Additional Interest"}, + ID: "testid3", + FirstName: "testfirstname3", + LastName: "testlastname3", + Points: 342, + IsVirtual: true, }, }, } @@ -579,24 +439,18 @@ func TestGetFilteredProfiles(t *testing.T) { if !reflect.DeepEqual(filtered_profile_list, &expected_filtered_profile_list) { t.Errorf("Wrong profile list. Expected %v, got %v", expected_filtered_profile_list, filtered_profile_list) } - - CleanupTestDB(t) } func TestGetProfileLeaderboard(t *testing.T) { SetupTestDB(t) + defer CleanupTestDB(t) profile := models.Profile{ - ID: "testid2", - FirstName: "testfirstname2", - LastName: "testlastname2", - Points: 340, - Timezone: "America/New York", - Description: "Hello", - Discord: "testdiscordusername2", - AvatarUrl: "https://yt3.ggpht.com/ytc/AAUvwniHNhQyp4hWj3nrADnils-6N3jNREP8rWKGDTp0Lg=s900-c-k-c0x00ffffff-no-rj", - TeamStatus: "NOT_LOOKING", - Interests: []string{"Cpp", "Machine Learning", "Additional Interest"}, + ID: "testid2", + FirstName: "testfirstname2", + LastName: "testlastname2", + Points: 340, + IsVirtual: true, } err := db.Insert("profiles", &profile) @@ -636,16 +490,11 @@ func TestGetProfileLeaderboard(t *testing.T) { // Insert another profile and test profile = models.Profile{ - ID: "testid3", - FirstName: "testfirstname3", - LastName: "testlastname3", - Points: 999, - Timezone: "America/New York", - Description: "Hello", - Discord: "testdiscordusername3", - AvatarUrl: "https://yt3.ggpht.com/ytc/AAUvwniHNhQyp4hWj3nrADnils-6N3jNREP8rWKGDTp0Lg=s900-c-k-c0x00ffffff-no-rj", - TeamStatus: "NOT_LOOKING", - Interests: []string{"Cpp"}, + ID: "testid3", + FirstName: "testfirstname3", + LastName: "testlastname3", + Points: 999, + IsVirtual: true, } err = db.Insert("profiles", &profile) @@ -720,44 +569,33 @@ func TestGetProfileLeaderboard(t *testing.T) { if !reflect.DeepEqual(leaderboard, &expected_leaderboard) { t.Errorf("Wrong profile info. Expected %v, got %v", expected_leaderboard, leaderboard) } - - CleanupTestDB(t) } func TestGetValidFilteredProfiles(t *testing.T) { SetupTestDB(t) + defer CleanupTestDB(t) profile := models.Profile{ - ID: "testid2", - FirstName: "testfirstname2", - LastName: "testlastname2", - Points: 340, - Timezone: "America/New York", - Description: "Hello", - Discord: "testdiscordusername2", - AvatarUrl: "https://yt3.ggpht.com/ytc/AAUvwniHNhQyp4hWj3nrADnils-6N3jNREP8rWKGDTp0Lg=s900-c-k-c0x00ffffff-no-rj", - TeamStatus: "NOT_LOOKING", - Interests: []string{"Cpp", "Machine Learning", "Additional Interest"}, + ID: "testid2", + FirstName: "testfirstname2", + LastName: "testlastname2", + Points: 340, + IsVirtual: false, } err := db.Insert("profiles", &profile) profile = models.Profile{ - ID: "testid3", - FirstName: "testfirstname3", - LastName: "testlastname3", - Points: 342, - Timezone: "America/New York", - Description: "Hello", - Discord: "testdiscordusername3", - AvatarUrl: "https://yt3.ggpht.com/ytc/AAUvwniHNhQyp4hWj3nrADnils-6N3jNREP8rWKGDTp0Lg=s900-c-k-c0x00ffffff-no-rj", - TeamStatus: "NOT_LOOKING", - Interests: []string{"Cpp", "Machine Learning"}, + ID: "testid3", + FirstName: "testfirstname3", + LastName: "testlastname3", + Points: 342, + IsVirtual: true, } err = db.Insert("profiles", &profile) parameters := map[string][]string{ - "interests": {"Cpp,Machine Learning"}, + "IsVirtual": {"true"}, "limit": {"0"}, } @@ -770,35 +608,29 @@ func TestGetValidFilteredProfiles(t *testing.T) { expected_filtered_profile_list := models.ProfileList{ Profiles: []models.Profile{ { - ID: "testid3", - FirstName: "testfirstname3", - LastName: "testlastname3", - Points: 342, - Timezone: "America/New York", - Description: "Hello", - Discord: "testdiscordusername3", - AvatarUrl: "https://yt3.ggpht.com/ytc/AAUvwniHNhQyp4hWj3nrADnils-6N3jNREP8rWKGDTp0Lg=s900-c-k-c0x00ffffff-no-rj", - TeamStatus: "NOT_LOOKING", - Interests: []string{"Cpp", "Machine Learning"}, + ID: "testid3", + FirstName: "testfirstname3", + LastName: "testlastname3", + Points: 342, + IsVirtual: true, }, }, } + if !reflect.DeepEqual(filtered_profile_list, &expected_filtered_profile_list) { + t.Errorf("Wrong profile list. Expected %v, got %v", expected_filtered_profile_list, filtered_profile_list) + } + profile = models.Profile{ - ID: "testid4", - FirstName: "testfirstname3", - LastName: "testlastname3", - Points: 342, - Timezone: "America/New York", - Description: "Hello", - Discord: "testdiscordusername3", - AvatarUrl: "https://yt3.ggpht.com/ytc/AAUvwniHNhQyp4hWj3nrADnils-6N3jNREP8rWKGDTp0Lg=s900-c-k-c0x00ffffff-no-rj", - TeamStatus: "LOOKING_FOR_TEAM", - Interests: []string{}, + ID: "testid4", + FirstName: "testfirstname4", + LastName: "testlastname4", + Points: 342, + IsVirtual: false, } err = db.Insert("profiles", &profile) - // Remove the interests filter. Now every profile should show up except for those that are "NOT_LOOKING" for a team. + // Remove the virtual status filter. Now every profile should show up. parameters = map[string][]string{ "limit": {"0"}, @@ -813,98 +645,50 @@ func TestGetValidFilteredProfiles(t *testing.T) { expected_filtered_profile_list = models.ProfileList{ Profiles: []models.Profile{ { - ID: "testid", - FirstName: "testfirstname", - LastName: "testlastname", - Points: 0, - Timezone: "America/Chicago", - Description: "Hi", - Discord: "testdiscordusername", - AvatarUrl: "https://imgs.smoothradio.com/images/191589?crop=16_9&width=660&relax=1&signature=Rz93ikqcAz7BcX6SKiEC94zJnqo=", - TeamStatus: "LOOKING_FOR_TEAM", - Interests: []string{"testinterest1", "testinterest2"}, + ID: "testid", + FirstName: "testfirstname", + LastName: "testlastname", + Points: 0, + IsVirtual: false, }, { - ID: "testid4", - FirstName: "testfirstname3", - LastName: "testlastname3", - Points: 342, - Timezone: "America/New York", - Description: "Hello", - Discord: "testdiscordusername3", - AvatarUrl: "https://yt3.ggpht.com/ytc/AAUvwniHNhQyp4hWj3nrADnils-6N3jNREP8rWKGDTp0Lg=s900-c-k-c0x00ffffff-no-rj", - TeamStatus: "LOOKING_FOR_TEAM", - Interests: []string{}, + ID: "testid2", + FirstName: "testfirstname2", + LastName: "testlastname2", + Points: 340, + IsVirtual: false, }, - }, - } - if !reflect.DeepEqual(filtered_profile_list, &expected_filtered_profile_list) { - t.Errorf("Wrong profile list. Expected %v, got %v", expected_filtered_profile_list, filtered_profile_list) - } - - // Add a TeamStatus filter. - - parameters = map[string][]string{ - "teamStatus": {"LOOKING_FOR_TEAM"}, - "limit": {"0"}, - } - - filtered_profile_list, err = service.GetValidFilteredProfiles(parameters) - - if err != nil { - t.Fatal(err) - } - - expected_filtered_profile_list = models.ProfileList{ - Profiles: []models.Profile{ { - ID: "testid", - FirstName: "testfirstname", - LastName: "testlastname", - Points: 0, - Timezone: "America/Chicago", - Description: "Hi", - Discord: "testdiscordusername", - AvatarUrl: "https://imgs.smoothradio.com/images/191589?crop=16_9&width=660&relax=1&signature=Rz93ikqcAz7BcX6SKiEC94zJnqo=", - TeamStatus: "LOOKING_FOR_TEAM", - Interests: []string{"testinterest1", "testinterest2"}, + ID: "testid3", + FirstName: "testfirstname3", + LastName: "testlastname3", + Points: 342, + IsVirtual: true, }, { - ID: "testid4", - FirstName: "testfirstname3", - LastName: "testlastname3", - Points: 342, - Timezone: "America/New York", - Description: "Hello", - Discord: "testdiscordusername3", - AvatarUrl: "https://yt3.ggpht.com/ytc/AAUvwniHNhQyp4hWj3nrADnils-6N3jNREP8rWKGDTp0Lg=s900-c-k-c0x00ffffff-no-rj", - TeamStatus: "LOOKING_FOR_TEAM", - Interests: []string{}, + ID: "testid4", + FirstName: "testfirstname4", + LastName: "testlastname4", + Points: 342, + IsVirtual: false, }, }, } - if !reflect.DeepEqual(filtered_profile_list, &expected_filtered_profile_list) { t.Errorf("Wrong profile list. Expected %v, got %v", expected_filtered_profile_list, filtered_profile_list) } - - CleanupTestDB(t) } func TestProfileFavorites(t *testing.T) { SetupTestDB(t) + defer CleanupTestDB(t) profile := models.Profile{ - ID: "testid2", - FirstName: "testfirstname2", - LastName: "testlastname2", - Points: 340, - Timezone: "America/New York", - Description: "Hello", - Discord: "testdiscordusername2", - AvatarUrl: "https://yt3.ggpht.com/ytc/AAUvwniHNhQyp4hWj3nrADnils-6N3jNREP8rWKGDTp0Lg=s900-c-k-c0x00ffffff-no-rj", - TeamStatus: "Not Looking", - Interests: []string{"Cpp", "Machine Learning", "Additional Interest"}, + ID: "testid2", + FirstName: "testfirstname2", + LastName: "testlastname2", + Points: 340, + IsVirtual: true, } err := db.Insert("profiles", &profile) @@ -967,16 +751,11 @@ func TestProfileFavorites(t *testing.T) { // Favorite another profile profile = models.Profile{ - ID: "testid3", - FirstName: "testfirstname3", - LastName: "testlastname3", - Points: 342, - Timezone: "America/New York", - Description: "Hello", - Discord: "testdiscordusername3", - AvatarUrl: "https://yt3.ggpht.com/ytc/AAUvwniHNhQyp4hWj3nrADnils-6N3jNREP8rWKGDTp0Lg=s900-c-k-c0x00ffffff-no-rj", - TeamStatus: "Found Team", - Interests: []string{"Cpp", "Machine Learning"}, + ID: "testid3", + FirstName: "testfirstname3", + LastName: "testlastname3", + Points: 342, + IsVirtual: true, } err = db.Insert("profiles", &profile) if err != nil { @@ -1013,6 +792,4 @@ func TestProfileFavorites(t *testing.T) { if !reflect.DeepEqual(profile_favorites, &expected_profile_favorites) { t.Errorf("Wrong favorite profile list. Expected %v, got %v", expected_profile_favorites, profile_favorites) } - - CleanupTestDB(t) } From 687dede45f09deeee1d53c9154f7ab20b1befa0b Mon Sep 17 00:00:00 2001 From: Jareth Gomes Date: Fri, 24 Dec 2021 16:08:30 -0600 Subject: [PATCH 09/30] Fixed test error in getValidFilteredProperties --- services/profile/service/profile_service.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/services/profile/service/profile_service.go b/services/profile/service/profile_service.go index 0c2ec4df..62fd3f24 100644 --- a/services/profile/service/profile_service.go +++ b/services/profile/service/profile_service.go @@ -295,9 +295,10 @@ func GetFilteredProfiles(parameters map[string][]string) (*models.ProfileList, e /* Returns a list of profiles filtered upon teamStatus and interests. Will be limited to only include the first "limit" results. Will also remove profiles with a TeamStatus set to "NOT_LOOKING" + + Sorta redundant since the Profile model doesn't have team status or interest fields anymore */ func GetValidFilteredProfiles(parameters map[string][]string) (*models.ProfileList, error) { - parameters["teamStatusNot"] = append(parameters["teamStatusNot"], "NOT_LOOKING") filtered_profile_list, err := GetFilteredProfiles(parameters) if err != nil { From 977aa5b01dd5d971ea262cff5f42e668fa488388 Mon Sep 17 00:00:00 2001 From: Jareth Gomes Date: Fri, 24 Dec 2021 16:23:10 -0600 Subject: [PATCH 10/30] Updated event service docs --- .../docs/reference/services/Event.md | 85 ++++++++++++++----- 1 file changed, 64 insertions(+), 21 deletions(-) diff --git a/documentation/docs/reference/services/Event.md b/documentation/docs/reference/services/Event.md index 99aac9bf..64b20c8b 100644 --- a/documentation/docs/reference/services/Event.md +++ b/documentation/docs/reference/services/Event.md @@ -23,7 +23,10 @@ Response format: } ], "sponsor": "Example sponsor", - "eventType": "WORKSHOP" + "eventType": "WORKSHOP", + "inPersonPoints": 10, + "inPersonVirtPoints": 5, + "virtualPoints": 3 } ``` @@ -51,7 +54,10 @@ Response format: } ], "sponsor": "Example sponsor", - "eventType": "WORKSHOP" + "eventType": "WORKSHOP", + "inPersonPoints": 10, + "inPersonVirtPoints": 5, + "virtualPoints": 3 }, { "id": "52fdfcab71282654f163f5f0f9a621d72", @@ -68,7 +74,10 @@ Response format: } ], "sponsor": "Example sponsor", - "eventType": "WORKSHOP" + "eventType": "WORKSHOP", + "inPersonPoints": 20, + "inPersonVirtPoints": 10, + "virtualPoints": 5 } ] } @@ -98,11 +107,14 @@ Response format: } ], "sponsor": "Example sponsor", - "eventType": "WORKSHOP" + "eventType": "WORKSHOP", + "inPersonPoints": 10, + "inPersonVirtPoints": 5, + "virtualPoints": 3 }, { "id": "9566c74d10037c4d7bbb0407d1e2c649", - "name": "Example Event 10", + "name": "Example Event 11", "description": "This is a description", "startTime": 1532202702, "endTime": 1532212702, @@ -115,7 +127,10 @@ Response format: } ], "sponsor": "Example sponsor", - "eventType": "WORKSHOP" + "eventType": "WORKSHOP", + "inPersonPoints": 20, + "inPersonVirtPoints": 10, + "virtualPoints": 5 } ] } @@ -144,7 +159,10 @@ Request format: "latitude": 40.1138, "longitude": -88.2249 } - ] + ], + "inPersonPoints": 10, + "inPersonVirtPoints": 5, + "virtualPoints": 3 } ``` @@ -165,7 +183,10 @@ Response format: } ], "sponsor": "Example sponsor", - "eventType": "WORKSHOP" + "eventType": "WORKSHOP", + "inPersonPoints": 10, + "inPersonVirtPoints": 5, + "virtualPoints": 3 } ``` @@ -191,7 +212,10 @@ Response format: } ], "sponsor": "Example sponsor", - "eventType": "WORKSHOP" + "eventType": "WORKSHOP", + "inPersonPoints": 10, + "inPersonVirtPoints": 7, + "virtualPoints": 3 } ``` @@ -217,7 +241,10 @@ Request format: "latitude": 40.1138, "longitude": -88.2249 } - ] + ], + "inPersonPoints": 20, + "inPersonVirtPoints": 10, + "virtualPoints": 5 } ``` @@ -238,7 +265,10 @@ Response format: } ], "sponsor": "Example sponsor", - "eventType": "WORKSHOP" + "eventType": "WORKSHOP", + "inPersonPoints": 20, + "inPersonVirtPoints": 10, + "virtualPoints": 5 } ``` @@ -367,28 +397,38 @@ Response format: GET /event/code/{id}/ ---------------------------- -Gets a struct that contains information about the event code (generated upon event creation) and expiration time. +Gets an array of structs that contains information about the event codes (generated upon event creation) and expiration times. By convention, event checkin codes will be 6 bytes long. Response format: ``` -{ - "id": "52fdfc072182654f163f5f0f9a621d72", - "code": "sample_code", - "expiration": 1521388800 -} - +[ + { + "id": "52fdfc072182654f163f5f0f9a621d72", + "code": "sample_code_1", + "isVirtual": false, + "expiration": 1521388800 + }, + { + "id": "52fdfc072182654f163f5f0f9a621d72", + "code": "sample_code_2", + "isVirtual": true, + "expiration": 1521388800 + } +] ``` -PUT /event/code/{id}/ +POST /event/code/ ---------------------------- -Updates a struct that contains information about the event code (generated upon event creation) and expiration time. +Creates/updates a struct that contains information about the event code (generated upon event creation) and expiration time. Request format: ``` { + "id": "52fdfc072182654f163f5f0f9a621d72", "code": "new_code", + "isVirtual": true, "expiration": 1521388800 } @@ -397,8 +437,9 @@ Request format: Response format: ``` { - "id": "52fdfc072182654f163f5f0f9a621d72", + "id": "52fdfc072182654f163f5f0f9a621d72", "code": "new_code", + "isVirtual": true, "expiration": 1521388800 } ``` @@ -409,6 +450,8 @@ POST /event/checkin/ Retrieves a struct that contains information about the event checkin status, point increment value, and total point number. Takes in a struct that contains an event checkin code. +The endpoint will check the use HackIllinois-Identity field to determine if the user redeeming the points registered for virtual or in-person. It will compare this to the scanned code and will award points accordingly. (e.g. If registered for virtual, give `virtualPoints`. If registered for in-person but attended virtually, give `inPersonVirtPoints`. If registered for in-person and attended in-person, give `inPersonPoints`.) + Valid values for `status` are `Success`, `InvalidCode`, `InvalidTime`, `AlreadyCheckedIn`. When `status != Success`, the `newPoints` and `totalPoints` fields will equal `-1` and should be ignored. Request format: From 0f6898c48a68913d874624c376a90fb0cb01631d Mon Sep 17 00:00:00 2001 From: Jareth Gomes Date: Fri, 24 Dec 2021 16:31:11 -0600 Subject: [PATCH 11/30] Updated profile service docs --- .../docs/reference/services/Profile.md | 120 ++++++------------ 1 file changed, 39 insertions(+), 81 deletions(-) diff --git a/documentation/docs/reference/services/Profile.md b/documentation/docs/reference/services/Profile.md index 90d85b27..d91c4d08 100644 --- a/documentation/docs/reference/services/Profile.md +++ b/documentation/docs/reference/services/Profile.md @@ -6,8 +6,6 @@ GET /profile/ Returns the profile stored for the current user. -Valid values for ``teamStatus`` are ``LOOKING_FOR_MEMBERS``, ``LOOKING_FOR_TEAM``, and ``NOT_LOOKING``. - ***The `id` in the profile service refers to a separate, randomly-generated, profile-only id. This is different from the (user) `id` used in other services. When a profile is created, a mapping from the user `id` to the profile `id` is stored in the database.*** We will distinguish user id and profile id by using `github123456` and `profileid123456` for each, respectively, in the examples below. @@ -19,12 +17,7 @@ Response format: "firstName": "John", "lastName": "Doe", "points": 2021, - "timezone": "Americas UTC+8", - "avatarUrl": "https://github.com/.../profile.jpg", - "discord": "patrick#1234", - "teamStatus": "LOOKING_FOR_TEAM", - "description": "Lorem Ipsum…", - "interests": ["C++", "Machine Learning"] + "isVirtual": false } ``` @@ -32,7 +25,7 @@ Response format: GET /profile/{id}/ ------------------------- -Returns the profile stored for user that has the ID ``{id}``. +Returns the profile stored for user that has the profile ID ``{id}``. Response format: ``` @@ -41,16 +34,29 @@ Response format: "firstName": "John", "lastName": "Doe", "points": 2021, - "timezone": "Americas UTC+8", - "avatarUrl": "https://github.com/.../profile.jpg", - "discord": "patrick#1234", - "teamStatus": "LOOKING_FOR_TEAM", - "description": "Lorem Ipsum…", - "interests": ["C++", "Machine Learning"] + "isVirtual": false } ``` -GET /profile/list/?teamStatus=value&interests=value,value,value&limit=value +GET /profile/user/{id}/ +------------------------- + +**Internal use only.** + +Returns the profile stored for user that has the user ID ``{id}``. + +Response format: +``` +{ + "id": "profileid123456", + "firstName": "John", + "lastName": "Doe", + "points": 2021, + "isVirtual": false +} +``` + +GET /profile/list/?isVirtual=value&interests=value,value,value&limit=value ------------------------- **Internal use only.** @@ -61,8 +67,6 @@ teamStatus is a string matching the user's team status. interests is a comma-separated string representing the user's interests. -- i.e if the user's interests are ["C++", "Machine Learning"], you can filter on this by sending ``interests="C++,Machine Learning"`` - If a ``limit`` parameter is provided, it will return the first matching ``limit`` profiles. Otherwise, it will return all of the matched profiles. If no parameters are provided, it returns all profiles that are in the database. @@ -76,24 +80,14 @@ Response format: "firstName": "John", "lastName": "Doe", "points": 2021, - "timezone": "Americas UTC+8", - "avatarUrl": "https://github.com/.../profile.jpg", - "discord": "patrick#1234", - "teamStatus": "LOOKING_FOR_TEAM", - "description": "Lorem Ipsum…", - "interests": ["C++", "Machine Learning"] + "isVirtual": false }, { "id": "profileid123456", "firstName": "John", "lastName": "Doe", "points": 2021, - "timezone": "Americas UTC+8", - "avatarUrl": "https://github.com/.../profile.jpg", - "discord": "patrick#1234", - "teamStatus": "LOOKING_FOR_MEMBERS", - "description": "Lorem Ipsum…", - "interests": ["C++", "Machine Learning"] + "isVirtual": false }, ] } @@ -109,12 +103,7 @@ Request format: { "firstName": "John", "lastName": "Doe", - "timezone": "Americas UTC+8", - "avatarUrl": "https://github.com/.../profile.jpg", - "discord": "patrick#1234", - "teamStatus": "LOOKING_FOR_TEAM", - "description": "Lorem Ipsum…", - "interests": ["C++", "Machine Learning"] + "isVirtual": false } ``` @@ -124,13 +113,8 @@ Response format: "id": "profileid123456", "firstName": "John", "lastName": "Doe", - "points": 2021, - "timezone": "Americas UTC+8", - "avatarUrl": "https://github.com/.../profile.jpg", - "discord": "patrick#1234", - "teamStatus": "LOOKING_FOR_TEAM", - "description": "Lorem Ipsum…", - "interests": ["C++", "Machine Learning"] + "points": 0, + "isVirtual": false } ``` @@ -144,14 +128,10 @@ Note you can not edit the ``points`` field through this. Request format: ``` { + "id": "profileid123456", "firstName": "John", "lastName": "Doe", - "timezone": "Americas UTC+8", - "avatarUrl": "https://github.com/.../profile.jpg", - "discord": "patrick#1234", - "teamStatus": "LOOKING_FOR_TEAM", - "description": "Lorem Ipsum…", - "interests": ["C++", "Machine Learning"] + "isVirtual": true } ``` @@ -161,13 +141,8 @@ Response format: "id": "profileid123456", "firstName": "John", "lastName": "Doe", - "points": 2021, - "timezone": "Americas UTC+8", - "avatarUrl": "https://github.com/.../profile.jpg", - "discord": "patrick#1234", - "teamStatus": "LOOKING_FOR_TEAM", - "description": "Lorem Ipsum…", - "interests": ["C++", "Machine Learning"] + "points": 0, + "isVirtual": true } ``` @@ -186,13 +161,8 @@ Response format: "id": "profileid123456", "firstName": "John", "lastName": "Doe", - "points": 2021, - "timezone": "Americas UTC+8", - "avatarUrl": "https://github.com/.../profile.jpg", - "discord": "patrick#1234", - "teamStatus": "LOOKING_FOR_TEAM", - "description": "Lorem Ipsum…", - "interests": ["C++", "Machine Learning"] + "points": 0, + "isVirtual": false } ``` @@ -223,21 +193,13 @@ Response format: } ``` -GET /profile/search/?teamStatus=value&interests=value,value,value&limit=value +GET /profile/search/?isVirtual=value&interests=value,value,value&limit=value ------------------------- -Returns a list of profiles matching the filter conditions. - -``teamStatus`` is a string matching the user's team status. Valid values for ``teamStatus`` are ``LOOKING_FOR_MEMBERS``, ``LOOKING_FOR_TEAM``, and ``NOT_LOOKING``. - -interests is a comma-separated string representing the user's interests. - -- i.e if the user's interests are ["C++", "Machine Learning"], you can filter on this by sending ``interests="C++,Machine Learning"`` +Returns a list of profiles matching the filter conditions. If a ``limit`` parameter is provided, it will return the first matching ``limit`` profiles. Otherwise, it will return all of the matched profiles. -Any users with the TeamStatus "NOT_LOOKING" will be removed. - Response format: ``` { @@ -247,12 +209,14 @@ Response format: "firstName": "John", "lastName": "Doe", "points": 2021, + "isVirtual": false }, { "id": "profileid123456", "firstName": "John", "lastName": "Doe", "points": 2021, + "isVirtual": false }, ] } @@ -308,14 +272,8 @@ Response format: "id": "profileid123456", "firstName": "John", "lastName": "Doe", - "points": 2021, - "timezone": "Americas UTC+8", - "avatarUrl": "https://github.com/.../profile.jpg", - "discord": "patrick#1234", - "teamStatus": "LOOKING_FOR_TEAM", - "description": "Lorem Ipsum…", - "interests": ["C++", "Machine Learning"] - "points": 10 + "points": 10, + "isVirtual": false } ``` From 3d68be6abdd153231e5a64e3a57702bdb4ab3430 Mon Sep 17 00:00:00 2001 From: Jareth Gomes Date: Mon, 27 Dec 2021 19:15:16 -0600 Subject: [PATCH 12/30] Renamed function GetEventCode to GetEventCodes --- services/event/controller/controller.go | 4 ++-- services/event/service/event_service.go | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/services/event/controller/controller.go b/services/event/controller/controller.go index 4722c7fa..2945b9a4 100644 --- a/services/event/controller/controller.go +++ b/services/event/controller/controller.go @@ -162,7 +162,7 @@ func GetEventCode(w http.ResponseWriter, r *http.Request) { return } - codes, err := service.GetEventCode(id) + codes, err := service.GetEventCodes(id) if err != nil { errors.WriteError(w, r, errors.DatabaseError(err.Error(), "Failed to receive event code information from database")) @@ -186,7 +186,7 @@ func UpdateEventCode(w http.ResponseWriter, r *http.Request) { return } - updated_codes, err := service.GetEventCode(eventCode.ID) + updated_codes, err := service.GetEventCodes(eventCode.ID) if err != nil { errors.WriteError(w, r, errors.DatabaseError(err.Error(), "Could not get updated event code and timestamp details.")) diff --git a/services/event/service/event_service.go b/services/event/service/event_service.go index d8801ad6..c2187b36 100644 --- a/services/event/service/event_service.go +++ b/services/event/service/event_service.go @@ -499,7 +499,7 @@ func CanRedeemPoints(event_code string) (bool, bool, string, error) { /* Returns the eventcode struct for the event with the given id */ -func GetEventCode(id string) (*[]models.EventCode, error) { +func GetEventCodes(id string) (*[]models.EventCode, error) { query := database.QuerySelector{ "id": id, } From 66f979eeb765c3f3f8c63d3c1b51ba899ef22148 Mon Sep 17 00:00:00 2001 From: Jareth Gomes Date: Mon, 27 Dec 2021 19:16:21 -0600 Subject: [PATCH 13/30] Fixed typo in function docs --- services/event/service/event_service.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/event/service/event_service.go b/services/event/service/event_service.go index c2187b36..f297c029 100644 --- a/services/event/service/event_service.go +++ b/services/event/service/event_service.go @@ -517,7 +517,7 @@ func GetEventCodes(id string) (*[]models.EventCode, error) { /* Updates the event code and end time with the given id - If no matching code is not found, then it is a new code and add it + If no matching code is found, then it is a new code and add it */ func UpdateEventCode(code string, eventCode models.EventCode) error { selector := database.QuerySelector{ From 576811a678d3d9502187064b141e6c33ca04de04 Mon Sep 17 00:00:00 2001 From: Jareth Gomes Date: Mon, 27 Dec 2021 19:19:11 -0600 Subject: [PATCH 14/30] Added user_id to be printed on error --- services/profile/controller/controller.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/profile/controller/controller.go b/services/profile/controller/controller.go index 73d601ae..04480d7e 100644 --- a/services/profile/controller/controller.go +++ b/services/profile/controller/controller.go @@ -83,7 +83,7 @@ func GetProfileByUserId(w http.ResponseWriter, r *http.Request) { profile_id, err := service.GetProfileIdFromUserId(user_id) if err != nil { - errors.WriteError(w, r, errors.DatabaseError(err.Error(), "Could not get profile id associated with the user")) + errors.WriteError(w, r, errors.DatabaseError(err.Error(), "Could not get profile id associated with the user id "+user_id)) return } From 911610aa58839a7ae857b7fc88e407115bb087fb Mon Sep 17 00:00:00 2001 From: Jareth Gomes Date: Sun, 2 Jan 2022 17:01:50 -0600 Subject: [PATCH 15/30] Changed route handler to Upsert from Update --- documentation/docs/reference/services/Event.md | 2 +- services/event/controller/controller.go | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/documentation/docs/reference/services/Event.md b/documentation/docs/reference/services/Event.md index 64b20c8b..94c05d32 100644 --- a/documentation/docs/reference/services/Event.md +++ b/documentation/docs/reference/services/Event.md @@ -421,7 +421,7 @@ Response format: POST /event/code/ ---------------------------- -Creates/updates a struct that contains information about the event code (generated upon event creation) and expiration time. +Upserts a struct that contains information about the event code (generated upon event creation) and expiration time. Request format: ``` diff --git a/services/event/controller/controller.go b/services/event/controller/controller.go index 2945b9a4..6e77bdc7 100644 --- a/services/event/controller/controller.go +++ b/services/event/controller/controller.go @@ -26,7 +26,7 @@ func SetupController(route *mux.Route) { router.HandleFunc("/", UpdateEvent).Methods("PUT") router.HandleFunc("/", GetAllEvents).Methods("GET") router.HandleFunc("/code/{id}/", GetEventCode).Methods("GET") - router.HandleFunc("/code/", UpdateEventCode).Methods("POST") + router.HandleFunc("/code/", UpsertEventCode).Methods("POST") router.HandleFunc("/checkin/", Checkin).Methods("POST") @@ -173,9 +173,9 @@ func GetEventCode(w http.ResponseWriter, r *http.Request) { } /* - Endpoint to update an event code and end time + Endpoint to upsert an event code and end time */ -func UpdateEventCode(w http.ResponseWriter, r *http.Request) { +func UpsertEventCode(w http.ResponseWriter, r *http.Request) { var eventCode models.EventCode json.NewDecoder(r.Body).Decode(&eventCode) From af23f52917f3a980a911a5b82b061d10c0c67394 Mon Sep 17 00:00:00 2001 From: Jareth Gomes Date: Sun, 2 Jan 2022 17:03:48 -0600 Subject: [PATCH 16/30] Changed GET codes route handler to plural name --- services/event/controller/controller.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/services/event/controller/controller.go b/services/event/controller/controller.go index 6e77bdc7..fa7c50b2 100644 --- a/services/event/controller/controller.go +++ b/services/event/controller/controller.go @@ -25,7 +25,7 @@ func SetupController(route *mux.Route) { router.HandleFunc("/", CreateEvent).Methods("POST") router.HandleFunc("/", UpdateEvent).Methods("PUT") router.HandleFunc("/", GetAllEvents).Methods("GET") - router.HandleFunc("/code/{id}/", GetEventCode).Methods("GET") + router.HandleFunc("/code/{id}/", GetEventCodes).Methods("GET") router.HandleFunc("/code/", UpsertEventCode).Methods("POST") router.HandleFunc("/checkin/", Checkin).Methods("POST") @@ -154,7 +154,7 @@ func UpdateEvent(w http.ResponseWriter, r *http.Request) { /* Endpoint to get the code associated with an event (or nil) */ -func GetEventCode(w http.ResponseWriter, r *http.Request) { +func GetEventCodes(w http.ResponseWriter, r *http.Request) { id := mux.Vars(r)["id"] if id == "" { From 8fe3f1df4232205e9de5bc361c581d0d6b119581 Mon Sep 17 00:00:00 2001 From: Jareth Gomes Date: Sun, 2 Jan 2022 17:25:48 -0600 Subject: [PATCH 17/30] Added requirement property to point fields --- services/event/models/event.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/services/event/models/event.go b/services/event/models/event.go index 15d0bd4a..9a6189c8 100644 --- a/services/event/models/event.go +++ b/services/event/models/event.go @@ -9,9 +9,9 @@ type Event struct { Locations []EventLocation `json:"locations" validate:"required,dive,required"` Sponsor string `json:"sponsor"` EventType string `json:"eventType" validate:"required,oneof=MEAL SPEAKER WORKSHOP MINIEVENT QNA OTHER"` - InPersonPoints int `json:"inPersonPoints"` - InPersonVirtPoints int `json:"inPersonVirtPoints"` - VirtualPoints int `json:"virtualPoints"` + InPersonPoints int `json:"inPersonPoints" validate:"required"` + InPersonVirtPoints int `json:"inPersonVirtPoints" validate:"required"` + VirtualPoints int `json:"virtualPoints" validate:"required"` } type EventLocation struct { From 5eb48aa66434a39fe673c88f6b183c17fae998bf Mon Sep 17 00:00:00 2001 From: Jareth Gomes Date: Sun, 2 Jan 2022 17:40:36 -0600 Subject: [PATCH 18/30] Added required fields to event structs --- services/event/tests/event_test.go | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/services/event/tests/event_test.go b/services/event/tests/event_test.go index ae5af2a1..cb055372 100644 --- a/services/event/tests/event_test.go +++ b/services/event/tests/event_test.go @@ -122,6 +122,9 @@ func TestGetAllEventsService(t *testing.T) { Longitude: 123.456, }, }, + InPersonPoints: 0, + InPersonVirtPoints: 0, + VirtualPoints: 0, } err := db.Insert("events", &event) @@ -425,6 +428,9 @@ func TestCreateEventService(t *testing.T) { Longitude: 123.456, }, }, + InPersonPoints: 0, + InPersonVirtPoints: 0, + VirtualPoints: 0, } err := service.CreateEvent("testid2", "testcode2", new_event) @@ -720,6 +726,9 @@ func TestIsEventActive(t *testing.T) { Longitude: 123.456, }, }, + InPersonPoints: 10, + InPersonVirtPoints: 7, + VirtualPoints: 5, } service.CreateEvent(new_event.ID, "testcode3", new_event) From bee5d901eeadd1b238a4c400cca1285f8badb352 Mon Sep 17 00:00:00 2001 From: Jareth Gomes Date: Sun, 2 Jan 2022 18:36:16 -0600 Subject: [PATCH 19/30] Revert "Added requirement property to point fields" and "Added required fields to event structs" This reverts commit 8fe3f1df4232205e9de5bc361c581d0d6b119581 and 5eb48aa66434a39fe673c88f6b183c17fae998bf. --- services/event/models/event.go | 6 +++--- services/event/tests/event_test.go | 9 --------- 2 files changed, 3 insertions(+), 12 deletions(-) diff --git a/services/event/models/event.go b/services/event/models/event.go index 9a6189c8..15d0bd4a 100644 --- a/services/event/models/event.go +++ b/services/event/models/event.go @@ -9,9 +9,9 @@ type Event struct { Locations []EventLocation `json:"locations" validate:"required,dive,required"` Sponsor string `json:"sponsor"` EventType string `json:"eventType" validate:"required,oneof=MEAL SPEAKER WORKSHOP MINIEVENT QNA OTHER"` - InPersonPoints int `json:"inPersonPoints" validate:"required"` - InPersonVirtPoints int `json:"inPersonVirtPoints" validate:"required"` - VirtualPoints int `json:"virtualPoints" validate:"required"` + InPersonPoints int `json:"inPersonPoints"` + InPersonVirtPoints int `json:"inPersonVirtPoints"` + VirtualPoints int `json:"virtualPoints"` } type EventLocation struct { diff --git a/services/event/tests/event_test.go b/services/event/tests/event_test.go index cb055372..ae5af2a1 100644 --- a/services/event/tests/event_test.go +++ b/services/event/tests/event_test.go @@ -122,9 +122,6 @@ func TestGetAllEventsService(t *testing.T) { Longitude: 123.456, }, }, - InPersonPoints: 0, - InPersonVirtPoints: 0, - VirtualPoints: 0, } err := db.Insert("events", &event) @@ -428,9 +425,6 @@ func TestCreateEventService(t *testing.T) { Longitude: 123.456, }, }, - InPersonPoints: 0, - InPersonVirtPoints: 0, - VirtualPoints: 0, } err := service.CreateEvent("testid2", "testcode2", new_event) @@ -726,9 +720,6 @@ func TestIsEventActive(t *testing.T) { Longitude: 123.456, }, }, - InPersonPoints: 10, - InPersonVirtPoints: 7, - VirtualPoints: 5, } service.CreateEvent(new_event.ID, "testcode3", new_event) From 6073e88a8a26350b8956ef917c308c3227592959 Mon Sep 17 00:00:00 2001 From: Jareth Gomes Date: Mon, 3 Jan 2022 00:41:23 -0600 Subject: [PATCH 20/30] Changed event code model to distingush code id and event id --- services/event/controller/controller.go | 4 ++-- services/event/models/event_code.go | 4 ++-- services/event/service/event_service.go | 12 ++++++------ 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/services/event/controller/controller.go b/services/event/controller/controller.go index fa7c50b2..0de3a4d0 100644 --- a/services/event/controller/controller.go +++ b/services/event/controller/controller.go @@ -179,14 +179,14 @@ func UpsertEventCode(w http.ResponseWriter, r *http.Request) { var eventCode models.EventCode json.NewDecoder(r.Body).Decode(&eventCode) - err := service.UpdateEventCode(eventCode.Code, eventCode) + err := service.UpdateEventCode(eventCode.CodeID, eventCode) if err != nil { errors.WriteError(w, r, errors.DatabaseError(err.Error(), "Could not update the code and timestamp of the event.")) return } - updated_codes, err := service.GetEventCodes(eventCode.ID) + updated_codes, err := service.GetEventCodes(eventCode.EventID) if err != nil { errors.WriteError(w, r, errors.DatabaseError(err.Error(), "Could not get updated event code and timestamp details.")) diff --git a/services/event/models/event_code.go b/services/event/models/event_code.go index c2269cd2..1493df1d 100644 --- a/services/event/models/event_code.go +++ b/services/event/models/event_code.go @@ -1,8 +1,8 @@ package models type EventCode struct { - ID string `json:"id"` - Code string `json:"code"` + CodeID string `json:"codeID"` + EventID string `json:"eventID"` IsVirtual bool `json:"isVirtual"` Expiration int64 `json:"expiration"` } diff --git a/services/event/service/event_service.go b/services/event/service/event_service.go index f297c029..c333440f 100644 --- a/services/event/service/event_service.go +++ b/services/event/service/event_service.go @@ -181,8 +181,8 @@ func CreateEvent(id string, code string, event models.Event) error { } event_code := models.EventCode{ - ID: id, - Code: code, + CodeID: code, + EventID: id, Expiration: event.EndTime, } @@ -480,7 +480,7 @@ func GetStats() (map[string]interface{}, error) { */ func CanRedeemPoints(event_code string) (bool, bool, string, error) { query := database.QuerySelector{ - "code": event_code, + "codeID": event_code, } var eventCode models.EventCode @@ -493,7 +493,7 @@ func CanRedeemPoints(event_code string) (bool, bool, string, error) { expiration_time := eventCode.Expiration current_time := time.Now().Unix() - return current_time < expiration_time, eventCode.IsVirtual, eventCode.ID, nil + return current_time < expiration_time, eventCode.IsVirtual, eventCode.EventID, nil } /* @@ -501,7 +501,7 @@ func CanRedeemPoints(event_code string) (bool, bool, string, error) { */ func GetEventCodes(id string) (*[]models.EventCode, error) { query := database.QuerySelector{ - "id": id, + "eventID": id, } eventCodes := []models.EventCode{} @@ -521,7 +521,7 @@ func GetEventCodes(id string) (*[]models.EventCode, error) { */ func UpdateEventCode(code string, eventCode models.EventCode) error { selector := database.QuerySelector{ - "code": code, + "codeID": code, } _, err := db.Upsert("eventcodes", selector, &eventCode) From 08eb9d48fad125f9103466bcde8419e988ba3040 Mon Sep 17 00:00:00 2001 From: Jareth Gomes Date: Mon, 3 Jan 2022 01:02:39 -0600 Subject: [PATCH 21/30] Fixed event code querying --- services/event/service/event_service.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/services/event/service/event_service.go b/services/event/service/event_service.go index c333440f..686b9316 100644 --- a/services/event/service/event_service.go +++ b/services/event/service/event_service.go @@ -480,7 +480,7 @@ func GetStats() (map[string]interface{}, error) { */ func CanRedeemPoints(event_code string) (bool, bool, string, error) { query := database.QuerySelector{ - "codeID": event_code, + "codeid": event_code, } var eventCode models.EventCode @@ -501,7 +501,7 @@ func CanRedeemPoints(event_code string) (bool, bool, string, error) { */ func GetEventCodes(id string) (*[]models.EventCode, error) { query := database.QuerySelector{ - "eventID": id, + "eventid": id, } eventCodes := []models.EventCode{} @@ -521,7 +521,7 @@ func GetEventCodes(id string) (*[]models.EventCode, error) { */ func UpdateEventCode(code string, eventCode models.EventCode) error { selector := database.QuerySelector{ - "codeID": code, + "codeid": code, } _, err := db.Upsert("eventcodes", selector, &eventCode) From 34174b7eed4723b375886b5493a64704131ae9b7 Mon Sep 17 00:00:00 2001 From: Jareth Gomes Date: Mon, 3 Jan 2022 01:03:49 -0600 Subject: [PATCH 22/30] Renamed update event code service function to upsert --- services/event/controller/controller.go | 2 +- services/event/service/event_service.go | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/services/event/controller/controller.go b/services/event/controller/controller.go index 0de3a4d0..5ca5f441 100644 --- a/services/event/controller/controller.go +++ b/services/event/controller/controller.go @@ -179,7 +179,7 @@ func UpsertEventCode(w http.ResponseWriter, r *http.Request) { var eventCode models.EventCode json.NewDecoder(r.Body).Decode(&eventCode) - err := service.UpdateEventCode(eventCode.CodeID, eventCode) + err := service.UpsertEventCode(eventCode.CodeID, eventCode) if err != nil { errors.WriteError(w, r, errors.DatabaseError(err.Error(), "Could not update the code and timestamp of the event.")) diff --git a/services/event/service/event_service.go b/services/event/service/event_service.go index 686b9316..9013c501 100644 --- a/services/event/service/event_service.go +++ b/services/event/service/event_service.go @@ -516,10 +516,10 @@ func GetEventCodes(id string) (*[]models.EventCode, error) { } /* - Updates the event code and end time with the given id + Upserts the event code and end time with the given id If no matching code is found, then it is a new code and add it */ -func UpdateEventCode(code string, eventCode models.EventCode) error { +func UpsertEventCode(code string, eventCode models.EventCode) error { selector := database.QuerySelector{ "codeid": code, } From bc616f81af724facdea764f30b67e0f8f8cdaf27 Mon Sep 17 00:00:00 2001 From: Jareth Gomes Date: Mon, 3 Jan 2022 01:06:06 -0600 Subject: [PATCH 23/30] Updated documentation with refactored event code model --- .../docs/reference/services/Event.md | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/documentation/docs/reference/services/Event.md b/documentation/docs/reference/services/Event.md index 94c05d32..76c45808 100644 --- a/documentation/docs/reference/services/Event.md +++ b/documentation/docs/reference/services/Event.md @@ -404,14 +404,14 @@ Response format: ``` [ { - "id": "52fdfc072182654f163f5f0f9a621d72", - "code": "sample_code_1", + "codeID": "sample_code_1", + "eventID": "52fdfc072182654f163f5f0f9a621d72", "isVirtual": false, "expiration": 1521388800 }, { - "id": "52fdfc072182654f163f5f0f9a621d72", - "code": "sample_code_2", + "codeID": "sample_code_2", + "eventID": "52fdfc072182654f163f5f0f9a621d72", "isVirtual": true, "expiration": 1521388800 } @@ -426,10 +426,10 @@ Upserts a struct that contains information about the event code (generated upon Request format: ``` { - "id": "52fdfc072182654f163f5f0f9a621d72", - "code": "new_code", + "codeID": "new_code", + "eventID": "52fdfc072182654f163f5f0f9a621d72", "isVirtual": true, - "expiration": 1521388800 + "expiration": 1521388800 } ``` @@ -437,10 +437,10 @@ Request format: Response format: ``` { - "id": "52fdfc072182654f163f5f0f9a621d72", - "code": "new_code", + "codeID": "new_code", + "eventID": "52fdfc072182654f163f5f0f9a621d72", "isVirtual": true, - "expiration": 1521388800 + "expiration": 1521388800 } ``` From 2ed5659c4634ff242406ab21f9bcc88de1b2f347 Mon Sep 17 00:00:00 2001 From: Jareth Gomes Date: Mon, 3 Jan 2022 01:09:59 -0600 Subject: [PATCH 24/30] Added note onupdating codes for POST /event/code in event docs --- documentation/docs/reference/services/Event.md | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/documentation/docs/reference/services/Event.md b/documentation/docs/reference/services/Event.md index 76c45808..6ddfa529 100644 --- a/documentation/docs/reference/services/Event.md +++ b/documentation/docs/reference/services/Event.md @@ -423,10 +423,12 @@ POST /event/code/ Upserts a struct that contains information about the event code (generated upon event creation) and expiration time. +NOTE: Once created, the code ID cannot be changed. + Request format: ``` { - "codeID": "new_code", + "codeID": "code", "eventID": "52fdfc072182654f163f5f0f9a621d72", "isVirtual": true, "expiration": 1521388800 @@ -437,7 +439,7 @@ Request format: Response format: ``` { - "codeID": "new_code", + "codeID": "code", "eventID": "52fdfc072182654f163f5f0f9a621d72", "isVirtual": true, "expiration": 1521388800 From c1b8d5f761a1ebff79a404a922a771488abb2023 Mon Sep 17 00:00:00 2001 From: Jareth Gomes Date: Mon, 3 Jan 2022 01:32:00 -0600 Subject: [PATCH 25/30] Added plurality to function names --- gateway/services/event.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/gateway/services/event.go b/gateway/services/event.go index e1e72908..194505c3 100644 --- a/gateway/services/event.go +++ b/gateway/services/event.go @@ -86,10 +86,10 @@ var EventRoutes = arbor.RouteCollection{ alice.New(middleware.AuthMiddleware([]models.Role{models.AdminRole, models.StaffRole}), middleware.IdentificationMiddleware).ThenFunc(UpdateEvent).ServeHTTP, }, arbor.Route{ - "GetEventCode", + "GetEventCodes", "GET", "/event/code/{id}/", - alice.New(middleware.AuthMiddleware([]models.Role{models.AdminRole, models.StaffRole}), middleware.IdentificationMiddleware).ThenFunc(GetEventCode).ServeHTTP, + alice.New(middleware.AuthMiddleware([]models.Role{models.AdminRole, models.StaffRole}), middleware.IdentificationMiddleware).ThenFunc(GetEventCodes).ServeHTTP, }, arbor.Route{ "UpdateEventCode", @@ -125,7 +125,7 @@ func UpdateEvent(w http.ResponseWriter, r *http.Request) { arbor.POST(w, config.EVENT_SERVICE+r.URL.String(), EventFormat, "", r) } -func GetEventCode(w http.ResponseWriter, r *http.Request) { +func GetEventCodes(w http.ResponseWriter, r *http.Request) { arbor.GET(w, config.EVENT_SERVICE+r.URL.String(), EventFormat, "", r) } From 32b89752a56a1e1a40684ebf6d79c23f03bcaf31 Mon Sep 17 00:00:00 2001 From: Jareth Gomes Date: Mon, 3 Jan 2022 01:42:11 -0600 Subject: [PATCH 26/30] Added required validators to all fields in event code --- services/event/models/event_code.go | 8 ++++---- services/event/service/event_service.go | 8 +++++++- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/services/event/models/event_code.go b/services/event/models/event_code.go index 1493df1d..9427073d 100644 --- a/services/event/models/event_code.go +++ b/services/event/models/event_code.go @@ -1,8 +1,8 @@ package models type EventCode struct { - CodeID string `json:"codeID"` - EventID string `json:"eventID"` - IsVirtual bool `json:"isVirtual"` - Expiration int64 `json:"expiration"` + CodeID string `json:"codeID" validate:"required"` + EventID string `json:"eventID" validate:"required"` + IsVirtual bool `json:"isVirtual" validate:"required"` + Expiration int64 `json:"expiration" validate:"required"` } diff --git a/services/event/service/event_service.go b/services/event/service/event_service.go index 9013c501..4684059c 100644 --- a/services/event/service/event_service.go +++ b/services/event/service/event_service.go @@ -520,11 +520,17 @@ func GetEventCodes(id string) (*[]models.EventCode, error) { If no matching code is found, then it is a new code and add it */ func UpsertEventCode(code string, eventCode models.EventCode) error { + err := validate.Struct(eventCode) + + if err != nil { + return err + } + selector := database.QuerySelector{ "codeid": code, } - _, err := db.Upsert("eventcodes", selector, &eventCode) + _, err = db.Upsert("eventcodes", selector, &eventCode) return err } From c91489bd97090b601974ad10b19c11809b017031 Mon Sep 17 00:00:00 2001 From: Jareth Gomes Date: Mon, 3 Jan 2022 21:50:15 -0600 Subject: [PATCH 27/30] Fixed code generation on event creation --- services/event/controller/controller.go | 21 +++++++++- services/event/service/event_service.go | 55 +++++++++++++++++++------ 2 files changed, 61 insertions(+), 15 deletions(-) diff --git a/services/event/controller/controller.go b/services/event/controller/controller.go index 5ca5f441..846d4c81 100644 --- a/services/event/controller/controller.go +++ b/services/event/controller/controller.go @@ -108,15 +108,32 @@ func CreateEvent(w http.ResponseWriter, r *http.Request) { json.NewDecoder(r.Body).Decode(&event) event.ID = utils.GenerateUniqueID() - var code = utils.GenerateUniqueCode() - err := service.CreateEvent(event.ID, code, event) + err := service.CreateEvent(event.ID, event) + + // It would be good to check if the error is due to failing validation (should be a 422) + // Tried using errors.Is(), but ironcally so we have a package common/errors which conflicts + // with the built-in errors package. if err != nil { errors.WriteError(w, r, errors.DatabaseError(err.Error(), "Could not create new event.")) return } + err = service.GenerateEventCode(false, event) + + if err != nil { + errors.WriteError(w, r, errors.DatabaseError(err.Error(), "Failed to create in-person code.")) + return + } + + err = service.GenerateEventCode(true, event) + + if err != nil { + errors.WriteError(w, r, errors.DatabaseError(err.Error(), "Failed to create virtual code.")) + return + } + updated_event, err := service.GetEvent(event.ID) if err != nil { diff --git a/services/event/service/event_service.go b/services/event/service/event_service.go index 4684059c..cb1afcfd 100644 --- a/services/event/service/event_service.go +++ b/services/event/service/event_service.go @@ -147,7 +147,7 @@ func GetFilteredEvents(parameters map[string][]string) (*models.EventList, error /* Creates an event with the given id */ -func CreateEvent(id string, code string, event models.Event) error { +func CreateEvent(id string, event models.Event) error { err := validate.Struct(event) if err != nil { @@ -176,18 +176,6 @@ func CreateEvent(id string, code string, event models.Event) error { err = db.Insert("eventtrackers", &event_tracker) - if err != nil { - return err - } - - event_code := models.EventCode{ - CodeID: code, - EventID: id, - Expiration: event.EndTime, - } - - err = db.Insert("eventcodes", &event_code) - return err } @@ -534,3 +522,44 @@ func UpsertEventCode(code string, eventCode models.EventCode) error { return err } + +func GenerateEventCode(isVirtual bool, event models.Event) error { + // The probability of a generated code colliding with an existing + // code is (N / 2^24) where N is the number of existing codes. + // We will attempt to insert the row 10 times each with a new random + // code, making the success rate 1 - ((N / 2^24)^10) + var code string + + for i := 0; i < 10; i++ { + code = utils.GenerateUniqueCode() + + query := database.QuerySelector{ + "codeid": code, + } + + err := db.FindOne("eventcodes", query, models.EventCode{}) + + if err == database.ErrNotFound { + eventCode := models.EventCode{ + CodeID: code, + EventID: event.ID, + IsVirtual: isVirtual, + Expiration: event.EndTime, + } + + err = db.Insert("eventcodes", &eventCode) + + return err + } + } + + err_str := "Failed to generate a unique " + + if isVirtual { + err_str += "virtual event code." + } else { + err_str += "in-person event code." + } + + return errors.New(err_str) +} From 43f4699fa3b078517ceed31988244b469dc17682 Mon Sep 17 00:00:00 2001 From: Jareth Gomes Date: Mon, 3 Jan 2022 21:52:19 -0600 Subject: [PATCH 28/30] Made point values in event model unsigned --- services/event/controller/controller.go | 6 +++--- services/event/models/event.go | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/services/event/controller/controller.go b/services/event/controller/controller.go index 846d4c81..34519824 100644 --- a/services/event/controller/controller.go +++ b/services/event/controller/controller.go @@ -279,7 +279,7 @@ func Checkin(w http.ResponseWriter, r *http.Request) { return } - points_to_award := 0 + var points_to_award uint = 0 if is_user_virtual { points_to_award = event.VirtualPoints } else if is_code_virtual { @@ -288,10 +288,10 @@ func Checkin(w http.ResponseWriter, r *http.Request) { points_to_award = event.InPersonPoints } - result.NewPoints = points_to_award + result.NewPoints = int(points_to_award) // Add this point value to given profile - profile, err := service.AwardPoints(id, points_to_award) + profile, err := service.AwardPoints(id, int(points_to_award)) if err != nil { errors.WriteError(w, r, errors.UnknownError(err.Error(), "Failed to award user with points")) diff --git a/services/event/models/event.go b/services/event/models/event.go index 15d0bd4a..6e81a991 100644 --- a/services/event/models/event.go +++ b/services/event/models/event.go @@ -9,9 +9,9 @@ type Event struct { Locations []EventLocation `json:"locations" validate:"required,dive,required"` Sponsor string `json:"sponsor"` EventType string `json:"eventType" validate:"required,oneof=MEAL SPEAKER WORKSHOP MINIEVENT QNA OTHER"` - InPersonPoints int `json:"inPersonPoints"` - InPersonVirtPoints int `json:"inPersonVirtPoints"` - VirtualPoints int `json:"virtualPoints"` + InPersonPoints uint `json:"inPersonPoints"` + InPersonVirtPoints uint `json:"inPersonVirtPoints"` + VirtualPoints uint `json:"virtualPoints"` } type EventLocation struct { From a00bc906ebf7f5b9ff7fdf717cd6bd432242f5b6 Mon Sep 17 00:00:00 2001 From: Jareth Gomes Date: Mon, 3 Jan 2022 23:05:41 -0600 Subject: [PATCH 29/30] Added tests for all event code service functions --- services/event/tests/event_test.go | 241 ++++++++++++++++++++++++++++- 1 file changed, 238 insertions(+), 3 deletions(-) diff --git a/services/event/tests/event_test.go b/services/event/tests/event_test.go index ae5af2a1..3a29283e 100644 --- a/services/event/tests/event_test.go +++ b/services/event/tests/event_test.go @@ -427,7 +427,7 @@ func TestCreateEventService(t *testing.T) { }, } - err := service.CreateEvent("testid2", "testcode2", new_event) + err := service.CreateEvent("testid2", new_event) if err != nil { t.Fatal(err) @@ -722,7 +722,7 @@ func TestIsEventActive(t *testing.T) { }, } - service.CreateEvent(new_event.ID, "testcode3", new_event) + service.CreateEvent(new_event.ID, new_event) is_active, err := service.IsEventActive("testid3") @@ -741,7 +741,7 @@ func TestIsEventActive(t *testing.T) { new_event.StartTime = TestTime new_event.EndTime = TestTime + ONE_MINUTE_IN_SECONDS*20 - service.CreateEvent(new_event.ID, "testcode4", new_event) + service.CreateEvent(new_event.ID, new_event) is_active, err = service.IsEventActive("testid4") @@ -846,3 +846,238 @@ func TestRemoveEventFavorite(t *testing.T) { CleanupTestDB(t) } + +/* + Tests event code generation for a given event id +*/ +func TestGenerateEventCode(t *testing.T) { + SetupTestDB(t) + + event, err := service.GetEvent("testid") + + if err != nil { + t.Fatal(err) + } + + err = service.GenerateEventCode(false, *event) + + if err != nil { + t.Fatal(err) + } + + query := database.QuerySelector{ + "eventid": event.ID, + } + + event_codes := []models.EventCode{} + + err = db.FindAll("eventcodes", query, &event_codes) + + if err != nil { + t.Fatal(err) + } + + expected_event_codes := []models.EventCode{ + { + CodeID: "some random code that's gonna get replaced here", + EventID: event.ID, + IsVirtual: false, + Expiration: event.EndTime, + }, + } + + for i, code := range event_codes { + expected_event_codes[i].CodeID = code.CodeID + } + + if !reflect.DeepEqual(&event_codes, &expected_event_codes) { + t.Errorf("Wrong event codes. Expected %v, got %v", &expected_event_codes, &event_codes) + } + + CleanupTestDB(t) +} + +/* + Tests updating an event code +*/ +func TestUpdateEventCode(t *testing.T) { + SetupTestDB(t) + + event_code := models.EventCode{ + CodeID: "abcdef", + EventID: "testid", + IsVirtual: false, + Expiration: 3133535820, + } + + err := db.Insert("eventcodes", &event_code) + + if err != nil { + t.Fatal(err) + } + + event_code.IsVirtual = true + event_code.Expiration = 1609735934 + + err = service.UpsertEventCode("abcdef", event_code) + + if err != nil { + t.Fatal(err) + } + + query := database.QuerySelector{ + "eventid": "testid", + } + + event_codes := []models.EventCode{} + + err = db.FindAll("eventcodes", query, &event_codes) + + if err != nil { + t.Fatal(err) + } + + expected_event_codes := []models.EventCode{ + { + CodeID: "abcdef", + EventID: "testid", + IsVirtual: true, + Expiration: 1609735934, + }, + } + + if !reflect.DeepEqual(&event_codes, &expected_event_codes) { + t.Errorf("Wrong event codes. Expected %v, got %v", &expected_event_codes, &event_codes) + } + + CleanupTestDB(t) +} + +/* + Tests inserting a new event code +*/ +func TestInsertEventCode(t *testing.T) { + SetupTestDB(t) + + event_code_1 := models.EventCode{ + CodeID: "abcdef", + EventID: "testid", + IsVirtual: false, + Expiration: 3133535820, + } + + err := db.Insert("eventcodes", &event_code_1) + + if err != nil { + t.Fatal(err) + } + + event_code_2 := models.EventCode{ + CodeID: "123456", + EventID: "testid", + IsVirtual: true, + Expiration: 3133535820, + } + + event_code_1.IsVirtual = true + event_code_1.Expiration = 1609735934 + + err = service.UpsertEventCode("123456", event_code_2) + + if err != nil { + t.Fatal(err) + } + + query := database.QuerySelector{ + "eventid": "testid", + } + + event_codes := []models.EventCode{} + + err = db.FindAll("eventcodes", query, &event_codes) + + if err != nil { + t.Fatal(err) + } + + expected_event_codes := []models.EventCode{ + { + CodeID: "abcdef", + EventID: "testid", + IsVirtual: false, + Expiration: 3133535820, + }, + { + CodeID: "123456", + EventID: "testid", + IsVirtual: true, + Expiration: 3133535820, + }, + } + + if !reflect.DeepEqual(&event_codes, &expected_event_codes) { + t.Errorf("Wrong event codes. Expected %v, got %v", &expected_event_codes, &event_codes) + } + + CleanupTestDB(t) +} + +/* + Tests getting all event codes for a given event id +*/ +func TestGetEventCodes(t *testing.T) { + SetupTestDB(t) + + event_code_1 := models.EventCode{ + CodeID: "abcdef", + EventID: "testid", + IsVirtual: false, + Expiration: 3133535820, + } + + event_code_2 := models.EventCode{ + CodeID: "123456", + EventID: "testid", + IsVirtual: true, + Expiration: 3133535820, + } + + err := db.Insert("eventcodes", &event_code_1) + + if err != nil { + t.Fatal(err) + } + + err = db.Insert("eventcodes", &event_code_2) + + if err != nil { + t.Fatal(err) + } + + event_codes, err := service.GetEventCodes("testid") + + if err != nil { + t.Fatal(err) + } + + expected_event_codes := []models.EventCode{ + { + CodeID: "abcdef", + EventID: "testid", + IsVirtual: false, + Expiration: 3133535820, + }, + { + CodeID: "123456", + EventID: "testid", + IsVirtual: true, + Expiration: 3133535820, + }, + } + + if !reflect.DeepEqual(event_codes, &expected_event_codes) { + t.Errorf("Wrong event codes. Expected %v, got %v", &expected_event_codes, &event_codes) + } + + CleanupTestDB(t) +} From b0f053ce8012debf59854c289c10abbf2d8bb6ca Mon Sep 17 00:00:00 2001 From: Jareth Gomes Date: Mon, 3 Jan 2022 23:08:53 -0600 Subject: [PATCH 30/30] Removed code str argument from UpsertEventCode function --- services/event/controller/controller.go | 2 +- services/event/service/event_service.go | 4 ++-- services/event/tests/event_test.go | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/services/event/controller/controller.go b/services/event/controller/controller.go index 34519824..2b2dd98f 100644 --- a/services/event/controller/controller.go +++ b/services/event/controller/controller.go @@ -196,7 +196,7 @@ func UpsertEventCode(w http.ResponseWriter, r *http.Request) { var eventCode models.EventCode json.NewDecoder(r.Body).Decode(&eventCode) - err := service.UpsertEventCode(eventCode.CodeID, eventCode) + err := service.UpsertEventCode(eventCode) if err != nil { errors.WriteError(w, r, errors.DatabaseError(err.Error(), "Could not update the code and timestamp of the event.")) diff --git a/services/event/service/event_service.go b/services/event/service/event_service.go index cb1afcfd..45af3d56 100644 --- a/services/event/service/event_service.go +++ b/services/event/service/event_service.go @@ -507,7 +507,7 @@ func GetEventCodes(id string) (*[]models.EventCode, error) { Upserts the event code and end time with the given id If no matching code is found, then it is a new code and add it */ -func UpsertEventCode(code string, eventCode models.EventCode) error { +func UpsertEventCode(eventCode models.EventCode) error { err := validate.Struct(eventCode) if err != nil { @@ -515,7 +515,7 @@ func UpsertEventCode(code string, eventCode models.EventCode) error { } selector := database.QuerySelector{ - "codeid": code, + "codeid": eventCode.CodeID, } _, err = db.Upsert("eventcodes", selector, &eventCode) diff --git a/services/event/tests/event_test.go b/services/event/tests/event_test.go index 3a29283e..ad69619b 100644 --- a/services/event/tests/event_test.go +++ b/services/event/tests/event_test.go @@ -919,7 +919,7 @@ func TestUpdateEventCode(t *testing.T) { event_code.IsVirtual = true event_code.Expiration = 1609735934 - err = service.UpsertEventCode("abcdef", event_code) + err = service.UpsertEventCode(event_code) if err != nil { t.Fatal(err) @@ -982,7 +982,7 @@ func TestInsertEventCode(t *testing.T) { event_code_1.IsVirtual = true event_code_1.Expiration = 1609735934 - err = service.UpsertEventCode("123456", event_code_2) + err = service.UpsertEventCode(event_code_2) if err != nil { t.Fatal(err)