Exemplo n.º 1
0
        public async Task <IHttpActionResult> UpdateMyInfo(UserInfoDto myInfoDto)
        {
            _logger.Debug(string.Format("Begin. Clinic: [{0}], Email: [{1}], FirstName: [{2}], Gender: [{3}], LastName: [{4}], PhoneNumber: [{5}], Title: [{6}]",
                                        myInfoDto.Clinic,
                                        myInfoDto.Email,
                                        myInfoDto.FirstName,
                                        myInfoDto.Gender.HasValue ? myInfoDto.Gender.Value.ToString() : string.Empty,
                                        myInfoDto.LastName,
                                        myInfoDto.PhoneNumber,
                                        myInfoDto.Title.HasValue ? myInfoDto.Title.Value.ToString() : string.Empty));
            var id   = User.Identity.GetUserId();
            var user = await NdUserManager.FindByIdAsync(id);

            var emailChanged = false;

            try
            {
                if (!string.IsNullOrWhiteSpace(myInfoDto.Clinic) && user.Clinic != myInfoDto.Clinic)
                {
                    user.Clinic = myInfoDto.Clinic;
                }
                if (!string.IsNullOrWhiteSpace(myInfoDto.Email) && user.Email != myInfoDto.Email)
                {
                    emailChanged        = true;
                    user.EmailConfirmed = false;
                    user.Email          = myInfoDto.Email;
                }
                if (!string.IsNullOrWhiteSpace(myInfoDto.FirstName) && user.FirstName != myInfoDto.FirstName)
                {
                    user.FirstName = myInfoDto.FirstName;
                }
                if (myInfoDto.Gender.HasValue && user.Gender != myInfoDto.Gender.Value)
                {
                    user.Gender = myInfoDto.Gender.Value;
                }
                if (!string.IsNullOrWhiteSpace(myInfoDto.LastName) && user.LastName != myInfoDto.LastName)
                {
                    user.LastName = myInfoDto.LastName;
                }
                if (!string.IsNullOrWhiteSpace(myInfoDto.PhoneNumber) && user.PhoneNumber != myInfoDto.PhoneNumber)
                {
                    user.PhoneNumber = myInfoDto.PhoneNumber;
                }
                if (myInfoDto.Title.HasValue && user.Title != myInfoDto.Title.Value)
                {
                    user.Title = myInfoDto.Title.Value;
                }
            }
            catch (Exception ex)
            {
                _logger.Error(string.Format("Update my info failed. Id: [{0}]", id), ex);
                return(InternalServerError(ex));
            }

            var result = await NdUserManager.UpdateAsync(user);

            if (!result.Succeeded)
            {
                _logger.Error(string.Format(
                                  "Update my info failed. Email: [{0}], Reason: [{1}]",
                                  user.Email,
                                  string.Join(Environment.NewLine, result.Errors)));
                return(GetErrorResult(result));
            }
            await NdDbContext.SaveChangesAsync();

            try
            {
                await NdUserManager.SendEmailAsync(id, "Account Information Chaged", NdEmailService.CreateAccountInformationChangedBody(_factory.CreateUserInfo(user)));
            }
            catch (Exception ex)
            {
                _logger.Error(string.Format("Error sending AccountInformationChanged email. Email: [{0}]", user.Email), ex);
                return(InternalServerError(ex));
            }

            if (emailChanged)
            {
                try
                {
                    string code = await NdUserManager.GenerateEmailConfirmationTokenAsync(id);

                    var callbackUrl = new Uri(Url.Link("ConfirmEmailRoute", new { userId = id, code = code }));
                    await NdUserManager.SendEmailAsync(id, "Confirm your account", NdEmailService.CreateConfirmEmailBody(callbackUrl.ToString()));
                }
                catch (Exception ex)
                {
                    _logger.Error(string.Format("Error sending ConfirmEmail email. Email: {0}]", user.Email), ex);
                    return(InternalServerError(ex));
                }
            }

            _logger.Debug(string.Format("My info updated. Id: [{0}]", id));
            return(Ok());
        }