diff --git a/Services/CO.CDP.Organisation.WebApi.Tests/UseCase/RegisterOrganisationUseCaseTest.cs b/Services/CO.CDP.Organisation.WebApi.Tests/UseCase/RegisterOrganisationUseCaseTest.cs index bb5ae7c4e..720a55fdf 100644 --- a/Services/CO.CDP.Organisation.WebApi.Tests/UseCase/RegisterOrganisationUseCaseTest.cs +++ b/Services/CO.CDP.Organisation.WebApi.Tests/UseCase/RegisterOrganisationUseCaseTest.cs @@ -10,6 +10,7 @@ using CO.CDP.OrganisationInformation.Persistence; using FluentAssertions; using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.Logging; using Moq; using Persistence = CO.CDP.OrganisationInformation.Persistence; @@ -22,7 +23,8 @@ public class RegisterOrganisationUseCaseTest : IClassFixture private readonly Mock _persons = new(); private readonly Mock _publisher = new(); private readonly Mock _notifyApiClient = new(); - private readonly IConfiguration _mockConfiguration; + private readonly IConfiguration _mockConfiguration; + private readonly Mock> _logger = new(); private readonly Guid _generatedGuid = Guid.NewGuid(); private readonly AutoMapperFixture _mapperFixture; private RegisterOrganisationUseCase UseCase => new( @@ -33,10 +35,11 @@ public class RegisterOrganisationUseCaseTest : IClassFixture _publisher.Object, _mapperFixture.Mapper, _mockConfiguration, + _logger.Object, () => _generatedGuid); public RegisterOrganisationUseCaseTest(AutoMapperFixture mapperFixture) - { + { _mapperFixture = mapperFixture; var inMemorySettings = new List> { @@ -51,6 +54,55 @@ public RegisterOrganisationUseCaseTest(AutoMapperFixture mapperFixture) .Build(); } + [Fact] + public async Task ItShouldLogErrorWhenConfigurationKeysAreMissing() + { + IConfiguration _configurationMock; + var inMemorySettings = new List> + { + new("GOVUKNotify:PersonInviteEmailTemplateId", ""), + new("OrganisationAppUrl", ""), + new("GOVUKNotify:RequestReviewApplicationEmailTemplateId", ""), + new("GOVUKNotify:SupportAdminEmailAddress", ""), + }; + + _configurationMock = new ConfigurationBuilder() + .AddInMemoryCollection(inMemorySettings) + .Build(); + + var person = GivenPersonExists(guid: Guid.NewGuid()); + + var command = GivenRegisterOrganisationCommand(personId: person.Guid, roles: [PartyRole.Buyer]); + + + _persons.Setup(p => p.Find(command.PersonId)).ReturnsAsync(person); + + var useCase = new RegisterOrganisationUseCase( + _identifierService.Object, + _repository.Object, + _persons.Object, + _notifyApiClient.Object, + _publisher.Object, + _mapperFixture.Mapper, + _configurationMock, + _logger.Object, + () => _generatedGuid + ); + + await useCase.Execute(command); + + _logger.Verify( + x => x.Log( + LogLevel.Error, + It.IsAny(), + It.Is((v, t) => v.ToString()!.Contains("Missing configuration keys: OrganisationAppUrl, GOVUKNotify:RequestReviewApplicationEmailTemplateId, GOVUKNotify:SupportAdminEmailAddress")), + It.IsAny(), + It.Is>((v, t) => true) + ), Times.Once); + + _notifyApiClient.Verify(g => g.SendEmail(It.IsAny()), Times.Never); + } + [Fact] public async Task ItReturnsTheRegisteredOrganisation() { diff --git a/Services/CO.CDP.Organisation.WebApi/UseCase/RegisterOrganisationUseCase.cs b/Services/CO.CDP.Organisation.WebApi/UseCase/RegisterOrganisationUseCase.cs index 8b62145a6..8b3ffdc0a 100644 --- a/Services/CO.CDP.Organisation.WebApi/UseCase/RegisterOrganisationUseCase.cs +++ b/Services/CO.CDP.Organisation.WebApi/UseCase/RegisterOrganisationUseCase.cs @@ -17,6 +17,7 @@ public class RegisterOrganisationUseCase( IPublisher publisher, IMapper mapper, IConfiguration configuration, + ILogger logger, Func guidFactory) : IUseCase { @@ -29,7 +30,8 @@ public RegisterOrganisationUseCase( IGovUKNotifyApiClient govUKNotifyApiClient, IPublisher publisher, IMapper mapper, - IConfiguration configuration) + IConfiguration configuration, + ILogger logger) : this(identifierService, organisationRepository, personRepository, @@ -37,6 +39,7 @@ public RegisterOrganisationUseCase( publisher, mapper, configuration, + logger, Guid.NewGuid) { } @@ -91,14 +94,21 @@ Person person } private async Task NotifyAdminOfApprovalRequest(OrganisationInformation.Persistence.Organisation organisation) { - var baseAppUrl = configuration.GetValue("OrganisationAppUrl") - ?? throw new Exception("Missing configuration key: OrganisationAppUrl"); + var baseAppUrl = configuration.GetValue("OrganisationAppUrl") ?? ""; + var templateId = configuration.GetValue("GOVUKNotify:RequestReviewApplicationEmailTemplateId") ?? ""; + var supportAdminEmailAddress = configuration.GetValue("GOVUKNotify:SupportAdminEmailAddress") ?? ""; - var templateId = configuration.GetValue("GOVUKNotify:RequestReviewApplicationEmailTemplateId") - ?? throw new Exception("Missing configuration key: GOVUKNotify:RequestReviewApplicationEmailTemplateId."); + var missingConfigs = new List(); - var supportAdminEmailAddress = configuration.GetValue("GOVUKNotify:SupportAdminEmailAddress") - ?? throw new Exception("Missing configuration key: GOVUKNotify:SupportAdminEmailAddress"); + if (string.IsNullOrEmpty(baseAppUrl)) missingConfigs.Add("OrganisationAppUrl"); + if (string.IsNullOrEmpty(templateId)) missingConfigs.Add("GOVUKNotify:RequestReviewApplicationEmailTemplateId"); + if (string.IsNullOrEmpty(supportAdminEmailAddress)) missingConfigs.Add("GOVUKNotify:SupportAdminEmailAddress"); + + if (missingConfigs.Count != 0) + { + logger.LogError(new Exception("Unable to send email to support admin"), $"Missing configuration keys: {string.Join(", ", missingConfigs)}. Unable to send email to support admin."); + return; + } var requestLink = new Uri(new Uri(baseAppUrl), $"/support/organisation/{organisation.Guid}/approval").ToString(); @@ -107,10 +117,10 @@ private async Task NotifyAdminOfApprovalRequest(OrganisationInformation.Persiste EmailAddress = supportAdminEmailAddress, TemplateId = templateId, Personalisation = new Dictionary - { - { "org_name", organisation.Name }, - { "request_link", requestLink } - } + { + { "org_name", organisation.Name }, + { "request_link", requestLink } + } }; await govUKNotifyApiClient.SendEmail(emailRequest); diff --git a/Services/CO.CDP.Organisation.WebApi/appsettings.json b/Services/CO.CDP.Organisation.WebApi/appsettings.json index 02c9d2e45..c30442978 100644 --- a/Services/CO.CDP.Organisation.WebApi/appsettings.json +++ b/Services/CO.CDP.Organisation.WebApi/appsettings.json @@ -18,7 +18,7 @@ "PersonInviteEmailTemplateId": "94beca4e-2ccc-4b57-8cab-6940305596db", "RequestReviewApplicationEmailTemplateId": "5b383432-5382-4a85-a19a-20e341332cf2", "BuyerApprovedEmailTemplateId": "ebb6702c-5c08-4ed1-a611-18311f5072ee", - "SupportAdminEmailAddress": "dharmesh.patel@goaco.com" + "SupportAdminEmailAddress": "" }, "ForwardedHeaders": { "KnownNetwork": ""