public void CoordinatorCreatedNoEmailWithDefaultEmailForCoordinatorSendEmail()
        {
            var ravenDocStore = MockRepository.GenerateMock<IRavenDocStore>();
            var session = MockRepository.GenerateMock<IDocumentSession>();
            var mailActioner = MockRepository.GenerateMock<IMailActioner>();
            var dateTimeMapper = MockRepository.GenerateMock<IDateTimeOlsenFromUtcMapping>();

            ravenDocStore.Expect(r => r.GetStore().OpenSession("Configuration"))
                .Return(session);
            var emailDefaultNotification = new EmailDefaultNotification { EmailAddresses = new List<string> { "*****@*****.**", "*****@*****.**" } };
            session.Expect(s => s.Load<EmailDefaultNotification>("EmailDefaultConfig")).Return(emailDefaultNotification);
            var mailgunConfig = new MailgunConfiguration {ApiKey = "key", DomainName = "domain", DefaultFrom = "*****@*****.**"};
            session.Expect(s => s.Load<MailgunConfiguration>("MailgunConfig")).Return(mailgunConfig);
            var message = new MailMessage();
            mailActioner.Expect(m => m.Send(Arg<MailgunConfiguration>.Is.Equal(mailgunConfig), Arg<MailMessage>.Is.NotNull)).WhenCalled(a => message = (MailMessage)(a.Arguments[1]));
            dateTimeMapper.Expect(d => d.DateTimeUtcToLocalWithOlsenZone(Arg<DateTime>.Is.Anything, Arg<string>.Is.Anything)).Return(DateTime.Now).Repeat.Any();

            var emailService = new EmailService { RavenDocStore = ravenDocStore, MailActioner = mailActioner, DateTimeOlsenFromUtcMapping = dateTimeMapper };
            var coordinatorComplete = new CoordinatorCreated { ScheduledMessages = new List<MessageSchedule> { new MessageSchedule { ScheduledTimeUtc = DateTime.Now }}, MetaData = new SmsMetaData()};
            emailService.Handle(new CoordinatorCreatedEmail(coordinatorComplete));

            Assert.That(message.From.Address, Is.EqualTo(mailgunConfig.DefaultFrom));
            Assert.That(message.To[0].Address, Is.EqualTo(emailDefaultNotification.EmailAddresses[0]));
            Assert.That(message.To[1].Address, Is.EqualTo(emailDefaultNotification.EmailAddresses[1]));
        }
        public void CoordinatorCreatedNoEmailNullDefaultEmailForCoordinatorNoAction()
        {
            var ravenDocStore = MockRepository.GenerateMock<IRavenDocStore>();
            var session = MockRepository.GenerateMock<IDocumentSession>();
            ravenDocStore.Expect(r => r.GetStore().OpenSession("Configuration"))
                .Return(session);
            session.Expect(s => s.Load<EmailDefaultNotification>("EmailDefaultConfig")).Return(null);

            var emailService = new EmailService { RavenDocStore = ravenDocStore };
            var coordinatorComplete = new CoordinatorCreated();
            emailService.Handle(new CoordinatorCreatedEmail(coordinatorComplete));
        }
        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 CoordinateMessagesCreated()
        {
            var coordinatorCreated = new CoordinatorCreated
            {
                CoordinatorId = Guid.NewGuid(),
                ScheduledMessages = new List<MessageSchedule>
                {
                    new MessageSchedule { Number = "04040044", ScheduledTimeUtc = DateTime.Now.AddMinutes(5)},
                    new MessageSchedule { Number = "07777777", ScheduledTimeUtc = DateTime.Now.AddMinutes(10)}
                },
                ConfirmationEmailAddress = "tony"
            };
            var ravenDocStore = MockRepository.GenerateMock<IRavenDocStore>();
            ravenDocStore.Expect(r => r.GetStore()).Return(DocumentStore);
            var coordinatorTracker = new CoordinatorTracker { RavenStore = ravenDocStore };
            coordinatorTracker.Handle(coordinatorCreated);

            using (var session = DocumentStore.OpenSession())
            {
                var coordinatorTrackingData = session.Load<CoordinatorTrackingData>(coordinatorCreated.CoordinatorId.ToString());
                Assert.That(coordinatorTrackingData.CurrentStatus, Is.EqualTo(CoordinatorStatusTracking.Started));
                Assert.That(coordinatorTrackingData.CoordinatorId, Is.EqualTo(coordinatorCreated.CoordinatorId));
                Assert.That(coordinatorTrackingData.MessageStatuses.Count, Is.EqualTo(2));
                Assert.That(coordinatorTrackingData.MessageStatuses[0].Number, Is.EqualTo(coordinatorCreated.ScheduledMessages[0].Number));
                Assert.That(coordinatorTrackingData.MessageStatuses[0].ScheduledSendingTimeUtc, Is.EqualTo(coordinatorCreated.ScheduledMessages[0].ScheduledTimeUtc));
                Assert.That(coordinatorTrackingData.MessageStatuses[0].Status, Is.EqualTo(MessageStatusTracking.WaitingForScheduling));
                Assert.That(coordinatorTrackingData.MessageStatuses[1].Number, Is.EqualTo(coordinatorCreated.ScheduledMessages[1].Number));
                Assert.That(coordinatorTrackingData.MessageStatuses[1].ScheduledSendingTimeUtc, Is.EqualTo(coordinatorCreated.ScheduledMessages[1].ScheduledTimeUtc));
                Assert.That(coordinatorTrackingData.MessageStatuses[1].Status, Is.EqualTo(MessageStatusTracking.WaitingForScheduling));
                Assert.That(coordinatorTrackingData.ConfirmationEmailAddress, Is.EqualTo(coordinatorCreated.ConfirmationEmailAddress));
            }
        }
 public CoordinatorCreatedEmail(CoordinatorCreated coordinatorCreated)
 {
     CoordinatorCreated = coordinatorCreated;
 }
 public CoordinatorCreatedEmail()
 {
     CoordinatorCreated = new CoordinatorCreated();
 }
        public void CoordinatorCreatedWithEmailNoDefaultEmailForCoordinatorSendEmail()
        {
            var ravenDocStore = MockRepository.GenerateMock<IRavenDocStore>();
            var session = MockRepository.GenerateMock<IDocumentSession>();
            var mailActioner = MockRepository.GenerateMock<IMailActioner>();

            ravenDocStore.Expect(r => r.GetStore().OpenSession("Configuration"))
                .Return(session);
            session.Expect(s => s.Load<EmailDefaultNotification>("EmailDefaultConfig")).Return(new EmailDefaultNotification());
            var mailgunConfig = new MailgunConfiguration {ApiKey = "key", DomainName = "domain", DefaultFrom = "*****@*****.**"};
            session.Expect(s => s.Load<MailgunConfiguration>("MailgunConfig")).Return(mailgunConfig);
            var message = new MailMessage();
            mailActioner.Expect(m => m.Send(Arg<MailgunConfiguration>.Is.Equal(mailgunConfig), Arg<MailMessage>.Is.NotNull)).WhenCalled(a => message = (MailMessage)(a.Arguments[1]));

            var emailService = new EmailService { RavenDocStore = ravenDocStore, MailActioner = mailActioner };
            var coordinatorComplete = new CoordinatorCreated { ConfirmationEmailAddress = "*****@*****.**", ScheduledMessages = new List<MessageSchedule> { new MessageSchedule { ScheduledTimeUtc = DateTime.Now }}};
            emailService.Handle(coordinatorComplete);

            Assert.That(message.From.ToString(), Is.EqualTo(mailgunConfig.DefaultFrom));
            Assert.That(message.To[0].Address, Is.EqualTo(coordinatorComplete.ConfirmationEmailAddress));
        }
        public void SaveCoordinator(CoordinatorCreated 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.MessageBody,
                        MessageCount = message.MessageCount,
                        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();
     }
 }