예제 #1
0
        public async Task <IActionResult> Delete(string id)
        {
            Agent agent = agentRepo.GetOne(new Guid(id));

            if (agent == null)
            {
                ModelState.AddModelError("Delete Agent", "No Agent was found with the specified Agent ID");
                return(NotFound(ModelState));
            }

            bool childExists = agentManager.CheckReferentialIntegrity(id);

            if (childExists)
            {
                ModelState.AddModelError("Delete Agent", "Referential Integrity in Schedule or Job table, please remove those before deleting this agent.");
                return(BadRequest(ModelState));
            }

            personRepo.ForceIgnoreSecurity();
            Person person = personRepo.Find(0, 1).Items?.Where(p => p.IsAgent && p.Name == agent.Name && !(p.IsDeleted ?? false))?.FirstOrDefault();

            if (person == null)
            {
                ModelState.AddModelError("Delete Agent", "Something went wrong, could not find Agent Person");
                return(BadRequest(ModelState));
            }
            else
            {
                personRepo.SoftDelete((Guid)person.Id);
            }
            personRepo.ForceSecurity();

            var aspUser = usersRepo.Find(0, 1).Items?.Where(u => u.PersonId == person.Id)?.FirstOrDefault();

            if (aspUser == null)
            {
                ModelState.AddModelError("Delete Agent", "Something went wrong, could not find Agent User");
                return(BadRequest(ModelState));
            }

            var user = await userManager.FindByIdAsync(aspUser.Id);

            var deleteResult = await userManager.DeleteAsync(user);

            if (!deleteResult.Succeeded)
            {
                ModelState.AddModelError("Delete Agent", "Something went wrong, unable to Delete Agent User");
                return(BadRequest(ModelState));
            }

            agentManager.DeleteExistingHeartbeats(agent.Id ?? Guid.Empty);

            await webhookPublisher.PublishAsync("Agents.AgentDeleted", agent.Id.ToString(), agent.Name).ConfigureAwait(false);

            return(await base.DeleteEntity(id));
        }