public void ResetProjectData(string projectId) { if (String.IsNullOrEmpty(projectId)) { return; } Project project = _projectRepository.GetByIdCached(projectId); if (project == null) { return; } try { _errorStackRepository.RemoveAllByProjectId(projectId); _errorRepository.RemoveAllByProjectId(projectId); _dayStackStats.RemoveAllByProjectId(projectId); _monthStackStats.RemoveAllByProjectId(projectId); _dayProjectStats.RemoveAllByProjectId(projectId); _monthProjectStats.RemoveAllByProjectId(projectId); project.ErrorCount = 0; project.StackCount = 0; _projectRepository.Update(project); IQueryable <Project> orgProjects = _projectRepository.GetByOrganizationId(project.OrganizationId); Organization organization = _organizationRepository.GetById(project.OrganizationId); organization.ErrorCount = orgProjects.Sum(p => p.ErrorCount); organization.StackCount = orgProjects.Sum(p => p.StackCount); _organizationRepository.Update(organization); } catch (Exception e) { Log.Error().Project(projectId).Exception(e).Message("Error resetting project data.").Report().Write(); throw; } }
private void TryDeleteOrganization(Organization organization) { try { Log.Info().Message("Checking to see if organization '{0}' with Id: '{1}' can be deleted.", organization.Name, organization.Id).Write(); ObjectId id; if (String.IsNullOrWhiteSpace(organization.Id) || !ObjectId.TryParse(organization.Id, out id)) { Log.Info().Message("Organization '{0}' with Id: '{1}' has an invalid id.", organization.Name, organization.Id).Write(); return; } if (id.CreationTime >= DateTime.Now.SubtractDays(90)) { Log.Info().Message("Organization '{0}' with Id: '{1}' has been created less than 90 days ago.", organization.Name, organization.Id).Write(); return; } if (organization.LastErrorDate >= DateTime.Now.SubtractDays(90)) { Log.Info().Message("Organization '{0}' with Id: '{1}' has had an exception newer than 90 days.", organization.Name, organization.Id).Write(); return; } if (!String.IsNullOrEmpty(organization.StripeCustomerId)) { Log.Info().Message("Organization '{0}' with Id: '{1}' has a stripe customer id and cannot be deleted.", organization.Name, organization.Id).Write(); return; } Log.Info().Message("Removing existing empty projects for the organization '{0}' with Id: '{1}'.", organization.Name, organization.Id).Write(); List <Project> projects = _projectRepository.GetByOrganizationId(organization.Id).ToList(); if (projects.Any(project => project.TotalErrorCount > 0)) { Log.Info().Message("Organization '{0}' with Id: '{1}' has a project with existing data. This organization will not be deleted.", organization.Name, organization.Id).Write(); return; } foreach (Project project in projects) { Log.Info().Message("Resetting all project data for project '{0}' with Id: '{1}'.", project.Name, project.Id).Write(); _errorStackRepository.RemoveAllByProjectId(project.Id); _errorRepository.RemoveAllByProjectId(project.Id); _dayStackStats.RemoveAllByProjectId(project.Id); _monthStackStats.RemoveAllByProjectId(project.Id); _dayProjectStats.RemoveAllByProjectId(project.Id); _monthProjectStats.RemoveAllByProjectId(project.Id); } Log.Info().Message("Deleting all projects for organization '{0}' with Id: '{1}'.", organization.Name, organization.Id).Write(); _projectRepository.Delete(projects); Log.Info().Message("Removing users from organization '{0}' with Id: '{1}'.", organization.Name, organization.Id).Write(); List <User> users = _userRepository.GetByOrganizationId(organization.Id).ToList(); foreach (User user in users) { if (user.OrganizationIds.All(oid => String.Equals(oid, organization.Id))) { Log.Info().Message("Removing user '{0}' as they do not belong to any other organizations.", user.Id, organization.Name, organization.Id).Write(); _userRepository.Delete(user.Id); } else { Log.Info().Message("Removing user '{0}' from organization '{1}' with Id: '{2}'", user.Id, organization.Name, organization.Id).Write(); user.OrganizationIds.Remove(organization.Id); _userRepository.Update(user); } } Log.Info().Message("Deleting organization '{0}' with Id: '{1}'.", organization.Name, organization.Id).Write(); _organizationRepository.Delete(organization); // TODO: Send notifications that the organization and projects have been updated. } catch (Exception ex) { ex.ToExceptionless().MarkAsCritical().AddTags("Remove Stale Accounts").AddObject(organization).Submit(); } }