public async Task <IResponse <ChangePasswordAccountDto> > Handle(ChangePasswordAccountCommand request, CancellationToken cancellationToken)
        {
            Account account = await _accountRepository.GetAsync(p => p.Email.Equals(request.Email));

            if (account == null)
            {
                return(Response <ChangePasswordAccountDto> .Bad("E-mail não encontrado"));
            }

            if (string.IsNullOrEmpty(account.ResetPassword))
            {
                return(Response <ChangePasswordAccountDto> .Bad("Senha temporaria não encontrada"));
            }

            if (!account.ResetPassword.Equals(request.ResetPassword))
            {
                return(Response <ChangePasswordAccountDto> .Bad("Senha temporaria invalida"));
            }

            if (account.Password.Equals(request.NewPassword))
            {
                return(Response <ChangePasswordAccountDto> .Bad("A nova senha não pode ser igual a antiga"));
            }

            DateTime dateExpiration      = DateTime.UtcNow.AddMinutes(_configToken.Value.Minutes);
            ChangePasswordAccountDto res = new ChangePasswordAccountDto
            {
                DateExpiration = dateExpiration,
                Token          = Functions.GenerateToken(account, dateExpiration, _configToken.Value.Key),
                IsReset        = false,
                IsVerifyCode   = false,
                Type           = account.Type
            };

            account.ResetPassword = null;
            account.IsConfirmed   = true;
            account.VerifyCode    = null;
            account.Password      = request.NewPassword;

            _accountRepository.Update(account);
            await _repositoryUnitWork.CompleteAsync();

            return(Response <ChangePasswordAccountDto> .Ok(res));
        }
        public async Task <IResponse> Handle(ResetPasswordAccountCommand request, CancellationToken cancellationToken)
        {
            Account account = await _accountRepository.GetAsync(p => p.Email.Equals(request.Email));

            if (account == null)
            {
                return(Response.Bad("E-mail não encontrado"));
            }

            string resetPassword = Functions.GenerateCode();

            account.ResetPassword = resetPassword.GenerateMd5();
            _accountRepository.Update(account);
            await _repositoryUnitWork.CompleteAsync();

            _srvMail.SendResetPasswordAsync(account, resetPassword);

            return(Response.Ok());
        }
        public async Task <IResponse <VerifyCodeAccountDto> > Handle(VerifyCodeAccountCommand request, CancellationToken cancellationToken)
        {
            Account account = await _accountRepository.GetAsync(p => p.Email.Equals(request.Email));

            if (account == null)
            {
                return(Response <VerifyCodeAccountDto> .Bad("E-mail não encontrado"));
            }

            if (account.IsConfirmed)
            {
                return(Response <VerifyCodeAccountDto> .Bad("E-mail já confirmado"));
            }

            if (!account.Password.Equals(request.Password))
            {
                return(Response <VerifyCodeAccountDto> .Bad("Senha inválida"));
            }

            if (!account.VerifyCode.Equals(request.Code))
            {
                return(Response <VerifyCodeAccountDto> .Bad("Código inválido"));
            }

            DateTime dateExpiration = DateTime.UtcNow.AddMinutes(_configToken.Value.Minutes);

            VerifyCodeAccountDto res = new VerifyCodeAccountDto
            {
                DateExpiration = dateExpiration,
                Token          = Functions.GenerateToken(account, dateExpiration, _configToken.Value.Key),
                IsReset        = false,
                IsVerifyCode   = false,
                Type           = account.Type
            };

            account.IsConfirmed   = true;
            account.VerifyCode    = null;
            account.ResetPassword = null;
            _accountRepository.Update(account);
            await _repositoryUnitWork.CompleteAsync();

            return(Response <VerifyCodeAccountDto> .Ok(res));
        }
        public async Task <IResponse> Handle(RegisterAccountCommand request, CancellationToken cancellationToken)
        {
            Account account = await _accountRepository.GetAsync(p =>
                                                                p.Email.Equals(request.Email));

            if (account != null && account.IsConfirmed)
            {
                return(Response.Bad("E-mail já cadastrado"));
            }

            if (account != null && !account.IsConfirmed)
            {
                return(Response.Bad("E-mail aguardando confirmação"));
            }

            string verifyCode = Functions.GenerateCode();

            account = new Account
            {
                FirstName   = request.FirstName,
                SecondName  = request.SecondName,
                IsConfirmed = false,
                Email       = request.Email,
                Password    = request.Password,
                VerifyCode  = verifyCode.GenerateMd5(),
                CreatedAt   = DateTime.Now,
                Type        = (int)request.Type
            };

            await _accountRepository.CreateAsync(account);

            await _repositoryUnitWork.CompleteAsync();

            _srvMail.SendRegisterAsync(account, verifyCode);
            return(Response.Ok());
        }
        public async Task <IResponse <SigninAccountDto> > Handle(SigninAccountCommand request, CancellationToken cancellationToken)
        {
            Account account = await _accountRepository.GetAsync(p => p.Email.Equals(request.Email));

            if (account == null)
            {
                return(Response <SigninAccountDto> .Bad("E-mail não encontrado"));
            }

            if (!string.IsNullOrEmpty(account.ResetPassword) && !account.ResetPassword.Equals(request.Password))
            {
                return(Response <SigninAccountDto> .Bad("Senha temporaria inválida"));
            }

            if (string.IsNullOrEmpty(account.ResetPassword) && !account.Password.Equals(request.Password))
            {
                return(Response <SigninAccountDto> .Bad("Senha inválida"));
            }

            if (!account.IsConfirmed)
            {
                return(Response <SigninAccountDto> .Bad("E-mail aguardando confirmação"));
            }

            account.LastSignin = DateTime.Now;
            _accountRepository.Update(account);
            await _repositoryUnitWork.CompleteAsync();

            SigninAccountDto res            = null;
            DateTime         dateExpiration = DateTime.UtcNow.AddMinutes(_configToken.Value.Minutes);

            if (!string.IsNullOrEmpty(account.ResetPassword))
            {
                res = new SigninAccountDto
                {
                    DateExpiration = null,
                    Token          = null,
                    IsReset        = true,
                    IsVerifyCode   = false,
                    Type           = null
                };
                return(Response <SigninAccountDto> .Ok(res));
            }

            if (!string.IsNullOrEmpty(account.VerifyCode))
            {
                res = new SigninAccountDto
                {
                    DateExpiration = null,
                    Token          = null,
                    IsReset        = false,
                    IsVerifyCode   = true,
                    Type           = null
                };
                return(Response <SigninAccountDto> .Ok(res));
            }

            res = new SigninAccountDto
            {
                DateExpiration = dateExpiration,
                Token          = Functions.GenerateToken(account, dateExpiration, _configToken.Value.Key),
                IsReset        = false,
                IsVerifyCode   = false,
                Type           = account.Type
            };

            return(Response <SigninAccountDto> .Ok(res));
        }