Example #1
0
        public async Task <IActionResult> ForgotPassword([FromBody] Contacts contact)
        {
            _logger.LogInformation(LoggerEvents.IsExistItem, "Check valid user by contacts({0})", JsonConvert.SerializeObject(contact));
            try
            {
                if (contact.Email == null && contact.Phone == null)
                {
                    return(ResponseResult(StatusCodes.Status400BadRequest, "User_StatusCode_400_BadRequest"));
                }

                var validUserId = contact.Phone != null ? await _contactService.CheckValidUserByPhoneAsync(contact.Phone) :
                                  contact.Email != null ? await _contactService.CheckValidUserByEmailAsync(contact.Email) : 0;

                if (validUserId == 0)
                {
                    return(Ok());
                }
                else
                {
                    await _userService.DeleteForgotPasswordOTPAsync(validUserId);

                    int.TryParse(_configuration["PasswordConfig:OTPExpiryDays"], out int expirationDays);
                    ForgotPassword forgotPassword = new ForgotPassword()
                    {
                        UserID        = validUserId,
                        OTPExpiryTime = DateTime.UtcNow.AddDays(expirationDays),
                        ResetOTP      = GetOTP(),
                        ResetTime     = null,
                        CreatedDate   = DateTime.UtcNow
                    };

                    var result = await _userService.CreateAsync(forgotPassword);
                }
                return(Ok());
            }
            catch (ArgumentException ex)
            {
                _logger.LogWarning(LoggerEvents.InsertItem, ex, "Phone or email not match({0})", JsonConvert.SerializeObject(contact));
                return(ResponseResult(StatusCodes.Status404NotFound, "Contact_StatusCode_404_NotFound"));
            }
            catch (Exception ex)
            {
                _logger.LogWarning(LoggerEvents.InsertItem, ex, "CreatePassword by contacts({0})", JsonConvert.SerializeObject(contact));
                return(ResponseResult(StatusCodes.Status500InternalServerError, "User_StatusCode_500_InternalServerError"));
            }
        }