@ResponseBody
public SuccessErrorResponse changeEmail(@RequestBody ChangeEmailRequest emailRequest, Principal principal) throws URISyntaxException {
String newEmail = emailRequest.newEmail;
SecurityHelper.UserDetails detailsToCheck = securityHelper.getUserDetailsByEmail(newEmail);
if (detailsToCheck != null) {
return new SuccessErrorResponse("Such email is already registered", null);
}
final long userId = getUserId(principal);
userManagement.createEmailRequest(userId, newEmail);
SecurityHelper.UserDetails userDetails = securityHelper.getUserDetails(userId);
String changeEmailUrl = getEmailChangeUrl(userDetails);
userManagement.sendEmailRequestInstructions(userDetails.id, newEmail, changeEmailUrl);
return new SuccessErrorResponse(null, "Check your email and confirm email changing.");
}