Skip to content

Commit

Permalink
DP-589 - Notify support admin refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
dpatel017 committed Oct 2, 2024
1 parent 1790c59 commit 1e31f7c
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -22,7 +23,8 @@ public class RegisterOrganisationUseCaseTest : IClassFixture<AutoMapperFixture>
private readonly Mock<Persistence.IPersonRepository> _persons = new();
private readonly Mock<IPublisher> _publisher = new();
private readonly Mock<IGovUKNotifyApiClient> _notifyApiClient = new();
private readonly IConfiguration _mockConfiguration;
private readonly IConfiguration _mockConfiguration;
private readonly Mock<ILogger<RegisterOrganisationUseCase>> _logger = new();
private readonly Guid _generatedGuid = Guid.NewGuid();
private readonly AutoMapperFixture _mapperFixture;
private RegisterOrganisationUseCase UseCase => new(
Expand All @@ -33,10 +35,11 @@ public class RegisterOrganisationUseCaseTest : IClassFixture<AutoMapperFixture>
_publisher.Object,
_mapperFixture.Mapper,
_mockConfiguration,
_logger.Object,
() => _generatedGuid);

public RegisterOrganisationUseCaseTest(AutoMapperFixture mapperFixture)
{
{
_mapperFixture = mapperFixture;
var inMemorySettings = new List<KeyValuePair<string, string?>>
{
Expand All @@ -51,6 +54,55 @@ public RegisterOrganisationUseCaseTest(AutoMapperFixture mapperFixture)
.Build();
}

[Fact]
public async Task ItShouldLogErrorWhenConfigurationKeysAreMissing()
{
IConfiguration _configurationMock;
var inMemorySettings = new List<KeyValuePair<string, string?>>
{
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<EventId>(),
It.Is<It.IsAnyType>((v, t) => v.ToString()!.Contains("Missing configuration keys: OrganisationAppUrl, GOVUKNotify:RequestReviewApplicationEmailTemplateId, GOVUKNotify:SupportAdminEmailAddress")),
It.IsAny<Exception>(),
It.Is<Func<It.IsAnyType, Exception?, string>>((v, t) => true)
), Times.Once);

_notifyApiClient.Verify(g => g.SendEmail(It.IsAny<EmailNotificationRequest>()), Times.Never);
}

[Fact]
public async Task ItReturnsTheRegisteredOrganisation()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public class RegisterOrganisationUseCase(
IPublisher publisher,
IMapper mapper,
IConfiguration configuration,
ILogger<RegisterOrganisationUseCase> logger,
Func<Guid> guidFactory)
: IUseCase<RegisterOrganisation, Model.Organisation>
{
Expand All @@ -29,14 +30,16 @@ public RegisterOrganisationUseCase(
IGovUKNotifyApiClient govUKNotifyApiClient,
IPublisher publisher,
IMapper mapper,
IConfiguration configuration)
IConfiguration configuration,
ILogger<RegisterOrganisationUseCase> logger)
: this(identifierService,
organisationRepository,
personRepository,
govUKNotifyApiClient,
publisher,
mapper,
configuration,
logger,
Guid.NewGuid)
{
}
Expand Down Expand Up @@ -91,14 +94,21 @@ Person person
}
private async Task NotifyAdminOfApprovalRequest(OrganisationInformation.Persistence.Organisation organisation)
{
var baseAppUrl = configuration.GetValue<string>("OrganisationAppUrl")
?? throw new Exception("Missing configuration key: OrganisationAppUrl");
var baseAppUrl = configuration.GetValue<string>("OrganisationAppUrl") ?? "";
var templateId = configuration.GetValue<string>("GOVUKNotify:RequestReviewApplicationEmailTemplateId") ?? "";
var supportAdminEmailAddress = configuration.GetValue<string>("GOVUKNotify:SupportAdminEmailAddress") ?? "";

var templateId = configuration.GetValue<string>("GOVUKNotify:RequestReviewApplicationEmailTemplateId")
?? throw new Exception("Missing configuration key: GOVUKNotify:RequestReviewApplicationEmailTemplateId.");
var missingConfigs = new List<string>();

var supportAdminEmailAddress = configuration.GetValue<string>("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();

Expand All @@ -107,10 +117,10 @@ private async Task NotifyAdminOfApprovalRequest(OrganisationInformation.Persiste
EmailAddress = supportAdminEmailAddress,
TemplateId = templateId,
Personalisation = new Dictionary<string, string>
{
{ "org_name", organisation.Name },
{ "request_link", requestLink }
}
{
{ "org_name", organisation.Name },
{ "request_link", requestLink }
}
};

await govUKNotifyApiClient.SendEmail(emailRequest);
Expand Down
2 changes: 1 addition & 1 deletion Services/CO.CDP.Organisation.WebApi/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"PersonInviteEmailTemplateId": "94beca4e-2ccc-4b57-8cab-6940305596db",
"RequestReviewApplicationEmailTemplateId": "5b383432-5382-4a85-a19a-20e341332cf2",
"BuyerApprovedEmailTemplateId": "ebb6702c-5c08-4ed1-a611-18311f5072ee",
"SupportAdminEmailAddress": "[email protected]"
"SupportAdminEmailAddress": ""
},
"ForwardedHeaders": {
"KnownNetwork": ""
Expand Down

0 comments on commit 1e31f7c

Please sign in to comment.