From 033fbd454fb81216d4f50d106f735a18daa67296 Mon Sep 17 00:00:00 2001 From: Pradeep Kumar Voorukonda Date: Fri, 20 Dec 2024 15:47:05 +0000 Subject: [PATCH 1/8] Code Refactor and Review --- ...scriptionsForTouchpointHttpTriggerTests.cs | 21 +- .../PatchSubscriptionsHttpTriggerTests.cs | 18 +- .../PostSubscriptionsHttpTriggerTests.cs | 26 +-- .../ModelTests/SubscriptionModelTests.cs | 2 +- ...onsForTouchpointHttpTriggerServiceTests.cs | 15 +- ...tchSubscriptionsHttpTriggerServiceTests.cs | 40 +--- ...ostSubscriptionsHttpTriggerServiceTests.cs | 38 +--- .../AppInsights/ApplicationInsightsLogger.cs | 108 --------- ...licationInsightsLoggerFactoryExtensions.cs | 27 --- .../ApplicationInsightsLoggerProvider.cs | 27 --- .../ApplicationInsightsSettings.cs | 10 - .../Cosmos/Client/DocumentDBClient.cs | 21 -- .../Cosmos/Helper/DocumentDBHelper.cs | 52 ----- .../Cosmos/Helper/IResourceHelper.cs | 8 - .../Cosmos/Helper/ResourceHelper.cs | 23 -- .../Cosmos/Provider/CosmosDBProvider.cs | 206 ++++++++++++++++++ .../Cosmos/Provider/DocumentDBProvider.cs | 152 ------------- ...mentDBProvider.cs => ICosmosDBProvider.cs} | 9 +- .../GetSubscriptionsByIdHttpTrigger.cs | 61 ------ .../GetSubscriptionsByIdHttpTriggerService.cs | 15 -- ...IGetSubscriptionsByIdHttpTriggerService.cs | 7 - .../function.json | 19 -- ...etSubscriptionsForTouchpointHttpTrigger.cs | 24 +- ...riptionsForTouchpointHttpTriggerService.cs | 12 +- ...riptionsForTouchpointHttpTriggerService.cs | 1 + .../function.json | 19 -- NCS.DSS.Subscriptions/Models/Customer.cs | 8 + .../SubscriptionsConfigurationSettings.cs | 14 ++ .../NCS.DSS.Subscriptions.csproj | 17 +- .../Function/PatchSubscriptionsHttpTrigger.cs | 32 ++- .../IPatchSubscriptionssHttpTriggerService.cs | 1 + .../PatchSubscriptionsHttpTriggerService.cs | 14 +- .../function.json | 19 -- .../Function/PostSubscriptionsHttpTrigger.cs | 32 ++- .../IPostSubscriptionsHttpTriggerService.cs | 2 + .../PostSubscriptionsHttpTriggerService.cs | 16 +- .../function.json | 28 --- NCS.DSS.Subscriptions/Program.cs | 79 +++++-- 38 files changed, 428 insertions(+), 795 deletions(-) delete mode 100644 NCS.DSS.Subscriptions/AppInsights/ApplicationInsightsLogger.cs delete mode 100644 NCS.DSS.Subscriptions/AppInsights/ApplicationInsightsLoggerFactoryExtensions.cs delete mode 100644 NCS.DSS.Subscriptions/AppInsights/ApplicationInsightsLoggerProvider.cs delete mode 100644 NCS.DSS.Subscriptions/AppInsights/ApplicationInsightsSettings.cs delete mode 100644 NCS.DSS.Subscriptions/Cosmos/Client/DocumentDBClient.cs delete mode 100644 NCS.DSS.Subscriptions/Cosmos/Helper/DocumentDBHelper.cs delete mode 100644 NCS.DSS.Subscriptions/Cosmos/Helper/IResourceHelper.cs delete mode 100644 NCS.DSS.Subscriptions/Cosmos/Helper/ResourceHelper.cs create mode 100644 NCS.DSS.Subscriptions/Cosmos/Provider/CosmosDBProvider.cs delete mode 100644 NCS.DSS.Subscriptions/Cosmos/Provider/DocumentDBProvider.cs rename NCS.DSS.Subscriptions/Cosmos/Provider/{IDocumentDBProvider.cs => ICosmosDBProvider.cs} (60%) delete mode 100644 NCS.DSS.Subscriptions/GetSubscriptionsByIdHttpTrigger/Function/GetSubscriptionsByIdHttpTrigger.cs delete mode 100644 NCS.DSS.Subscriptions/GetSubscriptionsByIdHttpTrigger/Service/GetSubscriptionsByIdHttpTriggerService.cs delete mode 100644 NCS.DSS.Subscriptions/GetSubscriptionsByIdHttpTrigger/Service/IGetSubscriptionsByIdHttpTriggerService.cs delete mode 100644 NCS.DSS.Subscriptions/GetSubscriptionsByIdHttpTrigger/function.json delete mode 100644 NCS.DSS.Subscriptions/GetSubscriptionsForTouchpointHttpTrigger/function.json create mode 100644 NCS.DSS.Subscriptions/Models/Customer.cs create mode 100644 NCS.DSS.Subscriptions/Models/SubscriptionsConfigurationSettings.cs delete mode 100644 NCS.DSS.Subscriptions/PatchSubscriptionsHttpTrigger/function.json delete mode 100644 NCS.DSS.Subscriptions/PostSubscriptionsHttpTrigger/function.json diff --git a/NCS.DSS.Subscriptions.Tests/FunctionTests/GetSubscriptionsForTouchpointHttpTriggerTests.cs b/NCS.DSS.Subscriptions.Tests/FunctionTests/GetSubscriptionsForTouchpointHttpTriggerTests.cs index 0f3f5de..e5518d2 100644 --- a/NCS.DSS.Subscriptions.Tests/FunctionTests/GetSubscriptionsForTouchpointHttpTriggerTests.cs +++ b/NCS.DSS.Subscriptions.Tests/FunctionTests/GetSubscriptionsForTouchpointHttpTriggerTests.cs @@ -1,10 +1,8 @@ -using DFC.Common.Standard.GuidHelper; -using DFC.HTTP.Standard; +using DFC.HTTP.Standard; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Logging; using Moq; -using NCS.DSS.Subscriptions.Cosmos.Helper; using NCS.DSS.Subscriptions.GetSubscriptionsForTouchpointHttpTrigger.Service; using System.Net; using GetSubscriptionsForTouchpointHttpTriggerrRun = NCS.DSS.Subscriptions.GetSubscriptionsForTouchpointHttpTrigger.Function.GetSubscriptionsForTouchpointHttpTrigger; @@ -19,27 +17,22 @@ public class GetSubscriptionsForTouchpointHttpTriggerTests private readonly Guid _customerId = Guid.Parse("1dd4d206-131a-44fd-8e2d-18b88b383f72"); private const string _touchPointId = "0000000001"; private HttpRequest _request; - private Mock _resourceHelper; private Mock _getSubscriptionsForTouchpointHttpTriggerService; private Mock _httpRequestHelper; private List _subscriptions; - private IGuidHelper _guidHelper; private GetSubscriptionsForTouchpointHttpTriggerrRun _getSubscriptionsForTouchpointHttpTrigger; [SetUp] public void Setup() { - _subscriptions = new List(); + _subscriptions = []; _request = (new DefaultHttpContext()).Request; - _resourceHelper = new Mock(); _httpRequestHelper = new Mock(); - var loggerHelper = new Mock>(); - _guidHelper = new GuidHelper(); + var logger = new Mock>(); _getSubscriptionsForTouchpointHttpTriggerService = new Mock(); _getSubscriptionsForTouchpointHttpTrigger = new GetSubscriptionsForTouchpointHttpTriggerrRun( - _resourceHelper.Object, _httpRequestHelper.Object, _getSubscriptionsForTouchpointHttpTriggerService.Object, - loggerHelper.Object + logger.Object ); } @@ -74,8 +67,8 @@ public async Task GetSubscriptionsForTouchpointHttpTrigger_ReturnsStatusCodeNoCo { // Arrange _httpRequestHelper.Setup(x => x.GetDssTouchpointId(_request)).Returns(_touchPointId); - _resourceHelper.Setup(x => x.DoesCustomerExist(It.IsAny())).Returns(Task.FromResult(true)); - _getSubscriptionsForTouchpointHttpTriggerService.Setup(x => x.GetSubscriptionsForTouchpointAsync(_customerId, _touchPointId)).Returns(Task.FromResult>(null)); + _getSubscriptionsForTouchpointHttpTriggerService.Setup(x => x.DoesCustomerExist(It.IsAny())).Returns(Task.FromResult(true)); + _getSubscriptionsForTouchpointHttpTriggerService.Setup(x => x.GetSubscriptionsForTouchpointAsync(_customerId, _touchPointId)).Returns(Task.FromResult>(_subscriptions)); // Act var result = await RunFunction(ValidCustomerId); @@ -88,7 +81,7 @@ public async Task GetSubscriptionsForTouchpointHttpTrigger_ReturnsStatusCodeOk_W { // Arrange _httpRequestHelper.Setup(x => x.GetDssTouchpointId(_request)).Returns(_touchPointId); - _resourceHelper.Setup(x => x.DoesCustomerExist(It.IsAny())).Returns(Task.FromResult(true)); + _getSubscriptionsForTouchpointHttpTriggerService.Setup(x => x.DoesCustomerExist(It.IsAny())).Returns(Task.FromResult(true)); _getSubscriptionsForTouchpointHttpTriggerService.Setup(x => x.GetSubscriptionsForTouchpointAsync(It.IsAny(), It.IsAny())).Returns(Task.FromResult(_subscriptions)); // Act diff --git a/NCS.DSS.Subscriptions.Tests/FunctionTests/PatchSubscriptionsHttpTriggerTests.cs b/NCS.DSS.Subscriptions.Tests/FunctionTests/PatchSubscriptionsHttpTriggerTests.cs index dec34de..cfae760 100644 --- a/NCS.DSS.Subscriptions.Tests/FunctionTests/PatchSubscriptionsHttpTriggerTests.cs +++ b/NCS.DSS.Subscriptions.Tests/FunctionTests/PatchSubscriptionsHttpTriggerTests.cs @@ -3,7 +3,6 @@ using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Logging; using Moq; -using NCS.DSS.Subscriptions.Cosmos.Helper; using NCS.DSS.Subscriptions.Helpers; using NCS.DSS.Subscriptions.Models; using NCS.DSS.Subscriptions.PatchSubscriptionsHttpTrigger.Service; @@ -27,7 +26,6 @@ public class PatchSubscriptionsHttpTriggerTests // local variables private HttpRequest _request; private Mock _validate; - private Mock _resourceHelper; private Mock _convertToDynamic; private Mock _patchSubscriptionsHttpTriggerService; private Mock _httpRequestHelper; @@ -43,18 +41,16 @@ public void Setup() _request = (new DefaultHttpContext()).Request; _validate = new Mock(); _validationResults = new List { }; - _resourceHelper = new Mock(); _convertToDynamic = new Mock(); _httpRequestHelper = new Mock(); - var loggerHelper = new Mock>(); + var logger = new Mock>(); _patchSubscriptionsHttpTriggerService = new Mock(); _patchSubscriptionsHttpTriggerRun = new PatchSubscriptionsHttpTriggerRun( - _resourceHelper.Object, _httpRequestHelper.Object, _validate.Object, _patchSubscriptionsHttpTriggerService.Object, _convertToDynamic.Object, - loggerHelper.Object + logger.Object ); } @@ -104,7 +100,7 @@ public async Task PatchSubscriptionsHttpTrigger_ReturnsStatusCodeUnprocessableEn // Arrange _httpRequestHelper.Setup(x => x.GetDssTouchpointId(_request)).Returns(_touchPointId); _httpRequestHelper.Setup(x => x.GetDssApimUrl(_request)).Returns(_apimUrl); - _httpRequestHelper.Setup(x => x.GetResourceFromRequest(_request)).Returns(Task.FromResult(null)); + _httpRequestHelper.Setup(x => x.GetResourceFromRequest(_request)).Returns(Task.FromResult(_subscriptionsPatch)); // Act var result = await RunFunction(ValidCustomerId, ValidSubscriptionId); @@ -144,7 +140,7 @@ public async Task PatchSubscriptionsHttpTrigger_ReturnsStatusCodeNoContent_WhenC _validationResults.Clear(); _validate.Setup(x => x.ValidateResource(It.IsAny())).Returns(_validationResults); - _resourceHelper.Setup(x => x.DoesCustomerExist(It.IsAny())).Returns(Task.FromResult(false)); + _patchSubscriptionsHttpTriggerService.Setup(x => x.DoesCustomerExist(It.IsAny())).Returns(Task.FromResult(false)); // Act var result = await RunFunction(ValidCustomerId, ValidSubscriptionId); @@ -165,9 +161,9 @@ public async Task PatchSubscriptionsHttpTrigger_ReturnsStatusCodeNoContent_WhenS _validationResults.Clear(); _validate.Setup(x => x.ValidateResource(It.IsAny())).Returns(_validationResults); - _resourceHelper.Setup(x => x.DoesCustomerExist(It.IsAny())).Returns(Task.FromResult(true)); + _patchSubscriptionsHttpTriggerService.Setup(x => x.DoesCustomerExist(It.IsAny())).Returns(Task.FromResult(true)); - _patchSubscriptionsHttpTriggerService.Setup(x => x.GetSubscriptionsForCustomerAsync(It.IsAny(), It.IsAny())).Returns(Task.FromResult(null)); + _patchSubscriptionsHttpTriggerService.Setup(x => x.GetSubscriptionsForCustomerAsync(It.IsAny(), It.IsAny())).Returns(Task.FromResult(_subscriptions)); // Act var result = await RunFunction(ValidCustomerId, ValidSubscriptionId); @@ -187,7 +183,7 @@ public async Task PatchSubscriptionsHttpTrigger_ReturnsStatusCodeOk_WhenRequestI _validationResults.Clear(); _validate.Setup(x => x.ValidateResource(It.IsAny())).Returns(_validationResults); - _resourceHelper.Setup(x => x.DoesCustomerExist(It.IsAny())).Returns(Task.FromResult(true)); + _patchSubscriptionsHttpTriggerService.Setup(x => x.DoesCustomerExist(It.IsAny())).Returns(Task.FromResult(true)); _patchSubscriptionsHttpTriggerService.Setup(x => x.GetSubscriptionsForCustomerAsync(It.IsAny(), It.IsAny())).Returns(Task.FromResult(_subscriptions)); _patchSubscriptionsHttpTriggerService.Setup(x => x.UpdateAsync(_subscriptions, _subscriptionsPatch)).Returns(Task.FromResult(_subscriptions)); diff --git a/NCS.DSS.Subscriptions.Tests/FunctionTests/PostSubscriptionsHttpTriggerTests.cs b/NCS.DSS.Subscriptions.Tests/FunctionTests/PostSubscriptionsHttpTriggerTests.cs index 87181fd..8dee2bd 100644 --- a/NCS.DSS.Subscriptions.Tests/FunctionTests/PostSubscriptionsHttpTriggerTests.cs +++ b/NCS.DSS.Subscriptions.Tests/FunctionTests/PostSubscriptionsHttpTriggerTests.cs @@ -3,7 +3,6 @@ using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Logging; using Moq; -using NCS.DSS.Subscriptions.Cosmos.Helper; using NCS.DSS.Subscriptions.Helpers; using NCS.DSS.Subscriptions.Models; using NCS.DSS.Subscriptions.PostSubscriptionsHttpTrigger.Service; @@ -29,7 +28,6 @@ public class PostSubscriptionsHttpTriggerTests private HttpRequest _request; private Mock _validate; private List _validationResults; - private Mock _resourceHelper; private Mock _convertToDynamic; private Mock _postSubscriptionsHttpTriggerService; private Mock _httpRequestHelper; @@ -42,17 +40,15 @@ public void Setup() _request = (new DefaultHttpContext()).Request; _validate = new Mock(); _validationResults = new List { }; - _resourceHelper = new Mock(); _convertToDynamic = new Mock(); _httpRequestHelper = new Mock(); - var loggerHelper = new Mock>(); + var logger = new Mock>(); _postSubscriptionsHttpTriggerService = new Mock(); _postSubscriptionsHttpTriggerRun = new PostSubscriptionsHttpTriggerRun( - _resourceHelper.Object, _httpRequestHelper.Object, _validate.Object, _postSubscriptionsHttpTriggerService.Object, - loggerHelper.Object, + logger.Object, _convertToDynamic.Object ); @@ -90,7 +86,7 @@ public async Task PostSubscriptionsHttpTrigger_ReturnsStatusCodeUnprocessableEnt // Arrange _httpRequestHelper.Setup(x => x.GetDssTouchpointId(_request)).Returns(_touchPointId); _httpRequestHelper.Setup(x => x.GetDssApimUrl(_request)).Returns(_apimUrl); - _httpRequestHelper.Setup(x => x.GetResourceFromRequest(_request)).Returns(Task.FromResult(null)); + _httpRequestHelper.Setup(x => x.GetResourceFromRequest(_request)).Returns(Task.FromResult(_subscriptions)); _postSubscriptionsHttpTriggerService.Setup(x => x.CreateAsync(_subscriptions)).Returns(Task.FromResult(_subscriptions)); @@ -130,7 +126,7 @@ public async Task PostSubscriptionsHttpTrigger_ReturnsStatusCodeNoContent_WhenCu _validationResults.Clear(); _validate.Setup(x => x.ValidateResource(It.IsAny())).Returns(_validationResults); - _resourceHelper.Setup(x => x.DoesCustomerExist(It.IsAny())).Returns(Task.FromResult(false)); + _postSubscriptionsHttpTriggerService.Setup(x => x.DoesCustomerExist(It.IsAny())).Returns(Task.FromResult(false)); // Act var result = await RunFunction(ValidCustomerId); @@ -149,8 +145,8 @@ public async Task PostSubscriptionsHttpTrigger_ReturnsStatusCodeConflict_WhenSub _validationResults.Clear(); _validate.Setup(x => x.ValidateResource(It.IsAny())).Returns(_validationResults); - _resourceHelper.Setup(x => x.DoesCustomerExist(It.IsAny())).Returns(Task.FromResult(true)); - _resourceHelper.Setup(x => x.DoesSubscriptionExist(It.IsAny(), It.IsAny())).Returns((Task.FromResult(_subscriptionId))); + _postSubscriptionsHttpTriggerService.Setup(x => x.DoesCustomerExist(It.IsAny())).Returns(Task.FromResult(true)); + _postSubscriptionsHttpTriggerService.Setup(x => x.DoesSubscriptionExist(It.IsAny(), It.IsAny())).Returns((Task.FromResult(_subscriptionId))); _validate.Setup(x => x.ValidateResultForDuplicateSubscriptionId(It.IsAny())).Returns(await Task.FromResult(new SubscriptionError() { })); // Act @@ -171,10 +167,10 @@ public async Task PostSubscriptionsHttpTrigger_ReturnsStatusCodeBadRequest_WhenS _validationResults.Clear(); _validate.Setup(x => x.ValidateResource(It.IsAny())).Returns(_validationResults); - _resourceHelper.Setup(x => x.DoesCustomerExist(It.IsAny())).Returns(Task.FromResult(true)); - _resourceHelper.Setup(x => x.DoesSubscriptionExist(It.IsAny(), It.IsAny())).Returns(Task.FromResult(null)); + _postSubscriptionsHttpTriggerService.Setup(x => x.DoesCustomerExist(It.IsAny())).Returns(Task.FromResult(true)); + _postSubscriptionsHttpTriggerService.Setup(x => x.DoesSubscriptionExist(It.IsAny(), It.IsAny())).Returns(Task.FromResult(null)); - _postSubscriptionsHttpTriggerService.Setup(x => x.CreateAsync(_subscriptions)).Returns(Task.FromResult(null)); + _postSubscriptionsHttpTriggerService.Setup(x => x.CreateAsync(_subscriptions)).Returns(Task.FromResult(_subscriptions)); // Act var result = await RunFunction(ValidCustomerId); @@ -194,8 +190,8 @@ public async Task PostSubscriptionsHttpTrigger_ReturnsStatusCodeCreated_WhenRequ _validationResults.Clear(); _validate.Setup(x => x.ValidateResource(It.IsAny())).Returns(_validationResults); - _resourceHelper.Setup(x => x.DoesCustomerExist(It.IsAny())).Returns(Task.FromResult(true)); - _resourceHelper.Setup(x => x.DoesSubscriptionExist(It.IsAny(), It.IsAny())).Returns(Task.FromResult(null)); + _postSubscriptionsHttpTriggerService.Setup(x => x.DoesCustomerExist(It.IsAny())).Returns(Task.FromResult(true)); + _postSubscriptionsHttpTriggerService.Setup(x => x.DoesSubscriptionExist(It.IsAny(), It.IsAny())).Returns(Task.FromResult(null)); _postSubscriptionsHttpTriggerService.Setup(x => x.CreateAsync(_subscriptions)).Returns(Task.FromResult(_subscriptions)); diff --git a/NCS.DSS.Subscriptions.Tests/ModelTests/SubscriptionModelTests.cs b/NCS.DSS.Subscriptions.Tests/ModelTests/SubscriptionModelTests.cs index a04943b..5cdebc4 100644 --- a/NCS.DSS.Subscriptions.Tests/ModelTests/SubscriptionModelTests.cs +++ b/NCS.DSS.Subscriptions.Tests/ModelTests/SubscriptionModelTests.cs @@ -7,7 +7,7 @@ public class SubscriptionModelTests private Models.Subscriptions _subscriptions; private readonly Guid _customerId = Guid.Parse("1dd4d206-131a-44fd-8e2d-18b88b383f72"); private readonly Guid? _subscriptionId = Guid.Parse("0f0ce9cd-5f0e-41f7-84e7-6532e01691ae"); - private const string _touchPointId = "0000000001"; + private string _touchPointId = "0000000001"; [SetUp] public void Setup() { diff --git a/NCS.DSS.Subscriptions.Tests/ServiceTests/GetSubscriptionsForTouchpointHttpTriggerServiceTests.cs b/NCS.DSS.Subscriptions.Tests/ServiceTests/GetSubscriptionsForTouchpointHttpTriggerServiceTests.cs index 1864058..59c7a18 100644 --- a/NCS.DSS.Subscriptions.Tests/ServiceTests/GetSubscriptionsForTouchpointHttpTriggerServiceTests.cs +++ b/NCS.DSS.Subscriptions.Tests/ServiceTests/GetSubscriptionsForTouchpointHttpTriggerServiceTests.cs @@ -8,19 +8,24 @@ namespace NCS.DSS.Subscriptions.Tests.ServiceTests public class GetSubscriptionsForTouchpointHttpTriggerServiceTests { private readonly IGetSubscriptionsForTouchpointHttpTriggerService _getSubscriptionsForTouchpointHttpTriggerService; - private readonly Mock _documentDbProvider; + private readonly Mock _cosmosDbProvider; private readonly Guid _customerId = Guid.Parse("58b43e3f-4a50-4900-9c82-a14682ee90fa"); private const string _touchPointId = "0000000001"; public GetSubscriptionsForTouchpointHttpTriggerServiceTests() { - _documentDbProvider = new Mock(); - _getSubscriptionsForTouchpointHttpTriggerService = new GetSubscriptionsForTouchpointHttpTriggerService(_documentDbProvider.Object); + _cosmosDbProvider = new Mock(); + _getSubscriptionsForTouchpointHttpTriggerService = new GetSubscriptionsForTouchpointHttpTriggerService(_cosmosDbProvider.Object); } [Test] public async Task GetSubscriptionsForTouchpointHttpTriggerServiceTests_GetSubscriptionsForTouchpointAsync_ReturnsNullWhenResourceCannotBeFound() { // Arrange - _documentDbProvider.Setup(x => x.GetSubscriptionsForTouchpointAsync(_customerId, _touchPointId)).Returns(Task.FromResult>(null)); + var subscriptions = new List() + { + new() + }; + + _cosmosDbProvider.Setup(x => x.GetSubscriptionsForTouchpointAsync(_customerId, _touchPointId)).Returns(Task.FromResult(subscriptions)); // Act var result = await _getSubscriptionsForTouchpointHttpTriggerService.GetSubscriptionsForTouchpointAsync(_customerId, _touchPointId); @@ -32,7 +37,7 @@ public async Task GetSubscriptionsForTouchpointHttpTriggerServiceTests_GetSubscr public async Task GetSubscriptionsForTouchpointHttpTriggerServiceTests_GetSubscriptionsForTouchpointAsync_ReturnsResource() { // Arrange - _documentDbProvider.Setup(x => x.GetSubscriptionsForTouchpointAsync(_customerId, _touchPointId)).Returns(Task.FromResult(new List())); + _cosmosDbProvider.Setup(x => x.GetSubscriptionsForTouchpointAsync(_customerId, _touchPointId)).Returns(Task.FromResult(new List())); // Act var result = await _getSubscriptionsForTouchpointHttpTriggerService.GetSubscriptionsForTouchpointAsync(_customerId, _touchPointId); diff --git a/NCS.DSS.Subscriptions.Tests/ServiceTests/PatchSubscriptionsHttpTriggerServiceTests.cs b/NCS.DSS.Subscriptions.Tests/ServiceTests/PatchSubscriptionsHttpTriggerServiceTests.cs index a2a9ff7..e203fd9 100644 --- a/NCS.DSS.Subscriptions.Tests/ServiceTests/PatchSubscriptionsHttpTriggerServiceTests.cs +++ b/NCS.DSS.Subscriptions.Tests/ServiceTests/PatchSubscriptionsHttpTriggerServiceTests.cs @@ -1,12 +1,9 @@ -using Microsoft.Azure.Documents; -using Microsoft.Azure.Documents.Client; +using Microsoft.Azure.Cosmos; using Moq; using NCS.DSS.Subscriptions.Cosmos.Provider; using NCS.DSS.Subscriptions.Models; using NCS.DSS.Subscriptions.PatchSubscriptionsHttpTrigger.Service; -using System.Collections.Specialized; using System.Net; -using System.Reflection; namespace NCS.DSS.Subscriptions.Tests.ServiceTests { @@ -14,7 +11,7 @@ namespace NCS.DSS.Subscriptions.Tests.ServiceTests public class PatchSubscriptionsHttpTriggerServiceTests { private readonly IPatchSubscriptionsHttpTriggerService _patchSubscriptionsHttpTriggerService; - private readonly Mock _documentDbProvider; + private readonly Mock _cosmosDbProvider; private readonly Guid _customerId = Guid.Parse("58b43e3f-4a50-4900-9c82-a14682ee90fa"); private readonly Guid _subscriptionId = Guid.Parse("4C2DD229-DF9D-4899-AC09-A21DC13EB176"); private readonly Models.Subscriptions _subscriptions; @@ -23,8 +20,8 @@ public PatchSubscriptionsHttpTriggerServiceTests() { _subscriptions = new Models.Subscriptions(); _subscriptionsPatch = new SubscriptionsPatch(); - _documentDbProvider = new Mock(); - _patchSubscriptionsHttpTriggerService = new PatchSubscriptionsHttpTriggerService(_documentDbProvider.Object); + _cosmosDbProvider = new Mock(); + _patchSubscriptionsHttpTriggerService = new PatchSubscriptionsHttpTriggerService(_cosmosDbProvider.Object); } [Test] public async Task PatchSubscriptionsHttpTriggerServiceTests_UpdateAsync_ReturnsNullWhenResourceCannotBeFound() @@ -39,29 +36,10 @@ public async Task PatchSubscriptionsHttpTriggerServiceTests_UpdateAsync_ReturnsN public async Task PatchSubscriptionsHttpTriggerServiceTests_UpdateAsync_ReturnsResourceUpdated() { // Arrange - const string documentServiceResponseClass = "Microsoft.Azure.Documents.DocumentServiceResponse, Microsoft.Azure.DocumentDB.Core, Version=2.2.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"; - const string dictionaryNameValueCollectionClass = "Microsoft.Azure.Documents.Collections.DictionaryNameValueCollection, Microsoft.Azure.DocumentDB.Core, Version=2.2.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"; - - var resourceResponse = new ResourceResponse(new Document()); - var documentServiceResponseType = Type.GetType(documentServiceResponseClass); - - const BindingFlags flags = BindingFlags.NonPublic | BindingFlags.Instance; - - var headers = new NameValueCollection { { "x-ms-request-charge", "0" } }; - - var headersDictionaryType = Type.GetType(dictionaryNameValueCollectionClass); - - var headersDictionaryInstance = Activator.CreateInstance(headersDictionaryType, headers); - - var arguments = new[] { Stream.Null, headersDictionaryInstance, HttpStatusCode.OK, null }; - - var documentServiceResponse = documentServiceResponseType?.GetTypeInfo().GetConstructors(flags)[0].Invoke(arguments); - - var responseField = typeof(ResourceResponse).GetTypeInfo().GetField("response", flags); - - responseField?.SetValue(resourceResponse, documentServiceResponse); - - _documentDbProvider.Setup(x => x.UpdateSubscriptionsAsync(_subscriptions)).Returns(Task.FromResult(resourceResponse)); + var resourceResponse = new Mock>(); + resourceResponse.Setup(x => x.Resource).Returns(_subscriptions); + resourceResponse.Setup(x => x.StatusCode).Returns(HttpStatusCode.OK); + _cosmosDbProvider.Setup(x => x.UpdateSubscriptionsAsync(_subscriptions)).Returns(Task.FromResult(resourceResponse.Object)); // Act var result = await _patchSubscriptionsHttpTriggerService.UpdateAsync(_subscriptions, _subscriptionsPatch); @@ -75,7 +53,7 @@ public async Task PatchSubscriptionsHttpTriggerServiceTests_UpdateAsync_ReturnsR public async Task PatchSubscriptionsHttpTriggerServiceTests_GetSubscriptionsForCustomerAsync_ReturnsResourceWhenResourceHasBeenFound() { // Arrange - _documentDbProvider.Setup(x => x.GetSubscriptionsForCustomerAsync(It.IsAny(), It.IsAny())).Returns(Task.FromResult(_subscriptions)); + _cosmosDbProvider.Setup(x => x.GetSubscriptionsForCustomerAsync(It.IsAny(), It.IsAny())).Returns(Task.FromResult(_subscriptions)); // Act var result = await _patchSubscriptionsHttpTriggerService.GetSubscriptionsForCustomerAsync(_customerId, _subscriptionId); diff --git a/NCS.DSS.Subscriptions.Tests/ServiceTests/PostSubscriptionsHttpTriggerServiceTests.cs b/NCS.DSS.Subscriptions.Tests/ServiceTests/PostSubscriptionsHttpTriggerServiceTests.cs index 80ff1a6..89e6951 100644 --- a/NCS.DSS.Subscriptions.Tests/ServiceTests/PostSubscriptionsHttpTriggerServiceTests.cs +++ b/NCS.DSS.Subscriptions.Tests/ServiceTests/PostSubscriptionsHttpTriggerServiceTests.cs @@ -1,11 +1,8 @@ -using Microsoft.Azure.Documents; -using Microsoft.Azure.Documents.Client; +using Microsoft.Azure.Cosmos; using Moq; using NCS.DSS.Subscriptions.Cosmos.Provider; using NCS.DSS.Subscriptions.PostSubscriptionsHttpTrigger.Service; -using System.Collections.Specialized; using System.Net; -using System.Reflection; namespace NCS.DSS.Subscriptions.Tests.ServiceTests { @@ -13,14 +10,14 @@ namespace NCS.DSS.Subscriptions.Tests.ServiceTests public class PostSubscriptionsHttpTriggerServiceTests { private readonly IPostSubscriptionsHttpTriggerService _postSubscriptionsHttpTriggerService; - private readonly Mock _documentDbProvider; + private readonly Mock _cosmosDbProvider; private readonly Models.Subscriptions _subscriptions; public PostSubscriptionsHttpTriggerServiceTests() { _subscriptions = new Models.Subscriptions(); - _documentDbProvider = new Mock(); - _postSubscriptionsHttpTriggerService = new PostSubscriptionsHttpTriggerService(_documentDbProvider.Object); + _cosmosDbProvider = new Mock(); + _postSubscriptionsHttpTriggerService = new PostSubscriptionsHttpTriggerService(_cosmosDbProvider.Object); } [Test] public async Task PostSubscriptionsHttpTriggerServiceTests_CreateAsync_ReturnsNullWhenResourceCannotBeFound() @@ -35,29 +32,10 @@ public async Task PostSubscriptionsHttpTriggerServiceTests_CreateAsync_ReturnsNu public async Task PostSubscriptionsHttpTriggerServiceTests_CreateAsync_ReturnsResource() { // Arrange - const string documentServiceResponseClass = "Microsoft.Azure.Documents.DocumentServiceResponse, Microsoft.Azure.DocumentDB.Core, Version=2.2.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"; - const string dictionaryNameValueCollectionClass = "Microsoft.Azure.Documents.Collections.DictionaryNameValueCollection, Microsoft.Azure.DocumentDB.Core, Version=2.2.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"; - - var resourceResponse = new ResourceResponse(new Document()); - var documentServiceResponseType = Type.GetType(documentServiceResponseClass); - - const BindingFlags flags = BindingFlags.NonPublic | BindingFlags.Instance; - - var headers = new NameValueCollection { { "x-ms-request-charge", "0" } }; - - var headersDictionaryType = Type.GetType(dictionaryNameValueCollectionClass); - - var headersDictionaryInstance = Activator.CreateInstance(headersDictionaryType, headers); - - var arguments = new[] { Stream.Null, headersDictionaryInstance, HttpStatusCode.Created, null }; - - var documentServiceResponse = documentServiceResponseType?.GetTypeInfo().GetConstructors(flags)[0].Invoke(arguments); - - var responseField = typeof(ResourceResponse).GetTypeInfo().GetField("response", flags); - - responseField?.SetValue(resourceResponse, documentServiceResponse); - - _documentDbProvider.Setup(x => x.CreateSubscriptionsAsync(_subscriptions)).Returns(Task.FromResult(resourceResponse)); + var resourceResponse = new Mock>(); + resourceResponse.Setup(x => x.Resource).Returns(_subscriptions); + resourceResponse.Setup(x => x.StatusCode).Returns(HttpStatusCode.Created); + _cosmosDbProvider.Setup(x => x.CreateSubscriptionsAsync(_subscriptions)).Returns(Task.FromResult(resourceResponse.Object)); // Act var result = await _postSubscriptionsHttpTriggerService.CreateAsync(_subscriptions); diff --git a/NCS.DSS.Subscriptions/AppInsights/ApplicationInsightsLogger.cs b/NCS.DSS.Subscriptions/AppInsights/ApplicationInsightsLogger.cs deleted file mode 100644 index 3cce459..0000000 --- a/NCS.DSS.Subscriptions/AppInsights/ApplicationInsightsLogger.cs +++ /dev/null @@ -1,108 +0,0 @@ -using Microsoft.ApplicationInsights; -using Microsoft.ApplicationInsights.DataContracts; -using Microsoft.ApplicationInsights.Extensibility; -using Microsoft.Extensions.Logging; - -namespace NCS.DSS.Subscriptions.AppInsights -{ - public class ApplicationInsightsLogger : ILogger - { - private readonly string _name; - private readonly Func _filter; - private readonly ApplicationInsightsSettings _settings; - private readonly TelemetryClient _telemetryClient; - - public ApplicationInsightsLogger(string name) - : this(name, null, new ApplicationInsightsSettings()) - { - } - - public ApplicationInsightsLogger(string name, Func filter, ApplicationInsightsSettings settings) - { - _name = string.IsNullOrEmpty(name) ? nameof(ApplicationInsightsLogger) : name; - _filter = filter; - _settings = settings; - _telemetryClient = new TelemetryClient(TelemetryConfiguration.CreateDefault()); - - if (_settings.LocalEnvironmentMode.HasValue) - { - TelemetryConfiguration.CreateDefault().TelemetryChannel.DeveloperMode = _settings.LocalEnvironmentMode; - } - - if (!_settings.LocalEnvironmentMode.Value) - { - if (string.IsNullOrWhiteSpace(_settings.InstrumentationKey)) - { - throw new ArgumentNullException(nameof(_settings.InstrumentationKey)); - } - - TelemetryConfiguration.CreateDefault().ConnectionString = _settings.InstrumentationKey; - _telemetryClient.InstrumentationKey = _settings.InstrumentationKey; - } - } - - public IDisposable BeginScope(TState state) - { - return NoopDisposable.Instance; - } - - public bool IsEnabled(LogLevel logLevel) - { - return (_filter == null || _filter(_name, logLevel)); - } - - public void Log(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func formatter) - { - if (!IsEnabled(logLevel)) - { - return; - } - - if (exception != null) - { - _telemetryClient.TrackException(new ExceptionTelemetry(exception)); - return; - } - - var message = string.Empty; - if (formatter != null) - { - message = formatter(state, exception); - } - else - { - if (state != null) - { - message += state; - } - } - if (!string.IsNullOrEmpty(message)) - { - _telemetryClient.TrackTrace(message, GetSeverityLevel(logLevel)); - } - } - - - private static SeverityLevel GetSeverityLevel(LogLevel logLevel) - { - switch (logLevel) - { - case LogLevel.Critical: return SeverityLevel.Critical; - case LogLevel.Error: return SeverityLevel.Error; - case LogLevel.Warning: return SeverityLevel.Warning; - case LogLevel.Information: return SeverityLevel.Information; - case LogLevel.Trace: - default: return SeverityLevel.Verbose; - } - } - - private class NoopDisposable : IDisposable - { - public static NoopDisposable Instance = new NoopDisposable(); - - public void Dispose() - { - } - } - } -} diff --git a/NCS.DSS.Subscriptions/AppInsights/ApplicationInsightsLoggerFactoryExtensions.cs b/NCS.DSS.Subscriptions/AppInsights/ApplicationInsightsLoggerFactoryExtensions.cs deleted file mode 100644 index 163c3a7..0000000 --- a/NCS.DSS.Subscriptions/AppInsights/ApplicationInsightsLoggerFactoryExtensions.cs +++ /dev/null @@ -1,27 +0,0 @@ -using ApplicationInsightsLogging; -using Microsoft.Extensions.Logging; - -namespace NCS.DSS.Subscriptions.AppInsights -{ - public static class ApplicationInsightsLoggerFactoryExtensions - { - public static ILoggerFactory AddApplicationInsights( - this ILoggerFactory factory, - Func filter, - ApplicationInsightsSettings settings) - { - factory.AddProvider(new ApplicationInsightsLoggerProvider(filter, settings)); - return factory; - } - - public static ILoggerFactory AddApplicationInsights( - this ILoggerFactory factory, - ApplicationInsightsSettings settings) - { - factory.AddProvider(new ApplicationInsightsLoggerProvider(null, settings)); - - return factory; - } - - } -} diff --git a/NCS.DSS.Subscriptions/AppInsights/ApplicationInsightsLoggerProvider.cs b/NCS.DSS.Subscriptions/AppInsights/ApplicationInsightsLoggerProvider.cs deleted file mode 100644 index 9d1a184..0000000 --- a/NCS.DSS.Subscriptions/AppInsights/ApplicationInsightsLoggerProvider.cs +++ /dev/null @@ -1,27 +0,0 @@ -using Microsoft.Extensions.Logging; -using NCS.DSS.Subscriptions.AppInsights; - -namespace ApplicationInsightsLogging -{ - public class ApplicationInsightsLoggerProvider : ILoggerProvider - { - private readonly Func _filter; - private readonly ApplicationInsightsSettings _settings; - - public ApplicationInsightsLoggerProvider(Func filter, ApplicationInsightsSettings settings) - { - _filter = filter; - _settings = settings; - } - - public ILogger CreateLogger(string name) - { - return new ApplicationInsightsLogger(name, _filter, _settings); - } - - public void Dispose() - { - - } - } -} diff --git a/NCS.DSS.Subscriptions/AppInsights/ApplicationInsightsSettings.cs b/NCS.DSS.Subscriptions/AppInsights/ApplicationInsightsSettings.cs deleted file mode 100644 index 2c79984..0000000 --- a/NCS.DSS.Subscriptions/AppInsights/ApplicationInsightsSettings.cs +++ /dev/null @@ -1,10 +0,0 @@ - -namespace NCS.DSS.Subscriptions.AppInsights -{ - public class ApplicationInsightsSettings - { - public bool? LocalEnvironmentMode { get; set; } - - public string InstrumentationKey { get; set; } - } -} diff --git a/NCS.DSS.Subscriptions/Cosmos/Client/DocumentDBClient.cs b/NCS.DSS.Subscriptions/Cosmos/Client/DocumentDBClient.cs deleted file mode 100644 index 2febd43..0000000 --- a/NCS.DSS.Subscriptions/Cosmos/Client/DocumentDBClient.cs +++ /dev/null @@ -1,21 +0,0 @@ -using Microsoft.Azure.Documents.Client; - -namespace NCS.DSS.Subscriptions.Cosmos.Client -{ - public static class DocumentDBClient - { - private static DocumentClient _documentClient; - - public static DocumentClient CreateDocumentClient() - { - if (_documentClient != null) - return _documentClient; - - _documentClient = new DocumentClient(new Uri( - Environment.GetEnvironmentVariable("Endpoint")), - Environment.GetEnvironmentVariable("Key")); - - return _documentClient; - } - } -} diff --git a/NCS.DSS.Subscriptions/Cosmos/Helper/DocumentDBHelper.cs b/NCS.DSS.Subscriptions/Cosmos/Helper/DocumentDBHelper.cs deleted file mode 100644 index 89ff65d..0000000 --- a/NCS.DSS.Subscriptions/Cosmos/Helper/DocumentDBHelper.cs +++ /dev/null @@ -1,52 +0,0 @@ -using Microsoft.Azure.Documents.Client; - -namespace NCS.DSS.Subscriptions.Cosmos.Helper -{ - public static class DocumentDBHelper - { - private static Uri _documentCollectionUri; - private static readonly string DatabaseId = Environment.GetEnvironmentVariable("DatabaseId"); - private static readonly string CollectionId = Environment.GetEnvironmentVariable("CollectionId"); - - private static Uri _customerDocumentCollectionUri; - private static readonly string CustomerDatabaseId = Environment.GetEnvironmentVariable("CustomerDatabaseId"); - private static readonly string CustomerCollectionId = Environment.GetEnvironmentVariable("CustomerCollectionId"); - - public static Uri CreateDocumentCollectionUri() - { - if (_documentCollectionUri != null) - return _documentCollectionUri; - - _documentCollectionUri = UriFactory.CreateDocumentCollectionUri( - DatabaseId, - CollectionId); - - return _documentCollectionUri; - } - - public static Uri CreateDocumentUri(Guid? subscriptionId) - { - return UriFactory.CreateDocumentUri(DatabaseId, CollectionId, subscriptionId.ToString()); - } - - #region CustomerDB - - public static Uri CreateCustomerDocumentCollectionUri() - { - if (_customerDocumentCollectionUri != null) - return _customerDocumentCollectionUri; - - _customerDocumentCollectionUri = UriFactory.CreateDocumentCollectionUri( - CustomerDatabaseId, CustomerCollectionId); - - return _customerDocumentCollectionUri; - } - - public static Uri CreateCustomerDocumentUri(Guid customerId) - { - return UriFactory.CreateDocumentUri(CustomerDatabaseId, CustomerCollectionId, customerId.ToString()); - } - - #endregion - } -} diff --git a/NCS.DSS.Subscriptions/Cosmos/Helper/IResourceHelper.cs b/NCS.DSS.Subscriptions/Cosmos/Helper/IResourceHelper.cs deleted file mode 100644 index 06cbcff..0000000 --- a/NCS.DSS.Subscriptions/Cosmos/Helper/IResourceHelper.cs +++ /dev/null @@ -1,8 +0,0 @@ -namespace NCS.DSS.Subscriptions.Cosmos.Helper -{ - public interface IResourceHelper - { - Task DoesCustomerExist(Guid customerId); - Task DoesSubscriptionExist(Guid customerId, string touchpointId); - } -} \ No newline at end of file diff --git a/NCS.DSS.Subscriptions/Cosmos/Helper/ResourceHelper.cs b/NCS.DSS.Subscriptions/Cosmos/Helper/ResourceHelper.cs deleted file mode 100644 index 723da19..0000000 --- a/NCS.DSS.Subscriptions/Cosmos/Helper/ResourceHelper.cs +++ /dev/null @@ -1,23 +0,0 @@ -using NCS.DSS.Subscriptions.Cosmos.Provider; - -namespace NCS.DSS.Subscriptions.Cosmos.Helper -{ - public class ResourceHelper : IResourceHelper - { - public async Task DoesCustomerExist(Guid customerId) - { - var documentDbProvider = new DocumentDBProvider(); - var doesCustomerExist = await documentDbProvider.DoesCustomerResourceExist(customerId); - - return doesCustomerExist; - } - - public async Task DoesSubscriptionExist(Guid customerId, string touchpointId) - { - var documentDbProvider = new DocumentDBProvider(); - var doesSubscriptionExist = await documentDbProvider.DoesSubscriptionExist(customerId, touchpointId); - - return doesSubscriptionExist; - } - } -} diff --git a/NCS.DSS.Subscriptions/Cosmos/Provider/CosmosDBProvider.cs b/NCS.DSS.Subscriptions/Cosmos/Provider/CosmosDBProvider.cs new file mode 100644 index 0000000..b0dd0bd --- /dev/null +++ b/NCS.DSS.Subscriptions/Cosmos/Provider/CosmosDBProvider.cs @@ -0,0 +1,206 @@ +using Microsoft.Azure.Cosmos; +using Microsoft.Azure.Cosmos.Linq; +using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Options; +using NCS.DSS.Subscriptions.Models; +using System.Net; + +namespace NCS.DSS.Subscriptions.Cosmos.Provider +{ + public class CosmosDBProvider : ICosmosDBProvider + { + private readonly Container _container; + private readonly Container _customerContainer; + private readonly PartitionKey _partitionKey = PartitionKey.None; + private readonly ILogger _logger; + public CosmosDBProvider( + CosmosClient cosmosClient, + IOptions configOptions, + ILogger logger) + { + var config = configOptions.Value; + _container = GetContainer(cosmosClient, config.DatabaseId, config.CollectionId); + _customerContainer = GetContainer(cosmosClient, config.CustomerDatabaseId, config.CustomerCollectionId); + _logger = logger; + } + private static Container GetContainer(CosmosClient cosmosClient, string databaseId, string collectionId) + => cosmosClient.GetContainer(databaseId, collectionId); + public async Task DoesCustomerResourceExist(Guid customerId) + { + try + { + var queryCust = _customerContainer.GetItemLinqQueryable().Where(x => x.id == customerId).ToFeedIterator(); + + while (queryCust.HasMoreResults) + { + var response = await queryCust.ReadNextAsync(); + if (response != null && response.Resource.Any()) + { + _logger.LogInformation("Customer Record found in Cosmos DB for {CustomerID}", customerId); + return true; + } + } + _logger.LogWarning("No Customer Record found with {CustomerID} in Cosmos DB", customerId); + return false; + } + catch (CosmosException ce) + { + _logger.LogError(ce, "Failed to find the Customer Record in Cosmos DB {CustomerID}. Exception {Exception}.", customerId, ce.Message); + throw; + } + } + + public async Task DoesSubscriptionExist(Guid customerId, string touchpointId) + { + try + { + var querySubs = _container.GetItemLinqQueryable() + .Where(x => x.CustomerId == customerId && x.TouchPointId == touchpointId).ToFeedIterator(); + + while (querySubs.HasMoreResults) + { + var response = await querySubs.ReadNextAsync(); + if (response != null && response.Resource.Any()) + { + _logger.LogInformation("Subscription Record found with Touchpoint ID {touchpointId} in Cosmos DB for Customer with ID {CustomerID}", touchpointId, customerId); + return response.Resource.FirstOrDefault().SubscriptionId; + } + } + _logger.LogWarning("No Subscription found with Touchpoint ID {touchpointId} and Customer ID {CustomerID} in Cosmos DB", touchpointId, customerId); + return null; + } + catch (CosmosException ce) + { + _logger.LogError(ce, "Failed to find the Subscription Record in Cosmos DB {CustomerID}. Exception {Exception}", customerId, ce.Message); + throw; + } + } + + public async Task> SearchAllSubscriptions() + { + try + { + var querySubs = _container.GetItemLinqQueryable().ToFeedIterator(); + + while (querySubs.HasMoreResults) + { + var response = await querySubs.ReadNextAsync(); + if (response != null && response.Resource.Any()) + { + _logger.LogInformation("Retrieved All Subscriptions in Cosmos Database"); + return response.Resource.ToList(); + } + } + _logger.LogWarning("No Subscriptions found in Cosmos DB"); + return null; + } + catch (CosmosException ce) + { + _logger.LogError(ce, "Failed to retrieve all Subscriptions in Cosmos DB . Exception {Exception}", ce.Message); + throw; + } + } + + + public async Task GetSubscriptionsForCustomerAsync(Guid? customerId, Guid? subscriptionId) + { + try + { + var querySubs = _container.GetItemLinqQueryable() + .Where(x => x.CustomerId == customerId && x.SubscriptionId == subscriptionId).ToFeedIterator(); + + while (querySubs.HasMoreResults) + { + var response = await querySubs.ReadNextAsync(); + if (response != null && response.Resource.Any()) + { + _logger.LogInformation("Subscription Record found with ID {subscriptionId} in Cosmos DB for Customer with ID {CustomerID}", subscriptionId, customerId); + return response.Resource.FirstOrDefault(); + } + } + _logger.LogWarning("No Subscription found with ID {subscriptionId} and Customer ID {CustomerID} in Cosmos DB", subscriptionId, customerId); + return null; + } + catch (CosmosException ce) + { + _logger.LogError(ce, "Failed to find the Subscription Record in Cosmos DB {CustomerID}. Exception {Exception}", customerId, ce.Message); + throw; + } + } + + + public async Task> GetSubscriptionsForTouchpointAsync(Guid? customerId, string touchpointId) + { + try + { + var querySubs = _container.GetItemLinqQueryable() + .Where(x => x.CustomerId == customerId && x.TouchPointId == touchpointId).ToFeedIterator(); + + while (querySubs.HasMoreResults) + { + var response = await querySubs.ReadNextAsync(); + if (response != null && response.Resource.Any()) + { + _logger.LogInformation("Subscriptions found with Touchpoint ID {touchpointId} in Cosmos DB for Customer with ID {CustomerID}", touchpointId, customerId); + return response.Resource.ToList(); + } + } + _logger.LogWarning("No Subscriptions found with Touchpoint ID {touchpointId} and Customer ID {CustomerID} in Cosmos DB", touchpointId, customerId); + return null; + } + catch (CosmosException ce) + { + _logger.LogError(ce, "Failed to find the Subscriptions in Cosmos DB {CustomerID}. Exception {Exception}", customerId, ce.Message); + throw; + } + } + + + public async Task> CreateSubscriptionsAsync(Models.Subscriptions subscriptions) + { + try + { + var response = await _container.CreateItemAsync(subscriptions, null); + if (response.StatusCode == HttpStatusCode.Created) + { + _logger.LogInformation("Subscription Record Created in Cosmos DB for {SubscriptionId}", subscriptions.SubscriptionId); + } + else + { + _logger.LogError("Failed and returned {StatusCode} to CreateSubscription Record in Cosmos DB for {SubscriptionId}", response.StatusCode, subscriptions.SubscriptionId); + } + return response; + } + catch (CosmosException ce) + { + _logger.LogError(ce, "Failed to CreateSubscription Record in Cosmos DB {SubscriptionId}. Exception {Exception}.", subscriptions.SubscriptionId, ce.Message); + throw; + } + + } + + public async Task> UpdateSubscriptionsAsync(Models.Subscriptions subscriptions) + { + var subscriptionId = subscriptions.SubscriptionId; + try + { + var response = await _container.ReplaceItemAsync(subscriptions, subscriptionId.ToString()); + if (response.StatusCode == HttpStatusCode.OK) + { + _logger.LogInformation("Session Record Updated in Cosmos DB for {SubscriptionId}", subscriptionId); + } + else + { + _logger.LogError("Failed and returned {StatusCode} to Update Session Record in Cosmos DB for {SubscriptionId}", response.StatusCode, subscriptionId); + } + return response; + } + catch (CosmosException ce) + { + _logger.LogError(ce, "Failed to Update Session Record in Cosmos DB {SubscriptionId}. Exception {Exception}.", subscriptionId, ce.Message); + throw; + } + } + + } +} \ No newline at end of file diff --git a/NCS.DSS.Subscriptions/Cosmos/Provider/DocumentDBProvider.cs b/NCS.DSS.Subscriptions/Cosmos/Provider/DocumentDBProvider.cs deleted file mode 100644 index f10c998..0000000 --- a/NCS.DSS.Subscriptions/Cosmos/Provider/DocumentDBProvider.cs +++ /dev/null @@ -1,152 +0,0 @@ -using Microsoft.Azure.Documents; -using Microsoft.Azure.Documents.Client; -using Microsoft.Azure.Documents.Linq; -using NCS.DSS.Subscriptions.Cosmos.Client; -using NCS.DSS.Subscriptions.Cosmos.Helper; - -namespace NCS.DSS.Subscriptions.Cosmos.Provider -{ - public class DocumentDBProvider : IDocumentDBProvider - { - public async Task DoesCustomerResourceExist(Guid customerId) - { - var documentUri = DocumentDBHelper.CreateCustomerDocumentUri(customerId); - - var client = DocumentDBClient.CreateDocumentClient(); - - if (client == null) - return false; - try - { - var response = await client.ReadDocumentAsync(documentUri); - if (response.Resource != null) - return true; - } - catch (DocumentClientException) - { - return false; - } - - return false; - } - - public async Task DoesSubscriptionExist(Guid customerId, string touchpointId) - { - var collectionUri = DocumentDBHelper.CreateDocumentCollectionUri(); - - var client = DocumentDBClient.CreateDocumentClient(); - - var subscriptionsByIdQuery = client?.CreateDocumentQuery(collectionUri, new FeedOptions { MaxItemCount = 1 }) - .Where(x => x.CustomerId == customerId && - x.TouchPointId == touchpointId && - x.Subscribe == true).AsDocumentQuery(); ; - - if (subscriptionsByIdQuery == null) - return null; - - var subscription = await subscriptionsByIdQuery.ExecuteNextAsync(); - - return subscription?.FirstOrDefault()?.SubscriptionId; - } - - public async Task> SearchAllSubscriptions() - { - var collectionUri = DocumentDBHelper.CreateDocumentCollectionUri(); - - var client = DocumentDBClient.CreateDocumentClient(); - - if (client == null) - return null; - - var queryCust = client.CreateDocumentQuery(collectionUri) - .AsDocumentQuery(); - - var subscriptions = new List(); - - while (queryCust.HasMoreResults) - { - var response = await queryCust.ExecuteNextAsync(); - subscriptions.AddRange(response); - } - - return subscriptions.Any() ? subscriptions : null; - } - - - public async Task GetSubscriptionsForCustomerAsync(Guid? customerId, Guid? subscriptionId) - { - var collectionUri = DocumentDBHelper.CreateDocumentCollectionUri(); - - var client = DocumentDBClient.CreateDocumentClient(); - - var subscriptionsByIdQuery = client - ?.CreateDocumentQuery(collectionUri, new FeedOptions { MaxItemCount = 1 }) - .Where(x => x.SubscriptionId == subscriptionId && x.CustomerId == customerId) - .AsDocumentQuery(); - - if (subscriptionsByIdQuery == null) - return null; - - var customer = await subscriptionsByIdQuery.ExecuteNextAsync(); - - return customer?.FirstOrDefault(); - } - - - public async Task> GetSubscriptionsForTouchpointAsync(Guid? customerId, string touchpointId) - { - var collectionUri = DocumentDBHelper.CreateDocumentCollectionUri(); - - var client = DocumentDBClient.CreateDocumentClient(); - - var subscriptionsForTouchpointQuery = client - ?.CreateDocumentQuery(collectionUri) - .Where(x => x.CustomerId == customerId && x.TouchPointId == touchpointId) - .AsDocumentQuery(); - - if (subscriptionsForTouchpointQuery == null) - return null; - - var subscriptions = new List(); - - while (subscriptionsForTouchpointQuery.HasMoreResults) - { - var response = await subscriptionsForTouchpointQuery.ExecuteNextAsync(); - subscriptions.AddRange(response); - } - - return subscriptions.Any() ? subscriptions : null; - } - - - public async Task> CreateSubscriptionsAsync(Models.Subscriptions subscriptions) - { - var collectionUri = DocumentDBHelper.CreateDocumentCollectionUri(); - - var client = DocumentDBClient.CreateDocumentClient(); - - if (client == null) - return null; - - var response = await client.CreateDocumentAsync(collectionUri, subscriptions); - - return response; - - } - - public async Task> UpdateSubscriptionsAsync(Models.Subscriptions subscriptions) - { - var documentUri = DocumentDBHelper.CreateDocumentUri(subscriptions.SubscriptionId); - - var client = DocumentDBClient.CreateDocumentClient(); - - if (client == null) - return null; - - var response = await client.ReplaceDocumentAsync(documentUri, subscriptions); - - return response; - } - - } -} \ No newline at end of file diff --git a/NCS.DSS.Subscriptions/Cosmos/Provider/IDocumentDBProvider.cs b/NCS.DSS.Subscriptions/Cosmos/Provider/ICosmosDBProvider.cs similarity index 60% rename from NCS.DSS.Subscriptions/Cosmos/Provider/IDocumentDBProvider.cs rename to NCS.DSS.Subscriptions/Cosmos/Provider/ICosmosDBProvider.cs index 96430e7..19bca70 100644 --- a/NCS.DSS.Subscriptions/Cosmos/Provider/IDocumentDBProvider.cs +++ b/NCS.DSS.Subscriptions/Cosmos/Provider/ICosmosDBProvider.cs @@ -1,16 +1,15 @@ -using Microsoft.Azure.Documents; -using Microsoft.Azure.Documents.Client; +using Microsoft.Azure.Cosmos; namespace NCS.DSS.Subscriptions.Cosmos.Provider { - public interface IDocumentDBProvider + public interface ICosmosDBProvider { Task DoesCustomerResourceExist(Guid customerId); Task DoesSubscriptionExist(Guid customerId, string touchpointId); Task> SearchAllSubscriptions(); Task GetSubscriptionsForCustomerAsync(Guid? customerId, Guid? subscriptionId); - Task> CreateSubscriptionsAsync(Models.Subscriptions subscriptions); - Task> UpdateSubscriptionsAsync(Models.Subscriptions subscriptions); + Task> CreateSubscriptionsAsync(Models.Subscriptions subscriptions); + Task> UpdateSubscriptionsAsync(Models.Subscriptions subscriptions); Task> GetSubscriptionsForTouchpointAsync(Guid? customerId, string touchpointId); } } \ No newline at end of file diff --git a/NCS.DSS.Subscriptions/GetSubscriptionsByIdHttpTrigger/Function/GetSubscriptionsByIdHttpTrigger.cs b/NCS.DSS.Subscriptions/GetSubscriptionsByIdHttpTrigger/Function/GetSubscriptionsByIdHttpTrigger.cs deleted file mode 100644 index 621e1bb..0000000 --- a/NCS.DSS.Subscriptions/GetSubscriptionsByIdHttpTrigger/Function/GetSubscriptionsByIdHttpTrigger.cs +++ /dev/null @@ -1,61 +0,0 @@ -using DFC.HTTP.Standard; -using NCS.DSS.Subscriptions.Cosmos.Helper; -using NCS.DSS.Subscriptions.GetSubscriptionsByIdHttpTrigger.Service; - -namespace NCS.DSS.Subscriptions.GetSubscriptionsByIdHttpTrigger.Function -{ - public class GetSubscriptionsByIdHttpTrigger - { - private readonly IResourceHelper _resourceHelper; - private readonly IGetSubscriptionsByIdHttpTriggerService _getSubscriptionsByIdService; - private readonly IHttpResponseMessageHelper _httpResponseMessageHelper; - - public GetSubscriptionsByIdHttpTrigger(IResourceHelper resourceHelper, IGetSubscriptionsByIdHttpTriggerService getSubscriptionsByIdService, IHttpResponseMessageHelper httpResponseMessageHelper) - { - _resourceHelper = resourceHelper; - _getSubscriptionsByIdService = getSubscriptionsByIdService; - _httpResponseMessageHelper = httpResponseMessageHelper; - } - - //[Function("GetById")] - //[ProducesResponseType(typeof(Models.Subscriptions), (int)HttpStatusCode.OK)] - //[Response(HttpStatusCode = (int)HttpStatusCode.OK, Description = "Subscriptions found", ShowSchema = true)] - //[Response(HttpStatusCode = (int)HttpStatusCode.NoContent, Description = "Subscriptions does not exist", ShowSchema = false)] - //[Response(HttpStatusCode = (int)HttpStatusCode.BadRequest, Description = "Request was malformed", ShowSchema = false)] - //[Response(HttpStatusCode = (int)HttpStatusCode.Unauthorized, Description = "API key is unknown or invalid", ShowSchema = false)] - //[Response(HttpStatusCode = (int)HttpStatusCode.Forbidden, Description = "Insufficient access", ShowSchema = false)] - //[Display(Name = "Get", Description = "Ability to retrieve a single subscriptions with a given SubscriptionsId for an individual customer.")] - //[Disable] - //public async Task Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "Customers/{customerId}/Subscriptions/{subscriptionId}")]HttpRequest req, ILogger log, string customerId, string subscriptionId) - //{ - // log.LogInformation("C# HTTP trigger function processed a request."); - - // if (!Guid.TryParse(customerId, out var customerGuid)) - // { - // log.LogWarning($"GetSubscriptionsByIdHttpTrigger customerId {customerId} BadRequest"); - // return _httpResponseMessageHelper.BadRequest(customerGuid); - // } - - // if (!Guid.TryParse(subscriptionId, out var subscriptionsGuid)) - // { - // log.LogWarning($"GetSubscriptionsByIdHttpTrigger subscriptionId {subscriptionId} BadRequest"); - // return _httpResponseMessageHelper.BadRequest(subscriptionsGuid); - // } - - // var doesCustomerExist = await _resourceHelper.DoesCustomerExist(customerGuid); - - // if (!doesCustomerExist) - // { - // log.LogWarning($"GetSubscriptionsByIdHttpTrigger customerGuid {customerGuid} NoContent"); - // return _httpResponseMessageHelper.NoContent(customerGuid); - // } - - // var subscriptions = await _getSubscriptionsByIdService.GetSubscriptionsForCustomerAsync(customerGuid, subscriptionsGuid); - // log.LogInformation($"GetSubscriptionsByIdHttpTrigger _getSubscriptionsByIdService customerGuid {customerGuid} subscriptionsGuid {subscriptionsGuid}"); - - // return subscriptions == null ? - // _httpResponseMessageHelper.NoContent(subscriptionsGuid) : - // _httpResponseMessageHelper.Ok(JsonHelper.SerializeObject(subscriptions)); - //} - } -} \ No newline at end of file diff --git a/NCS.DSS.Subscriptions/GetSubscriptionsByIdHttpTrigger/Service/GetSubscriptionsByIdHttpTriggerService.cs b/NCS.DSS.Subscriptions/GetSubscriptionsByIdHttpTrigger/Service/GetSubscriptionsByIdHttpTriggerService.cs deleted file mode 100644 index dc19ae5..0000000 --- a/NCS.DSS.Subscriptions/GetSubscriptionsByIdHttpTrigger/Service/GetSubscriptionsByIdHttpTriggerService.cs +++ /dev/null @@ -1,15 +0,0 @@ -using NCS.DSS.Subscriptions.Cosmos.Provider; - -namespace NCS.DSS.Subscriptions.GetSubscriptionsByIdHttpTrigger.Service -{ - public class GetSubscriptionsByIdHttpTriggerService : IGetSubscriptionsByIdHttpTriggerService - { - public async Task GetSubscriptionsForCustomerAsync(Guid customerId, Guid subscriptionId) - { - var documentDbProvider = new DocumentDBProvider(); - var subscriptions = await documentDbProvider.GetSubscriptionsForCustomerAsync(customerId, subscriptionId); - - return subscriptions; - } - } -} \ No newline at end of file diff --git a/NCS.DSS.Subscriptions/GetSubscriptionsByIdHttpTrigger/Service/IGetSubscriptionsByIdHttpTriggerService.cs b/NCS.DSS.Subscriptions/GetSubscriptionsByIdHttpTrigger/Service/IGetSubscriptionsByIdHttpTriggerService.cs deleted file mode 100644 index 0b25a92..0000000 --- a/NCS.DSS.Subscriptions/GetSubscriptionsByIdHttpTrigger/Service/IGetSubscriptionsByIdHttpTriggerService.cs +++ /dev/null @@ -1,7 +0,0 @@ -namespace NCS.DSS.Subscriptions.GetSubscriptionsByIdHttpTrigger.Service -{ - public interface IGetSubscriptionsByIdHttpTriggerService - { - Task GetSubscriptionsForCustomerAsync(Guid customerId, Guid subscriptionId); - } -} \ No newline at end of file diff --git a/NCS.DSS.Subscriptions/GetSubscriptionsByIdHttpTrigger/function.json b/NCS.DSS.Subscriptions/GetSubscriptionsByIdHttpTrigger/function.json deleted file mode 100644 index 8ec9773..0000000 --- a/NCS.DSS.Subscriptions/GetSubscriptionsByIdHttpTrigger/function.json +++ /dev/null @@ -1,19 +0,0 @@ -{ - "bindings": [ - { - "authLevel": "function", - "direction": "in", - "methods": [ "get" ], - "name": "req", - "type": "httpTrigger" - }, - { - "name": "$return", - "type": "http", - "direction": "out" - } - ], - "disabled": false, - "entryPoint": "NCS.DSS.Subscriptions.GetSubscriptionsByIdHttpTrigger.Run", - "scriptFile": "..\\bin\\NCS.DSS.Subscriptions.dll" -} \ No newline at end of file diff --git a/NCS.DSS.Subscriptions/GetSubscriptionsForTouchpointHttpTrigger/Function/GetSubscriptionsForTouchpointHttpTrigger.cs b/NCS.DSS.Subscriptions/GetSubscriptionsForTouchpointHttpTrigger/Function/GetSubscriptionsForTouchpointHttpTrigger.cs index 80a0b7f..b9a8d7b 100644 --- a/NCS.DSS.Subscriptions/GetSubscriptionsForTouchpointHttpTrigger/Function/GetSubscriptionsForTouchpointHttpTrigger.cs +++ b/NCS.DSS.Subscriptions/GetSubscriptionsForTouchpointHttpTrigger/Function/GetSubscriptionsForTouchpointHttpTrigger.cs @@ -4,7 +4,6 @@ using Microsoft.AspNetCore.Mvc; using Microsoft.Azure.Functions.Worker; using Microsoft.Extensions.Logging; -using NCS.DSS.Subscriptions.Cosmos.Helper; using NCS.DSS.Subscriptions.GetSubscriptionsForTouchpointHttpTrigger.Service; using System.ComponentModel.DataAnnotations; using System.Net; @@ -14,21 +13,18 @@ namespace NCS.DSS.Subscriptions.GetSubscriptionsForTouchpointHttpTrigger.Functio { public class GetSubscriptionsForTouchpointHttpTrigger { - private readonly IResourceHelper _resourceHelper; private readonly IHttpRequestHelper _httpRequestMessageHelper; private readonly IGetSubscriptionsForTouchpointHttpTriggerService _getSubscriptionsForTouchpointService; - private readonly ILogger _loggerHelper; + private readonly ILogger _logger; public GetSubscriptionsForTouchpointHttpTrigger( - IResourceHelper resourceHelper, IHttpRequestHelper httpRequestMessageHelper, IGetSubscriptionsForTouchpointHttpTriggerService getSubscriptionsForTouchpointService, - ILogger loggerHelper) + ILogger logger) { - _resourceHelper = resourceHelper; _httpRequestMessageHelper = httpRequestMessageHelper; _getSubscriptionsForTouchpointService = getSubscriptionsForTouchpointService; - _loggerHelper = loggerHelper; + _logger = logger; } [Function("GetByTouchpoint")] @@ -44,22 +40,28 @@ public async Task Run([HttpTrigger(AuthorizationLevel.Anonymous, var touchpointId = _httpRequestMessageHelper.GetDssTouchpointId(req); if (string.IsNullOrEmpty(touchpointId)) { - _loggerHelper.LogInformation("Unable to locate 'APIM-TouchpointId' in request header"); + _logger.LogInformation("Unable to locate 'APIM-TouchpointId' in request header"); return new BadRequestResult(); } if (!Guid.TryParse(customerId, out var customerGuid)) { - _loggerHelper.LogWarning($"GetSubscriptionsForTouchpointHttpTrigger Customers/{customerId}/Subscriptions/ BadRequest"); + _logger.LogWarning($"GetSubscriptionsForTouchpointHttpTrigger Customers/{customerId}/Subscriptions/ BadRequest"); return new BadRequestObjectResult(customerGuid); } + var doesCustomerExist = await _getSubscriptionsForTouchpointService.DoesCustomerExist(customerGuid); + if (!doesCustomerExist) + { + _logger.LogError($"PostSubscriptionsHttpTrigger Customers/{customerId}/Subscriptions does notvCustomerExist "); + return new NoContentResult(); + } var subscriptions = await _getSubscriptionsForTouchpointService.GetSubscriptionsForTouchpointAsync(customerGuid, touchpointId); - _loggerHelper.LogInformation($"GetSubscriptionsForTouchpointHttpTrigger Customers/{customerId}/Subscriptions"); + _logger.LogInformation($"GetSubscriptionsForTouchpointHttpTrigger Customers/{customerId}/Subscriptions"); if (subscriptions == null) { - _loggerHelper.LogWarning($"Subscriptions not found for customer id [{customerId}]"); + _logger.LogWarning($"Subscriptions not found for customer id [{customerId}]"); return new NoContentResult(); } else if (subscriptions.Count == 1) diff --git a/NCS.DSS.Subscriptions/GetSubscriptionsForTouchpointHttpTrigger/Service/GetSubscriptionsForTouchpointHttpTriggerService.cs b/NCS.DSS.Subscriptions/GetSubscriptionsForTouchpointHttpTrigger/Service/GetSubscriptionsForTouchpointHttpTriggerService.cs index 14c72b4..182c0cf 100644 --- a/NCS.DSS.Subscriptions/GetSubscriptionsForTouchpointHttpTrigger/Service/GetSubscriptionsForTouchpointHttpTriggerService.cs +++ b/NCS.DSS.Subscriptions/GetSubscriptionsForTouchpointHttpTrigger/Service/GetSubscriptionsForTouchpointHttpTriggerService.cs @@ -4,16 +4,20 @@ namespace NCS.DSS.Subscriptions.GetSubscriptionsForTouchpointHttpTrigger.Service { public class GetSubscriptionsForTouchpointHttpTriggerService : IGetSubscriptionsForTouchpointHttpTriggerService { - private readonly IDocumentDBProvider _documentDbProvider; - public GetSubscriptionsForTouchpointHttpTriggerService(IDocumentDBProvider documentDbProvider) + private readonly ICosmosDBProvider _cosmosDbProvider; + public GetSubscriptionsForTouchpointHttpTriggerService(ICosmosDBProvider cosmosDbProvider) { - _documentDbProvider = documentDbProvider; + _cosmosDbProvider = cosmosDbProvider; } public async Task> GetSubscriptionsForTouchpointAsync(Guid customerId, string TouchpointId) { - var subscriptions = await _documentDbProvider.GetSubscriptionsForTouchpointAsync(customerId, TouchpointId); + var subscriptions = await _cosmosDbProvider.GetSubscriptionsForTouchpointAsync(customerId, TouchpointId); return subscriptions; } + public async Task DoesCustomerExist(Guid customerId) + { + return await _cosmosDbProvider.DoesCustomerResourceExist(customerId); + } } } \ No newline at end of file diff --git a/NCS.DSS.Subscriptions/GetSubscriptionsForTouchpointHttpTrigger/Service/IGetSubscriptionsForTouchpointHttpTriggerService.cs b/NCS.DSS.Subscriptions/GetSubscriptionsForTouchpointHttpTrigger/Service/IGetSubscriptionsForTouchpointHttpTriggerService.cs index d0d649b..004bbc4 100644 --- a/NCS.DSS.Subscriptions/GetSubscriptionsForTouchpointHttpTrigger/Service/IGetSubscriptionsForTouchpointHttpTriggerService.cs +++ b/NCS.DSS.Subscriptions/GetSubscriptionsForTouchpointHttpTrigger/Service/IGetSubscriptionsForTouchpointHttpTriggerService.cs @@ -3,5 +3,6 @@ public interface IGetSubscriptionsForTouchpointHttpTriggerService { Task> GetSubscriptionsForTouchpointAsync(Guid customerId, string TouchpointId); + Task DoesCustomerExist(Guid customerId); } } \ No newline at end of file diff --git a/NCS.DSS.Subscriptions/GetSubscriptionsForTouchpointHttpTrigger/function.json b/NCS.DSS.Subscriptions/GetSubscriptionsForTouchpointHttpTrigger/function.json deleted file mode 100644 index 8ec9773..0000000 --- a/NCS.DSS.Subscriptions/GetSubscriptionsForTouchpointHttpTrigger/function.json +++ /dev/null @@ -1,19 +0,0 @@ -{ - "bindings": [ - { - "authLevel": "function", - "direction": "in", - "methods": [ "get" ], - "name": "req", - "type": "httpTrigger" - }, - { - "name": "$return", - "type": "http", - "direction": "out" - } - ], - "disabled": false, - "entryPoint": "NCS.DSS.Subscriptions.GetSubscriptionsByIdHttpTrigger.Run", - "scriptFile": "..\\bin\\NCS.DSS.Subscriptions.dll" -} \ No newline at end of file diff --git a/NCS.DSS.Subscriptions/Models/Customer.cs b/NCS.DSS.Subscriptions/Models/Customer.cs new file mode 100644 index 0000000..e1ade82 --- /dev/null +++ b/NCS.DSS.Subscriptions/Models/Customer.cs @@ -0,0 +1,8 @@ +namespace NCS.DSS.Subscriptions.Models +{ + public class Customer + { + public Guid? id { get; set; } + public DateTime? DateOfTermination { get; set; } + } +} \ No newline at end of file diff --git a/NCS.DSS.Subscriptions/Models/SubscriptionsConfigurationSettings.cs b/NCS.DSS.Subscriptions/Models/SubscriptionsConfigurationSettings.cs new file mode 100644 index 0000000..e3b3028 --- /dev/null +++ b/NCS.DSS.Subscriptions/Models/SubscriptionsConfigurationSettings.cs @@ -0,0 +1,14 @@ +namespace NCS.DSS.Subscriptions.Models +{ + public class SubscriptionsConfigurationSettings + { + public string SubscriptionsConnectionString => string.Format("AccountEndpoint={0};AccountKey={1};", Endpoint, Key); + public string CollectionId { get; set; } + public string CustomerCollectionId { get; set; } + public string CustomerDatabaseId { get; set; } + public string DatabaseId { get; set; } + public string Endpoint { get; set; } + public string Key { get; set; } + + } +} diff --git a/NCS.DSS.Subscriptions/NCS.DSS.Subscriptions.csproj b/NCS.DSS.Subscriptions/NCS.DSS.Subscriptions.csproj index 56de693..518a096 100644 --- a/NCS.DSS.Subscriptions/NCS.DSS.Subscriptions.csproj +++ b/NCS.DSS.Subscriptions/NCS.DSS.Subscriptions.csproj @@ -6,17 +6,20 @@ Exe enable + + + - - - - + + + + - - - + + + diff --git a/NCS.DSS.Subscriptions/PatchSubscriptionsHttpTrigger/Function/PatchSubscriptionsHttpTrigger.cs b/NCS.DSS.Subscriptions/PatchSubscriptionsHttpTrigger/Function/PatchSubscriptionsHttpTrigger.cs index 1e0a024..faa3ec9 100644 --- a/NCS.DSS.Subscriptions/PatchSubscriptionsHttpTrigger/Function/PatchSubscriptionsHttpTrigger.cs +++ b/NCS.DSS.Subscriptions/PatchSubscriptionsHttpTrigger/Function/PatchSubscriptionsHttpTrigger.cs @@ -4,7 +4,6 @@ using Microsoft.AspNetCore.Mvc; using Microsoft.Azure.Functions.Worker; using Microsoft.Extensions.Logging; -using NCS.DSS.Subscriptions.Cosmos.Helper; using NCS.DSS.Subscriptions.Helpers; using NCS.DSS.Subscriptions.Models; using NCS.DSS.Subscriptions.PatchSubscriptionsHttpTrigger.Service; @@ -17,26 +16,23 @@ namespace NCS.DSS.Subscriptions.PatchSubscriptionsHttpTrigger.Function { public class PatchSubscriptionsHttpTrigger { - private readonly IResourceHelper _resourceHelper; private readonly IHttpRequestHelper _httpRequestMessageHelper; private readonly IValidate _validate; private readonly IPatchSubscriptionsHttpTriggerService _subscriptionsPatchService; - private readonly ILogger _loggerHelper; + private readonly ILogger _logger; private readonly IConvertToDynamic _convertToDynamic; public PatchSubscriptionsHttpTrigger( - IResourceHelper resourceHelper, IHttpRequestHelper httpRequestMessageHelper, IValidate validate, IPatchSubscriptionsHttpTriggerService subscriptionsPatchService, IConvertToDynamic convertToDynamic, - ILogger loggerHelper) + ILogger logger) { - _resourceHelper = resourceHelper; _httpRequestMessageHelper = httpRequestMessageHelper; _validate = validate; _subscriptionsPatchService = subscriptionsPatchService; _convertToDynamic = convertToDynamic; - _loggerHelper = loggerHelper; + _logger = logger; } [Function("Patch")] @@ -53,21 +49,21 @@ public async Task Run([HttpTrigger(AuthorizationLevel.Anonymous, var touchpointId = _httpRequestMessageHelper.GetDssTouchpointId(req); if (string.IsNullOrEmpty(touchpointId)) { - _loggerHelper.LogWarning($"PatchSubscriptionsHttpTrigger Customers/{customerId}/Subscriptions/{subscriptionId} Unable to locate 'TouchpointId' in request header"); + _logger.LogWarning($"PatchSubscriptionsHttpTrigger Customers/{customerId}/Subscriptions/{subscriptionId} Unable to locate 'TouchpointId' in request header"); return new BadRequestResult(); } - _loggerHelper.LogInformation("C# HTTP trigger function processed a request. By Touchpoint " + touchpointId); + _logger.LogInformation("C# HTTP trigger function processed a request. By Touchpoint " + touchpointId); if (!Guid.TryParse(customerId, out var customerGuid)) { - _loggerHelper.LogWarning($"PatchSubscriptionsHttpTrigger Customers/{customerId}/Subscriptions/{subscriptionId} BadRequest CustomerId"); + _logger.LogWarning($"PatchSubscriptionsHttpTrigger Customers/{customerId}/Subscriptions/{subscriptionId} BadRequest CustomerId"); return new BadRequestObjectResult(customerGuid); } if (!Guid.TryParse(subscriptionId, out var subscriptionsGuid)) { - _loggerHelper.LogWarning($"PatchSubscriptionsHttpTrigger Customers/{customerId}/Subscriptions/{subscriptionId} BadRequest subscriptionId"); + _logger.LogWarning($"PatchSubscriptionsHttpTrigger Customers/{customerId}/Subscriptions/{subscriptionId} BadRequest subscriptionId"); return new BadRequestObjectResult(subscriptionsGuid); } @@ -79,13 +75,13 @@ public async Task Run([HttpTrigger(AuthorizationLevel.Anonymous, } catch (Exception ex) { - _loggerHelper.LogError($"PatchSubscriptionsHttpTrigger Customers/{customerId}/Subscriptions/{subscriptionId} exception {ex.Message}"); + _logger.LogError($"PatchSubscriptionsHttpTrigger Customers/{customerId}/Subscriptions/{subscriptionId} exception {ex.Message}"); return new UnprocessableEntityObjectResult(_convertToDynamic.ExcludeProperty(ex, ["TargetSite", "InnerException"])); } if (subscriptionsPatchRequest == null) { - _loggerHelper.LogError($"PatchSubscriptionsHttpTrigger Customers/{customerId}/Subscriptions/{subscriptionId} subscriptionsPatchRequest is null"); + _logger.LogError($"PatchSubscriptionsHttpTrigger Customers/{customerId}/Subscriptions/{subscriptionId} subscriptionsPatchRequest is null"); return new UnprocessableEntityObjectResult(req); } @@ -95,15 +91,15 @@ public async Task Run([HttpTrigger(AuthorizationLevel.Anonymous, if (errors != null && errors.Any()) { - _loggerHelper.LogError($"PatchSubscriptionsHttpTrigger Customers/{customerId}/Subscriptions/{subscriptionId} errors at ValidateResource "); + _logger.LogError($"PatchSubscriptionsHttpTrigger Customers/{customerId}/Subscriptions/{subscriptionId} errors at ValidateResource "); return new UnprocessableEntityObjectResult(errors); } - var doesCustomerExist = await _resourceHelper.DoesCustomerExist(customerGuid); + var doesCustomerExist = await _subscriptionsPatchService.DoesCustomerExist(customerGuid); if (!doesCustomerExist) { - _loggerHelper.LogWarning($"PatchSubscriptionsHttpTrigger Customers/{customerId}/Subscriptions/{subscriptionId} customer doesCustomerExist "); + _logger.LogWarning($"PatchSubscriptionsHttpTrigger Customers/{customerId}/Subscriptions/{subscriptionId} customer doesCustomerExist "); return new NoContentResult(); } @@ -111,12 +107,12 @@ public async Task Run([HttpTrigger(AuthorizationLevel.Anonymous, if (subscriptions == null) { - _loggerHelper.LogWarning($"PatchSubscriptionsHttpTrigger Customers/{customerId}/Subscriptions/{subscriptionId} subscriptions is null "); + _logger.LogWarning($"PatchSubscriptionsHttpTrigger Customers/{customerId}/Subscriptions/{subscriptionId} subscriptions is null "); return new NoContentResult(); } var updatedSubscriptions = await _subscriptionsPatchService.UpdateAsync(subscriptions, subscriptionsPatchRequest); - _loggerHelper.LogInformation($"PatchSubscriptionsHttpTrigger Customers/{customerId}/Subscriptions/{subscriptionId} updatedSubscriptions"); + _logger.LogInformation($"PatchSubscriptionsHttpTrigger Customers/{customerId}/Subscriptions/{subscriptionId} updatedSubscriptions"); return updatedSubscriptions == null ? diff --git a/NCS.DSS.Subscriptions/PatchSubscriptionsHttpTrigger/Service/IPatchSubscriptionssHttpTriggerService.cs b/NCS.DSS.Subscriptions/PatchSubscriptionsHttpTrigger/Service/IPatchSubscriptionssHttpTriggerService.cs index 5687fc3..a3cbb94 100644 --- a/NCS.DSS.Subscriptions/PatchSubscriptionsHttpTrigger/Service/IPatchSubscriptionssHttpTriggerService.cs +++ b/NCS.DSS.Subscriptions/PatchSubscriptionsHttpTrigger/Service/IPatchSubscriptionssHttpTriggerService.cs @@ -4,5 +4,6 @@ public interface IPatchSubscriptionsHttpTriggerService { Task UpdateAsync(Models.Subscriptions subscriptions, Models.SubscriptionsPatch subscriptionsPatch); Task GetSubscriptionsForCustomerAsync(Guid customerId, Guid subscriptionId); + Task DoesCustomerExist(Guid customerId); } } \ No newline at end of file diff --git a/NCS.DSS.Subscriptions/PatchSubscriptionsHttpTrigger/Service/PatchSubscriptionsHttpTriggerService.cs b/NCS.DSS.Subscriptions/PatchSubscriptionsHttpTrigger/Service/PatchSubscriptionsHttpTriggerService.cs index 401e5f0..c397841 100644 --- a/NCS.DSS.Subscriptions/PatchSubscriptionsHttpTrigger/Service/PatchSubscriptionsHttpTriggerService.cs +++ b/NCS.DSS.Subscriptions/PatchSubscriptionsHttpTrigger/Service/PatchSubscriptionsHttpTriggerService.cs @@ -5,10 +5,10 @@ namespace NCS.DSS.Subscriptions.PatchSubscriptionsHttpTrigger.Service { public class PatchSubscriptionsHttpTriggerService : IPatchSubscriptionsHttpTriggerService { - private readonly IDocumentDBProvider _documentDbProvider; - public PatchSubscriptionsHttpTriggerService(IDocumentDBProvider documentDbProvider) + private readonly ICosmosDBProvider _cosmosDbProvider; + public PatchSubscriptionsHttpTriggerService(ICosmosDBProvider cosmosDbProvider) { - _documentDbProvider = documentDbProvider; + _cosmosDbProvider = cosmosDbProvider; } public async Task UpdateAsync(Models.Subscriptions subscriptions, Models.SubscriptionsPatch subscriptionsPatch) { @@ -20,7 +20,7 @@ public PatchSubscriptionsHttpTriggerService(IDocumentDBProvider documentDbProvid subscriptions.Patch(subscriptionsPatch); - var response = await _documentDbProvider.UpdateSubscriptionsAsync(subscriptions); + var response = await _cosmosDbProvider.UpdateSubscriptionsAsync(subscriptions); var responseStatusCode = response.StatusCode; @@ -29,9 +29,13 @@ public PatchSubscriptionsHttpTriggerService(IDocumentDBProvider documentDbProvid public async Task GetSubscriptionsForCustomerAsync(Guid customerId, Guid subscriptionId) { - var subscriptions = await _documentDbProvider.GetSubscriptionsForCustomerAsync(customerId, subscriptionId); + var subscriptions = await _cosmosDbProvider.GetSubscriptionsForCustomerAsync(customerId, subscriptionId); return subscriptions; } + public async Task DoesCustomerExist(Guid customerId) + { + return await _cosmosDbProvider.DoesCustomerResourceExist(customerId); + } } } \ No newline at end of file diff --git a/NCS.DSS.Subscriptions/PatchSubscriptionsHttpTrigger/function.json b/NCS.DSS.Subscriptions/PatchSubscriptionsHttpTrigger/function.json deleted file mode 100644 index 174ae80..0000000 --- a/NCS.DSS.Subscriptions/PatchSubscriptionsHttpTrigger/function.json +++ /dev/null @@ -1,19 +0,0 @@ -{ - "bindings": [ - { - "authLevel": "function", - "name": "req", - "type": "httpTrigger", - "direction": "in", - "methods": [ "patch" ] - }, - { - "name": "$return", - "type": "http", - "direction": "out" - } - ], - "disabled": false, - "entryPoint": "NCS.DSS.Subscriptions.PatchSubscriptionsHttpTrigger.Run", - "scriptFile": "..\\bin\\NCS.DSS.Subscriptions.dll" -} \ No newline at end of file diff --git a/NCS.DSS.Subscriptions/PostSubscriptionsHttpTrigger/Function/PostSubscriptionsHttpTrigger.cs b/NCS.DSS.Subscriptions/PostSubscriptionsHttpTrigger/Function/PostSubscriptionsHttpTrigger.cs index 4fae58f..57c546e 100644 --- a/NCS.DSS.Subscriptions/PostSubscriptionsHttpTrigger/Function/PostSubscriptionsHttpTrigger.cs +++ b/NCS.DSS.Subscriptions/PostSubscriptionsHttpTrigger/Function/PostSubscriptionsHttpTrigger.cs @@ -4,7 +4,6 @@ using Microsoft.AspNetCore.Mvc; using Microsoft.Azure.Functions.Worker; using Microsoft.Extensions.Logging; -using NCS.DSS.Subscriptions.Cosmos.Helper; using NCS.DSS.Subscriptions.Helpers; using NCS.DSS.Subscriptions.PostSubscriptionsHttpTrigger.Service; using NCS.DSS.Subscriptions.Validation; @@ -16,26 +15,23 @@ namespace NCS.DSS.Subscriptions.PostSubscriptionsHttpTrigger.Function { public class PostSubscriptionsHttpTrigger { - private readonly IResourceHelper _resourceHelper; private readonly IHttpRequestHelper _httpRequestHelper; private readonly IValidate _validate; private readonly IPostSubscriptionsHttpTriggerService _subscriptionsPostService; - private readonly ILogger _loggerHelper; + private readonly ILogger _logger; private readonly IConvertToDynamic _convertToDynamic; public PostSubscriptionsHttpTrigger( - IResourceHelper resourceHelper, IHttpRequestHelper httpRequestMessageHelper, IValidate validate, IPostSubscriptionsHttpTriggerService subscriptionsPostService, - ILogger loggerHelper, + ILogger logger, IConvertToDynamic convertToDynamic) { - _resourceHelper = resourceHelper; _httpRequestHelper = httpRequestMessageHelper; _validate = validate; _subscriptionsPostService = subscriptionsPostService; - _loggerHelper = loggerHelper; + _logger = logger; _convertToDynamic = convertToDynamic; } @@ -53,15 +49,15 @@ public async Task Run([HttpTrigger(AuthorizationLevel.Anonymous, var touchpointId = _httpRequestHelper.GetDssTouchpointId(req); if (string.IsNullOrEmpty(touchpointId)) { - _loggerHelper.LogInformation($"PostSubscriptionsHttpTrigger Customers/{customerId}/Subscriptions Unable to locate 'TouchpointId' in request header"); + _logger.LogInformation($"PostSubscriptionsHttpTrigger Customers/{customerId}/Subscriptions Unable to locate 'TouchpointId' in request header"); return new BadRequestResult(); } - _loggerHelper.LogInformation("C# HTTP trigger function processed a request. By Touchpoint " + touchpointId); + _logger.LogInformation("C# HTTP trigger function processed a request. By Touchpoint " + touchpointId); if (!Guid.TryParse(customerId, out var customerGuid)) { - _loggerHelper.LogInformation($"PostSubscriptionsHttpTrigger Customers/{customerId}/Subscriptions customerGuid BadRequest"); + _logger.LogInformation($"PostSubscriptionsHttpTrigger Customers/{customerId}/Subscriptions customerGuid BadRequest"); return new BadRequestObjectResult(customerGuid); } @@ -73,13 +69,13 @@ public async Task Run([HttpTrigger(AuthorizationLevel.Anonymous, } catch (Exception ex) { - _loggerHelper.LogError($"PostSubscriptionsHttpTrigger Customers/{customerId}/Subscriptions JsonSerializationException"); + _logger.LogError($"PostSubscriptionsHttpTrigger Customers/{customerId}/Subscriptions JsonSerializationException"); return new UnprocessableEntityObjectResult(_convertToDynamic.ExcludeProperty(ex, ["TargetSite", "InnerException"])); } if (subscriptionsRequest == null) { - _loggerHelper.LogError($"PostSubscriptionsHttpTrigger Customers/{customerId}/Subscriptions subscriptionsRequest is null"); + _logger.LogError($"PostSubscriptionsHttpTrigger Customers/{customerId}/Subscriptions subscriptionsRequest is null"); return new UnprocessableEntityObjectResult(req); } @@ -89,30 +85,30 @@ public async Task Run([HttpTrigger(AuthorizationLevel.Anonymous, if (errors != null && errors.Count > 0) { - _loggerHelper.LogError($"PostSubscriptionsHttpTrigger Customers/{customerId}/Subscriptions errors in ValidateResource"); + _logger.LogError($"PostSubscriptionsHttpTrigger Customers/{customerId}/Subscriptions errors in ValidateResource"); return new UnprocessableEntityObjectResult(errors); } - var doesCustomerExist = await _resourceHelper.DoesCustomerExist(customerGuid); + var doesCustomerExist = await _subscriptionsPostService.DoesCustomerExist(customerGuid); if (!doesCustomerExist) { - _loggerHelper.LogError($"PostSubscriptionsHttpTrigger Customers/{customerId}/Subscriptions does notvCustomerExist "); + _logger.LogError($"PostSubscriptionsHttpTrigger Customers/{customerId}/Subscriptions does notvCustomerExist "); return new NoContentResult(); } - var doesSubscriptionExist = await _resourceHelper.DoesSubscriptionExist(customerGuid, touchpointId); + var doesSubscriptionExist = await _subscriptionsPostService.DoesSubscriptionExist(customerGuid, touchpointId); if (doesSubscriptionExist.HasValue) { var duplicateError = _validate.ValidateResultForDuplicateSubscriptionId(doesSubscriptionExist.GetValueOrDefault()); - _loggerHelper.LogError($"PostSubscriptionsHttpTrigger Customers/{customerId}/Subscriptions Subscription conflict. {duplicateError} "); + _logger.LogError($"PostSubscriptionsHttpTrigger Customers/{customerId}/Subscriptions Subscription conflict. {duplicateError} "); return new ConflictResult(); } var subscriptions = await _subscriptionsPostService.CreateAsync(subscriptionsRequest); - _loggerHelper.LogInformation($"PostSubscriptionsHttpTrigger Customers/{customerId}/Subscriptions CreateAsync called"); + _logger.LogInformation($"PostSubscriptionsHttpTrigger Customers/{customerId}/Subscriptions CreateAsync called"); return subscriptions == null ? new BadRequestObjectResult(customerGuid) diff --git a/NCS.DSS.Subscriptions/PostSubscriptionsHttpTrigger/Service/IPostSubscriptionsHttpTriggerService.cs b/NCS.DSS.Subscriptions/PostSubscriptionsHttpTrigger/Service/IPostSubscriptionsHttpTriggerService.cs index 56cf169..671dd36 100644 --- a/NCS.DSS.Subscriptions/PostSubscriptionsHttpTrigger/Service/IPostSubscriptionsHttpTriggerService.cs +++ b/NCS.DSS.Subscriptions/PostSubscriptionsHttpTrigger/Service/IPostSubscriptionsHttpTriggerService.cs @@ -3,5 +3,7 @@ public interface IPostSubscriptionsHttpTriggerService { Task CreateAsync(Models.Subscriptions subscriptions); + Task DoesCustomerExist(Guid customerId); + Task DoesSubscriptionExist(Guid customerId, string touchpointId); } } \ No newline at end of file diff --git a/NCS.DSS.Subscriptions/PostSubscriptionsHttpTrigger/Service/PostSubscriptionsHttpTriggerService.cs b/NCS.DSS.Subscriptions/PostSubscriptionsHttpTrigger/Service/PostSubscriptionsHttpTriggerService.cs index ca79f59..3115656 100644 --- a/NCS.DSS.Subscriptions/PostSubscriptionsHttpTrigger/Service/PostSubscriptionsHttpTriggerService.cs +++ b/NCS.DSS.Subscriptions/PostSubscriptionsHttpTrigger/Service/PostSubscriptionsHttpTriggerService.cs @@ -5,10 +5,10 @@ namespace NCS.DSS.Subscriptions.PostSubscriptionsHttpTrigger.Service { public class PostSubscriptionsHttpTriggerService : IPostSubscriptionsHttpTriggerService { - private readonly IDocumentDBProvider _documentDbProvider; - public PostSubscriptionsHttpTriggerService(IDocumentDBProvider documentDbProvider) + private readonly ICosmosDBProvider _cosmosDbProvider; + public PostSubscriptionsHttpTriggerService(ICosmosDBProvider cosmosDbProvider) { - _documentDbProvider = documentDbProvider; + _cosmosDbProvider = cosmosDbProvider; } public async Task CreateAsync(Models.Subscriptions subscriptions) { @@ -21,9 +21,17 @@ public PostSubscriptionsHttpTriggerService(IDocumentDBProvider documentDbProvide if (!subscriptions.LastModifiedDate.HasValue) subscriptions.LastModifiedDate = DateTime.Now; - var response = await _documentDbProvider.CreateSubscriptionsAsync(subscriptions); + var response = await _cosmosDbProvider.CreateSubscriptionsAsync(subscriptions); return response.StatusCode == HttpStatusCode.Created ? (dynamic)response.Resource : (Guid?)null; } + public async Task DoesCustomerExist(Guid customerId) + { + return await _cosmosDbProvider.DoesCustomerResourceExist(customerId); + } + public async Task DoesSubscriptionExist(Guid customerId,string touchpointId) + { + return await _cosmosDbProvider.DoesSubscriptionExist(customerId,touchpointId); + } } } \ No newline at end of file diff --git a/NCS.DSS.Subscriptions/PostSubscriptionsHttpTrigger/function.json b/NCS.DSS.Subscriptions/PostSubscriptionsHttpTrigger/function.json deleted file mode 100644 index bbd919f..0000000 --- a/NCS.DSS.Subscriptions/PostSubscriptionsHttpTrigger/function.json +++ /dev/null @@ -1,28 +0,0 @@ -{ - "bindings": [ - { - "authLevel": "function", - "name": "req", - "type": "httpTrigger", - "direction": "in", - "methods": [ "post" ] - }, - { - "direction": "out", - "name": "$return", - "type": "http" - }, - { - "type": "documentDB", - "name": "documents", - "databaseName": "Subscriptions", - "collectionName": "Subscriptions", - "authToken": "C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==", - "connection": "AccountEndpoint=https://localhost:8081/;AccountKey=C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==", - "direction": "in" - } - ], - "disabled": false, - "entryPoint": "NCS.DSS.Subscriptions.PostSubscriptionsHttpTrigger.Run", - "scriptFile": "..\\bin\\NCS.DSS.Subscriptions.dll" -} \ No newline at end of file diff --git a/NCS.DSS.Subscriptions/Program.cs b/NCS.DSS.Subscriptions/Program.cs index 0fbea2f..2264eed 100644 --- a/NCS.DSS.Subscriptions/Program.cs +++ b/NCS.DSS.Subscriptions/Program.cs @@ -1,32 +1,71 @@ using DFC.HTTP.Standard; using DFC.Swagger.Standard; +using Microsoft.Azure.Cosmos; +using Microsoft.Azure.Functions.Worker; +using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; -using NCS.DSS.Subscriptions.Cosmos.Helper; +using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Options; using NCS.DSS.Subscriptions.Cosmos.Provider; -using NCS.DSS.Subscriptions.GetSubscriptionsByIdHttpTrigger.Service; using NCS.DSS.Subscriptions.GetSubscriptionsForTouchpointHttpTrigger.Service; using NCS.DSS.Subscriptions.Helpers; +using NCS.DSS.Subscriptions.Models; using NCS.DSS.Subscriptions.PatchSubscriptionsHttpTrigger.Service; using NCS.DSS.Subscriptions.PostSubscriptionsHttpTrigger.Service; using NCS.DSS.Subscriptions.Validation; - -var host = new HostBuilder() - .ConfigureFunctionsWebApplication() - .ConfigureServices(services => +internal class Program +{ + private static async Task Main(string[] args) { - services.AddTransient(); - services.AddTransient(); - services.AddTransient(); - services.AddTransient(); - services.AddSingleton(); - services.AddSingleton(); - services.AddTransient(); - services.AddTransient(); - services.AddSingleton(); - services.AddSingleton(); - services.AddTransient(); - }) - .Build(); + var host = new HostBuilder() + .ConfigureFunctionsWebApplication() + .ConfigureAppConfiguration(configBuilder => + { + configBuilder.SetBasePath(Environment.CurrentDirectory) + .AddJsonFile("local.settings.json", optional: true, + reloadOnChange: false) + .AddEnvironmentVariables(); + }) + .ConfigureServices((context, services) => + { + var configuration = context.Configuration; + services.AddOptions() + .Bind(configuration); + services.AddApplicationInsightsTelemetryWorkerService(); + services.ConfigureFunctionsApplicationInsights(); + services.AddLogging(); + services.AddTransient(); + services.AddTransient(); + services.AddTransient(); + services.AddSingleton(); + services.AddSingleton(); + services.AddSingleton(sp => + { + var settings = sp.GetRequiredService>().Value; + var options = new CosmosClientOptions() + { + ConnectionMode = ConnectionMode.Gateway + }; + return new CosmosClient(settings.SubscriptionsConnectionString, options); + }); + services.AddTransient(); + services.AddSingleton(); + services.AddSingleton(); + services.AddTransient(); + services.Configure(options => + { + LoggerFilterRule toRemove = options.Rules.FirstOrDefault(rule => rule.ProviderName + == "Microsoft.Extensions.Logging.ApplicationInsights.ApplicationInsightsLoggerProvider"); + if (toRemove is not null) + { + options.Rules.Remove(toRemove); + } + }); + + }) + .Build(); -host.Run(); + await host.RunAsync(); + } +} \ No newline at end of file From e60d61d0b38de27277f30787b548b9b956106c08 Mon Sep 17 00:00:00 2001 From: Pradeep Kumar Voorukonda Date: Fri, 20 Dec 2024 16:43:06 +0000 Subject: [PATCH 2/8] Fix unit test cases --- .../FunctionTests/PatchSubscriptionsHttpTriggerTests.cs | 8 ++++---- .../FunctionTests/PostSubscriptionsHttpTriggerTests.cs | 4 ++-- ...tSubscriptionsForTouchpointHttpTriggerServiceTests.cs | 9 ++------- 3 files changed, 8 insertions(+), 13 deletions(-) diff --git a/NCS.DSS.Subscriptions.Tests/FunctionTests/PatchSubscriptionsHttpTriggerTests.cs b/NCS.DSS.Subscriptions.Tests/FunctionTests/PatchSubscriptionsHttpTriggerTests.cs index cfae760..5ee6386 100644 --- a/NCS.DSS.Subscriptions.Tests/FunctionTests/PatchSubscriptionsHttpTriggerTests.cs +++ b/NCS.DSS.Subscriptions.Tests/FunctionTests/PatchSubscriptionsHttpTriggerTests.cs @@ -29,7 +29,7 @@ public class PatchSubscriptionsHttpTriggerTests private Mock _convertToDynamic; private Mock _patchSubscriptionsHttpTriggerService; private Mock _httpRequestHelper; - private Models.Subscriptions _subscriptions; + private Models.Subscriptions? _subscriptions; private SubscriptionsPatch _subscriptionsPatch; private PatchSubscriptionsHttpTriggerRun _patchSubscriptionsHttpTriggerRun; private List _validationResults; @@ -100,7 +100,7 @@ public async Task PatchSubscriptionsHttpTrigger_ReturnsStatusCodeUnprocessableEn // Arrange _httpRequestHelper.Setup(x => x.GetDssTouchpointId(_request)).Returns(_touchPointId); _httpRequestHelper.Setup(x => x.GetDssApimUrl(_request)).Returns(_apimUrl); - _httpRequestHelper.Setup(x => x.GetResourceFromRequest(_request)).Returns(Task.FromResult(_subscriptionsPatch)); + _httpRequestHelper.Setup(x => x.GetResourceFromRequest(_request)).Returns(Task.FromResult(null)); // Act var result = await RunFunction(ValidCustomerId, ValidSubscriptionId); @@ -162,8 +162,8 @@ public async Task PatchSubscriptionsHttpTrigger_ReturnsStatusCodeNoContent_WhenS _validate.Setup(x => x.ValidateResource(It.IsAny())).Returns(_validationResults); _patchSubscriptionsHttpTriggerService.Setup(x => x.DoesCustomerExist(It.IsAny())).Returns(Task.FromResult(true)); - - _patchSubscriptionsHttpTriggerService.Setup(x => x.GetSubscriptionsForCustomerAsync(It.IsAny(), It.IsAny())).Returns(Task.FromResult(_subscriptions)); + _subscriptions = null; + _patchSubscriptionsHttpTriggerService.Setup(x => x.GetSubscriptionsForCustomerAsync(It.IsAny(), It.IsAny())).Returns(Task.FromResult(_subscriptions)); // Act var result = await RunFunction(ValidCustomerId, ValidSubscriptionId); diff --git a/NCS.DSS.Subscriptions.Tests/FunctionTests/PostSubscriptionsHttpTriggerTests.cs b/NCS.DSS.Subscriptions.Tests/FunctionTests/PostSubscriptionsHttpTriggerTests.cs index 8dee2bd..cde13dd 100644 --- a/NCS.DSS.Subscriptions.Tests/FunctionTests/PostSubscriptionsHttpTriggerTests.cs +++ b/NCS.DSS.Subscriptions.Tests/FunctionTests/PostSubscriptionsHttpTriggerTests.cs @@ -86,7 +86,7 @@ public async Task PostSubscriptionsHttpTrigger_ReturnsStatusCodeUnprocessableEnt // Arrange _httpRequestHelper.Setup(x => x.GetDssTouchpointId(_request)).Returns(_touchPointId); _httpRequestHelper.Setup(x => x.GetDssApimUrl(_request)).Returns(_apimUrl); - _httpRequestHelper.Setup(x => x.GetResourceFromRequest(_request)).Returns(Task.FromResult(_subscriptions)); + _httpRequestHelper.Setup(x => x.GetResourceFromRequest(_request)).Returns(Task.FromResult(null)); _postSubscriptionsHttpTriggerService.Setup(x => x.CreateAsync(_subscriptions)).Returns(Task.FromResult(_subscriptions)); @@ -170,7 +170,7 @@ public async Task PostSubscriptionsHttpTrigger_ReturnsStatusCodeBadRequest_WhenS _postSubscriptionsHttpTriggerService.Setup(x => x.DoesCustomerExist(It.IsAny())).Returns(Task.FromResult(true)); _postSubscriptionsHttpTriggerService.Setup(x => x.DoesSubscriptionExist(It.IsAny(), It.IsAny())).Returns(Task.FromResult(null)); - _postSubscriptionsHttpTriggerService.Setup(x => x.CreateAsync(_subscriptions)).Returns(Task.FromResult(_subscriptions)); + _postSubscriptionsHttpTriggerService.Setup(x => x.CreateAsync(_subscriptions)).Returns(Task.FromResult(null)); // Act var result = await RunFunction(ValidCustomerId); diff --git a/NCS.DSS.Subscriptions.Tests/ServiceTests/GetSubscriptionsForTouchpointHttpTriggerServiceTests.cs b/NCS.DSS.Subscriptions.Tests/ServiceTests/GetSubscriptionsForTouchpointHttpTriggerServiceTests.cs index 59c7a18..e0daab2 100644 --- a/NCS.DSS.Subscriptions.Tests/ServiceTests/GetSubscriptionsForTouchpointHttpTriggerServiceTests.cs +++ b/NCS.DSS.Subscriptions.Tests/ServiceTests/GetSubscriptionsForTouchpointHttpTriggerServiceTests.cs @@ -20,14 +20,9 @@ public GetSubscriptionsForTouchpointHttpTriggerServiceTests() public async Task GetSubscriptionsForTouchpointHttpTriggerServiceTests_GetSubscriptionsForTouchpointAsync_ReturnsNullWhenResourceCannotBeFound() { // Arrange - var subscriptions = new List() - { - new() - }; - _cosmosDbProvider.Setup(x => x.GetSubscriptionsForTouchpointAsync(_customerId, _touchPointId)).Returns(Task.FromResult(subscriptions)); - - // Act + _cosmosDbProvider.Setup(x => x.GetSubscriptionsForTouchpointAsync(_customerId, _touchPointId)).Returns(Task.FromResult>(null)); + // Act var result = await _getSubscriptionsForTouchpointHttpTriggerService.GetSubscriptionsForTouchpointAsync(_customerId, _touchPointId); // Assert From 8d61e2c0a0bb4907eb6290f77aaba3831d1f3137 Mon Sep 17 00:00:00 2001 From: Pradeep Kumar Voorukonda Date: Mon, 23 Dec 2024 09:25:51 +0000 Subject: [PATCH 3/8] DFC.Swagger.Standard downgraded to 0.1.27 as the latest version is not working --- NCS.DSS.Subscriptions/NCS.DSS.Subscriptions.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/NCS.DSS.Subscriptions/NCS.DSS.Subscriptions.csproj b/NCS.DSS.Subscriptions/NCS.DSS.Subscriptions.csproj index 518a096..622ba1d 100644 --- a/NCS.DSS.Subscriptions/NCS.DSS.Subscriptions.csproj +++ b/NCS.DSS.Subscriptions/NCS.DSS.Subscriptions.csproj @@ -14,7 +14,7 @@ - + From f30ca7674710b15d864f7c5a78768819abb986e7 Mon Sep 17 00:00:00 2001 From: Pradeep Kumar Voorukonda Date: Mon, 23 Dec 2024 11:51:31 +0000 Subject: [PATCH 4/8] additional logging --- ...scriptionsForTouchpointHttpTriggerTests.cs | 6 +- .../PatchSubscriptionsHttpTriggerTests.cs | 2 +- ...etSubscriptionsForTouchpointHttpTrigger.cs | 58 ++++++++++--- .../Function/PatchSubscriptionsHttpTrigger.cs | 86 +++++++++++++------ .../Function/PostSubscriptionsHttpTrigger.cs | 73 ++++++++++++---- 5 files changed, 166 insertions(+), 59 deletions(-) diff --git a/NCS.DSS.Subscriptions.Tests/FunctionTests/GetSubscriptionsForTouchpointHttpTriggerTests.cs b/NCS.DSS.Subscriptions.Tests/FunctionTests/GetSubscriptionsForTouchpointHttpTriggerTests.cs index e5518d2..e6b6e92 100644 --- a/NCS.DSS.Subscriptions.Tests/FunctionTests/GetSubscriptionsForTouchpointHttpTriggerTests.cs +++ b/NCS.DSS.Subscriptions.Tests/FunctionTests/GetSubscriptionsForTouchpointHttpTriggerTests.cs @@ -24,7 +24,9 @@ public class GetSubscriptionsForTouchpointHttpTriggerTests [SetUp] public void Setup() { - _subscriptions = []; + _subscriptions = [ + new Models.Subscriptions() + ]; _request = (new DefaultHttpContext()).Request; _httpRequestHelper = new Mock(); var logger = new Mock>(); @@ -47,7 +49,7 @@ public async Task GetSubscriptionsForTouchpointHttpTrigger_ReturnsStatusCodeBadR var result = await RunFunction(ValidCustomerId); // Assert - Assert.That(result, Is.InstanceOf()); + Assert.That(result, Is.InstanceOf()); } [Test] diff --git a/NCS.DSS.Subscriptions.Tests/FunctionTests/PatchSubscriptionsHttpTriggerTests.cs b/NCS.DSS.Subscriptions.Tests/FunctionTests/PatchSubscriptionsHttpTriggerTests.cs index 5ee6386..439b779 100644 --- a/NCS.DSS.Subscriptions.Tests/FunctionTests/PatchSubscriptionsHttpTriggerTests.cs +++ b/NCS.DSS.Subscriptions.Tests/FunctionTests/PatchSubscriptionsHttpTriggerTests.cs @@ -65,7 +65,7 @@ public async Task PatchSubscriptionsHttpTrigger_ReturnsStatusCodeBadRequest_When var result = await RunFunction(ValidCustomerId, ValidSubscriptionId); // Assert - Assert.That(result, Is.InstanceOf()); + Assert.That(result, Is.InstanceOf()); } [Test] diff --git a/NCS.DSS.Subscriptions/GetSubscriptionsForTouchpointHttpTrigger/Function/GetSubscriptionsForTouchpointHttpTrigger.cs b/NCS.DSS.Subscriptions/GetSubscriptionsForTouchpointHttpTrigger/Function/GetSubscriptionsForTouchpointHttpTrigger.cs index b9a8d7b..ccbaa12 100644 --- a/NCS.DSS.Subscriptions/GetSubscriptionsForTouchpointHttpTrigger/Function/GetSubscriptionsForTouchpointHttpTrigger.cs +++ b/NCS.DSS.Subscriptions/GetSubscriptionsForTouchpointHttpTrigger/Function/GetSubscriptionsForTouchpointHttpTrigger.cs @@ -8,6 +8,7 @@ using System.ComponentModel.DataAnnotations; using System.Net; using System.Text.Json; +using static Microsoft.ApplicationInsights.MetricDimensionNames.TelemetryContext; namespace NCS.DSS.Subscriptions.GetSubscriptionsForTouchpointHttpTrigger.Function { @@ -37,37 +38,70 @@ public GetSubscriptionsForTouchpointHttpTrigger( [Display(Name = "Get", Description = "Ability to retrieve a single subscriptions with a given SubscriptionsId for an individual customer.")] public async Task Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "Customers/{customerId}/Subscriptions/")] HttpRequest req, string customerId) { + var functionName = nameof(GetSubscriptionsForTouchpointHttpTrigger); + + _logger.LogInformation("Function {FunctionName} has been invoked", functionName); + + var correlationId = _httpRequestMessageHelper.GetDssCorrelationId(req); + if (string.IsNullOrEmpty(correlationId)) + _logger.LogInformation("Unable to locate 'DssCorrelationId' in request header"); + + if (!Guid.TryParse(correlationId, out var correlationGuid)) + { + _logger.LogInformation("Unable to parse 'DssCorrelationId' to a Guid. New Guid Generated."); + correlationGuid = Guid.NewGuid(); + } + var touchpointId = _httpRequestMessageHelper.GetDssTouchpointId(req); if (string.IsNullOrEmpty(touchpointId)) { - _logger.LogInformation("Unable to locate 'APIM-TouchpointId' in request header"); - return new BadRequestResult(); + var response = new BadRequestObjectResult(HttpStatusCode.BadRequest); + _logger.LogWarning("{CorrelationId} Response Status Code: {StatusCode}. Unable to locate 'TouchpointId' in request header", correlationId, response.StatusCode); + return response; } if (!Guid.TryParse(customerId, out var customerGuid)) { - _logger.LogWarning($"GetSubscriptionsForTouchpointHttpTrigger Customers/{customerId}/Subscriptions/ BadRequest"); - return new BadRequestObjectResult(customerGuid); + var response = new BadRequestObjectResult(customerGuid); + _logger.LogWarning("{CorrelationId} Response Status Code: {StatusCode}. Unable to parse 'customerId' to a Guid: {customerId}", correlationId, customerId); + return response; } + + _logger.LogInformation("{CorrelationId} Input validation has succeeded.", correlationId); + + _logger.LogInformation("{CorrelationId} Attempting to see if customer exists {customerGuid}", correlationId, customerGuid); var doesCustomerExist = await _getSubscriptionsForTouchpointService.DoesCustomerExist(customerGuid); if (!doesCustomerExist) { - _logger.LogError($"PostSubscriptionsHttpTrigger Customers/{customerId}/Subscriptions does notvCustomerExist "); - return new NoContentResult(); + var response = new NoContentResult(); + _logger.LogWarning("{CorrelationId} Response Status Code: {StatusCode}. Customer does not exist {customerGuid}", correlationId, response.StatusCode, customerGuid); + return response; } + _logger.LogInformation("{CorrelationId} Customer record found in Cosmos DB {customerGuid}", correlationId, customerGuid); + + _logger.LogInformation("{CorrelationId} Attempting to get Subscriptions for customer {customerGuid} and {touchpointId}", correlationId, customerGuid, touchpointId); var subscriptions = await _getSubscriptionsForTouchpointService.GetSubscriptionsForTouchpointAsync(customerGuid, touchpointId); - _logger.LogInformation($"GetSubscriptionsForTouchpointHttpTrigger Customers/{customerId}/Subscriptions"); if (subscriptions == null) { - _logger.LogWarning($"Subscriptions not found for customer id [{customerId}]"); - return new NoContentResult(); + var response = new NoContentResult(); + _logger.LogWarning("{CorrelationId} Response Status Code: {StatusCode}. Subscriptions does not exist for customer {customerGuid} and {touchpointId}", correlationId, response.StatusCode, customerGuid, touchpointId); + _logger.LogInformation("Function {FunctionName} has finished invoking", functionName); + return response; } - else if (subscriptions.Count == 1) - return new JsonResult(subscriptions[0], new JsonSerializerOptions()) { StatusCode = (int)HttpStatusCode.OK }; - return new JsonResult(subscriptions, new JsonSerializerOptions()) { StatusCode = (int)HttpStatusCode.OK }; + var jsonResponse = new JsonResult(subscriptions[0], new JsonSerializerOptions()) + { + StatusCode = (int)HttpStatusCode.OK + }; + _logger.LogInformation("{CorrelationId} Response Status Code: {StatusCode}. Get session succeeded for customer {customerGuid} and {touchpointId}", correlationId, jsonResponse.StatusCode, customerGuid, touchpointId); + _logger.LogInformation("Function {FunctionName} has finished invoking", functionName); + if (subscriptions.Count > 1) + { + jsonResponse.Value = subscriptions; + } + return jsonResponse; } } } \ No newline at end of file diff --git a/NCS.DSS.Subscriptions/PatchSubscriptionsHttpTrigger/Function/PatchSubscriptionsHttpTrigger.cs b/NCS.DSS.Subscriptions/PatchSubscriptionsHttpTrigger/Function/PatchSubscriptionsHttpTrigger.cs index faa3ec9..8d2e3b2 100644 --- a/NCS.DSS.Subscriptions/PatchSubscriptionsHttpTrigger/Function/PatchSubscriptionsHttpTrigger.cs +++ b/NCS.DSS.Subscriptions/PatchSubscriptionsHttpTrigger/Function/PatchSubscriptionsHttpTrigger.cs @@ -4,6 +4,7 @@ using Microsoft.AspNetCore.Mvc; using Microsoft.Azure.Functions.Worker; using Microsoft.Extensions.Logging; +using Microsoft.VisualBasic; using NCS.DSS.Subscriptions.Helpers; using NCS.DSS.Subscriptions.Models; using NCS.DSS.Subscriptions.PatchSubscriptionsHttpTrigger.Service; @@ -46,78 +47,113 @@ public PatchSubscriptionsHttpTrigger( [Display(Name = "Patch", Description = "Ability to update an existing subscriptions.")] public async Task Run([HttpTrigger(AuthorizationLevel.Anonymous, "patch", Route = "Customers/{customerId}/Subscriptions/{subscriptionId}")] HttpRequest req, string customerId, string subscriptionId) { + var functionName = nameof(PatchSubscriptionsHttpTrigger); + + _logger.LogInformation("Function {FunctionName} has been invoked", functionName); + + var correlationId = _httpRequestMessageHelper.GetDssCorrelationId(req); + + if (string.IsNullOrEmpty(correlationId)) + _logger.LogInformation("Unable to locate 'DssCorrelationId' in request header"); + + if (!Guid.TryParse(correlationId, out var correlationGuid)) + { + _logger.LogInformation("Unable to parse 'DssCorrelationId' to a Guid. New Guid Generated."); + correlationGuid = Guid.NewGuid(); + } var touchpointId = _httpRequestMessageHelper.GetDssTouchpointId(req); if (string.IsNullOrEmpty(touchpointId)) { - _logger.LogWarning($"PatchSubscriptionsHttpTrigger Customers/{customerId}/Subscriptions/{subscriptionId} Unable to locate 'TouchpointId' in request header"); - return new BadRequestResult(); + var response = new BadRequestObjectResult(HttpStatusCode.BadRequest); + _logger.LogWarning("{CorrelationId} Response Status Code: {StatusCode}. Unable to locate 'TouchpointId' in request header", correlationId, response.StatusCode); + return response; } - _logger.LogInformation("C# HTTP trigger function processed a request. By Touchpoint " + touchpointId); - if (!Guid.TryParse(customerId, out var customerGuid)) { - _logger.LogWarning($"PatchSubscriptionsHttpTrigger Customers/{customerId}/Subscriptions/{subscriptionId} BadRequest CustomerId"); - return new BadRequestObjectResult(customerGuid); + var response = new BadRequestObjectResult(customerGuid); + _logger.LogWarning("{CorrelationId} Response Status Code: {StatusCode}. Unable to parse 'customerId' to a Guid: {customerId}", correlationId, response.StatusCode, customerId); + return response; } if (!Guid.TryParse(subscriptionId, out var subscriptionsGuid)) { - _logger.LogWarning($"PatchSubscriptionsHttpTrigger Customers/{customerId}/Subscriptions/{subscriptionId} BadRequest subscriptionId"); - return new BadRequestObjectResult(subscriptionsGuid); + var response = new BadRequestObjectResult(subscriptionId); + _logger.LogWarning("{CorrelationId} Response Status Code: {StatusCode}. Unable to parse 'subscriptionId' to a Guid: {subscriptionId}", correlationId, response.StatusCode, subscriptionId); + return response; } - + _logger.LogInformation("{CorrelationId} Input validation has succeeded.", correlationId); SubscriptionsPatch subscriptionsPatchRequest; try { + _logger.LogInformation("{CorrelationId} Attempt to get resource from body of the request", correlationId); subscriptionsPatchRequest = await _httpRequestMessageHelper.GetResourceFromRequest(req); } catch (Exception ex) { - _logger.LogError($"PatchSubscriptionsHttpTrigger Customers/{customerId}/Subscriptions/{subscriptionId} exception {ex.Message}"); - return new UnprocessableEntityObjectResult(_convertToDynamic.ExcludeProperty(ex, ["TargetSite", "InnerException"])); + var response = new UnprocessableEntityObjectResult(_convertToDynamic.ExcludeProperty(ex, ["TargetSite", "InnerException"])); + _logger.LogError(ex, "{CorrelationId} Response Status Code: {StatusCode}. Unable to retrieve body from req", correlationId, response.StatusCode); + return response; } if (subscriptionsPatchRequest == null) { - _logger.LogError($"PatchSubscriptionsHttpTrigger Customers/{customerId}/Subscriptions/{subscriptionId} subscriptionsPatchRequest is null"); - return new UnprocessableEntityObjectResult(req); + var response = new UnprocessableEntityObjectResult(req); + _logger.LogWarning("{CorrelationId} Response Status Code: {StatusCode}. session patch request is null", correlationId, response.StatusCode); + return response; } subscriptionsPatchRequest.LastModifiedBy = touchpointId; - + _logger.LogInformation("{CorrelationId} Attempt to validate resource", correlationId); var errors = _validate.ValidateResource(subscriptionsPatchRequest); if (errors != null && errors.Any()) { - _logger.LogError($"PatchSubscriptionsHttpTrigger Customers/{customerId}/Subscriptions/{subscriptionId} errors at ValidateResource "); - return new UnprocessableEntityObjectResult(errors); + var response = new UnprocessableEntityObjectResult(errors); + _logger.LogWarning("{CorrelationId} Response Status Code: {StatusCode}. validation errors with resource {Errors}", correlationId, response.StatusCode, errors); + return response; } + _logger.LogInformation("{CorrelationId} Attempting to see if customer exists {customerGuid}", correlationId, customerGuid); var doesCustomerExist = await _subscriptionsPatchService.DoesCustomerExist(customerGuid); if (!doesCustomerExist) { - _logger.LogWarning($"PatchSubscriptionsHttpTrigger Customers/{customerId}/Subscriptions/{subscriptionId} customer doesCustomerExist "); - return new NoContentResult(); + var response = new NoContentResult(); + _logger.LogWarning("{CorrelationId} Response Status Code: {StatusCode}. Customer does not exist {CustomerId}", correlationId, customerGuid); + return response; } + _logger.LogInformation("{CorrelationId} Customer record found in Cosmos DB {customerGuid}", correlationId, customerGuid); + + _logger.LogInformation("{CorrelationId} Attempting to get Subscriptions for customer {customerGuid} and subscription with {subscriptionsGuid}", correlationId, customerGuid, subscriptionsGuid); var subscriptions = await _subscriptionsPatchService.GetSubscriptionsForCustomerAsync(customerGuid, subscriptionsGuid); if (subscriptions == null) { - _logger.LogWarning($"PatchSubscriptionsHttpTrigger Customers/{customerId}/Subscriptions/{subscriptionId} subscriptions is null "); - return new NoContentResult(); + var response = new NoContentResult(); + _logger.LogWarning("{CorrelationId} Response Status Code: {StatusCode}. Subscriptions does not exist for customer {customerGuid} and subscription with {subscriptionsGuid}", correlationId, response.StatusCode, customerGuid, subscriptionsGuid); + return response; } + _logger.LogInformation("{CorrelationId} Attempting to Patch Subscription with ID {subscriptionsGuid}", correlationId, subscriptionsGuid); var updatedSubscriptions = await _subscriptionsPatchService.UpdateAsync(subscriptions, subscriptionsPatchRequest); - _logger.LogInformation($"PatchSubscriptionsHttpTrigger Customers/{customerId}/Subscriptions/{subscriptionId} updatedSubscriptions"); - - return updatedSubscriptions == null ? - new BadRequestObjectResult(subscriptionsGuid) : - new JsonResult(updatedSubscriptions, new JsonSerializerOptions()) { StatusCode = (int)HttpStatusCode.OK }; + if (updatedSubscriptions == null) + { + var response = new BadRequestObjectResult(subscriptionsGuid); + _logger.LogWarning("{CorrelationId} Response Status Code: {StatusCode}. Failed to patch the Subscription with ID {subscriptionsGuid}", correlationId, response.StatusCode, subscriptionsGuid); + _logger.LogInformation("Function {FunctionName} has finished invoking", functionName); + return response; + } + else + { + var response = new JsonResult(updatedSubscriptions, new JsonSerializerOptions()) { StatusCode = (int)HttpStatusCode.OK }; + _logger.LogWarning("{CorrelationId} Response Status Code: {StatusCode}. Successfully patched the Subscription with ID {subscriptionsGuid}", correlationId, response.StatusCode, subscriptionsGuid); + _logger.LogInformation("Function {FunctionName} has finished invoking", functionName); + return response; + } } } } \ No newline at end of file diff --git a/NCS.DSS.Subscriptions/PostSubscriptionsHttpTrigger/Function/PostSubscriptionsHttpTrigger.cs b/NCS.DSS.Subscriptions/PostSubscriptionsHttpTrigger/Function/PostSubscriptionsHttpTrigger.cs index 57c546e..efb5ae5 100644 --- a/NCS.DSS.Subscriptions/PostSubscriptionsHttpTrigger/Function/PostSubscriptionsHttpTrigger.cs +++ b/NCS.DSS.Subscriptions/PostSubscriptionsHttpTrigger/Function/PostSubscriptionsHttpTrigger.cs @@ -46,6 +46,21 @@ public PostSubscriptionsHttpTrigger( [Display(Name = "Post", Description = "Ability to create a new subscriptions for a given customer")] public async Task Run([HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "Customers/{customerId}/Subscriptions")] HttpRequest req, string customerId) { + var functionName = nameof(PostSubscriptionsHttpTrigger); + + _logger.LogInformation("Function {FunctionName} has been invoked", functionName); + + var correlationId = _httpRequestHelper.GetDssCorrelationId(req); + + if (string.IsNullOrEmpty(correlationId)) + _logger.LogInformation("Unable to locate 'DssCorrelationId' in request header"); + + if (!Guid.TryParse(correlationId, out var correlationGuid)) + { + _logger.LogInformation("Unable to parse 'DssCorrelationId' to a Guid. New Guid Generated."); + correlationGuid = Guid.NewGuid(); + } + var touchpointId = _httpRequestHelper.GetDssTouchpointId(req); if (string.IsNullOrEmpty(touchpointId)) { @@ -53,66 +68,86 @@ public async Task Run([HttpTrigger(AuthorizationLevel.Anonymous, return new BadRequestResult(); } - _logger.LogInformation("C# HTTP trigger function processed a request. By Touchpoint " + touchpointId); - if (!Guid.TryParse(customerId, out var customerGuid)) { _logger.LogInformation($"PostSubscriptionsHttpTrigger Customers/{customerId}/Subscriptions customerGuid BadRequest"); return new BadRequestObjectResult(customerGuid); } + _logger.LogInformation("{CorrelationId} Input validation has succeeded.", correlationId); Models.Subscriptions subscriptionsRequest; try { + _logger.LogInformation("{CorrelationId} Attempt to get resource from body of the request", correlationId); + subscriptionsRequest = await _httpRequestHelper.GetResourceFromRequest(req); } catch (Exception ex) { - _logger.LogError($"PostSubscriptionsHttpTrigger Customers/{customerId}/Subscriptions JsonSerializationException"); - return new UnprocessableEntityObjectResult(_convertToDynamic.ExcludeProperty(ex, ["TargetSite", "InnerException"])); + var response = new UnprocessableEntityObjectResult(_convertToDynamic.ExcludeProperty(ex, ["TargetSite", "InnerException"])); + _logger.LogError(ex, "{CorrelationId} Response Status Code: {StatusCode}. Unable to retrieve body from req", correlationId, response.StatusCode); + return response; } if (subscriptionsRequest == null) { - _logger.LogError($"PostSubscriptionsHttpTrigger Customers/{customerId}/Subscriptions subscriptionsRequest is null"); - return new UnprocessableEntityObjectResult(req); + var response = new UnprocessableEntityObjectResult(req); + _logger.LogWarning("{CorrelationId} Response Status Code: {StatusCode}. session patch request is null", correlationId, response.StatusCode); + return response; } - + _logger.LogInformation("{CorrelationId} Attempt to set id's for subscription post", correlationId); subscriptionsRequest.SetIds(customerGuid, touchpointId); - + _logger.LogInformation("{CorrelationId} Attempt to validate resource", correlationId); var errors = _validate.ValidateResource(subscriptionsRequest); if (errors != null && errors.Count > 0) { - _logger.LogError($"PostSubscriptionsHttpTrigger Customers/{customerId}/Subscriptions errors in ValidateResource"); - return new UnprocessableEntityObjectResult(errors); + var response = new UnprocessableEntityObjectResult(errors); + _logger.LogWarning("{CorrelationId} Response Status Code: {StatusCode}. validation errors with resource {Errors}", correlationId, response.StatusCode, errors); + return response; } + _logger.LogInformation("{CorrelationId} Attempting to see if customer exists {customerGuid}", correlationId, customerGuid); + var doesCustomerExist = await _subscriptionsPostService.DoesCustomerExist(customerGuid); if (!doesCustomerExist) { - _logger.LogError($"PostSubscriptionsHttpTrigger Customers/{customerId}/Subscriptions does notvCustomerExist "); - return new NoContentResult(); + var response = new NoContentResult(); + _logger.LogWarning("{CorrelationId} Response Status Code: {StatusCode}. Customer does not exist {CustomerId}", correlationId, customerGuid); + return response; } + _logger.LogInformation("{CorrelationId} Attempting to see if Subscription already exists for {customerGuid} and touchpoint with {touchpointId}", correlationId, customerGuid,touchpointId ); var doesSubscriptionExist = await _subscriptionsPostService.DoesSubscriptionExist(customerGuid, touchpointId); if (doesSubscriptionExist.HasValue) { var duplicateError = _validate.ValidateResultForDuplicateSubscriptionId(doesSubscriptionExist.GetValueOrDefault()); - _logger.LogError($"PostSubscriptionsHttpTrigger Customers/{customerId}/Subscriptions Subscription conflict. {duplicateError} "); - return new ConflictResult(); + var response = new ConflictResult(); + _logger.LogWarning("{CorrelationId} Response Status Code: {StatusCode}. Subscriptions exists for customer {customerGuid} and touchpoint with {touchpointId}. Error {Error}", correlationId, response.StatusCode, customerGuid, touchpointId, duplicateError.ErrorMessage); + return response; } - var subscriptions = await _subscriptionsPostService.CreateAsync(subscriptionsRequest); + _logger.LogInformation("{CorrelationId} Attempting to Create Subscription for Customer {customerGuid}", correlationId, customerGuid); - _logger.LogInformation($"PostSubscriptionsHttpTrigger Customers/{customerId}/Subscriptions CreateAsync called"); + var subscriptions = await _subscriptionsPostService.CreateAsync(subscriptionsRequest); - return subscriptions == null - ? new BadRequestObjectResult(customerGuid) - : new JsonResult(subscriptions, new JsonSerializerOptions()) { StatusCode = (int)HttpStatusCode.Created }; + if (subscriptions == null) + { + var response = new BadRequestObjectResult(customerGuid); + _logger.LogWarning("{CorrelationId} Response Status Code: {StatusCode}. Failed to Create the Subscription for Customer {customerGuid}", correlationId, response.StatusCode, customerGuid); + _logger.LogInformation("Function {FunctionName} has finished invoking", functionName); + return response; + } + else + { + var response = new JsonResult(subscriptions, new JsonSerializerOptions()) { StatusCode = (int)HttpStatusCode.Created }; + _logger.LogWarning("{CorrelationId} Response Status Code: {StatusCode}. Successfully patched the Subscription for Customer {customerGuid}", correlationId, response.StatusCode, customerGuid); + _logger.LogInformation("Function {FunctionName} has finished invoking", functionName); + return response; + } } } } \ No newline at end of file From 38366dcffe4ab989737bab1ef0e6c2b6a5fd01f9 Mon Sep 17 00:00:00 2001 From: Pradeep Kumar Voorukonda Date: Mon, 23 Dec 2024 12:12:27 +0000 Subject: [PATCH 5/8] json ignore for some fields --- NCS.DSS.Subscriptions/Models/Subscriptions.cs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/NCS.DSS.Subscriptions/Models/Subscriptions.cs b/NCS.DSS.Subscriptions/Models/Subscriptions.cs index 117ab2f..147d8a0 100644 --- a/NCS.DSS.Subscriptions/Models/Subscriptions.cs +++ b/NCS.DSS.Subscriptions/Models/Subscriptions.cs @@ -1,5 +1,6 @@ using DFC.Swagger.Standard.Annotations; using System.ComponentModel.DataAnnotations; +using System.Text.Json.Serialization; namespace NCS.DSS.Subscriptions.Models { @@ -24,10 +25,12 @@ public class Subscriptions : ISubscription [Example(Description = "true")] public bool? Subscribe { get; set; } + [JsonIgnore] [Display(Description = "Last modified date & time")] [Example(Description = "2018-06-21T14:45:00")] public DateTime? LastModifiedDate { get; set; } + [JsonIgnore] [StringLength(10, MinimumLength = 10)] [RegularExpression(@"^[0-9]+$")] [Display(Description = "Identifier of the touchpoint who made the last change to the record")] From e0c992575dcee1177c975394e149178072f8b544 Mon Sep 17 00:00:00 2001 From: Pradeep Kumar Voorukonda Date: Mon, 23 Dec 2024 14:39:10 +0000 Subject: [PATCH 6/8] revert back changes --- NCS.DSS.Subscriptions/Models/Subscriptions.cs | 3 --- 1 file changed, 3 deletions(-) diff --git a/NCS.DSS.Subscriptions/Models/Subscriptions.cs b/NCS.DSS.Subscriptions/Models/Subscriptions.cs index 147d8a0..117ab2f 100644 --- a/NCS.DSS.Subscriptions/Models/Subscriptions.cs +++ b/NCS.DSS.Subscriptions/Models/Subscriptions.cs @@ -1,6 +1,5 @@ using DFC.Swagger.Standard.Annotations; using System.ComponentModel.DataAnnotations; -using System.Text.Json.Serialization; namespace NCS.DSS.Subscriptions.Models { @@ -25,12 +24,10 @@ public class Subscriptions : ISubscription [Example(Description = "true")] public bool? Subscribe { get; set; } - [JsonIgnore] [Display(Description = "Last modified date & time")] [Example(Description = "2018-06-21T14:45:00")] public DateTime? LastModifiedDate { get; set; } - [JsonIgnore] [StringLength(10, MinimumLength = 10)] [RegularExpression(@"^[0-9]+$")] [Display(Description = "Identifier of the touchpoint who made the last change to the record")] From 526f3372c64b85793c05a9fe216d6e6cd4a24993 Mon Sep 17 00:00:00 2001 From: Pradeep Kumar Voorukonda Date: Mon, 23 Dec 2024 14:50:34 +0000 Subject: [PATCH 7/8] bug fix on logging --- .../Function/PatchSubscriptionsHttpTrigger.cs | 3 +-- .../Function/PostSubscriptionsHttpTrigger.cs | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/NCS.DSS.Subscriptions/PatchSubscriptionsHttpTrigger/Function/PatchSubscriptionsHttpTrigger.cs b/NCS.DSS.Subscriptions/PatchSubscriptionsHttpTrigger/Function/PatchSubscriptionsHttpTrigger.cs index 8d2e3b2..0338ff7 100644 --- a/NCS.DSS.Subscriptions/PatchSubscriptionsHttpTrigger/Function/PatchSubscriptionsHttpTrigger.cs +++ b/NCS.DSS.Subscriptions/PatchSubscriptionsHttpTrigger/Function/PatchSubscriptionsHttpTrigger.cs @@ -4,7 +4,6 @@ using Microsoft.AspNetCore.Mvc; using Microsoft.Azure.Functions.Worker; using Microsoft.Extensions.Logging; -using Microsoft.VisualBasic; using NCS.DSS.Subscriptions.Helpers; using NCS.DSS.Subscriptions.Models; using NCS.DSS.Subscriptions.PatchSubscriptionsHttpTrigger.Service; @@ -111,7 +110,7 @@ public async Task Run([HttpTrigger(AuthorizationLevel.Anonymous, if (errors != null && errors.Any()) { var response = new UnprocessableEntityObjectResult(errors); - _logger.LogWarning("{CorrelationId} Response Status Code: {StatusCode}. validation errors with resource {Errors}", correlationId, response.StatusCode, errors); + _logger.LogWarning("{CorrelationId} Response Status Code: {StatusCode}. validation errors with resource {Errors}", correlationId, response.StatusCode,string.Join(',', errors)); return response; } _logger.LogInformation("{CorrelationId} Attempting to see if customer exists {customerGuid}", correlationId, customerGuid); diff --git a/NCS.DSS.Subscriptions/PostSubscriptionsHttpTrigger/Function/PostSubscriptionsHttpTrigger.cs b/NCS.DSS.Subscriptions/PostSubscriptionsHttpTrigger/Function/PostSubscriptionsHttpTrigger.cs index efb5ae5..987505a 100644 --- a/NCS.DSS.Subscriptions/PostSubscriptionsHttpTrigger/Function/PostSubscriptionsHttpTrigger.cs +++ b/NCS.DSS.Subscriptions/PostSubscriptionsHttpTrigger/Function/PostSubscriptionsHttpTrigger.cs @@ -104,7 +104,7 @@ public async Task Run([HttpTrigger(AuthorizationLevel.Anonymous, if (errors != null && errors.Count > 0) { var response = new UnprocessableEntityObjectResult(errors); - _logger.LogWarning("{CorrelationId} Response Status Code: {StatusCode}. validation errors with resource {Errors}", correlationId, response.StatusCode, errors); + _logger.LogWarning("{CorrelationId} Response Status Code: {StatusCode}. validation errors with resource {Errors}", correlationId, response.StatusCode,string.Join(',',errors)); return response; } From b9f2b5209a06828632c57b40d734fe6b59eb4209 Mon Sep 17 00:00:00 2001 From: Pradeep Kumar Voorukonda Date: Mon, 23 Dec 2024 14:53:54 +0000 Subject: [PATCH 8/8] log fix --- .../Function/PatchSubscriptionsHttpTrigger.cs | 4 ++-- .../Function/PostSubscriptionsHttpTrigger.cs | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/NCS.DSS.Subscriptions/PatchSubscriptionsHttpTrigger/Function/PatchSubscriptionsHttpTrigger.cs b/NCS.DSS.Subscriptions/PatchSubscriptionsHttpTrigger/Function/PatchSubscriptionsHttpTrigger.cs index 0338ff7..3f82e9e 100644 --- a/NCS.DSS.Subscriptions/PatchSubscriptionsHttpTrigger/Function/PatchSubscriptionsHttpTrigger.cs +++ b/NCS.DSS.Subscriptions/PatchSubscriptionsHttpTrigger/Function/PatchSubscriptionsHttpTrigger.cs @@ -109,8 +109,8 @@ public async Task Run([HttpTrigger(AuthorizationLevel.Anonymous, if (errors != null && errors.Any()) { - var response = new UnprocessableEntityObjectResult(errors); - _logger.LogWarning("{CorrelationId} Response Status Code: {StatusCode}. validation errors with resource {Errors}", correlationId, response.StatusCode,string.Join(',', errors)); + var response = new UnprocessableEntityObjectResult(string.Join(',', errors)); + _logger.LogWarning("{CorrelationId} Response Status Code: {StatusCode}. validation errors with resource {Errors}", correlationId, response.StatusCode,errors); return response; } _logger.LogInformation("{CorrelationId} Attempting to see if customer exists {customerGuid}", correlationId, customerGuid); diff --git a/NCS.DSS.Subscriptions/PostSubscriptionsHttpTrigger/Function/PostSubscriptionsHttpTrigger.cs b/NCS.DSS.Subscriptions/PostSubscriptionsHttpTrigger/Function/PostSubscriptionsHttpTrigger.cs index 987505a..ae4295d 100644 --- a/NCS.DSS.Subscriptions/PostSubscriptionsHttpTrigger/Function/PostSubscriptionsHttpTrigger.cs +++ b/NCS.DSS.Subscriptions/PostSubscriptionsHttpTrigger/Function/PostSubscriptionsHttpTrigger.cs @@ -103,8 +103,8 @@ public async Task Run([HttpTrigger(AuthorizationLevel.Anonymous, if (errors != null && errors.Count > 0) { - var response = new UnprocessableEntityObjectResult(errors); - _logger.LogWarning("{CorrelationId} Response Status Code: {StatusCode}. validation errors with resource {Errors}", correlationId, response.StatusCode,string.Join(',',errors)); + var response = new UnprocessableEntityObjectResult(string.Join(',', errors)); + _logger.LogWarning("{CorrelationId} Response Status Code: {StatusCode}. validation errors with resource {Errors}", correlationId, response.StatusCode,errors); return response; }