diff --git a/modules/utils/utils.go b/modules/utils/utils.go index 2356562..bb5c186 100644 --- a/modules/utils/utils.go +++ b/modules/utils/utils.go @@ -2,6 +2,7 @@ package utils import ( "encoding/json" + "time" "clickandjoin.app/managementsystem/modules/structs" gocnjhelper "git.clickandjoin.umbach.dev/ClickandJoin/go-cnj-helper" @@ -32,6 +33,10 @@ func UnmarshalReceivedMessage(body []byte, message any) error { return nil } +func GetCurrentTimestamp() int64 { + return time.Now().Unix() +} + func ValidatorInit() { cnjvalidator.Validate.RegisterStructValidationMapRules(cnjglobals.GeneralRules, structs.UpdateUserRequest{}, diff --git a/routers/api/v1/user/user.go b/routers/api/v1/user/user.go index 4b610e3..9caee6b 100644 --- a/routers/api/v1/user/user.go +++ b/routers/api/v1/user/user.go @@ -5,6 +5,7 @@ import ( "clickandjoin.app/managementsystem/modules/scylladb" "clickandjoin.app/managementsystem/modules/structs" + "clickandjoin.app/managementsystem/modules/utils" gocnjhelper "git.clickandjoin.umbach.dev/ClickandJoin/go-cnj-helper" "git.clickandjoin.umbach.dev/ClickandJoin/go-cnj-helper/cnjvalidator" "git.clickandjoin.umbach.dev/ClickandJoin/go-cnj-helper/dbstructs" @@ -65,32 +66,41 @@ func UpdateUser(c *fiber.Ctx) error { } // check whether the email in the requested has been changed in relation to the user - if foundUser.Email != body.Email { + if body.Email != foundUser.Email { if status, err := cnjvalidator.HandleEmailValidation(scylladb.Session, scylladb.Cluster.Keyspace, body.Email); err != nil { return c.SendStatus(status) } } accountNameLc := strings.ToLower(body.AccountName) + lastAccountNameLc := foundUser.LastAccountNameLc // check whether the account name in the requested has been changed in relation to the user - if foundUser.AccountNameLc != accountNameLc { - if status, err := cnjvalidator.HandleAccountNameValidation(scylladb.Session, scylladb.Cluster.Keyspace, body.AccountName); err != nil { - return c.SendStatus(status) + if accountNameLc != foundUser.AccountNameLc { + lastAccountNameLc = foundUser.AccountNameLc + + if accountNameLc != foundUser.LastAccountNameLc { + if status, err := cnjvalidator.HandleAccountNameValidation(scylladb.Session, scylladb.Cluster.Keyspace, body.AccountName); err != nil { + return c.SendStatus(status) + } } } + gocnjhelper.LogDebugf("lastAc %s", lastAccountNameLc, accountNameLc != foundUser.AccountNameLc) + updatedUser := dbstructs.User{ - Id: params.UserId, - Username: body.Username, - AccountName: body.AccountName, - AccountNameLc: strings.ToLower(body.AccountName), - Email: body.Email, - Description: body.Description, - AccountStatus: body.AccountStatus, + Id: params.UserId, + Username: body.Username, + AccountName: body.AccountName, + AccountNameLc: accountNameLc, + LastAccountNameLc: lastAccountNameLc, + AccountNameUpdatedAt: utils.GetCurrentTimestamp(), + Email: body.Email, + Description: body.Description, + AccountStatus: body.AccountStatus, } - if err := scylladb.Session.Query(gocnjhelper.DbMUsers.Update("username", "account_name", "account_name_lc", "email", "description", "account_status")).BindStruct(updatedUser).ExecRelease(); err != nil { + if err := scylladb.Session.Query(gocnjhelper.DbMUsers.Update("username", "account_name", "account_name_lc", "last_account_name_lc", "account_name_updated_at", "email", "description", "account_status")).BindStruct(updatedUser).ExecRelease(); err != nil { gocnjhelper.LogErrorf("Failed to update user, err: %s", err.Error()) return c.SendStatus(fiber.StatusInternalServerError) }