Пример #1
0
        public IHttpActionResult EditActivity()
        {
            try
            {
                using (MaxMasterDbEntities db = new MaxMasterDbEntities())
                {
                    var form           = HttpContext.Current.Request.Form;
                    var taskId         = form.Get("taskId");
                    var userId         = User.Identity.GetUserId();
                    var hoursWorked    = form.Get("hoursWorked");
                    var quantityWorked = form.Get("quantityWorked");
                    int orgId          = Convert.ToInt32(form.Get("OrgId"));
                    var actionType     = form.Get("actionType");

                    var activityNotification = db.AddActivityNotifications(taskId, User.Identity.GetUserId());

                    Activity mainTask = db.Activities.Find(taskId);

                    if (actionType == "Assign")
                    {
                        var assigneeList = JsonConvert.DeserializeObject <List <Assignee> >(form.Get("assigneeList"));

                        if (assigneeList.Count() > 1)
                        {
                            foreach (var assignee in assigneeList)
                            {
                                var  parentTaskLog     = JsonConvert.DeserializeObject <List <ActivityLog> >(form.Get("ParentTaskDetails"));
                                bool CreateNewActivity = AddChildTask(mainTask, assignee, parentTaskLog);
                                if (!CreateNewActivity)
                                {
                                    return(Content(HttpStatusCode.InternalServerError, "Faied to assignee task"));
                                }
                            }
                        }
                        else
                        {
                            if (mainTask.ParentTask_Id != null)
                            {
                                ActivitiesLog parentLog = new ActivitiesLog();

                                parentLog.TaskId     = mainTask.ParentTask_Id;
                                parentLog.TaskDate   = MasterDataController.GetIndianTime(DateTime.Now);
                                parentLog.AssignedBy = User.Identity.GetUserId();
                                parentLog.AssignedTo = assigneeList[0].AssigneeId;
                                parentLog.Status     = "Open";
                                if (hoursWorked != "" && hoursWorked != "NaN")
                                {
                                    parentLog.HoursWorked = Convert.ToInt32(form.Get("hoursWorked"));
                                }
                                parentLog.Description = form.Get("description");
                                if (quantityWorked != "" && quantityWorked != "NaN")
                                {
                                    parentLog.QuantityWorked = Convert.ToInt32(form.Get("quantityWorked"));
                                }

                                db.ActivitiesLogs.Add(parentLog);
                            }

                            mainTask.Status        = "Open";
                            mainTask.TaskOwner     = assigneeList[0].AssigneeId;
                            mainTask.LastUpdatedBy = User.Identity.GetUserId();
                            mainTask.LastUpdated   = MasterDataController.GetIndianTime(DateTime.Now);
                        }
                    }

                    else
                    {
                        var      childTasks = db.Activities.Where(x => x.ParentTask_Id == mainTask.TaskId).ToList();
                        Activity parentTask = db.Activities.Where(x => x.TaskId == mainTask.ParentTask_Id).FirstOrDefault();

                        ActivitiesLog activityLog = new ActivitiesLog();

                        if (childTasks.Count > 0)
                        {
                            foreach (var child in childTasks)
                            {
                                ActivitiesLog childLog = new ActivitiesLog();

                                childLog.TaskId        = child.TaskId;
                                childLog.AssignedBy    = User.Identity.GetUserId();
                                childLog.TaskDate      = MasterDataController.GetIndianTime(DateTime.Now);
                                childLog.Description   = form.Get("description");
                                childLog.BudgetedHours = Convert.ToInt32(form.Get("budgetedHours"));

                                db.ActivitiesLogs.Add(childLog);
                            }
                        }

                        if (actionType == "InProcess")
                        {
                            activityLog.AssignedTo = User.Identity.GetUserId();
                            activityLog.StartDate  = MasterDataController.GetIndianTime(Convert.ToDateTime(form.Get("edos")));
                            activityLog.EndDate    = MasterDataController.GetIndianTime(Convert.ToDateTime(form.Get("edoc")));
                            activityLog.Status     = "InProcess";

                            mainTask.Status = "InProcess";
                        }
                        else if (actionType == "Pending")
                        {
                            activityLog.AssignedTo = User.Identity.GetUserId();
                            activityLog.StartDate  = MasterDataController.GetIndianTime(Convert.ToDateTime(form.Get("edos")));
                            activityLog.EndDate    = MasterDataController.GetIndianTime(Convert.ToDateTime(form.Get("edoc")));
                            activityLog.Status     = "Pending";

                            mainTask.Status = "Pending";
                        }
                        else if (actionType == "Resolved")
                        {
                            activityLog.AssignedTo = mainTask.CreatedBy;
                            activityLog.AssignedBy = User.Identity.GetUserId();
                            activityLog.Status     = "Resolved";

                            mainTask.TaskOwner = mainTask.CreatedBy;
                            mainTask.Status    = "Resolved";
                        }
                        else if (actionType == "AcceptToClose")
                        {
                            activityLog.Status     = "Closed by assignee";
                            activityLog.AssignedBy = mainTask.TaskOwner;
                            activityLog.AssignedTo = mainTask.CreatedBy;

                            mainTask.TaskOwner = mainTask.CreatedBy;
                            mainTask.Status    = "Closed";
                        }
                        else if (actionType == "Comments")
                        {
                            activityLog.AssignedTo = mainTask.TaskOwner;
                            activityLog.AssignedBy = User.Identity.GetUserId();

                            mainTask.Status = mainTask.Status;
                        }
                        else
                        {
                            if (actionType == "Reopen")
                            {
                                activityLog.AssignedTo = mainTask.LastUpdatedBy;
                                activityLog.AssignedBy = User.Identity.GetUserId();
                                activityLog.Status     = "Reopened";

                                mainTask.Status = "Reopened";
                            }
                        }

                        if (parentTask != null)
                        {
                            ActivitiesLog parentLog = new ActivitiesLog();

                            parentLog.TaskId     = parentTask.ParentTask_Id;
                            parentLog.TaskDate   = MasterDataController.GetIndianTime(DateTime.Now);
                            parentLog.AssignedBy = User.Identity.GetUserId();
                            parentLog.AssignedTo = parentTask.TaskOwner;

                            if (actionType == "Pending" || actionType == "Comments")
                            {
                                parentLog.Status = "Pending";
                            }
                            else if (actionType == "InProcess")
                            {
                                parentLog.Status = "InProcess";
                            }
                            else if (actionType == "Assign")
                            {
                                parentLog.Status = "Open";
                            }
                            else if (actionType == "Reopen")
                            {
                                parentLog.Status = "Reopened";
                            }
                            else if (actionType == "AcceptToClose")
                            {
                                var childTasksList = db.Activities.Where(x => x.ParentTask_Id == mainTask.TaskId).ToList();
                            }
                            else if (actionType == "Resolved")
                            {
                            }
                            else
                            {
                                parentLog.Status = "";
                            }

                            parentLog.BudgetedHours = Convert.ToInt32(form.Get("budgetedHours"));
                            if (hoursWorked != "" && hoursWorked != "NaN")
                            {
                                parentLog.HoursWorked = Convert.ToInt32(form.Get("hoursWorked"));
                            }
                            parentLog.Description = form.Get("description");
                            if (quantityWorked != "" && quantityWorked != "NaN")
                            {
                                parentLog.QuantityWorked = Convert.ToInt32(form.Get("quantityWorked"));
                            }
                            db.ActivitiesLogs.Add(parentLog);
                        }

                        activityLog.AssignedBy    = User.Identity.GetUserId();
                        activityLog.TaskDate      = MasterDataController.GetIndianTime(DateTime.Now);
                        activityLog.Description   = form.Get("description");
                        activityLog.BudgetedHours = Convert.ToInt32(form.Get("budgetedHours"));
                        if (hoursWorked != "" && hoursWorked != "NaN")
                        {
                            activityLog.HoursWorked = Convert.ToInt32(form.Get("hoursWorked"));
                        }

                        if (quantityWorked != "" && quantityWorked != "NaN")
                        {
                            activityLog.QuantityWorked = Convert.ToInt32(form.Get("quantityWorked"));
                        }
                        mainTask.ActivitiesLogs.Add(activityLog);

                        mainTask.LastUpdatedBy = User.Identity.GetUserId();
                        mainTask.LastUpdated   = MasterDataController.GetIndianTime(DateTime.Now);

                        db.Entry(mainTask).State = System.Data.Entity.EntityState.Modified;
                    }
                }

                return(Ok());
            }
            catch (Exception ex)
            {
                new Error().logAPIError(System.Reflection.MethodBase.GetCurrentMethod().Name, ex.ToString(), ex.StackTrace);
                return(Content(HttpStatusCode.InternalServerError, "An error occured, please try again later"));
            }
        }