Ejemplo n.º 1
0
        public void InsertExternalReferences(InsertResidentRequest request, int residentId)
        {
            try
            {
                foreach (var externalReference in request.ExternalReferences)
                {
                    //check if external reference already exists for resident
                    var externalRefExists = _residentContactContext.ExternalSystemIds.Any(x => x.ExternalSystemLookupId == externalReference.ExternalSystemId &&
                                                                                          x.ExternalIdName == externalReference.ExternalReferenceName && x.ExternalIdValue == externalReference.ExternalReferenceValue &&
                                                                                          x.ResidentId == residentId);

                    if (!externalRefExists)
                    {
                        var newExternalReference = new ExternalSystemId()
                        {
                            ExternalIdName         = externalReference.ExternalReferenceName,
                            ExternalIdValue        = externalReference.ExternalReferenceValue,
                            ExternalSystemLookupId = externalReference.ExternalSystemId,
                            ResidentId             = residentId
                        };

                        _residentContactContext.ExternalSystemIds.Add(newExternalReference);
                    }
                }

                _residentContactContext.SaveChanges();
            }
            catch (DbUpdateException ex)
            {
                throw new ExternalReferenceNotInsertedException(ex.InnerException.Message);
            }
        }
        public async Task ShouldReturn200IfResidentIsAlreadyPresentInDatabase()
        {
            var residentInDb = E2ETestsHelper.AddResidentRecordToTheDatabase(ResidentContactContext);
            var crmReference = E2ETestsHelper.AddCrmContactIdForResidentId(ResidentContactContext, residentInDb.Id);

            var request = new InsertResidentRequest
            {
                FirstName          = _faker.Random.Word(),
                LastName           = _faker.Random.Word(),
                DateOfBirth        = _faker.Date.Past(),
                ExternalReferences = new List <InsertExternalReferenceRequest> {
                    new InsertExternalReferenceRequest {
                        ExternalReferenceValue = crmReference.ExternalIdValue,
                        ExternalReferenceName  = "ContactId",
                        ExternalSystemId       = crmReference.ExternalSystemLookupId
                    }
                }
            };

            var url = new Uri("/api/v1/residents", UriKind.Relative);

            using var requestContent =
                      new StringContent(JsonConvert.SerializeObject(request), Encoding.UTF8, "application/json");
            var response = await Client.PostAsync(url, requestContent).ConfigureAwait(true);

            var content = response.Content.ReadAsStringAsync().Result;
            var insertResidentResponse = JsonConvert.DeserializeObject <InsertResidentResponse>(content);

            response.StatusCode.Should().Be(200);
            insertResidentResponse.ResidentRecordAlreadyPresent.Should().BeTrue();
        }
Ejemplo n.º 3
0
        public InsertResidentResponse Execute(InsertResidentRequest request)
        {
            var resident = _residentGateway.InsertNewResident(request);

            //even if resident already in db, attempt to insert possible new external references
            _residentGateway.InsertExternalReferences(request, resident.ResidentId);
            return(resident);
        }
Ejemplo n.º 4
0
        public void EnsureThatInsertExternalReferenceRecordUseCaseCallsGateway()
        {
            var request = new InsertResidentRequest();

            _mockResidentGateway.Setup(x => x.InsertNewResident(request)).Returns(new InsertResidentResponse());
            _classUnderTest.Execute(request);

            _mockResidentGateway.Verify(x => x.InsertNewResident(request), Times.Once);
            _mockResidentGateway.Verify(x => x.InsertExternalReferences(request, It.IsAny <int>()), Times.Once);
        }
Ejemplo n.º 5
0
        public void EnsureThatUseCaseReturnsTheIdReturnedByGateway()
        {
            var request    = new InsertResidentRequest();
            var expectedId = 1;

            _mockResidentGateway.Setup(x => x.InsertNewResident(request)).Returns(new InsertResidentResponse {
                ResidentId = expectedId, ResidentRecordAlreadyPresent = false
            });
            var result = _classUnderTest.Execute(request);

            result.ResidentId.Should().Be(expectedId);
            result.ResidentRecordAlreadyPresent.Should().BeFalse();
        }
        private void CheckResidentHasBeenInserted(int residentId, InsertResidentRequest request)
        {
            var resident = ResidentContactContext.Residents.Where(x => x.Id == residentId).FirstOrDefault();

            resident.FirstName.Should().Be(request.FirstName);
            resident.LastName.Should().Be(request.LastName);
            resident.Gender.Should().Be(request.Gender);
            resident.DateOfBirth.Should().Be(request.DateOfBirth);

            var externalReferences = ResidentContactContext.ExternalSystemIds.Where(x => x.ResidentId == residentId).ToList();

            externalReferences.Count.Should().Be(request.ExternalReferences.Count);
        }
        public async Task ShouldReturn400IfRequestParametersAreMissing()
        {
            var request = new InsertResidentRequest
            {
                FirstName = _faker.Random.Word()
            };

            var url = new Uri("/api/v1/residents", UriKind.Relative);

            using var requestContent =
                      new StringContent(JsonConvert.SerializeObject(request), Encoding.UTF8, "application/json");
            var response = await Client.PostAsync(url, requestContent).ConfigureAwait(true);

            response.StatusCode.Should().Be(400);
        }
        public IActionResult InsertExternalReference([FromBody] InsertResidentRequest request)
        {
            try
            {
                var resident = _insertExternalReferenceRecordUseCase.Execute(request);
                if (resident.ResidentRecordAlreadyPresent)
                {
                    return(Ok(resident));
                }

                return(CreatedAtAction("ViewResidentRecord", resident));
            }
            catch (ResidentNotInsertedException ex)
            {
                return(StatusCode(500, $"Resident could not be inserted - {ex.Message}"));
            }
            catch (ExternalReferenceNotInsertedException ex)
            {
                return(StatusCode(500, $"External reference could not be inserted - {ex.Message}"));
            }
        }
Ejemplo n.º 9
0
        public InsertResidentResponse InsertNewResident(InsertResidentRequest request)
        {
            try
            {
                //find CRM system lookup id to compare against request
                var crmLookupId = _residentContactContext.ExternalSystemLookups.Where(x => x.Name == "CRM").FirstOrDefault();
                var crmId       = request.ExternalReferences.Find(x => x.ExternalReferenceName == "ContactId" && x.ExternalSystemId == crmLookupId.Id);
                if (crmId != null)
                {   //check if resident exists
                    var residentExists = FindResidentIdByContactId(crmId.ExternalReferenceValue);
                    //if resident already present, return id
                    if (residentExists != null)
                    {
                        return new InsertResidentResponse {
                                   ResidentId = (int)residentExists, ResidentRecordAlreadyPresent = true
                        }
                    }
                    ;
                }

                var newResident = new Resident
                {
                    FirstName   = request.FirstName,
                    LastName    = request.LastName,
                    DateOfBirth = request.DateOfBirth,
                    Gender      = request.Gender
                };

                _residentContactContext.Residents.Add(newResident);
                _residentContactContext.SaveChanges();

                return(new InsertResidentResponse {
                    ResidentId = newResident.Id, ResidentRecordAlreadyPresent = false
                });
            }
            catch (DbUpdateException ex)
            {
                throw new ResidentNotInsertedException(ex.InnerException.Message);
            }
        }
        public async Task ShouldReturn201WhenNewResidentRecordIsInserted()
        {
            var externalSystemLookup = new ExternalSystemLookup
            {
                Name = _faker.Random.Word()
            };

            ResidentContactContext.ExternalSystemLookups.Add(externalSystemLookup);
            ResidentContactContext.SaveChanges();

            var request = new InsertResidentRequest
            {
                FirstName          = _faker.Random.Word(),
                LastName           = _faker.Random.Word(),
                DateOfBirth        = _faker.Date.Past(),
                ExternalReferences = new List <InsertExternalReferenceRequest> {
                    new InsertExternalReferenceRequest {
                        ExternalReferenceValue = _faker.Random.Word(),
                        ExternalReferenceName  = _faker.Random.Word(),
                        ExternalSystemId       = externalSystemLookup.Id
                    }
                }
            };

            var url = new Uri("/api/v1/residents", UriKind.Relative);

            using var requestContent =
                      new StringContent(JsonConvert.SerializeObject(request), Encoding.UTF8, "application/json");
            var response = await Client.PostAsync(url, requestContent).ConfigureAwait(true);

            var content = response.Content.ReadAsStringAsync().Result;
            var insertResidentResponse = JsonConvert.DeserializeObject <InsertResidentResponse>(content);

            response.StatusCode.Should().Be(201);
            CheckResidentHasBeenInserted(insertResidentResponse.ResidentId, request);
        }