public async Task <IActionResult> RequestContributor(SurveyContributorRequestViewModel contributorRequestViewModel) { try { if (!ModelState.IsValid) { return(RedirectToAction(nameof(Contributors), new { id = contributorRequestViewModel.SurveyId })); } var existingContributors = await _surveyService.GetSurveyContributorsAsync(contributorRequestViewModel.SurveyId); if (existingContributors.Succeeded) { if (existingContributors.Item.Contributors.Any(item => String.Equals(item.Email, contributorRequestViewModel.EmailAddress, StringComparison.OrdinalIgnoreCase))) { ViewBag.SurveyId = contributorRequestViewModel.SurveyId; ViewBag.Message = contributorRequestViewModel.EmailAddress + " is already a contributor"; return(View()); } if (existingContributors.Item.Requests.Any(item => String.Equals(item.EmailAddress, contributorRequestViewModel.EmailAddress, StringComparison.OrdinalIgnoreCase))) { ViewBag.SurveyId = contributorRequestViewModel.SurveyId; ViewBag.Message = contributorRequestViewModel.EmailAddress + " has already been requested before"; return(View()); } } await _surveyService.AddContributorRequestAsync(new ContributorRequest { SurveyId = contributorRequestViewModel.SurveyId, EmailAddress = contributorRequestViewModel.EmailAddress }); ViewBag.Message = $"Contribution Requested for {contributorRequestViewModel.EmailAddress}"; ViewBag.SurveyId = contributorRequestViewModel.SurveyId; var result = await _surveyService.GetSurveyContributorsAsync(contributorRequestViewModel.SurveyId); if (result.Succeeded) { return(View("Contributors", result.Item)); } else { ViewBag.Message = "Unexpected Error"; return(View("~/Views/Shared/Error.cshtml")); } } catch (AuthenticationException) { return(ReAuthenticateUser()); } catch { ViewBag.Message = "Unexpected Error"; } return(View("~/Views/Shared/Error.cshtml")); }
public async Task RequestContributor_SavesRequest() { var contributorRequestViewModel = new SurveyContributorRequestViewModel { SurveyId = 123, EmailAddress = "*****@*****.**" }; var apiResult = A.Fake <ApiResult>(); var invitations = new List <ContributorRequest>(); A.CallTo(() => _surveyService.AddContributorRequestAsync(A <ContributorRequest> .Ignored)).Invokes((ContributorRequest r) => invitations.Add(r)).Returns(apiResult); // RequestContributor looks for existing contributors var contributorsDto = new ContributorsDTO { Contributors = new List <UserDTO>(), Requests = new List <ContributorRequest>() }; var apiResult2 = A.Fake <ApiResult <ContributorsDTO> >(); A.CallTo(() => apiResult2.Succeeded).Returns(true); A.CallTo(() => apiResult2.Item).Returns(contributorsDto); A.CallTo(() => _surveyService.GetSurveyContributorsAsync(A <int> .Ignored)).Returns(Task.FromResult(apiResult2)); var result = await _target.RequestContributor(contributorRequestViewModel); Assert.Equal(123, invitations[0].SurveyId); Assert.Equal("*****@*****.**", invitations[0].EmailAddress); }