public VisitRequestViewModel GetViewModel()
        {
            var viewModel = new VisitRequestViewModel();

            var companyDetailsFromPeninsula = _companyDetailsService.GetCompanyDetails(_companyId);

            var sites = _siteService.Search(new SearchSitesRequest
            {
                CompanyId = _companyId,
                AllowedSiteIds = _allowedSiteIds
            });

            var selectedSite = sites.First(s => s.Parent == null); //Defaults to Main site

            viewModel.Sites = sites.Select(AutoCompleteViewModel.ForSite).AddDefaultOption().ToList();
            viewModel.SiteId = selectedSite.Id;
            viewModel.SiteName = selectedSite.Name;
            viewModel.VisitTimePreference = _visitTimePreference;
            viewModel.PersonNameToVisit = _personNameToVisit;
            viewModel.EmailAddress = _emailAddress;
            viewModel.CAN = companyDetailsFromPeninsula.CAN;
            viewModel.CompanyName = companyDetailsFromPeninsula.CompanyName;
            viewModel.Postcode = companyDetailsFromPeninsula.PostCode;
            
            return viewModel;
        }
        public void Setup()
        {
           
            _visitRequestService = new Mock<IVisitRequestService>();
            _passedViewModel = new Mock<VisitRequestViewModel>();
            
            //View Model Factory
            _visitRequestViewModelFactory = new Mock<IVisitRequestViewModelFactory>();
            _visitRequestViewModelFactory.Setup(x => x.WithCompanyId(It.IsAny<long>()))
               .Returns(_visitRequestViewModelFactory.Object);
            _visitRequestViewModelFactory.Setup(x => x.WithEmailAddress(It.IsAny<string>()))
                .Returns(_visitRequestViewModelFactory.Object);
            _visitRequestViewModelFactory.Setup(x => x.WithPersonToVisit(It.IsAny<string>()))
                .Returns(_visitRequestViewModelFactory.Object);
            _visitRequestViewModelFactory.Setup(x => x.WithAllowedSiteIds(It.IsAny<IList<long>>()))
               .Returns(_visitRequestViewModelFactory.Object);
            _visitRequestViewModelFactory.Setup(x => x.WithVisitTimePreference(It.IsAny<string>()))
               .Returns(_visitRequestViewModelFactory.Object);
            _visitRequestViewModelFactory.Setup(x => x.GetViewModel())
              .Returns(_passedViewModel.Object);

            //InvalidCastException view Model with wrong email address
            _inValidViewModel = new VisitRequestViewModel()
            {
                Comments = "abc",
                SiteId = 1,
                ContactNumber = "1231",
                EmailAddress = "asa@asd",
                PersonNameToVisit = "xyz",
                Sites = null,
                VisitFrom = DateTime.Now.Date.ToString(),
                VisitTo = DateTime.Now.Date.ToString(),
                VisitTimePreference = "AM"
            };

            _validViewModel = new VisitRequestViewModel()
            {
                Comments = "abc",
                SiteId = 1,
                ContactNumber = "1231",
                EmailAddress = "*****@*****.**",
                PersonNameToVisit = "xyz",
                Sites = null,
                VisitFrom = DateTime.Now.Date.ToString(),
                VisitTo = DateTime.Now.Date.ToString(),
                VisitTimePreference = "AM"
            };
            _businessSafeSessionManager = new Mock<IBusinessSafeSessionManager>();
        }
 private RequestVisitRequest MapVisitRequestViewModelToRequestVisitRequest(VisitRequestViewModel model)
 {
     return  new RequestVisitRequest()
     {
         CompanyName = model.CompanyName,
         CAN = model.CAN,
         ContactName = model.PersonNameToVisit,
         ContactEmail = model.EmailAddress,
         ContactPhone = model.ContactNumber,
         DateTo = model.VisitTo,
         DateFrom = model.VisitFrom,
         RequestTime = model.VisitTimePreference,
         Comments = model.Comments,
         SiteName = model.SiteName,
         Postcode = model.Postcode
     };
 }
        public ActionResult Create(VisitRequestViewModel postedViewModel)
        {
          var viewModel = _visitRequestViewModelFactory
                                       .WithCompanyId(CurrentUser.CompanyId)
                                       .WithAllowedSiteIds(CurrentUser.GetSitesFilter())
                                       .GetViewModel(postedViewModel);

           postedViewModel.Validate(new ValidationContext(this, null, null), ModelState);

            if (ModelState.IsValid)
            {
                var request = MapVisitRequestViewModelToRequestVisitRequest(viewModel);
                SentVisitRequestNotification(request);
                TempData["Message"] = "Thank you for your request. A member of our BusinessSafe diary booking team will be in contact with you shortly to confirm your booking.";
                return RedirectToAction("Index");  
            }

            return View("Index", viewModel);
        }
        public VisitRequestViewModel GetViewModel(VisitRequestViewModel postedViewModel)
        {
            var viewModel = new VisitRequestViewModel();

            var companyDetailsFromPeninsula = _companyDetailsService.GetCompanyDetails(_companyId);

            var sites = _siteService.Search(new SearchSitesRequest
            {
                CompanyId = _companyId,
                AllowedSiteIds = _allowedSiteIds
            });

            viewModel.Sites = sites.Select(AutoCompleteViewModel.ForSite).AddDefaultOption().ToList();

            if (postedViewModel.SiteId.HasValue)
            {
                var selectedSite = sites.First(s => s.Id == postedViewModel.SiteId);
                viewModel.SiteId = selectedSite.Id;
                viewModel.SiteName = selectedSite.Name;
            }

            viewModel.VisitTimePreference = postedViewModel.VisitTimePreference;
            viewModel.PersonNameToVisit = postedViewModel.PersonNameToVisit;
            viewModel.EmailAddress = postedViewModel.EmailAddress;
            viewModel.VisitFrom = postedViewModel.VisitFrom;
            viewModel.VisitTo = postedViewModel.VisitTo;
            viewModel.Comments = postedViewModel.Comments;
            viewModel.ContactNumber = postedViewModel.ContactNumber;

            viewModel.CAN = companyDetailsFromPeninsula.CAN;
            viewModel.CompanyName = companyDetailsFromPeninsula.CompanyName;
            viewModel.Postcode = companyDetailsFromPeninsula.PostCode;

            return viewModel;
        }