* At this point, we have a valid registration code. We may safely clear the
* registration
*/
_userIndexRegistrationService.clearRegistrationForUserIndexKey(userIndex.getId());
User targetUser = _userDao.getUserForId(registration.getUserId());
if (targetUser == null)
return null;
User phoneUser = userIndex.getUser();
/**
* If the user index is already registered, our work is done
*/
if (phoneUser.equals(targetUser))
return userIndex;
/**
* If the phone user only has one index (the phoneNumber index), we merge
* the phone user with the registration target user. Otherwise, we keep the
* phone user, but just transfer the phone index
*/
if (phoneUser.getUserIndices().size() == 1) {
mergeUsers(userIndex.getUser(), targetUser);
} else {
userIndex.setUser(targetUser);
targetUser.getUserIndices().add(userIndex);
phoneUser.getUserIndices().remove(userIndex);
_userDao.saveOrUpdateUsers(phoneUser, targetUser);
}
// Refresh the user index
return getUserIndexForId(userIndex.getId());