public JsonResult RefreshFromVendor(Guid?id)
        {
            try
            {
                Campaign campaign = Db.Campaigns
                                    .Include(x => x.Testing)
                                    .Include(x => x.Approved)
                                    .Include(x => x.Segments)
                                    .Include(x => x.Trackings)
                                    //.Where(x => x.Status == (int)CampaignStatus.Monitoring || x.Segments.Any(s => s.SegmentStatus == (int)SegmentStatus.Monitoring))
                                    .Where(x => x.Approved != null)
                                    .FirstOrDefault(x => x.Id == id);

                // Update Tracking
                if (campaign == null)
                {
                    throw new AdsException("Campaign not found.");
                }

                // On refresh it is deciding which vendor to call
                if (IsNxs)
                {
                    ClickMeterModelProcessor.PopulateFromClickMeter(Db, campaign);
                }
                else
                {
                    ProDataApiManager.FetchAndUpdateTrackings(Db, campaign);
                }

                return(Json(new JsonResponse()
                {
                    IsSucess = true
                }));
            }
            catch (Exception ex)
            {
                return(Json(new JsonResponse()
                {
                    IsSucess = false, ErrorMessage = ex.Message
                }));
            }
        }
        public static void FetchAndCheckForQcRules()
        {
            //DateTime dtFrom = DateTime.ParseExact("10/15/2017", "MM/dd/2017",CultureInfo.InvariantCulture);
            // For only campaigns of last 15 days
            DateTime dtFrom = DateTime.Now.AddDays(-30);

            using (var db = new WfpictContext())
            {
                bool isAutoProcessTracking = false;
                var  settingAuto           = db.Settings.FirstOrDefault(x => x.Key == StringConstants.KeyAutoProcessTracking);
                if (settingAuto != null)
                {
                    isAutoProcessTracking = int.Parse(settingAuto.Value) == 1;
                }
                if (isAutoProcessTracking)
                {
                    // Delete old log
                    db.SystemLogs.Where(x => x.LogType == (int)LogType.RulesProcessing || x.LogType == (int)LogType.Vendor).Delete();
                    db.SaveChanges();

                    LogHelper.AddLog(db, LogType.RulesProcessing, "", $"FetchAndCheckForQCRules started at {DateTime.Now}");
                    // any camp that is in monitoring or any whose any segment is in monitoring

                    List <Campaign> campaigns = db.Campaigns
                                                .Include(x => x.Testing)
                                                .Include(x => x.Approved)
                                                .Include(x => x.Segments)
                                                .Include(x => x.Trackings)
                                                .Where(x => x.Status == (int)CampaignStatus.Monitoring || x.Segments.Any(s => s.SegmentStatus == (int)SegmentStatus.Monitoring))
                                                .Where(x => x.Approved != null)
                                                .Where(x => DbFunctions.TruncateTime(x.CreatedAt) >= dtFrom)
                                                .Where(x => !string.IsNullOrEmpty(x.Testing.ClickMeterRotatorLinkId))
                                                .ToList();

                    LogHelper.AddLog(db, LogType.RulesProcessing, "", $"FetchAndCheckForQCRules processing {campaigns.Count} campaigns.");

                    // Update Tracking clicked opened
                    int index = 1;
                    foreach (var campaign in campaigns)
                    {
                        if (IsNxs)
                        {
                            ClickMeterModelProcessor.PopulateFromClickMeter(db, campaign);
                        }
                        else
                        {
                            ProDataApiManager.FetchAndUpdateTrackings(db, campaign);
                        }
                        LogHelper.AddLog(db, LogType.RulesProcessing, "", $"FetchAndCheckForQCRules completed {index} out of {campaigns.Count} campaigns.");
                        index++;
                    }

                    // Send them 5 days = 120
                    // Expire notifications that are > 120 hrs
                    var toBeExpired = db.Notifications.ToList()
                                      .Where(x => x.FoundAt != null && DateTime.Now.Subtract(x.FoundAt.Value).Hours >= 120)
                                      .ToList();
                    if (toBeExpired.Count > 0)
                    {
                        LogHelper.AddLog(db, LogType.RulesProcessing, "", "Expiring 120hrs old notifications");
                        foreach (var notification in toBeExpired)
                        {
                            notification.Status = (int)NotificationStatus.Expired;
                        }
                        db.SaveChanges();
                    }

                    LogHelper.AddLog(db, LogType.RulesProcessing, "", $"FetchAndCheckForQCRules finished at {DateTime.Now}, {campaigns.Count} campaigns refreshed.");
                }
            }
        }