added validation check for email and account name
parent
5c07f9c4de
commit
4ffed40ef8
|
@ -2,6 +2,7 @@ package utils
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"time"
|
||||||
|
|
||||||
"clickandjoin.app/managementsystem/modules/structs"
|
"clickandjoin.app/managementsystem/modules/structs"
|
||||||
gocnjhelper "git.clickandjoin.umbach.dev/ClickandJoin/go-cnj-helper"
|
gocnjhelper "git.clickandjoin.umbach.dev/ClickandJoin/go-cnj-helper"
|
||||||
|
@ -32,6 +33,10 @@ func UnmarshalReceivedMessage(body []byte, message any) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func GetCurrentTimestamp() int64 {
|
||||||
|
return time.Now().Unix()
|
||||||
|
}
|
||||||
|
|
||||||
func ValidatorInit() {
|
func ValidatorInit() {
|
||||||
cnjvalidator.Validate.RegisterStructValidationMapRules(cnjglobals.GeneralRules,
|
cnjvalidator.Validate.RegisterStructValidationMapRules(cnjglobals.GeneralRules,
|
||||||
structs.UpdateUserRequest{},
|
structs.UpdateUserRequest{},
|
||||||
|
|
|
@ -5,6 +5,7 @@ import (
|
||||||
|
|
||||||
"clickandjoin.app/managementsystem/modules/scylladb"
|
"clickandjoin.app/managementsystem/modules/scylladb"
|
||||||
"clickandjoin.app/managementsystem/modules/structs"
|
"clickandjoin.app/managementsystem/modules/structs"
|
||||||
|
"clickandjoin.app/managementsystem/modules/utils"
|
||||||
gocnjhelper "git.clickandjoin.umbach.dev/ClickandJoin/go-cnj-helper"
|
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/cnjvalidator"
|
||||||
"git.clickandjoin.umbach.dev/ClickandJoin/go-cnj-helper/dbstructs"
|
"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
|
// 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 {
|
if status, err := cnjvalidator.HandleEmailValidation(scylladb.Session, scylladb.Cluster.Keyspace, body.Email); err != nil {
|
||||||
return c.SendStatus(status)
|
return c.SendStatus(status)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
accountNameLc := strings.ToLower(body.AccountName)
|
accountNameLc := strings.ToLower(body.AccountName)
|
||||||
|
lastAccountNameLc := foundUser.LastAccountNameLc
|
||||||
|
|
||||||
// check whether the account name in the requested has been changed in relation to the user
|
// check whether the account name in the requested has been changed in relation to the user
|
||||||
if foundUser.AccountNameLc != accountNameLc {
|
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 {
|
if status, err := cnjvalidator.HandleAccountNameValidation(scylladb.Session, scylladb.Cluster.Keyspace, body.AccountName); err != nil {
|
||||||
return c.SendStatus(status)
|
return c.SendStatus(status)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
gocnjhelper.LogDebugf("lastAc %s", lastAccountNameLc, accountNameLc != foundUser.AccountNameLc)
|
||||||
|
|
||||||
updatedUser := dbstructs.User{
|
updatedUser := dbstructs.User{
|
||||||
Id: params.UserId,
|
Id: params.UserId,
|
||||||
Username: body.Username,
|
Username: body.Username,
|
||||||
AccountName: body.AccountName,
|
AccountName: body.AccountName,
|
||||||
AccountNameLc: strings.ToLower(body.AccountName),
|
AccountNameLc: accountNameLc,
|
||||||
|
LastAccountNameLc: lastAccountNameLc,
|
||||||
|
AccountNameUpdatedAt: utils.GetCurrentTimestamp(),
|
||||||
Email: body.Email,
|
Email: body.Email,
|
||||||
Description: body.Description,
|
Description: body.Description,
|
||||||
AccountStatus: body.AccountStatus,
|
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())
|
gocnjhelper.LogErrorf("Failed to update user, err: %s", err.Error())
|
||||||
return c.SendStatus(fiber.StatusInternalServerError)
|
return c.SendStatus(fiber.StatusInternalServerError)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue