public void CoordinateMessagesCompleteWithAllMessagesComplete()
        {
            var coordinatorId = Guid.NewGuid();

            using (var session = DocumentStore.OpenSession())
            {
                var coordinatorTrackingData = new CoordinatorTrackingData
                {
                    CoordinatorId = coordinatorId,
                    MessageStatuses = new List<MessageSendingStatus> { new MessageSendingStatus { Number = "2323", ScheduledSendingTimeUtc = DateTime.Now, ActualSentTimeUtc = DateTime.Now, Cost = 0.33m, Status = MessageStatusTracking.CompletedSuccess } }
                };
                session.Store(coordinatorTrackingData, coordinatorId.ToString());
                session.SaveChanges();
            }

            var coordinatorCompleted = new CoordinatorCompleted { CoordinatorId = coordinatorId };

            var ravenDocStore = MockRepository.GenerateMock<IRavenDocStore>();
            ravenDocStore.Expect(r => r.GetStore()).Return(DocumentStore);
            var coordinatorTracker = new CoordinatorTracker() { RavenStore = ravenDocStore };
            coordinatorTracker.Handle(coordinatorCompleted);

            using (var session = DocumentStore.OpenSession())
            {
                var trackingData = session.Load<CoordinatorTrackingData>(coordinatorId.ToString());
                Assert.That(trackingData.CurrentStatus, Is.EqualTo(CoordinatorStatusTracking.Completed));
            }
        }
        public void CoordinateMessagesCompleteWithAllMessagesCompleteAndSendsConfirmationEmail()
        {
            var coordinatorId = Guid.NewGuid();

            using (var session = DocumentStore.OpenSession())
            {
                var coordinatorTrackingData = new CoordinatorTrackingData
                {
                    CoordinatorId = coordinatorId,
                    MessageStatuses = new List<MessageSendingStatus> { new MessageSendingStatus { Number = "2323", ScheduledSendingTimeUtc = DateTime.Now, ActualSentTimeUtc = DateTime.Now, Cost = 0.33m, Status = MessageStatusTracking.CompletedSuccess } },
                    ConfirmationEmailAddress = "email"
                };
                session.Store(coordinatorTrackingData, coordinatorId.ToString());
                session.SaveChanges();
            }

            var coordinatorCompleted = new CoordinatorCompleted { CoordinatorId = coordinatorId };

            var ravenDocStore = MockRepository.GenerateMock<IRavenDocStore>();
            var bus = MockRepository.GenerateMock<IBus>();
            ravenDocStore.Expect(r => r.GetStore()).Return(DocumentStore);
            bus.Expect(b => b.Send(Arg<CoordinatorCompleteEmail>.Is.Anything));

            var coordinatorTracker = new CoordinatorTracker { RavenStore = ravenDocStore, Bus = bus};
            coordinatorTracker.Handle(coordinatorCompleted);

            using (var session = DocumentStore.OpenSession())
            {
                var trackingData = session.Load<CoordinatorTrackingData>(coordinatorId.ToString());
                Assert.That(trackingData.CurrentStatus, Is.EqualTo(CoordinatorStatusTracking.Completed));
            }
            bus.VerifyAllExpectations();
        }
        public CoordinatorOverview(CoordinatorTrackingData coordinatorTrackingData, List<ScheduledMessagesStatusCountInCoordinatorIndex.ReduceResult> coordinatorSummary)
        {
            var sentSummary = coordinatorSummary.FirstOrDefault(s => s.Status == MessageStatus.Sent.ToString());
            var failedSummary = coordinatorSummary.FirstOrDefault(s => s.Status == MessageStatus.Failed.ToString());
            var scheduledSummary = coordinatorSummary.FirstOrDefault(s => s.Status == MessageStatus.Scheduled.ToString());
            var cancelledSummary = coordinatorSummary.FirstOrDefault(s => s.Status == MessageStatus.Cancelled.ToString());
            var waitingForSchedulingSummary = coordinatorSummary.FirstOrDefault(s => s.Status == MessageStatus.WaitingForScheduling.ToString());
            var pausedSummary = coordinatorSummary.FirstOrDefault(s => s.Status == MessageStatus.Paused.ToString());

            CoordinatorId = coordinatorTrackingData.CoordinatorId;
            CreationDateUtc = coordinatorTrackingData.CreationDateUtc;
            CompletionDateUtc = coordinatorTrackingData.CompletionDateUtc;
            CurrentStatus = coordinatorTrackingData.CurrentStatus;
            Topic = coordinatorTrackingData.MetaData.Topic;
            Tags = coordinatorTrackingData.MetaData.Tags;
            MessageCount = coordinatorTrackingData.MessageCount;
            MessageStatusCounter = new MessageStatusCounters
            {
                SentCount = sentSummary == null ? 0 : sentSummary.Count,
                ScheduledCount = scheduledSummary == null ? 0 : scheduledSummary.Count,
                FailedCount = failedSummary == null ? 0 : failedSummary.Count,
                CancelledCount = cancelledSummary == null ? 0 : cancelledSummary.Count,
                WaitingForSchedulingCount =
                    waitingForSchedulingSummary == null ? 0 : waitingForSchedulingSummary.Count,
                PausedCount = pausedSummary == null ? 0 : pausedSummary.Count,
            };
            MessageBody = coordinatorTrackingData.MessageBody;
        }
        public void CreaetExcludeMultiplePreviousCoordinatorMessagesRemovesMatchingNumbers_TrickleBetweenDates()
        {
            var CoordinatorToExclude1 = Guid.NewGuid();
            var CoordinatorToExclude2 = Guid.NewGuid();
            var model = new CoordinatedSharedMessageModel
            {
                Numbers = "04040404040, 1, 2, 3, 7, 12",
                Message = "asfdkjadfskl asflkj;faskjf;aslkjf;lasdkjfaslkfjas;lkfjslkfjas;lkfjsalkfjas;fklasj;flksdjf;lkasjflskdjflkasjflksjlk lskaf jlsk fdaskl dflksjfalk sflkj sfkl jlkjs flkj skjkj sadflkjsaflj",
                StartTime = DateTime.Now.AddHours(2),
                SendAllBy = DateTime.Now.AddHours(3),
                CoordinatorsToExclude = new List<Guid> { CoordinatorToExclude1, CoordinatorToExclude2 },
                Topic = "frank",
                UserTimeZone = "Australia/Sydney"
            };

            var bus = MockRepository.GenerateMock<IBus>();
            var mapper = MockRepository.GenerateMock<ICoordinatorModelToMessageMapping>();
            var ravenDocStore = MockRepository.GenerateMock<IRavenDocStore>();
            var docStore = MockRepository.GenerateMock<IDocumentStore>();
            var configSession = MockRepository.GenerateMock<IDocumentSession>();
            var trackingSession = MockRepository.GenerateMock<IDocumentSession>();
            var currentUser = MockRepository.GenerateStub<ICurrentUser>();

            ravenDocStore.Expect(r => r.GetStore()).Return(docStore);
            docStore.Expect(d => d.OpenSession("Configuration")).Return(configSession);
            docStore.Expect(d => d.OpenSession("SmsTracking")).Return(trackingSession);
            configSession.Expect(d => d.Load<CountryCodeReplacement>("CountryCodeConfig")).Return(new CountryCodeReplacement());
            var previousCoordinatorToExclude1 = new CoordinatorTrackingData(new List<MessageSendingStatus> { new MessageSendingStatus { Number = "04040404040" }, new MessageSendingStatus { Number = "1" } });
            trackingSession.Expect(d => d.Load<CoordinatorTrackingData>(CoordinatorToExclude1.ToString())).Return(
                previousCoordinatorToExclude1);
            var previousCoordinatorToExclude2 = new CoordinatorTrackingData(new List<MessageSendingStatus> { new MessageSendingStatus { Number = "7" } });
            trackingSession.Expect(d => d.Load<CoordinatorTrackingData>(CoordinatorToExclude2.ToString())).Return(
                previousCoordinatorToExclude2);

            var coordinatorMessage = new CoordinatedSharedMessageModel();
            var excludeList1 = previousCoordinatorToExclude1.GetListOfCoordinatedSchedules(ravenDocStore.GetStore()).Select(s => s.Number).ToList();
            var excludeList2 = previousCoordinatorToExclude2.GetListOfCoordinatedSchedules(ravenDocStore.GetStore()).Select(s => s.Number).ToList();

            List<string> excludeList = null;
            mapper
                .Expect(m => m.MapToTrickleOverPeriod(Arg<CoordinatedSharedMessageModel>.Is.Anything, Arg<CountryCodeReplacement>.Is.Anything, Arg<List<string>>.Is.Anything, Arg<string>.Is.Anything))
                .Return(new TrickleSmsOverCalculatedIntervalsBetweenSetDates())
                .WhenCalled(t => coordinatorMessage = (CoordinatedSharedMessageModel)(t.Arguments[0]))
                .WhenCalled(t => excludeList = (List<string>)(t.Arguments[2]));
            bus.Expect(b => b.Send(Arg<TrickleSmsOverCalculatedIntervalsBetweenSetDates>.Is.NotNull));

            var controller = new CoordinatorController { ControllerContext = new ControllerContext(), Bus = bus, Mapper = mapper, RavenDocStore = ravenDocStore, CurrentUser = currentUser };
            var actionResult = (RedirectToRouteResult)controller.Create(model);

            Assert.That(actionResult.RouteValues["action"], Is.EqualTo("Details"));
            Assert.That(coordinatorMessage.Message, Is.EqualTo(model.Message.Substring(0, 160)));
            Assert.That(excludeList.ToList(), Is.EqualTo(excludeList1.Union(excludeList2).Distinct().ToList()));

            bus.VerifyAllExpectations();
            mapper.VerifyAllExpectations();
            configSession.VerifyAllExpectations();
            trackingSession.VerifyAllExpectations();
        }
        public void CoordinateMessagesCompleteWithSomeIncompleteMessagesThrowsException()
        {
            var coordinatorId = Guid.NewGuid();

            using (var session = DocumentStore.OpenSession())
            {
                var coordinatorTrackingData = new CoordinatorTrackingData
                {
                    CoordinatorId = coordinatorId,
                    MessageStatuses = new List<MessageSendingStatus> { new MessageSendingStatus { Number = "2323", ScheduledSendingTimeUtc = DateTime.Now, ActualSentTimeUtc = DateTime.Now, Cost = 0.33m, Status = MessageStatusTracking.Paused } }
                };
                session.Store(coordinatorTrackingData, coordinatorId.ToString());
                session.SaveChanges();
            }

            var coordinatorCompleted = new CoordinatorCompleted { CoordinatorId = coordinatorId };

            var ravenDocStore = MockRepository.GenerateMock<IRavenDocStore>();
            ravenDocStore.Expect(r => r.GetStore()).Return(DocumentStore);
            var coordinatorTracker = new CoordinatorTracker { RavenStore = ravenDocStore };
            Assert.That(() => coordinatorTracker.Handle(coordinatorCompleted), Throws.Exception.With.Message.EqualTo("Cannot complete coordinator - some messages are not yet complete."));
        }
        public void CoordinateMessagesOneMessageScheduled()
        {
            var coordinatorId = Guid.NewGuid();
            const string updatedNumber = "04040044";
            using (var session = DocumentStore.OpenSession())
            {
                var message = new CoordinatorCreated
                {
                    CoordinatorId = coordinatorId,
                    ScheduledMessages = new List<MessageSchedule> {
                new MessageSchedule { Number = updatedNumber, ScheduledTimeUtc = DateTime.Now.AddMinutes(5)},
                new MessageSchedule { Number = "07777777", ScheduledTimeUtc = DateTime.Now.AddMinutes(10)}
                }
                };
                var coordinatorTrackingData = new CoordinatorTrackingData
                {
                    CoordinatorId = message.CoordinatorId,
                    MessageStatuses = message.ScheduledMessages
                        .Select(s => new MessageSendingStatus { Number = s.Number, ScheduledSendingTimeUtc = s.ScheduledTimeUtc }).
                        ToList()
                };
                session.Store(coordinatorTrackingData, message.CoordinatorId.ToString());
                session.SaveChanges();
            }

            var smsScheduled = new SmsScheduled { CoordinatorId = coordinatorId, ScheduleSendingTimeUtc = DateTime.UtcNow };

            var ravenDocStore = MockRepository.GenerateMock<IRavenDocStore>();
            ravenDocStore.Expect(r => r.GetStore()).Return(DocumentStore);
            var coordinatorTracker = new ScheduleTracker { RavenStore = ravenDocStore };
            coordinatorTracker.Handle(smsScheduled);

            using (var session = DocumentStore.OpenSession())
            {
                var trackingData = session.Load<CoordinatorTrackingData>(coordinatorId.ToString());
                var updatedMessageData = trackingData.MessageStatuses.First(m => m.Number == updatedNumber);
                Assert.That(updatedMessageData.Status, Is.EqualTo(MessageStatusTracking.Scheduled));
                Assert.That(updatedMessageData.ScheduledSendingTimeUtc, Is.EqualTo(smsScheduled.ScheduleSendingTimeUtc));
                Assert.That(updatedMessageData.ActualSentTimeUtc, Is.Null);
                Assert.That(updatedMessageData.Cost, Is.Null);
            }
        }
        public void CoordinateMessagesOneMessagePausedCurrentStatusSentThrowsException()
        {
            var coordinatorId = Guid.NewGuid();
            const string updatedNumber = "04040044";
            using (var session = DocumentStore.OpenSession())
            {
                var message = new CoordinatorCreated
                {
                    CoordinatorId = coordinatorId,
                    ScheduledMessages = new List<MessageSchedule> {
                new MessageSchedule { Number = updatedNumber, ScheduledTimeUtc = DateTime.Now.AddMinutes(5)},
                new MessageSchedule { Number = "07777777", ScheduledTimeUtc = DateTime.Now.AddMinutes(10)}
                }
                };
                var coordinatorTrackingData = new CoordinatorTrackingData
                {
                    CoordinatorId = message.CoordinatorId,
                    MessageStatuses = message.ScheduledMessages
                        .Select(s => new MessageSendingStatus { Number = s.Number, ScheduledSendingTimeUtc = s.ScheduledTimeUtc, Status = MessageStatusTracking.CompletedSuccess }).
                        ToList()
                };
                session.Store(coordinatorTrackingData, message.CoordinatorId.ToString());
                session.SaveChanges();
            }

            var messagePaused = new MessageSchedulePaused { CoordinatorId = coordinatorId };

            var ravenDocStore = MockRepository.GenerateMock<IRavenDocStore>();
            ravenDocStore.Expect(r => r.GetStore()).Return(DocumentStore);
            var coordinatorTracker = new ScheduleTracker { RavenStore = ravenDocStore };
            Assert.That(() => coordinatorTracker.Handle(messagePaused), Throws.Exception.With.Message.EqualTo("Cannot record pausing of message - it is already recorded as complete."));
        }
 public void Setup()
 {
     using (var session = base.DocumentStore.OpenSession())
     {
         var coordinatorTrackingData = new CoordinatorTrackingData
         {
             CoordinatorId = _coordinatorId,
             CurrentStatus = CoordinatorStatusTracking.Started
         };
         var message1 = new ScheduleTrackingData
                            {
                                SmsData = new SmsData("12313", "message"),
                                CoordinatorId = _coordinatorId,
                                MessageStatus = MessageStatus.Sent
                            };
         var message2 = new ScheduleTrackingData
                            {
                                SmsData = new SmsData("434039", "message"),
                                CoordinatorId = _coordinatorId,
                                MessageStatus = MessageStatus.Scheduled
                            };
         session.Store(coordinatorTrackingData, _coordinatorId.ToString());
         session.Store(message1, Guid.NewGuid().ToString());
         session.Store(message2, Guid.NewGuid().ToString());
         session.SaveChanges();
     }
 }
 private string CoordinatorToExcludeText(CoordinatorTrackingData coordinatorTrackingData)
 {
     return string.Format(
         "'{0}', {1} Sent, Started {2}",
         coordinatorTrackingData.MetaData.Topic,
         coordinatorTrackingData.GetListOfCoordinatedSchedules(RavenDocStore.GetStore()).Count(c => c.Status == MessageStatusTracking.CompletedSuccess).ToString(),
         coordinatorTrackingData.CreationDateUtc.ToLocalTime().ToShortDateString());
 }
        public void SaveCoordinator(CoordinatorCreatedWithEmailAndSms message)
        {
            bool trackingDataExists;
            using (var session = RavenDocStore.GetStore().OpenSession(Database))
            {
                var coordinatorTrackingData = session.Load<CoordinatorTrackingData>(message.CoordinatorId.ToString());
                trackingDataExists = coordinatorTrackingData != null;
            }

            if (trackingDataExists) return;
            using (var session = RavenDocStore.GetStore().BulkInsert(Database))
            {
                var coordinatorTrackingData = new CoordinatorTrackingData
                {
                    CoordinatorId = message.CoordinatorId,
                    CreationDateUtc = message.CreationDateUtc,
                    MetaData = message.MetaData,
                    ConfirmationEmailAddress = String.Join(", ", message.ConfirmationEmailAddresses),
                    UserOlsenTimeZone = message.UserOlsenTimeZone,
                    CurrentStatus = CoordinatorStatusTracking.Started,
                    MessageBody = message.SmsMessage,
                    MessageCount = message.SmsCount + message.EmailCount,
                    EmailData = message.EmailData,
                    EmailCount = message.EmailCount,
                    Username = message.UserName
                };
                session.Store(coordinatorTrackingData, message.CoordinatorId.ToString());
            }
        }
 public void SaveCoordinator(CoordinatorCreated message)
 {
     using (var session = RavenDocStore.GetStore().OpenSession())
     {
         var coordinatorTrackingData = new CoordinatorTrackingData
         {
             CoordinatorId = message.CoordinatorId,
             CreationDateUtc = message.CreationDateUtc,
             MetaData = message.MetaData,
             ConfirmationEmailAddress = String.Join(", ", message.ConfirmationEmailAddresses),
             UserOlsenTimeZone = message.UserOlsenTimeZone,
             CurrentStatus = CoordinatorStatusTracking.Started,
             MessageBody = message.MessageBody,
             MessageCount = message.MessageCount
         };
         session.Store(coordinatorTrackingData, message.CoordinatorId.ToString());
         session.SaveChanges();
     }
 }
 public void Setup()
 {
     using (var session = base.DocumentStore.OpenSession())
     {
         var coordinatorTrackingData = new CoordinatorTrackingData
         {
             CoordinatorId = _coordinatorId,
             CurrentStatus = CoordinatorStatusTracking.Started,
             MessageStatuses = new List<MessageSendingStatus>
             {
                 new MessageSendingStatus
                 {
                     Number = "12313",
                     Status = MessageStatusTracking.CompletedSuccess
                 },
                 new MessageSendingStatus
                 {
                     Number = "434039",
                     Status = MessageStatusTracking.Scheduled
                 }
             }
         };
         session.Store(coordinatorTrackingData, _coordinatorId.ToString());
         session.SaveChanges();
     }
 }