public async Task SetUp()
        {
            QnaDataContext = DataContextHelpers.GetInMemoryDataContext();

            NotRequiredProcessor = new NotRequiredProcessor();
            TagProcessingService = new TagProcessingService(QnaDataContext);
            SetAnswersBase       = new SetAnswersBase(QnaDataContext, NotRequiredProcessor, TagProcessingService, null);

            ApplicationId = Guid.NewGuid();

            ApplicationDataJson = JsonConvert.SerializeObject(new
            {
                OrgType = "OrgType1"
            });

            await QnaDataContext.Applications.AddAsync(new Data.Entities.Application {
                Id = ApplicationId, ApplicationData = ApplicationDataJson
            });

            await QnaDataContext.SaveChangesAsync();

            NextAction = new Next {
                Action = "NextPage", ReturnId = "2"
            };
        }
        public async Task SetUp()
        {
            var dbContextOptions = new DbContextOptionsBuilder <QnaDataContext>()
                                   .UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString())
                                   .Options;

            var context = new QnaDataContext(dbContextOptions);

            ApplicationId = Guid.NewGuid();

            context.ApplicationSequences.AddRange(new[]
            {
                new ApplicationSequence {
                    ApplicationId = ApplicationId, IsActive = false, SequenceNo = 1
                },
                new ApplicationSequence {
                    ApplicationId = ApplicationId, IsActive = true, SequenceNo = 2
                },
                new ApplicationSequence {
                    ApplicationId = Guid.NewGuid(), IsActive = true, SequenceNo = 1
                }
            });


            await context.Applications.AddAsync(new Data.Entities.Application()
            {
                Id = ApplicationId
            });

            await context.SaveChangesAsync();

            var mapper = new Mapper(new MapperConfiguration(config => { config.AddMaps(AppDomain.CurrentDomain.GetAssemblies()); }));

            Handler = new GetCurrentSequenceHandler(context, mapper);
        }
        public async Task <HandlerResponse <Project> > Handle(CreateProjectRequest request, CancellationToken cancellationToken)
        {
            await _dataContext.Projects.AddAsync(request.Project, cancellationToken);

            await _dataContext.SaveChangesAsync(cancellationToken);

            return(new HandlerResponse <Project>(request.Project));
        }
        public async Task <HandlerResponse <WorkflowSection> > Handle(CreateWorkflowSectionRequest request, CancellationToken cancellationToken)
        {
            await _dataContext.WorkflowSections.AddAsync(request.Section, cancellationToken);

            await _dataContext.SaveChangesAsync(cancellationToken);

            return(new HandlerResponse <WorkflowSection>(request.Section));
        }
        private async Task <Data.Entities.Application> CreateNewApplication(Data.Entities.Application application, CancellationToken cancellationToken)
        {
            var newApplication = new Data.Entities.Application
            {
                ApplicationStatus = application.ApplicationStatus,
                WorkflowId        = application.WorkflowId,
                Reference         = application.Reference,
                CreatedAt         = SystemTime.UtcNow(),
                ApplicationData   = application.ApplicationData
            };

            _dataContext.Applications.Add(newApplication);
            await _dataContext.SaveChangesAsync(cancellationToken);

            _logger.LogInformation($"Created Application entity: {newApplication.Id}");

            return(newApplication);
        }
        public async Task <HandlerResponse <Workflow> > Handle(CreateWorkflowRequest request, CancellationToken cancellationToken)
        {
            var project = await _dataContext.Projects.SingleOrDefaultAsync(p => p.Id == request.ProjectId, cancellationToken : cancellationToken);

            request.Workflow.ApplicationDataSchema = project.ApplicationDataSchema;

            await _dataContext.Workflows.AddAsync(request.Workflow, cancellationToken);

            await _dataContext.SaveChangesAsync(cancellationToken);

            return(new HandlerResponse <Workflow>(request.Workflow));
        }
        public async Task SetUp()
        {
            var dbContextOptions = new DbContextOptionsBuilder <QnaDataContext>()
                                   .UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString())
                                   .Options;

            var context = new QnaDataContext(dbContextOptions);

            ApplicationId = Guid.NewGuid();

            context.Applications.Add(new Data.Entities.Application {
                Id = ApplicationId, ApplicationData = "{}"
            });

            context.ApplicationSequences.AddRange(new[]
            {
                new ApplicationSequence {
                    ApplicationId = ApplicationId, IsActive = false, SequenceNo = 1
                },
                new ApplicationSequence {
                    ApplicationId = ApplicationId, IsActive = true, SequenceNo = 2
                }
            });

            context.ApplicationSections.AddRange(new[]
            {
                new ApplicationSection {
                    ApplicationId = ApplicationId, SequenceNo = 1, SectionNo = 1, QnAData = new QnAData {
                        Pages = new List <Page>()
                    }
                },
                new ApplicationSection {
                    ApplicationId = ApplicationId, SequenceNo = 1, SectionNo = 2, QnAData = new QnAData {
                        Pages = new List <Page>()
                    }
                },
                new ApplicationSection {
                    ApplicationId = ApplicationId, SequenceNo = 2, SectionNo = 1, QnAData = new QnAData {
                        Pages = new List <Page>()
                    }
                }
            });

            await context.SaveChangesAsync();

            var mapper = new Mapper(new MapperConfiguration(config => { config.AddMaps(AppDomain.CurrentDomain.GetAssemblies()); }));

            Handler = new GetSectionsHandler(context, mapper, new NotRequiredProcessor());
        }
Beispiel #8
0
        public async Task <HandlerResponse <RemovePageAnswerResponse> > Handle(RemovePageAnswerRequest request, CancellationToken cancellationToken)
        {
            await GetSectionAndPage(request.ApplicationId, request.SectionId, request.PageId);

            if (Application == null || Section == null || Page == null)
            {
                return(new HandlerResponse <RemovePageAnswerResponse>(false, $"ApplicationId {request.ApplicationId}, Section {request.SectionId} or PageId {request.PageId} does not exist."));
            }

            if (Page.AllowMultipleAnswers == false)
            {
                return(new HandlerResponse <RemovePageAnswerResponse>(false, $"ApplicationId {request.ApplicationId}, Section {request.SectionId}, PageId {request.PageId} does not AllowMultipleAnswers "));
            }

            var pageOfAnswers = Page.PageOfAnswers.SingleOrDefault(poa => poa.Id == request.AnswerId);

            if (pageOfAnswers == null)
            {
                return(new HandlerResponse <RemovePageAnswerResponse>(false, $"AnswerId {request.AnswerId} does not exist."));
            }

            Page.PageOfAnswers.Remove(pageOfAnswers);

            if (Page.PageOfAnswers.Count == 0)
            {
                Page.Complete = false;
                if (Page.HasFeedback)
                {
                    foreach (var feedback in Page.Feedback.Where(feedback => feedback.IsNew).Select(feedback => feedback))
                    {
                        feedback.IsCompleted = false;
                    }
                }
            }
            else
            {
                MarkFeedbackComplete(Page);
            }

            Section.QnAData = QnaData;



            await _dataContext.SaveChangesAsync(CancellationToken.None);

            return(new HandlerResponse <RemovePageAnswerResponse>(new RemovePageAnswerResponse(Page)));
        }
        public async Task <HandlerResponse <StartApplicationResponse> > Handle(StartApplicationRequest request, CancellationToken cancellationToken)
        {
            var latestWorkflow = await _dataContext.Workflows.AsNoTracking()
                                 .SingleOrDefaultAsync(w => w.Type == request.WorkflowType && w.Status == "Live", cancellationToken);

            if (latestWorkflow is null)
            {
                _logger.LogError($"Workflow type {request.WorkflowType} does not exist");
                return(new HandlerResponse <StartApplicationResponse>(false, $"Workflow Type does not exist."));
            }

            try
            {
                _applicationDataIsInvalid = !_applicationDataValidator.IsValid(latestWorkflow.ApplicationDataSchema, request.ApplicationData);
            }
            catch (JsonReaderException)
            {
                _logger.LogError("Supplied ApplicationData is not valid JSON");
                return(new HandlerResponse <StartApplicationResponse>(false, $"Supplied ApplicationData is not valid JSON."));
            }

            if (_applicationDataIsInvalid)
            {
                _logger.LogError("Supplied ApplicationData is not valid using Project's Schema");
                return(new HandlerResponse <StartApplicationResponse>(false, $"Supplied ApplicationData is not valid using Project's Schema."));
            }

            var newApplication = await CreateNewApplication(request, latestWorkflow, cancellationToken, request.ApplicationData);

            if (newApplication is null)
            {
                _logger.LogError($"Workflow type {request.WorkflowType} does not exist");
                return(new HandlerResponse <StartApplicationResponse>(false, $"WorkflowType '{request.WorkflowType}' does not exist."));
            }

            await CopyWorkflows(cancellationToken, newApplication);

            await _dataContext.SaveChangesAsync(cancellationToken);

            _logger.LogInformation($"Successfully created new Application. Application Id = {newApplication.Id} | Workflow = {request.WorkflowType}");
            return(new HandlerResponse <StartApplicationResponse>(new StartApplicationResponse {
                ApplicationId = newApplication.Id
            }));
        }
        public async Task <HandlerResponse <Page> > Handle(UpsertFeedbackRequest request, CancellationToken cancellationToken)
        {
            var section = await _dataContext.ApplicationSections.SingleOrDefaultAsync(sec => sec.ApplicationId == request.ApplicationId && sec.Id == request.SectionId, cancellationToken);

            if (section is null)
            {
                return(new HandlerResponse <Page>(success: false, message: $"SectionId {request.SectionId} does not exist in ApplicationId {request.ApplicationId}"));
            }

            var qnaData = new QnAData(section.QnAData);

            var page = qnaData.Pages.SingleOrDefault(p => p.PageId == request.PageId);

            if (page is null)
            {
                return(new HandlerResponse <Page>(success: false, message: $"PageId {request.PageId} does not exist"));
            }

            if (page.Feedback is null)
            {
                page.Feedback = new List <Feedback>();
            }

            if (page.Feedback.All(f => f.Id != request.Feedback.Id))
            {
                request.Feedback.Date  = SystemTime.UtcNow();
                request.Feedback.IsNew = true;
                page.Feedback.Add(request.Feedback);
            }
            else
            {
                var existingFeedback = page.Feedback.Single(f => f.Id == request.Feedback.Id);
                existingFeedback.Date        = request.Feedback.Date;
                existingFeedback.From        = request.Feedback.From;
                existingFeedback.Message     = request.Feedback.Message;
                existingFeedback.IsCompleted = request.Feedback.IsCompleted;
                existingFeedback.IsNew       = request.Feedback.IsNew;
            }

            section.QnAData = qnaData;
            await _dataContext.SaveChangesAsync(cancellationToken);

            return(new HandlerResponse <Page>(page));
        }
Beispiel #11
0
        public async Task <HandlerResponse <Project> > Handle(UpsertProjectRequest request, CancellationToken cancellationToken)
        {
            var existingProject = await _dataContext.Projects.SingleOrDefaultAsync(project => project.Id == request.ProjectId, cancellationToken : cancellationToken);

            if (existingProject == null)
            {
                await _dataContext.Projects.AddAsync(request.Project, cancellationToken);
            }
            else
            {
                existingProject.Name = request.Project.Name;
                existingProject.ApplicationDataSchema = request.Project.ApplicationDataSchema;
                existingProject.CreatedBy             = request.Project.CreatedBy;
            }

            await _dataContext.SaveChangesAsync(cancellationToken);

            return(new HandlerResponse <Project>(existingProject));
        }
        public async Task <HandlerResponse <WorkflowSequence> > Handle(UpsertWorkflowSequenceRequest request, CancellationToken cancellationToken)
        {
            var existingSequence = await _dataContext.WorkflowSequences.SingleOrDefaultAsync(sequence => sequence.Id == request.SequenceId && sequence.WorkflowId == request.WorkflowId, cancellationToken : cancellationToken);

            if (existingSequence == null)
            {
                await _dataContext.WorkflowSequences.AddAsync(request.Sequence, cancellationToken);
            }
            else
            {
                existingSequence.IsActive   = request.Sequence.IsActive;
                existingSequence.SectionId  = request.Sequence.SectionId;
                existingSequence.SectionNo  = request.Sequence.SectionNo;
                existingSequence.SequenceNo = request.Sequence.SequenceNo;
            }

            await _dataContext.SaveChangesAsync(cancellationToken);

            return(new HandlerResponse <WorkflowSequence>(existingSequence));
        }
Beispiel #13
0
        public async Task <HandlerResponse <WorkflowSection> > Handle(UpsertWorkflowSectionRequest request, CancellationToken cancellationToken)
        {
            var existingSection = await _dataContext.WorkflowSections.SingleOrDefaultAsync(sec => sec.Id == request.SectionId && sec.ProjectId == request.ProjectId, cancellationToken : cancellationToken);

            if (existingSection == null)
            {
                await _dataContext.WorkflowSections.AddAsync(request.Section, cancellationToken);
            }
            else
            {
                existingSection.Title       = request.Section.Title;
                existingSection.DisplayType = request.Section.DisplayType;
                existingSection.LinkTitle   = request.Section.LinkTitle;
                existingSection.QnAData     = request.Section.QnAData;
            }

            await _dataContext.SaveChangesAsync(cancellationToken);

            return(new HandlerResponse <WorkflowSection>(existingSection));
        }
        public async Task <HandlerResponse <AddPageAnswerResponse> > Handle(AddPageAnswerRequest request, CancellationToken cancellationToken)
        {
            await GetSectionAndPage(request.ApplicationId, request.SectionId, request.PageId);

            if (Application == null || Section == null || Page == null)
            {
                return(new HandlerResponse <AddPageAnswerResponse>(false, $"ApplicationId {request.ApplicationId}, Section {request.SectionId} or PageId {request.PageId} does not exist."));
            }

            if (Page.AllowMultipleAnswers == false)
            {
                return(new HandlerResponse <AddPageAnswerResponse>(false, $"ApplicationId {request.ApplicationId}, Section {request.SectionId}, PageId {request.PageId} does not AllowMultipleAnswers"));
            }

            if (Page.PageOfAnswers == null)
            {
                Page.PageOfAnswers = new List <PageOfAnswers>();
            }

            var validationErrors = _answerValidator.Validate(request.Answers, Page);

            if (validationErrors.Any())
            {
                return(new HandlerResponse <AddPageAnswerResponse>(new AddPageAnswerResponse(validationErrors)));
            }

            Page.PageOfAnswers.Add(new PageOfAnswers()
            {
                Id = Guid.NewGuid(), Answers = request.Answers
            });

            Page.Complete = true;

            MarkFeedbackComplete(Page);

            Section.QnAData = QnaData;

            await _dataContext.SaveChangesAsync(CancellationToken.None);

            return(new HandlerResponse <AddPageAnswerResponse>(new AddPageAnswerResponse(Page)));
        }
        public async Task <HandlerResponse <Workflow> > Handle(UpsertWorkflowRequest request, CancellationToken cancellationToken)
        {
            var existingWorkflow = await _dataContext.Workflows.SingleOrDefaultAsync(sec => sec.Id == request.WorkflowId && sec.ProjectId == request.ProjectId, cancellationToken : cancellationToken);

            if (existingWorkflow == null)
            {
                request.Workflow.ProjectId = request.ProjectId;
                await _dataContext.Workflows.AddAsync(request.Workflow, cancellationToken);
            }
            else
            {
                existingWorkflow.Status      = request.Workflow.Status;
                existingWorkflow.Description = request.Workflow.Description;
                existingWorkflow.Type        = request.Workflow.Type;
                existingWorkflow.Version     = request.Workflow.Version;
            }

            await _dataContext.SaveChangesAsync(cancellationToken);

            return(new HandlerResponse <Workflow>(existingWorkflow));
        }
        public async Task <HandlerResponse <bool> > Handle(CompleteFeedbackWithinSequenceRequest request, CancellationToken cancellationToken)
        {
            var application = await _dataContext.Applications.FirstOrDefaultAsync(app => app.Id == request.ApplicationId, cancellationToken : cancellationToken);

            if (application is null)
            {
                return(new HandlerResponse <bool>(false, "Application does not exist"));
            }

            var sequence = await _dataContext.ApplicationSequences.FirstOrDefaultAsync(seq => seq.Id == request.SequenceId, cancellationToken : cancellationToken);

            if (sequence is null)
            {
                return(new HandlerResponse <bool>(false, "Sequence does not exist"));
            }

            var sections = await _dataContext.ApplicationSections.Where(section => section.SequenceId == request.SequenceId).ToListAsync(cancellationToken);

            foreach (var section in sections)
            {
                var qnaData = new QnAData(section.QnAData);

                foreach (var page in qnaData.Pages)
                {
                    if (page.HasNewFeedback)
                    {
                        page.Feedback.ForEach(f => f.IsNew       = false);
                        page.Feedback.ForEach(f => f.IsCompleted = true);
                    }
                }

                section.QnAData = qnaData;
            }

            await _dataContext.SaveChangesAsync(cancellationToken);

            return(new HandlerResponse <bool>(true));
        }
        public async Task <HandlerResponse <Page> > Handle(DeleteFeedbackRequest request, CancellationToken cancellationToken)
        {
            var section = await _dataContext.ApplicationSections.SingleOrDefaultAsync(sec => sec.ApplicationId == request.ApplicationId && sec.Id == request.SectionId, cancellationToken);

            if (section is null)
            {
                return(new HandlerResponse <Page>(success: false, message: $"SectionId {request.SectionId} does not exist in ApplicationId {request.ApplicationId}"));
            }

            var qnaData = new QnAData(section.QnAData);

            var page = qnaData.Pages.SingleOrDefault(p => p.PageId == request.PageId);

            if (page is null)
            {
                return(new HandlerResponse <Page>(success: false, message: $"PageId {request.PageId} does not exist"));
            }

            if (page.Feedback is null)
            {
                return(new HandlerResponse <Page>(success: false, message: $"Feedback {request.FeedbackId} does not exist"));
            }

            var existingFeedback = page.Feedback.SingleOrDefault(f => f.Id == request.FeedbackId);

            if (existingFeedback is null)
            {
                return(new HandlerResponse <Page>(success: false, message: $"Feedback {request.FeedbackId} does not exist"));
            }

            page.Feedback.Remove(existingFeedback);

            section.QnAData = qnaData;
            await _dataContext.SaveChangesAsync(cancellationToken);

            return(new HandlerResponse <Page>(page));
        }
Beispiel #18
0
        public async Task Then_only_live_workflows_are_returned()
        {
            var dbContextOptions = new DbContextOptionsBuilder <QnaDataContext>()
                                   .UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString())
                                   .Options;

            var context = new QnaDataContext(dbContextOptions);

            var projectId = Guid.NewGuid();

            context.Workflows.AddRange(new []
            {
                new Workflow()
                {
                    Id = Guid.NewGuid(), Status = WorkflowStatus.Live, ProjectId = projectId
                },
                new Workflow()
                {
                    Id = Guid.NewGuid(), ProjectId = projectId
                },
                new Workflow()
                {
                    Id = Guid.NewGuid(), Status = WorkflowStatus.Live, ProjectId = projectId
                },
            });

            await context.SaveChangesAsync();

            var mapper = new Mapper(new MapperConfiguration(config => { config.AddMaps(AppDomain.CurrentDomain.GetAssemblies()); }));

            var handler = new GetWorkflowsHandler(context, mapper);

            var results = await handler.Handle(new GetWorkflowsRequest(projectId), CancellationToken.None);

            results.Value.Should().BeOfType <List <Workflow> >();
            results.Value.Count.Should().Be(2);
        }
        public async Task SetUp()
        {
            _dataContext = DataContextHelpers.GetInMemoryDataContext();

            _applicationId = Guid.NewGuid();
            _sectionId     = Guid.NewGuid();

            _feedbackId = Guid.NewGuid();
            var section = new ApplicationSection
            {
                ApplicationId = _applicationId,
                Id            = _sectionId,
                QnAData       = new QnAData
                {
                    Pages = new List <Page>
                    {
                        new Page
                        {
                            PageId   = "1",
                            Feedback = new List <Feedback>
                            {
                                new Feedback
                                {
                                    Id = _feedbackId
                                }
                            }
                        }
                    }
                }
            };

            await _dataContext.ApplicationSections.AddAsync(section);

            await _dataContext.SaveChangesAsync();

            _handler = new UpsertFeedbackHandler(_dataContext);
        }
        public async Task <HandlerResponse <string> > Handle(SetApplicationDataRequest request, CancellationToken cancellationToken)
        {
            var application = await _dataContext.Applications.SingleOrDefaultAsync(app => app.Id == request.ApplicationId, cancellationToken);

            if (application is null)
            {
                return(new HandlerResponse <string>(success: false, message: "Application does not exist."));
            }

            var workflow = await _dataContext.Workflows.SingleOrDefaultAsync(wf => wf.Id == application.WorkflowId, cancellationToken);

            var serializedApplicationData = JsonConvert.SerializeObject(request.ApplicationData);

            if (!_applicationDataValidator.IsValid(workflow.ApplicationDataSchema, serializedApplicationData))
            {
                return(new HandlerResponse <string>(success: false, message: "ApplicationData does not validated against the Project's Schema."));
            }

            application.ApplicationData = serializedApplicationData;

            await _dataContext.SaveChangesAsync(cancellationToken);

            return(new HandlerResponse <string>(serializedApplicationData));
        }
        public async Task SetUp()
        {
            DataContext = DataContextHelpers.GetInMemoryDataContext();
            var fileStorageConfig = GetFileStorageConfig();
            var logger            = Substitute.For <ILogger <CreateSnapshotHandler> >();

            Handler = new CreateSnapshotHandler(DataContext, fileStorageConfig, logger);

            ApplicationId = Guid.NewGuid();
            SequenceId    = Guid.NewGuid();
            SectionId     = Guid.NewGuid();
            PageId        = "1";
            QuestionId    = "Q1";
            Filename      = "file.txt";

            Container = await GetContainer(fileStorageConfig);

            await DataContext.Applications.AddAsync(new Data.Entities.Application()
            {
                Id = ApplicationId, ApplicationData = "{}"
            });

            await DataContext.ApplicationSequences.AddAsync(new ApplicationSequence()
            {
                ApplicationId = ApplicationId,
                Id            = SequenceId
            });

            await DataContext.ApplicationSections.AddAsync(new ApplicationSection()
            {
                ApplicationId = ApplicationId,
                Id            = SectionId,
                SequenceId    = SequenceId,
                QnAData       = new QnAData()
                {
                    Pages = new List <Page>
                    {
                        new Page()
                        {
                            SectionId  = SectionId,
                            SequenceId = SequenceId,
                            PageId     = PageId,
                            Questions  = new List <Question> {
                                new Question()
                                {
                                    QuestionId = QuestionId, Input = new Input {
                                        Type = "FileUpload"
                                    }
                                }
                            },
                            PageOfAnswers = new List <PageOfAnswers>()
                            {
                                new PageOfAnswers {
                                    Id = Guid.NewGuid(), Answers = new List <Answer> {
                                        new Answer {
                                            QuestionId = QuestionId, Value = Filename
                                        }
                                    }
                                }
                            },
                            Next   = new List <Next>(),
                            Active = true
                        }
                    }
                }
            });

            await DataContext.SaveChangesAsync();

            await AddFile(ApplicationId, SequenceId, SectionId, PageId, QuestionId, Filename, Container);
        }
        public async Task SetUp()
        {
            DataContext = DataContextHelpers.GetInMemoryDataContext();
            var validator = Substitute.For <IAnswerValidator>();

            NotRequiredProcessor = new NotRequiredProcessor();
            TagProcessingService = new TagProcessingService(DataContext);

            validator.Validate(Arg.Any <List <Answer> >(), Arg.Any <Page>()).Returns(new List <KeyValuePair <string, string> >());

            Handler = new SetPageAnswersHandler(DataContext, validator, NotRequiredProcessor, TagProcessingService);

            ApplicationId = Guid.NewGuid();
            SectionId     = Guid.NewGuid();
            await DataContext.ApplicationSections.AddAsync(new ApplicationSection()
            {
                ApplicationId = ApplicationId,
                Id            = SectionId,
                QnAData       = new QnAData()
                {
                    Pages = new List <Page>
                    {
                        new Page()
                        {
                            PageId    = "1",
                            Questions = new List <Question> {
                                new Question()
                                {
                                    QuestionId = "Q1", Input = new Input()
                                }
                            },
                            PageOfAnswers = new List <PageOfAnswers>(),
                            Next          = new List <Next>
                            {
                                new Next()
                                {
                                    Action = "NextPage", ReturnId = "2", Conditions = new List <Condition>()
                                    {
                                        new Condition {
                                            QuestionId = "Q1", MustEqual = "Yes"
                                        }
                                    }
                                },
                                new Next()
                                {
                                    Action = "NextPage", ReturnId = "4", Conditions = new List <Condition>()
                                    {
                                        new Condition {
                                            QuestionId = "Q1", MustEqual = "No"
                                        }
                                    }
                                }
                            },
                            Active = true
                        },
                        new Page()
                        {
                            PageId    = "2",
                            Questions = new List <Question> {
                                new Question()
                                {
                                    QuestionId = "Q2", Input = new Input()
                                }
                            },
                            PageOfAnswers = new List <PageOfAnswers>(),
                            Next          = new List <Next>
                            {
                                new Next()
                                {
                                    Action = "NextPage", ReturnId = "3", Conditions = new List <Condition>()
                                    {
                                        new Condition {
                                            QuestionId = "Q2", MustEqual = "Yes"
                                        }
                                    }
                                },
                                new Next()
                                {
                                    Action = "NextPage", ReturnId = "5", Conditions = new List <Condition>()
                                    {
                                        new  Condition {
                                            QuestionId = "Q2", MustEqual = "No"
                                        }
                                    }
                                }
                            },
                            Active            = false,
                            ActivatedByPageId = "1"
                        },
                        new Page()
                        {
                            PageId    = "3",
                            Questions = new List <Question> {
                                new Question()
                                {
                                    QuestionId = "Q3", Input = new Input()
                                }
                            },
                            PageOfAnswers = new List <PageOfAnswers>(),
                            Next          = new List <Next>
                            {
                                new Next()
                                {
                                    Action = "NextPage", ReturnId = "7"
                                }
                            },
                            Active            = false,
                            ActivatedByPageId = "2"
                        },
                        new Page()
                        {
                            PageId    = "4",
                            Questions = new List <Question> {
                                new Question()
                                {
                                    QuestionId = "Q4", Input = new Input()
                                }
                            },
                            PageOfAnswers = new List <PageOfAnswers>(),
                            Next          = new List <Next>
                            {
                                new Next()
                                {
                                    Action = "NextPage", ReturnId = "6"
                                }
                            },
                            Active            = false,
                            ActivatedByPageId = "1"
                        },
                        new Page()
                        {
                            PageId    = "5",
                            Questions = new List <Question> {
                                new Question()
                                {
                                    QuestionId = "Q5", Input = new Input()
                                }
                            },
                            PageOfAnswers = new List <PageOfAnswers>(),
                            Next          = new List <Next>
                            {
                                new Next()
                                {
                                    Action = "NextPage", ReturnId = "7"
                                }
                            },
                            Active            = false,
                            ActivatedByPageId = "2"
                        },
                        new Page()
                        {
                            PageId    = "6",
                            Questions = new List <Question> {
                                new Question()
                                {
                                    QuestionId = "Q6", Input = new Input()
                                }
                            },
                            PageOfAnswers = new List <PageOfAnswers>(),
                            Next          = new List <Next>
                            {
                                new Next()
                                {
                                    Action = "NextPage", ReturnId = "8"
                                }
                            },
                            Active            = false,
                            ActivatedByPageId = "1"
                        },
                        new Page()
                        {
                            PageId    = "7",
                            Questions = new List <Question> {
                                new Question()
                                {
                                    QuestionId = "Q7", Input = new Input()
                                }
                            },
                            PageOfAnswers = new List <PageOfAnswers>(),
                            Next          = new List <Next>
                            {
                                new Next()
                                {
                                    Action = "NextPage", ReturnId = "8"
                                }
                            },
                            Active            = false,
                            ActivatedByPageId = "1"
                        },
                        new Page()
                        {
                            PageId    = "8",
                            Questions = new List <Question> {
                                new Question()
                                {
                                    QuestionId = "Q8", Input = new Input()
                                }
                            },
                            PageOfAnswers = new List <PageOfAnswers>(),
                            Next          = new List <Next>
                            {
                                new Next()
                                {
                                    Action = "NextPage", ReturnId = "9"
                                }
                            },
                            Active = true
                        },
                        new Page()
                        {
                            PageId    = "9",
                            Questions = new List <Question> {
                                new Question()
                                {
                                    QuestionId = "Q9", Input = new Input()
                                }
                            },
                            PageOfAnswers = new List <PageOfAnswers>(),
                            Next          = new List <Next>
                            {
                                new Next()
                                {
                                    Action = "NextPage", ReturnId = "10"
                                }
                            },
                            Active = true
                        },
                        new Page()
                        {
                            PageId    = "10",
                            Questions = new List <Question> {
                                new Question()
                                {
                                    QuestionId = "Q10", Input = new Input()
                                }
                            },
                            PageOfAnswers = new List <PageOfAnswers>(),
                            Next          = new List <Next>
                            {
                                new Next()
                                {
                                    Action = "ReturnToSequence", ReturnId = "1"
                                }
                            },
                            Active = true
                        }
                    }
                }
            });

            await DataContext.Applications.AddAsync(new Data.Entities.Application()
            {
                Id = ApplicationId, ApplicationData = "{}"
            });

            await DataContext.SaveChangesAsync();
        }
        public async Task SetUp()
        {
            DataContext = DataContextHelpers.GetInMemoryDataContext();

            var applicationDataValidator = Substitute.For <IApplicationDataValidator>();

            applicationDataValidator.IsValid("", "").ReturnsForAnyArgs(true);
            var logger = Substitute.For <ILogger <StartApplicationHandler> >();

            Handler = new StartApplicationHandler(DataContext, applicationDataValidator, logger);

            WorkflowId = Guid.NewGuid();

            var projectId = Guid.NewGuid();
            await DataContext.Workflows.AddAsync(
                new Workflow()
            {
                Type = "EPAO", Status = WorkflowStatus.Live, Id = WorkflowId, ProjectId = projectId
            });

            var workflowSections = new[]
            {
                new WorkflowSection {
                    Id = Guid.NewGuid(), Title = "Section 1", QnAData = new QnAData()
                    {
                        Pages = new List <Page>()
                        {
                            new Page()
                            {
                                Title = "[PageTitleToken1]"
                            },
                            new Page()
                            {
                                Title = "[PageTitleToken2]"
                            }
                        }
                    }
                },
                new WorkflowSection {
                    Id = Guid.NewGuid(), Title = "Section 2", QnAData = new QnAData()
                    {
                        Pages = new List <Page>()
                    }
                },
                new WorkflowSection {
                    Id = Guid.NewGuid(), Title = "Section 3", QnAData = new QnAData()
                    {
                        Pages = new List <Page>()
                    }
                },
                new WorkflowSection {
                    Id = Guid.NewGuid(), Title = "Section 4", QnAData = new QnAData()
                    {
                        Pages = new List <Page>()
                    }
                },
                new WorkflowSection {
                    Id = Guid.NewGuid(), Title = "Invalid section", QnAData = new QnAData()
                    {
                        Pages = new List <Page>()
                    }
                }
            };

            await DataContext.WorkflowSections.AddRangeAsync(workflowSections);

            await DataContext.WorkflowSequences.AddRangeAsync(new[]
            {
                new WorkflowSequence {
                    WorkflowId = WorkflowId, SectionId = workflowSections[0].Id, SectionNo = 1, SequenceNo = 1, IsActive = true
                },
                new WorkflowSequence {
                    WorkflowId = WorkflowId, SectionId = workflowSections[1].Id, SectionNo = 2, SequenceNo = 1, IsActive = true
                },
                new WorkflowSequence {
                    WorkflowId = WorkflowId, SectionId = workflowSections[2].Id, SectionNo = 3, SequenceNo = 1, IsActive = true
                },
                new WorkflowSequence {
                    WorkflowId = WorkflowId, SectionId = workflowSections[3].Id, SectionNo = 4, SequenceNo = 2, IsActive = false
                },
                new WorkflowSequence {
                    WorkflowId = Guid.NewGuid()
                },
                new WorkflowSequence {
                    WorkflowId = Guid.NewGuid()
                },
            });

            await DataContext.Projects.AddAsync(new Project { Id = projectId, ApplicationDataSchema = "" });

            await DataContext.SaveChangesAsync();
        }
        public async Task SetUp()
        {
            DataContext = DataContextHelpers.GetInMemoryDataContext();
            Handler     = new ResetPageAnswersHandler(DataContext, new NotRequiredProcessor(), new TagProcessingService(DataContext));
            GetApplicationDataHandler = new GetApplicationDataHandler(DataContext);
            GetPageHandler            = new GetPageHandler(DataContext);

            ApplicationId = Guid.NewGuid();
            SectionId     = Guid.NewGuid();
            await DataContext.ApplicationSections.AddAsync(new ApplicationSection()
            {
                ApplicationId = ApplicationId,
                Id            = SectionId,
                QnAData       = new QnAData()
                {
                    Pages = new List <Page>
                    {
                        new Page()
                        {
                            PageId    = "1",
                            Questions = new List <Question> {
                                new Question()
                                {
                                    QuestionId = "Q1", QuestionTag = "Q1", Input = new Input()
                                }
                            },
                            PageOfAnswers = new List <PageOfAnswers> {
                                new PageOfAnswers {
                                    Answers = new List <Answer> {
                                        new Answer {
                                            QuestionId = "Q1", Value = "Yes"
                                        }
                                    }
                                }
                            },
                            Next = new List <Next>
                            {
                                new Next()
                                {
                                    Action = "NextPage", ReturnId = "2", Conditions = new List <Condition>()
                                    {
                                        new Condition {
                                            QuestionId = "Q1", MustEqual = "Yes"
                                        }
                                    }
                                },
                                new Next()
                                {
                                    Action = "NextPage", ReturnId = "3", Conditions = new List <Condition>()
                                    {
                                        new Condition {
                                            QuestionId = "Q1", MustEqual = "No"
                                        }
                                    }
                                }
                            },
                            Feedback = new List <Feedback> {
                                new Feedback {
                                    IsCompleted = true
                                }
                            },
                            Active   = true,
                            Complete = true
                        },
                        new Page()
                        {
                            PageId    = "2",
                            Questions = new List <Question> {
                                new Question()
                                {
                                    QuestionId = "Q2", Input = new Input()
                                }
                            },
                            PageOfAnswers = new List <PageOfAnswers>(),
                            Next          = new List <Next>
                            {
                                new Next()
                                {
                                    Action = "ReturnToSequence", Conditions = new List <Condition>()
                                },
                            },
                            Active            = true,
                            ActivatedByPageId = "1"
                        },
                        new Page()
                        {
                            PageId    = "3",
                            Questions = new List <Question> {
                                new Question()
                                {
                                    QuestionId = "Q3", Input = new Input()
                                }
                            },
                            PageOfAnswers = new List <PageOfAnswers>(),
                            Next          = new List <Next>
                            {
                                new Next()
                                {
                                    Action = "ReturnToSequence", Conditions = new List <Condition>()
                                }
                            },
                            Active            = false,
                            ActivatedByPageId = "1"
                        }
                    }
                }
            });

            await DataContext.Applications.AddAsync(new Data.Entities.Application()
            {
                Id = ApplicationId, ApplicationData = "{ \"Q1\" : \"Yes\" }"
            });

            await DataContext.SaveChangesAsync();
        }
Beispiel #25
0
        public async Task <HandlerResponse <bool> > Handle(DeleteFileRequest request, CancellationToken cancellationToken)
        {
            var section = await _dataContext.ApplicationSections.FirstOrDefaultAsync(sec => sec.Id == request.SectionId && sec.ApplicationId == request.ApplicationId, cancellationToken);

            if (section == null)
            {
                return(new HandlerResponse <bool>(success: false, message: $"Section {request.SectionId} in Application {request.ApplicationId} does not exist."));
            }

            var qnaData = new QnAData(section.QnAData);
            var page    = qnaData.Pages.FirstOrDefault(p => p.PageId == request.PageId);

            if (page == null)
            {
                return(new HandlerResponse <bool>(success: false, message: $"Page {request.PageId} in Application {request.ApplicationId} does not exist."));
            }

            if (page.Questions.All(q => q.Input.Type != "FileUpload"))
            {
                return(new HandlerResponse <bool>(success: false, message: $"Page {request.PageId} in Application {request.ApplicationId} does not contain any File Upload questions."));
            }

            if (page.PageOfAnswers == null || !page.PageOfAnswers.Any())
            {
                return(new HandlerResponse <bool>(success: false, message: $"Page {request.PageId} in Application {request.ApplicationId} does not contain any uploads."));
            }

            var container = await ContainerHelpers.GetContainer(_fileStorageConfig.Value.StorageConnectionString, _fileStorageConfig.Value.ContainerName);

            var directory = ContainerHelpers.GetDirectory(request.ApplicationId, section.SequenceId, request.SectionId, request.PageId, request.QuestionId, container);

            var answer = page.PageOfAnswers.SingleOrDefault(poa => poa.Answers.Any(a => a.QuestionId == request.QuestionId && a.Value == request.FileName));

            if (answer is null)
            {
                return(new HandlerResponse <bool>(success: false, message: $"Question {request.QuestionId} on Page {request.PageId} in Application {request.ApplicationId} does not contain an upload named {request.FileName}."));
            }

            page.PageOfAnswers.Remove(answer);

            if (page.PageOfAnswers.Count == 0)
            {
                page.Complete = false;
                if (page.HasFeedback)
                {
                    foreach (var feedback in page.Feedback.Where(feedback => feedback.IsNew).Select(feedback => feedback))
                    {
                        feedback.IsCompleted = false;
                    }
                }
            }
            else
            {
                var answers = page.PageOfAnswers?.SelectMany(p => p.Answers);
                if (answers != null)
                {
                    var validationErrors = _answerValidator.Validate(answers.ToList(), page);
                    if (validationErrors != null && validationErrors.Any())
                    {
                        page.Complete = false;
                    }
                }
            }

            section.QnAData = qnaData;
            await _dataContext.SaveChangesAsync(cancellationToken);

            var blobRef = directory.GetBlobReference(request.FileName);
            await blobRef.DeleteAsync(cancellationToken);

            await RemoveApplicationDataForThisQuestion(request.ApplicationId, request.QuestionId, page);

            return(new HandlerResponse <bool>(true));
        }
        public async Task SetUp()
        {
            DataContext          = DataContextHelpers.GetInMemoryDataContext();
            NotRequiredProcessor = new NotRequiredProcessor();
            TagProcessingService = new TagProcessingService(DataContext);
            var fileStorageConfig = GetFileStorageConfig();

            var encryptionService = Substitute.For <IEncryptionService>();

            encryptionService.Encrypt(Arg.Any <Stream>()).Returns(callinfo => callinfo.ArgAt <Stream>(0)); // Don't Encrypt stream
            encryptionService.Decrypt(Arg.Any <Stream>()).Returns(callinfo => callinfo.ArgAt <Stream>(0)); // Don't Decrypt stream

            var validator = Substitute.For <IAnswerValidator>();

            validator.Validate(Arg.Any <List <Answer> >(), Arg.Any <Page>()).Returns(new List <KeyValuePair <string, string> >());

            var fileContentValidator = Substitute.For <IFileContentValidator>();

            fileContentValidator.Validate(Arg.Any <IFormFileCollection>()).Returns(new List <KeyValuePair <string, string> >());

            Handler = new SubmitPageOfFilesHandler(DataContext, fileStorageConfig, encryptionService, validator, fileContentValidator, NotRequiredProcessor, TagProcessingService);

            ApplicationId = Guid.NewGuid();
            SectionId     = Guid.NewGuid();
            await DataContext.ApplicationSections.AddAsync(new ApplicationSection()
            {
                ApplicationId = ApplicationId,
                Id            = SectionId,
                QnAData       = new QnAData()
                {
                    Pages = new List <Page>
                    {
                        new Page()
                        {
                            PageId    = "1",
                            Questions = new List <Question> {
                                new Question()
                                {
                                    QuestionId = "Q1", Input = new Input {
                                        Type = "FileUpload"
                                    }
                                }
                            },
                            PageOfAnswers = new List <PageOfAnswers>(),
                            Next          = new List <Next> {
                                new Next {
                                }
                            },
                            Active = true
                        }
                    }
                }
            });

            await DataContext.Applications.AddAsync(new Data.Entities.Application()
            {
                Id = ApplicationId, ApplicationData = "{}"
            });

            await DataContext.SaveChangesAsync();
        }