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); foreach (EmailChangedEvent ev in user.EmailChangedEvents) { _messageBus.SendEmailChangedMessage(ev.UserId, ev.NewEmail); } return("OK"); }
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 sut = new UserController(db, messageBusMock.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); }