Exemple #1
0
        public void Changing_email_from_corporate_to_non_corporate()
        {
            // Arrange
            var  db   = new Database(ConnectionString);
            User user = CreateUser("*****@*****.**", UserType.Employee, db);

            CreateCompany("mycorp.com", 1, db);

            var messageBusMock = new Mock <IMessageBus>();
            var loggerMock     = new Mock <IDomainLogger>();
            var sut            = new UserController(
                db, messageBusMock.Object, loggerMock.Object);

            // Act
            string result = sut.ChangeEmail(user.UserId, "*****@*****.**");

            // Assert
            Assert.Equal("OK", result);

            object[] userData   = db.GetUserById(user.UserId);
            User     userFromDb = UserFactory.Create(userData);

            Assert.Equal("*****@*****.**", userFromDb.Email);
            Assert.Equal(UserType.Customer, userFromDb.Type);

            object[] companyData   = db.GetCompany();
            Company  companyFromDb = CompanyFactory.Create(companyData);

            Assert.Equal(0, companyFromDb.NumberOfEmployees);

            messageBusMock.Verify(
                x => x.SendEmailChangedMessage(user.UserId, "*****@*****.**"),
                Times.Once);
            loggerMock.Verify(
                x => x.UserTypeHasChanged(
                    user.UserId, UserType.Employee, UserType.Customer),
                Times.Once);
        }
Exemple #2
0
        public string ChangeEmail(int userId, string newEmail)
        {
            object[] userData = _database.GetUserById(userId);
            User     user     = UserFactory.Create(userData);

            string error = user.CanChangeEmail();

            if (error != null)
            {
                return(error);
            }

            object[] companyData = _database.GetCompany();
            Company  company     = CompanyFactory.Create(companyData);

            user.ChangeEmail(newEmail, company);

            _database.SaveCompany(company);
            _database.SaveUser(user);
            _eventDispatcher.Dispatch(user.DomainEvents);

            return("OK");
        }