package user import ( "strings" "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" "github.com/gofiber/fiber/v2" ) func UpdateUser(c *fiber.Ctx) error { // swagger:operation PATCH /user/:userId user userUpdateUser // --- // summary: Update an user // consumes: // - application/json // produces: // - application/json // parameters: // - name: body // in: body // schema: // "$ref": "#/definitions/UpdateUserRequest" // responses: // '200': // description: User was updated // '500': // description: Internal server error var params structs.UserIdParam if err := c.ParamsParser(¶ms); err != nil { gocnjhelper.LogErrorf("Failed to parse params %s", err) return c.Status(fiber.StatusBadRequest).JSON(err) } errValidation := cnjvalidator.ValidateStruct(params) if errValidation != nil { return c.Status(fiber.StatusBadRequest).JSON(errValidation) } var body structs.UpdateUserRequest if err := c.BodyParser(&body); err != nil { gocnjhelper.LogErrorf("Failed to parse request data %s", err) return c.Status(fiber.StatusBadRequest).JSON(err) } errValidation = cnjvalidator.ValidateStruct(body) if errValidation != nil { return c.Status(fiber.StatusBadRequest).JSON(errValidation) } // get requested user from db foundUser := dbstructs.User{Id: params.UserId} if err := scylladb.Session.Query(gocnjhelper.DbMUsers.Get("email", "account_name_lc", "last_account_name_lc")).BindStruct(foundUser).GetRelease(&foundUser); err != nil { gocnjhelper.LogErrorf("Failed to find user, err: %s", err.Error()) return c.SendStatus(fiber.StatusInternalServerError) } // check whether the email in the requested has been changed in relation to the user 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 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: 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", "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) } return c.SendStatus(fiber.StatusOK) }