From 3279b30b3de8e4e9aaa6558ceaa9048b5b9314bd Mon Sep 17 00:00:00 2001 From: Arda Serdar Pektezol <1669855+pektezol@users.noreply.github.com> Date: Sun, 3 Sep 2023 16:20:16 +0300 Subject: feat: added logs for user update (#55) Former-commit-id: b1cfb15cd595e93494b370dda6572e48a175d46e --- backend/handlers/login.go | 8 ++++---- backend/handlers/logs.go | 15 ++++++++++----- backend/handlers/user.go | 7 +++++++ 3 files changed, 21 insertions(+), 9 deletions(-) (limited to 'backend/handlers') diff --git a/backend/handlers/login.go b/backend/handlers/login.go index 5949fdd..85ffd63 100644 --- a/backend/handlers/login.go +++ b/backend/handlers/login.go @@ -38,7 +38,7 @@ func Login(c *gin.Context) { default: steamID, err := openID.ValidateAndGetId() if err != nil { - CreateLog(steamID, LogTypeLogin, LogDescriptionUserLoginFailValidate) + CreateLog(steamID, LogTypeUser, LogDescriptionUserLoginFailValidate) c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) return } @@ -49,7 +49,7 @@ func Login(c *gin.Context) { if checkSteamID == 0 { user, err := GetPlayerSummaries(steamID, os.Getenv("API_KEY")) if err != nil { - CreateLog(steamID, LogTypeLogin, LogDescriptionUserLoginFailSummary) + CreateLog(steamID, LogTypeUser, LogDescriptionUserLoginFailSummary) c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) return } @@ -79,12 +79,12 @@ func Login(c *gin.Context) { // Sign and get the complete encoded token as a string using the secret tokenString, err := token.SignedString([]byte(os.Getenv("SECRET_KEY"))) if err != nil { - CreateLog(steamID, LogTypeLogin, LogDescriptionUserLoginFailToken) + CreateLog(steamID, LogTypeUser, LogDescriptionUserLoginFailToken) c.JSON(http.StatusBadRequest, models.ErrorResponse("Failed to generate token.")) return } c.SetCookie("token", tokenString, 3600*24*30, "/", "", true, true) - CreateLog(steamID, LogTypeLogin, LogDescriptionUserLoginSuccess) + CreateLog(steamID, LogTypeUser, LogDescriptionUserLoginSuccess) c.Redirect(http.StatusTemporaryRedirect, "/") // c.JSON(http.StatusOK, models.Response{ // Success: true, diff --git a/backend/handlers/logs.go b/backend/handlers/logs.go index 8880607..2b8223a 100644 --- a/backend/handlers/logs.go +++ b/backend/handlers/logs.go @@ -12,13 +12,18 @@ import ( const ( LogTypeMod string = "Mod" - LogTypeLogin string = "User" + LogTypeUser string = "User" LogTypeRecord string = "Record" - LogDescriptionUserLoginSuccess string = "LoginSuccess" - LogDescriptionUserLoginFailToken string = "LoginTokenFail" - LogDescriptionUserLoginFailValidate string = "LoginValidateFail" - LogDescriptionUserLoginFailSummary string = "LoginSummaryFail" + LogDescriptionUserLoginSuccess string = "LoginSuccess" + LogDescriptionUserLoginFailToken string = "LoginTokenFail" + LogDescriptionUserLoginFailValidate string = "LoginValidateFail" + LogDescriptionUserLoginFailSummary string = "LoginSummaryFail" + LogDescriptionUserUpdateSuccess string = "UpdateSuccess" + LogDescriptionUserUpdateFail string = "UpdateFail" + LogDescriptionUserUpdateSummaryFail string = "UpdateSummaryFail" + LogDescriptionUserUpdateCountrySuccess string = "UpdateCountrySuccess" + LogDescriptionUserUpdateCountryFail string = "UpdateCountryFail" LogDescriptionMapSummaryCreate string = "MapSummaryCreate" LogDescriptionMapSummaryEdit string = "MapSummaryEdit" diff --git a/backend/handlers/user.go b/backend/handlers/user.go index e0f1dff..742a57c 100644 --- a/backend/handlers/user.go +++ b/backend/handlers/user.go @@ -646,6 +646,7 @@ func UpdateUser(c *gin.Context) { } profile, err := GetPlayerSummaries(user.(models.User).SteamID, os.Getenv("API_KEY")) if err != nil { + CreateLog(user.(models.User).SteamID, LogTypeUser, LogDescriptionUserUpdateSummaryFail) c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) return } @@ -653,9 +654,11 @@ func UpdateUser(c *gin.Context) { _, err = database.DB.Exec(`UPDATE users SET username = $1, avatar_link = $2, country_code = $3, updated_at = $4 WHERE steam_id = $5`, profile.PersonaName, profile.AvatarFull, profile.LocCountryCode, time.Now().UTC(), user.(models.User).SteamID) if err != nil { + CreateLog(user.(models.User).SteamID, LogTypeUser, LogDescriptionUserUpdateFail) c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) return } + CreateLog(user.(models.User).SteamID, LogTypeUser, LogDescriptionUserUpdateSuccess) c.JSON(http.StatusOK, models.Response{ Success: true, Message: "Successfully updated user.", @@ -690,21 +693,25 @@ func UpdateCountryCode(c *gin.Context) { } code := c.Query("country_code") if code == "" { + CreateLog(user.(models.User).SteamID, LogTypeUser, LogDescriptionUserUpdateCountryFail) c.JSON(http.StatusNotFound, models.ErrorResponse("Enter a valid country code.")) return } var validCode string err := database.DB.QueryRow(`SELECT country_code FROM countries WHERE country_code = $1`, code).Scan(&validCode) if err != nil { + CreateLog(user.(models.User).SteamID, LogTypeUser, LogDescriptionUserUpdateCountryFail) c.JSON(http.StatusNotFound, models.ErrorResponse(err.Error())) return } // Valid code, update profile _, err = database.DB.Exec(`UPDATE users SET country_code = $1 WHERE steam_id = $2`, validCode, user.(models.User).SteamID) if err != nil { + CreateLog(user.(models.User).SteamID, LogTypeUser, LogDescriptionUserUpdateCountryFail) c.JSON(http.StatusNotFound, models.ErrorResponse(err.Error())) return } + CreateLog(user.(models.User).SteamID, LogTypeUser, LogDescriptionUserUpdateCountrySuccess) c.JSON(http.StatusOK, models.Response{ Success: true, Message: "Successfully updated country code.", -- cgit v1.2.3