/// <summary> /// Inserts a new user entity to database. User entity will /// be marked as unregistered by assigning RegistrationToken /// </summary> /// <param name="newUser">New user to be inserted</param> public User RegisterNewUser(PostUser newUser) { using (var transaction = new TransactionScope()) { newUser.RegistrationToken = HashingUtil.GenerateToken(); var userInRespository = usersDao.SelectByEmail(newUser.Email); if (userInRespository != null) { if (userInRespository.RegistrationToken == null) { throw new UniqueFieldException($"User with email {newUser.Email} is already registered", nameof(PostUser.Email)); } // If user exists but has not yet finished registration, renew their registration token userInRespository.RegistrationToken = newUser.RegistrationToken; usersDao.UpdateUser(userInRespository); } else { userInRespository = usersDao.InsertAndReturnNewUser(newUser); } userIntegrations.CreateInvitation(newUser); transaction.Complete(); return(userInRespository); } }