public void AutoArchivePolicies() { Logger.Debug("Policy Archive Started"); var servicePolicy = new ServicePolicy(); var allPolicies = _uow.PolicyRepository.Get(x => !x.Archived); foreach (var policy in allPolicies) { if (policy.AutoArchiveType == EnumPolicy.AutoArchiveType.AfterXdays) { int intArchiveDays; if (!int.TryParse(policy.AutoArchiveSub, out intArchiveDays)) { continue; } if (intArchiveDays <= 0) { continue; } var dateCutOff = DateTime.UtcNow - TimeSpan.FromDays(intArchiveDays); if (policy.StartDate < dateCutOff && policy.StartDate > _utcCutoff) { servicePolicy.DeactivatePolicy(policy.Id); servicePolicy.ArchivePolicy(policy.Id); } } else if (policy.AutoArchiveType == EnumPolicy.AutoArchiveType.WhenComplete) { var groupsWithThisPolicy = servicePolicy.GetPolicyGroups(policy.Id); var memberIds = new List <int>(); foreach (var group in groupsWithThisPolicy) { var groupMembers = new ServiceGroup().GetGroupMembers(group.Id); memberIds.AddRange(groupMembers.Select(member => member.Id)); } var distinctMembers = memberIds.Distinct().ToList(); if (!distinctMembers.Any()) { continue; } var successCount = _uow.PolicyHistoryRepository.Get(x => x.PolicyId == policy.Id && x.Result == EnumPolicyHistory.RunResult.Success).ToList().GroupBy(x => x.ComputerId).Count(); if (successCount == distinctMembers.Count) { servicePolicy.DeactivatePolicy(policy.Id); servicePolicy.ArchivePolicy(policy.Id); } } } }
public DtoActionResult Archive(int id) { var policy = _policyServices.GetPolicy(id); var result = _policyServices.ArchivePolicy(id); if (result == null) { throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound)); } if (result.Success) { var auditLog = new EntityAuditLog(); auditLog.ObjectType = "Policy"; auditLog.ObjectId = result.Id; auditLog.ObjectName = policy.Name; auditLog.ObjectJson = JsonConvert.SerializeObject(policy); auditLog.UserId = _userId; auditLog.AuditType = EnumAuditEntry.AuditType.Archive; _auditLogService.AddAuditLog(auditLog); } return(result); }