Esempio n. 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 }));
        }