public void EmailMapper_DoesNotMap_EmailReference()
        {
            //Arrange
            const string reference = "ABCD1234";
            var email1 = new Email { EmailReference = reference };
            var email2 = new Email { EmailReference = string.Empty };

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

            //Assert
            email1.EmailReference.Should().Be(reference);
        }
        public void EmailMapper_DoesNotMap_DateDeleted()
        {
            //Arrange
            var dateDeleted = DateTime.UtcNow;
            var email1 = new Email { DateDeleted = dateDeleted };
            var email2 = new Email { DateDeleted = DateTime.MinValue };

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

            //Assert
            email1.DateDeleted.Should().Be(dateDeleted);
        }
        public void EmailMapper_DoesNotMap_EmailId()
        {
            //Arrange
            const int id = 123;
            var email1 = new Email { EmailId = id };
            var email2 = new Email { EmailId = 0 };

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

            //Assert
            email1.EmailId.Should().Be(id);
        }
        public bool UpdateCustomerEmail(string applicationReference, string customerReference, string emailAddressReference, Email email)
        {
            Check.If(applicationReference).IsNotNullOrEmpty();
            Check.If(customerReference).IsNotNullOrEmpty();
            Check.If(emailAddressReference).IsNotNullOrEmpty();
            Check.If(email).IsNotNull();

            if (CustomerIsMasterTenant(customerReference) && EmailIsReserved(customerReference, email.Address))
            {
                return false;
            }

            return _emailRepository.UpdateCustomerEmailForApplication(applicationReference, customerReference,
                emailAddressReference, email);
        }
        public string CreateCustomerEmail(string applicationReference, string customerReference, Email email)
        {
            Check.If(applicationReference).IsNotNullOrEmpty();
            Check.If(customerReference).IsNotNullOrEmpty();
            Check.If(email).IsNotNull();

            if (!string.IsNullOrEmpty(email.EmailReference))
            {
                return null;
            }

            if (CustomerIsMasterTenant(customerReference) && EmailIsReserved(customerReference, email.Address))
            {
                return null;
            }

            var result = _emailRepository.CreateCustomerEmailForApplication(applicationReference, customerReference,
                email.CreateReference(_referenceGenerator));

            return result ? email.EmailReference : null;
        }
        public bool CreateCustomerEmailForApplication(string applicationReference, string customerReference, Email email)
        {
            if (string.IsNullOrEmpty(applicationReference) || string.IsNullOrEmpty(customerReference))
                return false;

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

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

            if (customer.IsNull())
                return false;

            customer.Emails.Add(email);
            customer.MarkUpdated();
            application.MarkUpdated();

            return _applicationsContext.SaveChanges() > 0;
        }
        public void EmailMapper_Maps_AddressType()
        {
            //Arrange
            const EmailType type = EmailType.Personal;
            var email1 = new Email { EmailAddressType = EmailType.ThirdParty };
            var email2 = new Email { EmailAddressType = type };

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

            //Assert
            email1.EmailAddressType.Should().Be(type);
        }
        public void EmailMapper_Maps_Address()
        {
            //Arrange
            const string emailAddress = "*****@*****.**";
            var address1 = new Email { Address = string.Empty };
            var address2 = new Email { Address = emailAddress };

            //Act
            _mapper.Map(address2, address1);

            //Assert
            address1.Address.Should().Be(emailAddress);
        }
        public void EmailMapper_Maps_DoNotUse()
        {
            //Arrange
            var email1 = new Email { DoNotUse = false };
            var email2 = new Email { DoNotUse = true };

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

            //Assert
            email1.DoNotUse.Should().Be(true);
        }
        public bool UpdateCustomerEmailForApplication(string applicationReference, string customerReference, string emailReference,
            Email email)
        {
            var application =
                _applicationsContext.Applications
                    .Active()
                    .NotArchived()
                    .Include(c => c.Customers.Select(e => e.Emails))
                    .FirstOrDefault(a => a.ApplicationReference == applicationReference);

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

            var existingEmail = existingCustomer?.Emails.Active().FirstOrDefault(e => e.EmailReference == emailReference);

            if (existingEmail.IsNull())
                return false;

            var isDirty = _emailMapper.Map(email, existingEmail);

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

            return !isDirty || _applicationsContext.SaveChanges() > 0;
        }