public List<Praise> GetTestPraises(User user, int number)
 {
     var Praises = new List<Praise>();
     var today = DateTime.Now;// by default each concern is today- move month back by 1
     for (int i = 1; i <= number; i++)
     {
         var date = DateTime.Now.AddMonths(0 - i);
         var Praise = new Praise();
         string SicknessNumber = i.ToString();
         Praise.Id = IlluminateDatabase.GenerateId<Praise>();
         Praise.EventDate = date;
         Praise.SubjectUser = new SimpleUser();
         Praise.SubjectUser.UserId = user.Id;
         Praise.SubjectUser.FullName = user.FirstName + " " + user.LastName;
         Praises.Add(Praise);
     }
     return Praises;
 }
 public void CreateTestPraises(int numberofpraises, List<User> users)
 {
     using (var orgSession = OrgStore.OpenSession())
     {
         for (int k = 0; k < users.Count; k++)
         {
             var Praises = new List<Praise>();
             var today = DateTime.Now;// by default each concern is today- move month back by 1
             for (int i = 1; i <= numberofpraises; i++)
             {
                 var date = DateTime.Now.AddMonths(0 - i);
                 var Praise = new Praise();
                 string PraiseNumber = i.ToString();
                 Praise.Id = IlluminateDatabase.GenerateId<Praise>();
                 Praise.EventDate = date;
                 Praise.SubjectUser = new SimpleUser();
                 Praise.SubjectUser.UserId = users[k].Id;
                 Praise.SubjectUser.FullName = users[k].FirstName + " " + users[k].LastName;
                 Praises.Add(Praise);
             }
             foreach (Praise praise in Praises)
             {
                 orgSession.Store(praise);
                 orgSession.SaveChanges();
             }
             var check = orgSession.Query<Praise>().ToList();
             // this gets the same error.... 
             //var check2 = orgSession.Query<Concern>().ToList();
         }
     }
 }
        public void NoPraiseInferenceInferenceAlreadyMade()
        {
            CreateTestConfig();
            var store = GetTestStore();
            CreateTestUsers(store);
           
            using (var session = store.OpenSession())
            {
                var existingInference = new Common.Inference.Inference
                {
                    Id = IlluminateDatabase.GenerateId<Common.Inference.Inference>(),
                    InferenceTypeName = NoPraiseInferenceType.ThisName,
                    MadeAt = DateTime.Today.AddDays(-1),
                    UserId = _user1.Id
                };
                session.Store(existingInference);
                var praise = new Praise
                {
                    Id = IlluminateDatabase.GenerateId<Praise>(),
                    SubjectUser = _user2.ToSimpleUser(),
                    EventDate = DateTime.Today.AddDays((-1))
                };
                session.Store(praise);
                session.SaveChanges();
            }

            var processor = new InferenceProcessor(new[] { store }, new IInferenceType[] {
                new NoPraiseInferenceType(_typeConfig) 
                                       {
                                           Actions = new IInferenceAction[]
                                                         {
                                                             new NotifyManagerAction()
                                                         }}}, MockRepository.GenerateStub<log4net.ILog>());
            processor.Process();
            using (var session = store.OpenSession())
            {
                Assert.AreEqual(session.Query<Common.Inference.Inference>().Count(), 1);
                Assert.AreEqual(session.Query<Notification>().Count(), 0);
            }
        }
        private void CreateTestPraise(IDocumentStore store)
        {
            using (var session = store.OpenSession())
            {
                //for user 1 generate praise that should cause inference
                var startDate = DateTime.Today.AddDays(0 - _typeConfig.PeriodDays).AddDays(1);
                for (var count = 0; count < _typeConfig.NumberInPeriod + 1; count++)
                {
                    var praise = new Praise()
                    {
                        EventDate = startDate,
                        Id = IlluminateDatabase.GenerateId<Concern>(),
                        SubjectUser = _user1.ToSimpleUser()
                    };

                    session.Store(praise);
                    startDate = startDate.AddDays(1);
                }
                startDate = DateTime.Today.AddDays(0 - _typeConfig.PeriodDays).AddDays(1);

                //for user 2 add too few concerns to trigger inference
                for (var count = 0; count < _typeConfig.NumberInPeriod - 1; count++)
                {
                    var praise = new Praise()
                    {
                        EventDate = startDate,
                        Id = IlluminateDatabase.GenerateId<Concern>(),
                        SubjectUser = _user2.ToSimpleUser()
                    };
                    session.Store(praise);
                    startDate = startDate.AddDays(1);
                }

                session.SaveChanges();
            }
        }
        /// <summary>
        /// Create a priase for a person
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public JsonResult CreatePraise(PraiseViewModel model)
        {
            //remove Id from modelstate if tehre. 
            ModelState.Remove("Id");
            ModelState.Remove("EventDate");
            ModelState.Remove("CreatedBy");
            
            if (ModelState.IsValid)
            {
                using (var orgSession = OrgStore.OpenSession())
                {
                    //who is the priase for
                    var target = orgSession.Load<User>(model.SubjectUserId);

                    var praise = new Praise
                                      {
                                          CreatedBy = OrgUser.ToSimpleUser(),
                                          EventDate = DateTime.Now,
                                          ConsolidatedCompletionDate = DateTime.Now,
                                          Comment = model.Comment,
                                          SubjectUser = target.ToSimpleUser(),
                                          Level = model.Level,
                                          Id = IlluminateDatabase.GenerateId<Praise>()
                                      };

                    orgSession.Store(praise);
                    //also send the target an email letting them know
                    var notify = new Notification
                    {

                        Body =Email.GetPraiseEmailBody(target,OrgUser,model.Comment),
                        SendDate = DateTime.Now,
                        Title = string.Format("Congratulations you have been praised"),
                        From = EmailRecipient.GetIlluminateRecipient(),
                        NotificationRecipients = new[]
                                                                          {
                                                                              new NotificationRecipient
                                                                                  {

                                                                                      NotificationDeliveryTypes =
                                                                                          
                                                                                          NotificationDeliveryTypes
                                                                                              .Email ,
                                                                                      
                                                                                      Users = new List<SimpleUser>{target.ToSimpleUser()}

                                                                                  }
                                                                          },
                    };
                    orgSession.Store(notify);
                    orgSession.SaveChanges();

                    return JsonSuccess(praise.ToTimelineItems(), "Praise created");
                }
            }
            return JsonError("Invalid data");
        }
        private Praise GenerateAPraise(IUser user,Random rand)
        {
            var manager = user.Relationships.FirstOrDefault(r => r.RelationshipName == "LineManager");
            if (manager == null)
            {
                return null;
            }
            var now = DateTime.UtcNow.Date;
            var date = RandomDate(rand, now.AddDays(0 - _config.DaysHistory), now);
            var praise = new Praise
                             {
                                 Comment =FormatComment( _config.PraiseConfig.PotentialComments.ElementAt(rand.Next(0,
                                                                                         _config.PraiseConfig.PotentialComments
                                                                                                .Count() - 1)),user)
                                    ,
                                 CreatedBy = manager,
                                 Id = IlluminateDatabase.GenerateId<Praise>(),
                                 Level = rand.Next(Praise.Min, Praise.Max+1),
                                    ConsolidatedCompletionDate = date,
                                    EventDate = date,
                                    SubjectUser = user.ToSimpleUser()
                             };

           return praise;
        }