public async Task PostSendsAddApiRequestCommandWithCorrectViewModel()
        {
            var viewModel = new RequestApiViewModel { Status = "new" };
            var mediator = new Mock<IMediator>();

            var sut = new RequestApiController(mediator.Object);
            await sut.Post(viewModel);

            mediator.Verify(x => x.SendAsync(It.Is<AddApiRequestCommand>(y => y.ViewModel == viewModel)), Times.Once);
        }
        public async Task PostEnqueuesProcessApiRequestsJobWithCorrectViewModel()
        {
            var viewModel = new RequestApiViewModel { Status = "new" };
            var mediator = new Mock<IMediator>();
            var backgroundJobClient = new Mock<IBackgroundJobClient>();

            var sut = new RequestApiController(mediator.Object, backgroundJobClient.Object);
            await sut.Post(viewModel);

            backgroundJobClient.Verify(x => x.Create(It.Is<Job>(job => 
                job.Method.Name == nameof(ProcessApiRequests.Process) && 
                job.Args[0] == viewModel), It.IsAny<EnqueuedState>()), Times.Once);
        }
        public void InvokeIGeocoderWithTheCorrectParameters()
        {
            var requestId = Guid.NewGuid();
            var geoCoder = new Mock<IGeocoder>();
            var viewModel = new RequestApiViewModel { Address = "address", City = "city", State = "state", Zip = "zip" };
            var sut = new ProcessApiRequests(Context, Mock.Of<IMediator>(), geoCoder.Object)
            {
                NewRequestId = () => requestId
            };

            sut.Process(viewModel);

            geoCoder.Verify(x => x.Geocode(viewModel.Address, viewModel.City, viewModel.State, viewModel.Zip, string.Empty), Times.Once);
        }
        public async Task AddRequest()
        {
            var requestId = Guid.NewGuid();
            var dateAdded = DateTime.UtcNow;

            var viewModel = new RequestApiViewModel
            {
                ProviderRequestId = "ProviderRequestId",
                Name = "Name",
                Address = "Address",
                City = "City",
                State = "state",
                Zip = "zip",
                Phone = "phone",
                Email = "email",
                Latitude = 10,
                Longitude = 10,
                Status = "new",
                ProviderData = "ProviderData"
            };
            
            var message = new AddApiRequestCommand { ViewModel = viewModel };

            var sut = new AddApiRequestCommandHandler(Context, Mock.Of<IGeocoder>(), Mock.Of<IMediator>())
            {
                NewRequestId = () => requestId,
                DateTimeUtcNow = () => dateAdded
            };
            await sut.Handle(message);

            var request = Context.Requests.Single(x => x.RequestId == requestId);

            Assert.Equal(request.RequestId, requestId);
            Assert.Equal(request.ProviderId, viewModel.ProviderRequestId);
            Assert.Equal(request.ProviderData, viewModel.ProviderData);
            Assert.Equal(request.Address, viewModel.Address);
            Assert.Equal(request.City, viewModel.City);
            Assert.Equal(request.DateAdded, dateAdded);
            Assert.Equal(request.Email, viewModel.Email);
            Assert.Equal(request.Name, viewModel.Name);
            Assert.Equal(request.State, viewModel.State);
            Assert.Equal(request.Zip, viewModel.Zip);
            Assert.Equal(request.Status, RequestStatus.Unassigned);
            Assert.Equal(request.Source, RequestSource.Api);
            Assert.Equal(request.Latitude, viewModel.Latitude);
            Assert.Equal(request.Longitude, viewModel.Longitude);
        }
        public void AddRequest()
        {
            var requestId = Guid.NewGuid();
            var dateAdded = DateTime.UtcNow;
            const string postalCode = "11111";

            var viewModel = new RequestApiViewModel
            {
                ProviderRequestId = "ProviderRequestId",
                Status = "new",
                Name = "Name",
                Address = "Address",
                City = "City",
                State = "state",
                Zip = postalCode,
                Phone = "111-111-1111",
                Email = "*****@*****.**",
                ProviderData = "ProviderData"
            };

            var sut = new ProcessApiRequests(Context, Mock.Of<IMediator>(), Mock.Of<IGeocoder>())
            {
                NewRequestId = () => requestId,
                DateTimeUtcNow = () => dateAdded
            };
            sut.Process(viewModel);

            var request = Context.Requests.Single(x => x.RequestId == requestId);

            Assert.Equal(request.RequestId, requestId);
            Assert.Equal(request.OrganizationId, 1);
            Assert.Equal(request.ProviderRequestId, viewModel.ProviderRequestId);
            Assert.Equal(request.ProviderData, viewModel.ProviderData);
            Assert.Equal(request.Address, viewModel.Address);
            Assert.Equal(request.City, viewModel.City);
            Assert.Equal(request.DateAdded, dateAdded);
            Assert.Equal(request.Email, viewModel.Email);
            Assert.Equal(request.Name, viewModel.Name);
            Assert.Equal(request.State, viewModel.State);
            Assert.Equal(request.Zip, viewModel.Zip);
            Assert.Equal(request.Status, RequestStatus.Unassigned);
            Assert.Equal(request.Source, RequestSource.Api);
        }
Exemplo n.º 6
0
        public void Process(RequestApiViewModel viewModel)
        {
            //since this is Hangfire job code, it needs to be idempotent, this could be re-tried if there is a failure
            var requestExists = context.Requests.Any(x => x.ProviderRequestId == viewModel.ProviderRequestId);
            if (!requestExists)
            {
                var request = new Request
                {
                    RequestId = NewRequestId(),
                    //TODO mgmccarthy: this is hard-coded for now to 1, which is HTBox Org's Id in dev b/c SampleDataGenerator always creates it first. We'll need something more robust when we go to production.
                    OrganizationId = 1,
                    ProviderRequestId = viewModel.ProviderRequestId,
                    ProviderData = viewModel.ProviderData,
                    Address = viewModel.Address,
                    City = viewModel.City,
                    DateAdded = DateTimeUtcNow(),
                    Email = viewModel.Email,
                    Name = viewModel.Name,
                    Phone = viewModel.Phone,
                    State = viewModel.State,
                    Zip = viewModel.Zip,
                    Status = RequestStatus.Unassigned,
                    Source = RequestSource.Api
                };

                //FYI, this is a web service call
                var address = geocoder.Geocode(viewModel.Address, viewModel.City, viewModel.State, viewModel.Zip, string.Empty).FirstOrDefault();
                request.Latitude = address?.Coordinates.Latitude ?? 0;
                request.Longitude = address?.Coordinates.Longitude ?? 0;

                context.Add(request);
                context.SaveChanges();

                mediator.Publish(new ApiRequestProcessedNotification { RequestId = request.RequestId });
            }   
        }