diff --git a/application/CohortManager/src/Functions/DemographicServices/DemographicDataManagementFunction/DemographicDataFunction.cs b/application/CohortManager/src/Functions/DemographicServices/DemographicDataManagementFunction/DemographicDataFunction.cs index d1ce11b6f..3b59a9c56 100644 --- a/application/CohortManager/src/Functions/DemographicServices/DemographicDataManagementFunction/DemographicDataFunction.cs +++ b/application/CohortManager/src/Functions/DemographicServices/DemographicDataManagementFunction/DemographicDataFunction.cs @@ -24,6 +24,24 @@ public DemographicDataFunction(ILogger logger, ICreateR [Function("DemographicDataFunction")] public async Task Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", "post")] HttpRequestData req) + { + return await Main(req, false); + } + + + /// + /// Gets filtered demographic data from the demographic data service, + /// this endpoint is used by the external BI product + /// + /// The NHS number to get the demographic data for. + /// JSON response containing the Primary Care Provider & Preferred Language + [Function("DemographicDataFunctionExternal")] + public async Task RunExternal([HttpTrigger(AuthorizationLevel.Anonymous, "get")] HttpRequestData req) + { + return await Main(req, true); + } + + private async Task Main(HttpRequestData req, bool externalRequest) { var participantData = new Participant(); try @@ -56,6 +74,14 @@ public async Task Run([HttpTrigger(AuthorizationLevel.Anonymou _logger.LogInformation("demographic function failed"); return _createResponse.CreateHttpResponse(HttpStatusCode.NotFound, req); } + + // Filters out unnsecessry data for use in the BI prdoduct + if (externalRequest) + { + var filterdData = JsonSerializer.Deserialize(data); + data = JsonSerializer.Serialize(filterdData); + } + return _createResponse.CreateHttpResponse(HttpStatusCode.OK, req, data); } } diff --git a/application/CohortManager/src/Functions/DemographicServices/DemographicDataManagementFunction/FilteredDemographicData.cs b/application/CohortManager/src/Functions/DemographicServices/DemographicDataManagementFunction/FilteredDemographicData.cs new file mode 100644 index 000000000..03b516f9b --- /dev/null +++ b/application/CohortManager/src/Functions/DemographicServices/DemographicDataManagementFunction/FilteredDemographicData.cs @@ -0,0 +1,9 @@ +/// Demographic fields required in requests from the BI product. + +namespace NHS.CohortManager.DemographicServices; + +public class FilteredDemographicData +{ + public string? PrimaryCareProvider { get; set; } + public string? PreferredLanguage { get; set; } +} diff --git a/tests/UnitTests/DemographicServicesTests/DemographicDataFunctionTests/DemographicDataFunctionTests.cs b/tests/UnitTests/DemographicServicesTests/DemographicDataFunctionTests/DemographicDataFunctionTests.cs index 577e88c18..92c425a42 100644 --- a/tests/UnitTests/DemographicServicesTests/DemographicDataFunctionTests/DemographicDataFunctionTests.cs +++ b/tests/UnitTests/DemographicServicesTests/DemographicDataFunctionTests/DemographicDataFunctionTests.cs @@ -17,7 +17,7 @@ public class DemographicDataFunctionTests { private readonly Mock> _logger = new(); private readonly Mock _createResponse = new(); - private readonly Mock _callFunction = new(); + private readonly Mock _callFunctionMock = new(); private readonly Mock _context = new(); private Mock _request; private readonly Mock _webResponse = new(); @@ -59,20 +59,20 @@ public DemographicDataFunctionTests() _webResponse.Setup(x => x.StatusCode).Returns(HttpStatusCode.OK); - _callFunction.Setup(call => call.SendPost(It.IsAny(), It.IsAny())) + _callFunctionMock.Setup(call => call.SendPost(It.IsAny(), It.IsAny())) .Returns(Task.FromResult(_webResponse.Object)); _webResponse.Setup(x => x.StatusCode).Returns(HttpStatusCode.OK); - _callFunction.Setup(call => call.SendGet(It.IsAny())) + _callFunctionMock.Setup(call => call.SendGet(It.IsAny())) .Returns(Task.FromResult("")); } [TestMethod] - public async Task Run_return_DemographicDataSavedPostRequest_OK() + public async Task RunPost_ValidRequest_ReturnOk() { // Arrange var json = JsonSerializer.Serialize(_participant); - var sut = new DemographicDataFunction(_logger.Object, _createResponse.Object, _callFunction.Object); + var sut = new DemographicDataFunction(_logger.Object, _createResponse.Object, _callFunctionMock.Object); _request = _setupRequest.Setup(json); @@ -85,15 +85,15 @@ public async Task Run_return_DemographicDataSavedPostRequest_OK() } [TestMethod] - public async Task Run_return_DemographicDataSavedPostRequest_InternalServerEver() + public async Task RunPost_DataServiceReturns500_ReturnInternalServerError() { // Arrange var json = JsonSerializer.Serialize(_participant); - var sut = new DemographicDataFunction(_logger.Object, _createResponse.Object, _callFunction.Object); + var sut = new DemographicDataFunction(_logger.Object, _createResponse.Object, _callFunctionMock.Object); _request = _setupRequest.Setup(json); _webResponse.Setup(x => x.StatusCode).Returns(HttpStatusCode.InternalServerError); - _callFunction.Setup(call => call.SendPost(It.IsAny(), It.IsAny())) + _callFunctionMock.Setup(call => call.SendPost(It.IsAny(), It.IsAny())) .Returns(Task.FromResult(_webResponse.Object)); // Act @@ -105,18 +105,18 @@ public async Task Run_return_DemographicDataSavedPostRequest_InternalServerEver( } [TestMethod] - public async Task Run_return_DemographicDataGetRequest_OK() + public async Task RunGet_ValidRequest_ReturnOk() { // Arrange var json = JsonSerializer.Serialize(_participant); - var sut = new DemographicDataFunction(_logger.Object, _createResponse.Object, _callFunction.Object); + var sut = new DemographicDataFunction(_logger.Object, _createResponse.Object, _callFunctionMock.Object); _request = _setupRequest.Setup(json); // Act _request.Setup(x => x.Query).Returns(new System.Collections.Specialized.NameValueCollection() { { "Id", "1" } }); - _callFunction.Setup(call => call.SendGet(It.IsAny())) + _callFunctionMock.Setup(call => call.SendGet(It.IsAny())) .Returns(Task.FromResult("data")); @@ -132,7 +132,7 @@ public async Task Run_return_DemographicDataNotSaved_InternalServerError() { // Arrange var json = JsonSerializer.Serialize(_participant); - var sut = new DemographicDataFunction(_logger.Object, _createResponse.Object, _callFunction.Object); + var sut = new DemographicDataFunction(_logger.Object, _createResponse.Object, _callFunctionMock.Object); _request = _setupRequest.Setup(json); @@ -146,7 +146,7 @@ public async Task Run_return_DemographicDataNotSaved_InternalServerError() _webResponse.Setup(x => x.StatusCode).Returns(HttpStatusCode.InternalServerError); - _callFunction.Setup(call => call.SendPost(It.Is(s => s.Contains("DemographicDataFunctionURI")), It.IsAny())) + _callFunctionMock.Setup(call => call.SendPost(It.Is(s => s.Contains("DemographicDataFunctionURI")), It.IsAny())) .Returns(Task.FromResult(_webResponse.Object)); // Act @@ -157,11 +157,11 @@ public async Task Run_return_DemographicDataNotSaved_InternalServerError() } [TestMethod] - public async Task Run_Return_DemographicFunctionThrows_InternalServerError() + public async Task RunPost_CallFunctionThrowsError_ReturnInternalServerError() { // Arrange var json = JsonSerializer.Serialize(_participant); - var sut = new DemographicDataFunction(_logger.Object, _createResponse.Object, _callFunction.Object); + var sut = new DemographicDataFunction(_logger.Object, _createResponse.Object, _callFunctionMock.Object); _request = _setupRequest.Setup(json); @@ -175,7 +175,7 @@ public async Task Run_Return_DemographicFunctionThrows_InternalServerError() _webResponse.Setup(x => x.StatusCode).Returns(HttpStatusCode.InternalServerError); - _callFunction.Setup(call => call.SendPost(It.Is(s => s.Contains("DemographicDataFunctionURI")), It.IsAny())) + _callFunctionMock.Setup(call => call.SendPost(It.Is(s => s.Contains("DemographicDataFunctionURI")), It.IsAny())) .ThrowsAsync(new Exception("there was an error")); // Act @@ -193,4 +193,33 @@ public async Task Run_Return_DemographicFunctionThrows_InternalServerError() It.IsAny>() ), Times.AtLeastOnce(), "There has been an error saving demographic data:"); } + + [TestMethod] + public async Task RunExternal_ValidRequest_ReturnOk() + { + // Arrange + var json = JsonSerializer.Serialize(_participant); + + _request = _setupRequest.Setup(json); + + Demographic DataServiceResponse = new() + { + PrimaryCareProvider = "Blerg", + PreferredLanguage = "Francais" + }; + + _request.Setup(x => x.Query).Returns(new System.Collections.Specialized.NameValueCollection() { { "Id", "1" } }); + + _callFunctionMock.Setup(call => call.SendGet(It.IsAny())) + .ReturnsAsync(JsonSerializer.Serialize(DataServiceResponse)); + + _request.Setup(r => r.Method).Returns("GET"); + var sut = new DemographicDataFunction(_logger.Object, _createResponse.Object, _callFunctionMock.Object); + + // Act + var result = await sut.RunExternal(_request.Object); + + // Assert + Assert.AreEqual(HttpStatusCode.OK, result.StatusCode); + } }