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 })); } }
private static void SendToVendorMulti(OrderVia orderVia, WfpictContext db, Campaign campaign, Vendor vendor, string[] segmentsSelected, string whiteLabelDomain) { var segments = db.CampaignSegments .Where(c => c.CampaignId == campaign.Id && segmentsSelected.Contains(c.SegmentNumber)) .ToList(); foreach (var segment in segments) { string sentOrder, queuedCampaignId = null; switch (orderVia) { case OrderVia.Email: sentOrder = EmailHelper.SendApprovedToVendor(vendor, campaign, segment); break; case OrderVia.Api: var response = ProDataApiManager.Create(campaign, segment, whiteLabelDomain); if (response.status != ProDataApiManager.Success) { throw new AdsException(response.ErrorMessage); } sentOrder = response.RequestMessage; queuedCampaignId = response.queued_pending_campaign_id.ToString(); break; default: throw new AdsException("Invalid orderVia while sending to vendor multiple"); } segment.SegmentStatus = (int)SegmentStatus.Monitoring; var campaignTracking = db.CampaignTrackings.FirstOrDefault(x => x.CampaignId == campaign.Id && x.SegmentNumber == segment.SegmentNumber); if (campaignTracking == null) { var trackingId = Guid.NewGuid(); var tracking = new CampaignTracking() { Id = trackingId, CreatedAt = DateTime.Now, CampaignId = campaign.Id, OrderNumber = campaign.OrderNumber, SegmentNumber = segment.SegmentNumber, Quantity = segment.Quantity, DateSent = DateTime.Now, SentOrder = sentOrder, IsCreatedThroughApi = orderVia == OrderVia.Api, QueuedCampaignId = queuedCampaignId }; db.CampaignTrackings.Add(tracking); db.SaveChanges(); } else { campaignTracking.Quantity = segment.Quantity; campaignTracking.DateSent = DateTime.Now; campaignTracking.SentOrder = sentOrder; campaignTracking.IsCreatedThroughApi = orderVia == OrderVia.Api; campaignTracking.QueuedCampaignId = queuedCampaignId; } LogHelper.AddLog(db, LogType.Vendor, campaign.OrderNumber, $"Multi {segment.SegmentNumber} has been sent to vendor successfully."); } }
private static void SendToVendorSingle(OrderVia orderVia, WfpictContext db, Campaign campaign, Vendor vendor, string whiteLabelDomain) { string sentOrder, queuedCampaignId = null; switch (orderVia) { case OrderVia.Email: sentOrder = EmailHelper.SendApprovedToVendor(vendor, campaign, null); break; case OrderVia.Api: var response = ProDataApiManager.Create(campaign, null, whiteLabelDomain); if (response.status != ProDataApiManager.Success) { throw new AdsException(response.ErrorMessage); } sentOrder = response.RequestMessage; queuedCampaignId = response.queued_pending_campaign_id.ToString(); break; default: throw new AdsException("Invalid orderVia while sending to vendor single"); } string orderNumberRdp = campaign.ReBroadcasted ? campaign.ReBroadcastedOrderNumber : campaign.OrderNumber; long quantity = campaign.ReBroadcasted ? campaign.ReBroadcastedQuantity : campaign.Approved.Quantity; var campaignTracking = db.CampaignTrackings.FirstOrDefault(x => x.CampaignId == campaign.Id && x.OrderNumber == orderNumberRdp && string.IsNullOrEmpty(x.SegmentNumber)); if (campaignTracking == null) { var trackingId = Guid.NewGuid(); var tracking = new CampaignTracking() { Id = trackingId, CreatedAt = DateTime.Now, CampaignId = campaign.Id, OrderNumber = orderNumberRdp, SegmentNumber = string.Empty, Quantity = quantity, DateSent = DateTime.Now, SentOrder = sentOrder, IsCreatedThroughApi = orderVia == OrderVia.Api, QueuedCampaignId = queuedCampaignId }; db.CampaignTrackings.Add(tracking); } else { campaignTracking.Quantity = quantity; campaignTracking.DateSent = DateTime.Now; campaignTracking.SentOrder = sentOrder; campaignTracking.IsCreatedThroughApi = orderVia == OrderVia.Api; campaignTracking.QueuedCampaignId = queuedCampaignId; } db.SaveChanges(); LogHelper.AddLog(db, LogType.Vendor, campaign.OrderNumber, !campaign.ReBroadcasted ? "Order has been sent to vendor successfully." : "Order Rebroad has been sent to vendor sucessfully."); }
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."); } } }