Skip to content

Instantly share code, notes, and snippets.

@emilybache
Created January 18, 2022 10:29
Show Gist options
  • Save emilybache/1e84df0b4d71f612e0759d3aefc30f08 to your computer and use it in GitHub Desktop.
Save emilybache/1e84df0b4d71f612e0759d3aefc30f08 to your computer and use it in GitHub Desktop.
public void changeEmail(String newEmail)
{
Object[] data = Database.getUserById(userId);
email = (String)data[1];
userType = (UserType)data[2];
if (Objects.equals(email, newEmail))
return;
Object[] companyData = Database.getCompany(userId);
int companyId = (int)companyData[0];
String companyDomainName = (String)companyData[1];
int numberOfEmployees = (int)companyData[2];
String emailDomain = newEmail.split("@")[1];
boolean isEmailCorporate = Objects.equals(emailDomain, companyDomainName);
UserType newType = isEmailCorporate
? UserType.Employee
: UserType.Customer;
if (userType != newType)
{
int delta = newType == UserType.Employee ? 1 : -1;
int newNumber = numberOfEmployees + delta;
Database.saveCompany(companyId, companyDomainName, newNumber);
}
email = newEmail;
userType = newType;
Database.saveUser(this);
MessageBus.sendEmailChangedMessage(this.userId, newEmail);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment