public async Task RequestContributor_SavesRequest()
        {
            var contributorRequestViewModel = new SurveyContributorRequestViewModel { SurveyId = 123, EmailAddress = "*****@*****.**" };

            var apiResult = new Mock<ApiResult>();
            var invitations = new List<ContributorRequest>();
            _surveyService.Setup(c => c.AddContributorRequestAsync(It.IsAny<ContributorRequest>()))
                .ReturnsAsync(apiResult.Object)
                .Callback<ContributorRequest>(c => invitations.Add(c));

            // RequestContributor looks for existing contributors
            var contributorsDto = new ContributorsDTO
            {
                Contributors = new List<UserDTO>(),
                Requests = new List<ContributorRequest>()
            };

            var apiResult2 = new Mock<ApiResult<ContributorsDTO>>();
            apiResult2.Setup(x => x.Succeeded).Returns(true);
            apiResult2.Setup(x => x.Item).Returns(contributorsDto);

            _surveyService.Setup(c => c.GetSurveyContributorsAsync(It.IsAny<int>()))
                .ReturnsAsync(apiResult2.Object);

            var result = await _target.RequestContributor(contributorRequestViewModel);

            Assert.Equal(123, invitations[0].SurveyId);
            Assert.Equal("*****@*****.**", invitations[0].EmailAddress);
        }
        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");
        }