/// <summary> /// Deletes all dependencies for the specified agent /// </summary> /// <param name="agentId"></param> public void DeleteAgentDependencies(Agent agent) { bool childExists = CheckReferentialIntegrity(agent.Id.ToString()); if (childExists) { throw new EntityOperationException("Referential Integrity in Schedule or job table, please remove those before deleting this agent"); } _personRepo.ForceIgnoreSecurity(); Person person = _personRepo.Find(0, 1).Items?.Where(p => p.IsAgent && p.Name == agent.Name && !(p.IsDeleted ?? false))?.FirstOrDefault(); if (person != null) { _personRepo.SoftDelete((Guid)person.Id); } _personRepo.ForceSecurity(); var aspUser = _usersRepo.Find(0, 1).Items?.Where(u => u.PersonId == person.Id)?.FirstOrDefault(); if (aspUser != null) { var user = _userManager.FindByIdAsync(aspUser.Id).Result; var deleteResult = _userManager.DeleteAsync(user).Result; if (!deleteResult.Succeeded) { throw new EntityOperationException("Something went wrong, unable to delete agent user"); } } //delete all group members with this agent id var allAgentMembers = GetAllMembersInGroup(agent.Id.ToString()).Items; foreach (var member in allAgentMembers) { _agentGroupMemberRepository.SoftDelete(member.Id ?? Guid.Empty); } DeleteExistingHeartbeats(agent.Id ?? Guid.Empty); }