public async Task <IActionResult> DeleteGroup(DeleteGroupView model, CancellationToken cancellationToken) { if (!await _authorizationService.CanUserEditDepartmentGroupAsync(UserId, model.Group.DepartmentGroupId)) { Unauthorized(); } var group = await _departmentGroupsService.GetGroupByIdAsync(model.Group.DepartmentGroupId); var users = _departmentGroupsService.GetAllUsersForGroup(model.Group.DepartmentGroupId); var childGroups = await _departmentGroupsService.GetAllChildDepartmentGroupsAsync(model.Group.DepartmentGroupId); var units = await _unitsService.GetAllUnitsForGroupAsync(model.Group.DepartmentGroupId); var shifts = await _shiftsService.GetShiftGroupsByGroupIdAsync(model.Group.DepartmentGroupId); if (childGroups.Count > 0 || users.Count > 0 || units.Count > 0 || shifts.Count > 0) { model.Group = group; if (users != null) { model.UserCount = users.Count; } else { model.UserCount = 0; } if (childGroups != null) { model.ChildGroupCount = childGroups.Count; } else { model.ChildGroupCount = 0; } if (units != null) { model.UnitsCount = units.Count; } else { model.UnitsCount = 0; } if (shifts != null) { model.ShiftsCount = shifts.Count; } else { model.ShiftsCount = 0; } model.Message = string.Format("Cannot delete the {0} group because it is the parent to other groups, has users or units in it or has shifts assigned to it.", model.Group.Name); return(View(model)); } var department = await _departmentsService.GetDepartmentByIdAsync(group.DepartmentId); if (department.IsUserAnAdmin(UserId) || group.IsUserGroupAdmin(UserId)) { var auditEvent = new AuditEvent(); auditEvent.DepartmentId = DepartmentId; auditEvent.UserId = UserId; auditEvent.Type = AuditLogTypes.GroupRemoved; auditEvent.Before = group.CloneJson(); _eventAggregator.SendMessage <AuditEvent>(auditEvent); await _deleteService.DeleteGroupAsync(group.DepartmentGroupId, UserId); } return(RedirectToAction("Index", "Groups", new { Area = "User" })); }