예제 #1
0
        public JsonResult Create(LeaveAbsenceViewModel data)
        {
            if (ModelState.IsValid)
            {
                using (var orgSession = OrgStore.OpenSession())
                {
                    var type = orgSession.Load<LeaveType>(data.LeaveTypeId);
                    var subject = orgSession.Load<User>(data.UserId);

                    var leave = new Leave
                                    {
                                        Subject = subject.ToSimpleUser(),
                                        LeaveType = type,
                                        StartOfProcess = data.StartOfProcess,
                                        EndOfProcess = data.EndOfProcess,
                                        ConsolidatedCompletionDate = data.EndOfProcess ?? DateTime.Now,
                                        Title = data.Title,
                                        Summary = data.Summary,
                                        UserCreatedProcess = OrgUser.ToSimpleUser(),
                                        Id = IlluminateDatabase.GenerateId<Leave>(),
                                        Approval = null,
                                        
                                    };


                    //If leave was created by a manager, then approve it already. 
                    if (subject.CanBeManagedBy((User)OrgUser)) //TODO: might be good to return the relationship that caused the approval.
                    {
                        leave.Approval = Approval.CreateApproval(OrgUser.ToSimpleUser());  
                    }

                    //get the workflow config
                    var config = orgSession.Query<LeaveWorkflowConfiguration>().FirstOrDefault();
                    if (config == null)
                    {
                        GetLogger().Error(string.Format("Cannot get sickness config for {0} organisation - no document in database, creating a default", OrgKey));
                        config = LeaveWorkflowConfiguration.GetDefault();
                        orgSession.Store(config);
                        orgSession.SaveChanges();
                    }

                    //create a new default leave workflow and assign to Leave - store it in the DB
                    leave.Workflow = new DefaultLeaveWorkflow(config);
                    orgSession.Store(leave);

                    //update the user state. 
                    if (leave.StartOfProcess.Date <= DateTime.Today && leave.ConsolidatedCompletionDate.Date >= DateTime.Today)
                    {
                        subject.CurrentState = "onleave";
                    }
                    //handle the create leave event
                    leave.HandleEvent(OrgStore, "Leave", "Create", leave);

                    orgSession.SaveChanges();

                    return JsonSuccess(leave.ToTimelineItems(), "Leave item created");
                }
            }

            return JsonError("Invalid data: " + ViewData.ModelState.Values.Any(x => x.Errors.Count >= 1));
        }
예제 #2
0
        public JsonResult Update(LeaveAbsenceViewModel data)
        {

            //find task/process from the tasks. 
            using (var session = OrgStore.OpenSession())
            {
                var leave = session.Load<Leave>(data.Id);
                
                
                var subject = session.Load<User>(leave.Subject.UserId);

                var originaldata =
                    new
                    {
                        start = leave.StartOfProcess,
                        end = leave.EndOfProcess,
                        consolidatedend = leave.ConsolidatedCompletionDate
                    };


                //set values
                leave.StartOfProcess = data.StartOfProcess;
                leave.EndOfProcess = data.EndOfProcess;
                leave.ConsolidatedCompletionDate = data.EndOfProcess ?? DateTime.Now; 


                leave.LeaveType = session.Load<LeaveType>(data.LeaveTypeId); 
                leave.Title = data.Title;
                leave.Summary = data.Summary;

                //update the user state. 
                if (leave.StartOfProcess.Date <= DateTime.Today && leave.ConsolidatedCompletionDate.Date >= DateTime.Today)
                {
                    subject.CurrentState = "onleave";
                }
                if (data.EndOfProcess != null)
                {
                    subject.CurrentState = string.Empty;
                }

                leave.HandleEvent(OrgStore, "Leave", "Update", leave);

                session.SaveChanges();


                //TODO: what if new things created as part of the completion?
                return JsonSuccess(leave.ToTimelineItems(), "Leave Updated");
            }


        }