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 <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));
        }