public void TelephoneMapper_Maps_DoNotUse()
        {
            //Arrange
            var telephoneNumber1 = new TelephoneNumber { DoNotUse = false };
            var telephoneNumber2 = new TelephoneNumber { DoNotUse = true };

            //Act
            _mapper.Map(telephoneNumber2, telephoneNumber1);

            //Assert
            telephoneNumber1.DoNotUse.Should().Be(true);
        }
        public void TelephoneNumberMapper_DoesNotMap_DateCreated()
        {
            //Arrange
            var dateCreated = DateTime.UtcNow;
            var telephoneNumber1 = new TelephoneNumber { DateCreated = dateCreated };
            var telephoneNumber2 = new TelephoneNumber { DateCreated = DateTime.MinValue };

            //Act
            _mapper.Map(telephoneNumber2, telephoneNumber1);

            //Assert
            telephoneNumber1.DateCreated.Should().Be(dateCreated);
        }
        public void TelephoneMapper_Maps_TelephoneType()
        {
            //Arrange
            const TelephoneType type = TelephoneType.Personal;
            var telephoneNumber1 = new TelephoneNumber { TelephoneType = TelephoneType.ThirdParty };
            var telephoneNumber2 = new TelephoneNumber { TelephoneType = type };

            //Act
            _mapper.Map(telephoneNumber2, telephoneNumber1);

            //Assert
            telephoneNumber1.TelephoneType.Should().Be(type);
        }
        public void TelephoneMapper_Maps_AddressType()
        {
            //Arrange
            const TelephoneNumberType type = TelephoneNumberType.Mobile;
            var email1 = new TelephoneNumber { TelephoneNumberType = TelephoneNumberType.Unknown };
            var email2 = new TelephoneNumber { TelephoneNumberType = type };

            //Act
            _mapper.Map(email2, email1);

            //Assert
            email1.TelephoneNumberType.Should().Be(type);
        }
        public string CreateCustomerTelephoneNumber(string applicationReference, string customerReference, TelephoneNumber telephone)
        {
            Check.If(applicationReference).IsNotNullOrEmpty();
            Check.If(customerReference).IsNotNullOrEmpty();
            Check.If(telephone).IsNotNull();

            if (!string.IsNullOrEmpty(telephone.TelephoneNumberReference))
            {
                return null;
            }

            if (CustomerIsMasterTenant(customerReference) &&
                TelephoneNumberIsMobile(telephone.TelephoneNumberType) &&
                MobileIsReserved(customerReference, telephone.Number))
            {
                return null;
            }

            var result = _telephoneNumberRepository.CreateCustomerTelephoneNumberForApplication(applicationReference, customerReference,
                telephone.CreateReference(_referenceGenerator));

            return result ? telephone.TelephoneNumberReference : null;
        }
        public bool CreateCustomerTelephoneNumberForApplication(string applicationReference, string customerReference,
            TelephoneNumber telephone)
        {
            if (string.IsNullOrEmpty(applicationReference) || string.IsNullOrEmpty(customerReference))
                return false;

            var application =
                _applicationsContext.Applications
                    .Active()
                    .NotArchived()
                    .Include(c => c.Customers.Select(a => a.Addresses))
                    .FirstOrDefault(a => a.ApplicationReference == applicationReference);

            var customer = application?.Customers.Active().FirstOrDefault(c => c.CustomerReference == customerReference);

            if (customer.IsNull())
                return false;

            customer.TelephoneNumbers.Add(telephone);
            customer.MarkUpdated();
            application.MarkUpdated();

            return _applicationsContext.SaveChanges() > 0;
        }
        public bool UpdateCustomerTelephoneNumberForApplication(string applicationReference, string customerReference,
            string telephoneNumberReference, TelephoneNumber telephone)
        {
            var application =
                _applicationsContext.Applications
                    .Active()
                    .NotArchived()
                    .Include(c => c.Customers.Select(t => t.TelephoneNumbers))
                    .FirstOrDefault(a => a.ApplicationReference == applicationReference);

            var existingCustomer =
                application?.Customers.Active().FirstOrDefault(c => c.CustomerReference == customerReference);

            var existingTelephoneNumber =
                existingCustomer?.TelephoneNumbers.Active()
                    .FirstOrDefault(a => a.TelephoneNumberReference == telephoneNumberReference);

            if (existingTelephoneNumber.IsNull())
                return false;

            var isDirty = _telephoneNumberMapper.Map(telephone, existingTelephoneNumber);

            if (isDirty)
            {
                existingCustomer.MarkUpdated();
                application.MarkUpdated();
            }

            return !isDirty || _applicationsContext.SaveChanges() > 0;
        }
        public bool UpdateCustomerTelephoneNumber(string applicationReference, string customerReference, string telephoneNumberReference,
            TelephoneNumber telephone)
        {
            Check.If(applicationReference).IsNotNullOrEmpty();
            Check.If(customerReference).IsNotNullOrEmpty();
            Check.If(telephoneNumberReference).IsNotNullOrEmpty();
            Check.If(telephone).IsNotNull();

            if (CustomerIsMasterTenant(customerReference) &&
                TelephoneNumberIsMobile(telephone.TelephoneNumberType) &&
                MobileIsReserved(customerReference, telephone.Number))
            {
                return false;
            }

            return _telephoneNumberRepository.UpdateCustomerTelephoneNumberForApplication(applicationReference, customerReference,
                telephoneNumberReference, telephone);
        }
        public void TelephoneNumberMapper_DoesNotMap_TelephoneNumberId()
        {
            //Arrange
            const int id = 123;
            var telephoneNumber1 = new TelephoneNumber { TelephoneNumberId = id };
            var telephoneNumber2 = new TelephoneNumber { TelephoneNumberId = 0 };

            //Act
            _mapper.Map(telephoneNumber2, telephoneNumber1);

            //Assert
            telephoneNumber1.TelephoneNumberId.Should().Be(id);
        }
        public void TelephoneNumberMapper_Maps_Number()
        {
            //Arrange
            const string number = "07793123456";
            var telephoneNumber1 = new TelephoneNumber { Number = string.Empty };
            var telephoneNumber2 = new TelephoneNumber { Number = number };

            //Act
            _mapper.Map(telephoneNumber2, telephoneNumber1);

            //Assert
            telephoneNumber1.Number.Should().Be(number);
        }
        public void TelephoneNumberMapper_DoesNotMap_TelephoneNumberReference()
        {
            //Arrange
            const string reference = "ABCD1234";
            var telephoneNumber1 = new TelephoneNumber { TelephoneNumberReference = reference };
            var telephoneNumber2 = new TelephoneNumber { TelephoneNumberReference = string.Empty };

            //Act
            _mapper.Map(telephoneNumber2, telephoneNumber1);

            //Assert
            telephoneNumber1.TelephoneNumberReference.Should().Be(reference);
        }