public async Task <QuestionResponseData> SaveResponseAsync(QuestionResponseData data) { if (data == null) { throw new ValidationException("Response must be provided"); } var(questionId, response) = await GetLatestResponseAsync(data.FeedbackQuestionCode, data.LoadId); if (response == null) { response = new Response { QuestionId = questionId, Attributes = new Dictionary <string, string> { { LOADSHOP_LOAD_ID, data.LoadId.ToString() } } }; } response.Answer = data.Answer; response.ResponseDateUtc = DateTime.UtcNow; response.Responder = _userContext.UserName; response = await _client.SaveResponseAsync(response); return(data); }
public async Task QuestionIdNotFound() { var data = new QuestionResponseData { FeedbackQuestionCode = FeedbackQuestionCodeEnum.LB_ShipperReuseCarrierQuestionId, Answer = true, LoadId = VALID_LOAD_ID }; Func <Task> action = async() => await _svc.SaveResponseAsync(data); await action.Should().ThrowAsync <Exception>(); }
public async Task UpdatesExistingResponse() { _config.Setup(_ => _.GetSection(It.Is <string>(s => s == FeedbackQuestionCodeEnum.LB_ShipperReuseCarrierQuestionId.ToString()))).Returns(_configSection.Object); _configSection.Setup(a => a.Value).Returns("10"); var latestResponse = new Response { ResponseId = 15, ResponseDateUtc = new DateTime(2020, 3, 18, 1, 0, 1), Responder = "old user", Answer = false, QuestionId = 10, Attributes = new Dictionary <string, string> { { "LoadshopLoadId", VALID_LOAD_ID.ToString() } } }; _feedbackClient.Setup(_ => _.SearchResponsesAsync(It.IsAny <ResponseSearchCriteria>())).ReturnsAsync( new List <Response> { new Response() { ResponseId = 1, ResponseDateUtc = new DateTime(2020, 3, 18, 1, 0, 0) }, latestResponse, new Response() { ResponseId = 2, ResponseDateUtc = new DateTime(2020, 3, 18, 1, 0, 0) } }); _feedbackClient.Setup(_ => _.SaveResponseAsync(It.IsAny <Response>())).ReturnsAsync(new Response() { ResponseId = 15 }); var data = new QuestionResponseData { FeedbackQuestionCode = FeedbackQuestionCodeEnum.LB_ShipperReuseCarrierQuestionId, Answer = true, LoadId = VALID_LOAD_ID }; await _svc.SaveResponseAsync(data); _feedbackClient.Verify(_ => _.SaveResponseAsync(It.Is <Response>(x => x.QuestionId == 10 && x.Attributes["LoadshopLoadId"] == VALID_LOAD_ID.ToString() && x.Answer == true && x.ResponseDateUtc != new DateTime(2020, 3, 18, 1, 0, 1) &&//date should be updated x.Responder == VALID_USER_NAME ))); }
public async Task NullLoadId() { _config.Setup(_ => _.GetSection(It.Is <string>(s => s == FeedbackQuestionCodeEnum.LB_ShipperReuseCarrierQuestionId.ToString()))).Returns(_configSection.Object); _configSection.Setup(a => a.Value).Returns("10"); var data = new QuestionResponseData { FeedbackQuestionCode = FeedbackQuestionCodeEnum.LB_ShipperReuseCarrierQuestionId, Answer = true, LoadId = null }; Func <Task> action = async() => await _svc.SaveResponseAsync(data); (await action.Should().ThrowAsync <Exception>()).WithMessage("Load Id is required"); }
public async Task SearchResponsesThrowsException() { _config.Setup(_ => _.GetSection(It.Is <string>(s => s == FeedbackQuestionCodeEnum.LB_ShipperReuseCarrierQuestionId.ToString()))).Returns(_configSection.Object); _configSection.Setup(a => a.Value).Returns("10"); _feedbackClient.Setup(_ => _.SearchResponsesAsync(It.IsAny <ResponseSearchCriteria>())).ThrowsAsync(new Exception("Testing Exception")); var data = new QuestionResponseData { FeedbackQuestionCode = FeedbackQuestionCodeEnum.LB_ShipperReuseCarrierQuestionId, Answer = true, LoadId = VALID_LOAD_ID }; Func <Task> action = async() => await _svc.SaveResponseAsync(data); (await action.Should().ThrowAsync <Exception>()).WithMessage("Testing Exception"); }
public bool Run(SurveyTransferMessage message) { Tenant tenant = this.tenantStore.GetTenant(message.Tenant); this.surveySqlStore.Reset(tenant.SqlAzureConnectionString, message.Tenant, message.SlugName); Survey surveyWithQuestions = this.surveyStore.GetSurveyByTenantAndSlugName(message.Tenant, message.SlugName, true); IEnumerable <string> answerIds = this.surveyAnswerStore.GetSurveyAnswerIds(message.Tenant, surveyWithQuestions.SlugName); SurveyData surveyData = surveyWithQuestions.ToDataModel(); foreach (var answerId in answerIds) { SurveyAnswer surveyAnswer = this.surveyAnswerStore.GetSurveyAnswer(surveyWithQuestions.Tenant, surveyWithQuestions.SlugName, answerId); var responseData = new ResponseData { Id = Guid.NewGuid().ToString(), CreatedOn = surveyAnswer.CreatedOn }; foreach (var answer in surveyAnswer.QuestionAnswers) { QuestionAnswer answerCopy = answer; var questionResponseData = new QuestionResponseData { QuestionId = (from question in surveyData.QuestionDatas where question.QuestionText == answerCopy.QuestionText select question.Id).FirstOrDefault(), Answer = answer.Answer }; responseData.QuestionResponseDatas.Add(questionResponseData); } if (responseData.QuestionResponseDatas.Count > 0) { surveyData.ResponseDatas.Add(responseData); } } this.surveySqlStore.SaveSurvey(tenant.SqlAzureConnectionString, surveyData); return(true); }
public async Task CallsSearchResponsesWithExpectedValues() { _config.Setup(_ => _.GetSection(It.Is <string>(s => s == FeedbackQuestionCodeEnum.LB_ShipperReuseCarrierQuestionId.ToString()))).Returns(_configSection.Object); _configSection.Setup(a => a.Value).Returns("10"); var data = new QuestionResponseData { FeedbackQuestionCode = FeedbackQuestionCodeEnum.LB_ShipperReuseCarrierQuestionId, Answer = true, LoadId = VALID_LOAD_ID }; await _svc.SaveResponseAsync(data); _feedbackClient.Verify(_ => _.SearchResponsesAsync(It.Is <ResponseSearchCriteria>(x => x.QuestionId == 10 && x.Attributes["LoadshopLoadId"] == VALID_LOAD_ID.ToString() ))); }
public async Task UnauthorizedUser() { _config.Setup(_ => _.GetSection(It.Is <string>(s => s == FeedbackQuestionCodeEnum.LB_ShipperReuseCarrierQuestionId.ToString()))).Returns(_configSection.Object); _configSection.Setup(a => a.Value).Returns("10"); _securityService.Setup(_ => _.GetAuthorizedCustomersforUserAsync()).ReturnsAsync((new List <CustomerData>()).AsReadOnly()); _svc = CreateService(); var data = new QuestionResponseData { FeedbackQuestionCode = FeedbackQuestionCodeEnum.LB_ShipperReuseCarrierQuestionId, Answer = true, LoadId = VALID_LOAD_ID }; Func <Task> action = async() => await _svc.SaveResponseAsync(data); (await action.Should().ThrowAsync <Exception>()).WithMessage("Load not found"); }
public async Task MissingLoad() { _config.Setup(_ => _.GetSection(It.Is <string>(s => s == FeedbackQuestionCodeEnum.LB_ShipperReuseCarrierQuestionId.ToString()))).Returns(_configSection.Object); _configSection.Setup(a => a.Value).Returns("10"); _dbBuilder = new MockDbBuilder(); _dbBuilder.WithUser(VALID_USER); _db = _dbBuilder.Build(); _svc = CreateService(); var data = new QuestionResponseData { FeedbackQuestionCode = FeedbackQuestionCodeEnum.LB_ShipperReuseCarrierQuestionId, Answer = true, LoadId = VALID_LOAD_ID }; Func <Task> action = async() => await _svc.SaveResponseAsync(data); (await action.Should().ThrowAsync <Exception>()).WithMessage("Load not found"); }
public async Task CreatesNewResponse(bool answer) { _config.Setup(_ => _.GetSection(It.Is <string>(s => s == FeedbackQuestionCodeEnum.LB_ShipperReuseCarrierQuestionId.ToString()))).Returns(_configSection.Object); _configSection.Setup(a => a.Value).Returns("10"); _feedbackClient.Setup(_ => _.SaveResponseAsync(It.IsAny <Response>())).ReturnsAsync(new Response() { ResponseId = 15 }); var data = new QuestionResponseData { FeedbackQuestionCode = FeedbackQuestionCodeEnum.LB_ShipperReuseCarrierQuestionId, Answer = answer, LoadId = VALID_LOAD_ID }; await _svc.SaveResponseAsync(data); _feedbackClient.Verify(_ => _.SaveResponseAsync(It.Is <Response>(x => x.QuestionId == 10 && x.Attributes["LoadshopLoadId"] == VALID_LOAD_ID.ToString() && x.Answer == answer ))); }
public async Task <IActionResult> Post([FromBody] QuestionResponseData data) { return(Success(await _feedbackService.SaveResponseAsync(data))); }