Exemple #1
0
        public IActionResult BulkSave(LessonIndexViewModel updatedModel)
        {
            UserSessionContext userSessionContext = new UserSessionContext(this.HttpContext);
            ApplicationContext appContext         = new ApplicationContext(this.HttpContext);
            LessonBusiness     lessonManager      = new LessonBusiness(DbContext);
            var              selectedLessonIds    = string.IsNullOrWhiteSpace(updatedModel.BulkSelectedLessons) ? new List <int>() : updatedModel.BulkSelectedLessons.Split(',').Select(x => int.Parse(x)).ToList();
            var              selectedLessons      = userSessionContext.LastSearchResults.Where(x => selectedLessonIds.Contains(x.Id));
            List <string>    errorMessages        = new List <string>();
            bool             hasSuccesses         = false;
            List <EmailInfo> emailsToSend         = new List <EmailInfo>();
            List <Lesson>    AssignLessonIds      = new List <Lesson>();

            if (updatedModel.BulkLesson.SaveAction == Enumerations.SaveAction.ActionLog ||
                updatedModel.BulkLesson.SaveAction == Enumerations.SaveAction.CsvLog ||
                updatedModel.BulkLesson.SaveAction == Enumerations.SaveAction.CsvLogAll ||
                updatedModel.BulkLesson.SaveAction == Enumerations.SaveAction.ActionLogAll)
            {
                Enumerations.LogType logType = Enumerations.LogType.ActionLog;

                if (updatedModel.BulkLesson.SaveAction == Enumerations.SaveAction.CsvLog ||
                    updatedModel.BulkLesson.SaveAction == Enumerations.SaveAction.CsvLogAll)
                {
                    logType = userSessionContext.CurrentUser.RoleId == (int)Enumerations.Role.Administrator ? Enumerations.LogType.AdminLog : Enumerations.LogType.GenericLog;
                }

                if (updatedModel.BulkLesson.SaveAction == Enumerations.SaveAction.CsvLogAll ||
                    updatedModel.BulkLesson.SaveAction == Enumerations.SaveAction.ActionLogAll)
                {
                    userSessionContext.ExportLog = lessonManager.GenerateLog(userSessionContext.CurrentUser, userSessionContext.LastSystemSearch, logType);
                }
                else
                {
                    userSessionContext.ExportLog = lessonManager.GenerateLog(selectedLessonIds, logType);
                }
            }
            else
            {
                foreach (var selectedLesson in selectedLessons)
                {
                    if (updatedModel.BulkLesson.SaveAction == Enumerations.SaveAction.Delete ||
                        updatedModel.BulkLesson.SaveAction == Enumerations.SaveAction.UnDelete)
                    {
                        //Just save, no need to validate
                        lessonManager.SaveLesson(selectedLesson, userSessionContext.CurrentUser, null, updatedModel.BulkLesson.SaveAction);
                        hasSuccesses = true;
                    }
                    else
                    {
                        List <Lesson> successLessons = new List <Lesson>();

                        if (updatedModel.BulkLesson.SaveAction == Enumerations.SaveAction.BPOToClose)
                        {
                            selectedLesson.Resolution          = updatedModel.BulkLesson.Resolution;
                            selectedLesson.LessonTypeValidId   = updatedModel.BulkLesson.LessonTypeValidId;
                            selectedLesson.LessonTypeInvalidId = updatedModel.BulkLesson.LessonTypeInvalidId;
                        }

                        var model = LessonViewModel.ToViewModel(this.HttpContext, selectedLesson);
                        model.TransferBpoDisciplineId = updatedModel.BulkLesson.TransferBpoDisciplineId;
                        model.SaveAction = updatedModel.BulkLesson.SaveAction;

                        var validatedErrors = model.Validate(null);

                        if (validatedErrors.Count() == 0)
                        {
                            string comment = null;
                            switch (updatedModel.BulkLesson.SaveAction)
                            {
                            case Enumerations.SaveAction.AdminToClarification:
                            case Enumerations.SaveAction.BPOToClarification:
                            case Enumerations.SaveAction.ClarificationToBPO:
                            case Enumerations.SaveAction.ClarificationToAdmin:
                                comment = updatedModel.BulkLesson.ClarificationComment;
                                break;

                            case Enumerations.SaveAction.AdminToBPO:
                            case Enumerations.SaveAction.BPOToBPO:
                                selectedLesson.DisciplineId = updatedModel.BulkLesson.TransferBpoDisciplineId;
                                comment = updatedModel.BulkLesson.TransferBpoComment;
                                break;

                            case Enumerations.SaveAction.BPOToClose:
                                comment = updatedModel.BulkLesson.CloseComment;
                                break;

                            case Enumerations.SaveAction.AssignToUser:
                                //selectedLesson.ValidatedBy = updatedModel.BulkLesson.AssignToUserId;
                                selectedLesson.AssignTo = updatedModel.BulkLesson.AssignToUserId;
                                break;
                            }

                            var result = lessonManager.SaveLesson(selectedLesson, userSessionContext.CurrentUser, comment, updatedModel.BulkLesson.SaveAction);

                            successLessons.Add(selectedLesson);

                            hasSuccesses = true;
                        }
                        else
                        {
                            foreach (var validationResult in validatedErrors)
                            {
                                errorMessages.Add(model.Id + ": " + validationResult.ErrorMessage);
                            }
                        }

                        //Send email notification
                        switch (updatedModel.BulkLesson.SaveAction)
                        {
                        case Enumerations.SaveAction.DraftToNew:
                            emailsToSend.AddRange(GenerateNotifications(successLessons, Enumerations.NotificationEmailType.N1_NewToAdmin));
                            break;

                        case Enumerations.SaveAction.AdminToClarification:
                            emailsToSend.AddRange(GenerateNotifications(successLessons, Enumerations.NotificationEmailType.N3_AdminToClarification));
                            break;

                        case Enumerations.SaveAction.AdminToBPO:
                        case Enumerations.SaveAction.BPOToBPO:
                        case Enumerations.SaveAction.ClosedToBPO:     //TODO: Validate this notification should be sent
                            emailsToSend.AddRange(GenerateNotifications(successLessons, Enumerations.NotificationEmailType.N2_AdminToBPO_And_BPOToBPO));
                            break;

                        case Enumerations.SaveAction.BPOToClarification:
                            emailsToSend.AddRange(GenerateNotifications(successLessons, Enumerations.NotificationEmailType.N5_BPOToClarification));
                            break;

                        case Enumerations.SaveAction.BPOToClose:
                            emailsToSend.AddRange(GenerateNotifications(successLessons, Enumerations.NotificationEmailType.N7_BPOToCloseLLC));
                            break;

                        case Enumerations.SaveAction.ClarificationToAdmin:
                            emailsToSend.AddRange(GenerateNotifications(successLessons, Enumerations.NotificationEmailType.N4_ClarificationToAdmin));
                            break;

                        case Enumerations.SaveAction.ClarificationToBPO:
                            emailsToSend.AddRange(GenerateNotifications(successLessons, Enumerations.NotificationEmailType.N6_ClarificationToBPO));
                            break;

                        case Enumerations.SaveAction.AssignToUser:
                            if (successLessons.Count > 0)
                            {
                                AssignLessonIds.Add(successLessons.First());
                            }
                            break;
                        }
                    }
                }

                if (AssignLessonIds.Count > 0)
                {
                    emailsToSend.AddRange(GenerateNotification(AssignLessonIds));
                }

                if (emailsToSend.Count > 0)
                {
                    LessonBusiness businessManager = new LessonBusiness(DbContext);
                    businessManager.SendEmails(emailsToSend);
                }
            }

            ModelState.Clear();

            if (errorMessages.Count > 0)
            {
                string message = hasSuccesses ? "Items have been saved, however the following Lessons could not be processed:" : "No Lessons could be processed.  The following errors occured:";
                errorMessages.Insert(0, message);

                foreach (var error in errorMessages)
                {
                    this.AddError(error);
                }
            }
            else if (hasSuccesses)
            {
                SetSuccessfulSave();
            }

            //Make sure to re-populate correctly if we came from a system initiated search back to MyLessons
            TempData["MyLessonModel"] = userSessionContext.LastSystemSearch;

            return(RedirectToActionPermanent("Index", new { pageAction = updatedModel.BulkLesson.ReturnToAction }));
        }
Exemple #2
0
        public IActionResult Index(Enumerations.PageAction pageAction, int?lessonId)
        {
            UserSessionContext userContext = new UserSessionContext(this.HttpContext);

            var            lessonModel     = new LessonViewModel(this.HttpContext);
            LessonBusiness businessManager = new LessonBusiness(DbContext);

            SearchViewModel userSearch = new SearchViewModel();

            //Set Lesson List search based on page action
            switch (pageAction)
            {
            case Enumerations.PageAction.Submit:
                //Show only current user's draft lessons
                userSearch = new SearchViewModel {
                    OwnerSid = userContext.CurrentUser.Sid, Status = (int?)Enumerations.LessonStatus.Draft
                };
                break;

            case Enumerations.PageAction.Edit:
                //Show only editable lessons
                userSearch = new SearchViewModel {
                    ShowEditableOnly = true
                };
                break;

            case Enumerations.PageAction.Search:
                //Show the last search
                userSearch = userContext.LastSearch;
                break;

            case Enumerations.PageAction.MyLessons:
                //Show all owned lessons (or filtered lessons of comming from dashboard)
                userSearch = (SearchViewModel)TempData["MyLessonModel"] ?? new SearchViewModel {
                    ShowOnlyOwnedLessons = true
                };
                break;

            default:
                throw new ArgumentOutOfRangeException("pageAction");
            }

            if (TempData.ContainsKey("Lesson"))
            {
                //Existing lesson (Edit Validation)
                lessonModel = (LessonViewModel)TempData["Lesson"];
            }
            else if (lessonId.HasValue &&
                     (pageAction == Enumerations.PageAction.Edit || pageAction == Enumerations.PageAction.Submit))
            {
                //Existing Lesson (Edit Existing)
                var lesson = businessManager.GetLessonById(lessonId.Value);
                lessonModel = LessonViewModel.ToViewModel(this.HttpContext, lesson);

                if (!Utils.IsLessonVisible(lessonModel, userContext.CurrentUser))
                {
                    return(RedirectToAction("Search"));
                }
            }
            else
            {
                if (pageAction != Enumerations.PageAction.Edit)
                {
                    if (lessonId.HasValue)
                    {
                        //Submit with "Save Changes", leave on submit tab with the recently added lesson
                        var lesson = businessManager.GetLessonById(lessonId.Value);
                        lessonModel = LessonViewModel.ToViewModel(this.HttpContext, lesson);
                    }
                    else
                    {
                        //New Lesson (Submit)
                        lessonModel = userContext.DraftDefaults;
                    }
                }
                else
                {
                    int unused = 0;
                    //Edit / Review, set to first lesson in search list
                    var lesson = businessManager.GetLessonsPaged(userContext.CurrentUser, userSearch, false, true, 0, 1, out unused).FirstOrDefault();
                    lessonModel = LessonViewModel.ToViewModel(this.HttpContext, lesson);
                    if (lesson != null)
                    {
                        lessonId = lesson.Id;
                    }
                    else
                    {
                        ViewBag.NothingToEdit = true;
                    }
                }
            }

            lessonModel.ReturnToAction = pageAction;

            var submittedUsers = businessManager.GetSubmittedByUsers();

            ViewBag.SubmittedByUsers = submittedUsers;

            userContext.LastSystemSearch = userSearch;

            LessonIndexViewModel model = new LessonIndexViewModel(this.HttpContext)
            {
                PageAction  = pageAction,
                LessonId    = lessonId,
                Lesson      = lessonModel,
                SearchModel = userSearch
            };

            return(View("Index", model));
        }