Beispiel #1
0
        public ActionResult UpdateProfile(UpdateProfileModel model)
        {
            UserEntity userEntity = null;

            try
            {
                var userId = Guid.Parse(User.Identity.GetUserId());

                var securityUserInfo = this.AmiClient.GetUser(userId.ToString());
                userEntity = this.GetUserEntityBySecurityUserKey(userId);

                if (securityUserInfo == null || userEntity == null)
                {
                    TempData["error"] = Locale.UserNotFound;

                    return(RedirectToAction("Index", "Home"));
                }

                if (!model.IsValidNameLength(model.GivenName))
                {
                    this.ModelState.AddModelError(nameof(model.GivenName), Locale.GivenNameLength100);
                }

                if (!model.IsValidNameLength(model.Surname))
                {
                    this.ModelState.AddModelError(nameof(model.Surname), Locale.SurnameLength100);
                }

                if (ModelState.IsValid)
                {
                    securityUserInfo.User.Email       = model.Email;
                    securityUserInfo.User.PhoneNumber = model.PhoneNumber;

                    this.AmiClient.UpdateUser(userId, securityUserInfo);
                    var updatedUser = this.ImsiClient.Update <UserEntity>(model.ToUserEntity(userEntity));

                    var language = updatedUser.LanguageCommunication.FirstOrDefault(u => u.IsPreferred);

                    var code = LocalizationConfig.LanguageCode.English;

                    switch (language?.LanguageCode)
                    {
                    // only swahili is currently supported
                    case LocalizationConfig.LanguageCode.Swahili:
                        code = LocalizationConfig.LanguageCode.Swahili;
                        break;

                    default:
                        break;
                    }

                    Response.Cookies.Add(new HttpCookie(LocalizationConfig.LanguageCookieName, code));

                    TempData["success"] = Locale.ProfileUpdatedSuccessfully;

                    return(RedirectToAction("Index", "Home"));
                }
            }
            catch (Exception e)
            {
                Trace.TraceError($"Unable to update profile: {e}");
            }

            if (userEntity != null)
            {
                model = BuildUpdateModelMetaData(model, userEntity);
            }

            TempData["error"] = Locale.UnableToUpdateProfile;

            return(View(model));
        }