public void GenerateNewCardNumber_CorrectCardType_ReturnCorrectCardNumber(CardType type)
        {
            // Act
            var cardNumber       = _cardNumberGenerator.GenerateNewCardNumber(type);
            var validationResult = _cardChecker.CheckCardEmitter(cardNumber);

            // Assert
            Assert.True(validationResult);
        }
        public Card GenerateFakeCard(CardPostDto cardDto)
        {
            var card = new Card
            {
                Transactions = new List <Transaction>(),
                CardNumber   = _cardNumberGenerator.GenerateNewCardNumber(CardType.MAESTRO),
                CardName     = cardDto.Name,
                Currency     = (Currency)cardDto.Currency,
                CardType     = (CardType)cardDto.Type,
                DtOpenCard   = DateTime.Parse("01-01-2019")
            };

            _cardService.TryAddBonusOnOpen(card);

            return(card);
        }
        /// <inheritdoc />
        public (Card, IEnumerable <CustomModelError>) TryOpenNewCard(
            User user,
            string shortCardName,
            Currency currency,
            CardType cardType)
        {
            // Validation
            var validationResult = new List <CustomModelError>();

            validationResult.AddError(() => cardType == CardType.OTHER,
                                      "type", "Wrong type card", "Неверный тип карты", TypeCriticalException.CARD);

            if (validationResult.HasErrors())
            {
                return(null, validationResult);
            }

            // Select
            var cardNumber   = _cardNumberGenerator.GenerateNewCardNumber(cardType);
            var cardExisting = _cardRepository.Get(user, cardNumber) != null;

            // Validation
            validationResult.AddError(() => cardExisting,
                                      "internal", "Card exist", "Карта с таким номером уже существует", TypeCriticalException.CARD);

            if (validationResult.HasErrors())
            {
                return(null, validationResult);
            }

            // Create
            var newCard = new Card
            {
                User         = user,
                CardNumber   = cardNumber,
                CardName     = shortCardName,
                Currency     = currency,
                CardType     = cardType,
                Transactions = new List <Transaction>()
            };

            try
            {
                _cardRepository.Add(newCard);

                var addBonusOnOpenFlag = _cardService.TryAddBonusOnOpen(newCard);

                validationResult.AddError(() => !addBonusOnOpenFlag,
                                          "internal",
                                          "Add bonus to card failed",
                                          "Ошибка при открытии карты",
                                          TypeCriticalException.CARD);

                if (validationResult.HasErrors())
                {
                    return(null, validationResult);
                }

                _cardRepository.Save();
            }
            catch (Exception e)
            {
                return(null,
                       validationResult.AddError("internal", e.Message ?? "Internal error", "Что то пошло не так",
                                                 TypeCriticalException.CARD));
            }

            return(newCard, validationResult);
        }