public void AssignNextAvailableInstance(string email, string domain, string nickname) { var instance = _masterRepository.RetrieveNextAvailableInstance(); if (instance == null) { throw new Exception("There are no Instances available for assignment"); } var user = _userManager.FindByName(email); if (user == null) { throw new Exception($"Unable to locate User {email}"); } var login = user.Logins.Where(x => x.LoginProvider == "Shopify" && x.ProviderKey == domain).ToList(); if (login == null) { throw new Exception($"Unable to locate Domain {domain} for User {email}"); } var domainInstance = _masterRepository.RetrieveInstanceByDomain(domain); if (domainInstance != null) { throw new Exception($"Instance {domainInstance.Id} already assigned to domain {domainInstance.OwnerDomain}"); } _masterRepository.AssignInstanceToUser(instance.Id, user.Id, nickname, domain); _logger.Info($"Assigned User {user.Email} to Instance {instance.Id}"); }