Skip to content

Commit

Permalink
PIDP-980 - Update Keycloak Email on First Update (#568)
Browse files Browse the repository at this point in the history
Co-authored-by: Panos Hatzinikolaou <45132887+Paahn@users.noreply.github.com>
  • Loading branch information
james-hollinger and Paahn committed Jul 15, 2024
1 parent b233ec0 commit aa37dd8
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 43 deletions.
15 changes: 9 additions & 6 deletions backend/webapi.tests/Features/Parties/Demographics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace PidpTests.Features.Parties;
public class DemographicsTests : InMemoryDbTest
{
[Fact]
public async void UpdatePartyDemographics_NewParty_PropertiesUpdatedNoDomainEvent()
public async Task UpdatePartyDemographics_NewParty_PropertiesUpdatedDomainEvent()
{
var party = this.TestDb.HasAParty(party =>
{
Expand All @@ -19,7 +19,7 @@ public async void UpdatePartyDemographics_NewParty_PropertiesUpdatedNoDomainEven
var command = new Command
{
Id = party.Id,
Email = "pidp.test@goh.com",
Email = "pidp.test@email.com",
Phone = "5555555555",
PreferredFirstName = "PFirst",
PreferredMiddleName = "PMid",
Expand All @@ -36,11 +36,14 @@ public async void UpdatePartyDemographics_NewParty_PropertiesUpdatedNoDomainEven
Assert.Equal(command.PreferredMiddleName, updatedParty.PreferredMiddleName);
Assert.Equal(command.PreferredLastName, updatedParty.PreferredLastName);

Assert.Empty(this.PublishedEvents.OfType<PartyEmailUpdated>());
Assert.Single(this.PublishedEvents.OfType<PartyEmailUpdated>());
var emailEvent = this.PublishedEvents.OfType<PartyEmailUpdated>().Single();
Assert.Equal(party.Id, emailEvent.PartyId);
Assert.Equal(command.Email, emailEvent.NewEmail);
}

[Fact]
public async void UpdatePartyDemographics_ExistingParty_PropertiesUpdatedDomainEvent()
public async Task UpdatePartyDemographics_ExistingParty_PropertiesUpdatedDomainEvent()
{
var party = this.TestDb.HasAParty(party =>
{
Expand Down Expand Up @@ -79,7 +82,7 @@ public async void UpdatePartyDemographics_ExistingParty_PropertiesUpdatedDomainE
}

[Fact]
public async void UpdatePartyDemographics_ExistingParty_TrimPreferredNames()
public async Task UpdatePartyDemographics_ExistingParty_TrimPreferredNames()
{
var party = this.TestDb.HasAParty(party =>
{
Expand Down Expand Up @@ -114,7 +117,7 @@ public async void UpdatePartyDemographics_ExistingParty_TrimPreferredNames()
}

[Fact]
public async void UpdatePartyDemographics_ExistingPartySameEmail_NoDomainEvent()
public async Task UpdatePartyDemographics_ExistingPartySameEmail_NoDomainEvent()
{
var party = this.TestDb.HasAParty(party => party.Email = "existing@email.com");
var command = new Command
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public static Party HasAParty(this PidpDbContext context, Action<Party>? config
{
var party = new Party();
config?.Invoke(party);
if (!party.Credentials.Any())
if (party.Credentials.Count == 0)
{
party.Credentials.Add(new Credential { UserId = Guid.NewGuid() });
}
Expand Down
53 changes: 17 additions & 36 deletions backend/webapi/Features/Parties/Demographics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,16 +63,10 @@ public CommandValidator()
}
}

public class QueryHandler : IQueryHandler<Query, Command>
public class QueryHandler(IMapper mapper, PidpDbContext context) : IQueryHandler<Query, Command>
{
private readonly IMapper mapper;
private readonly PidpDbContext context;

public QueryHandler(IMapper mapper, PidpDbContext context)
{
this.mapper = mapper;
this.context = context;
}
private readonly IMapper mapper = mapper;
private readonly PidpDbContext context = context;

public async Task<Command> HandleAsync(Query query)
{
Expand All @@ -83,20 +77,17 @@ public async Task<Command> HandleAsync(Query query)
}
}

public class CommandHandler : ICommandHandler<Command>
public class CommandHandler(PidpDbContext context) : ICommandHandler<Command>
{
private readonly PidpDbContext context;

public CommandHandler(PidpDbContext context) => this.context = context;
private readonly PidpDbContext context = context;

public async Task HandleAsync(Command command)
{
var party = await this.context.Parties
.Include(party => party.Credentials)
.SingleAsync(party => party.Id == command.Id);

if (party.Email != null
&& party.Email != command.Email)
if (party.Email != command.Email)
{
party.DomainEvents.Add(new PartyEmailUpdated(party.Id, party.PrimaryUserId, command.Email!));
}
Expand All @@ -111,11 +102,9 @@ public async Task HandleAsync(Command command)
}
}

public class PartyEmailUpdatedHandler : INotificationHandler<PartyEmailUpdated>
public class PartyEmailUpdatedHandler(IBus bus) : INotificationHandler<PartyEmailUpdated>
{
private readonly IBus bus;

public PartyEmailUpdatedHandler(IBus bus) => this.bus = bus;
private readonly IBus bus = bus;

public async Task Handle(PartyEmailUpdated notification, CancellationToken cancellationToken)
{
Expand All @@ -126,24 +115,16 @@ public async Task Handle(PartyEmailUpdated notification, CancellationToken cance
}
}

public class PartyEmailUpdatedBcProviderConsumer : IConsumer<PartyEmailUpdated>
public class PartyEmailUpdatedBcProviderConsumer(
IBCProviderClient client,
PidpConfiguration config,
PidpDbContext context,
ILogger<PartyEmailUpdatedBcProviderConsumer> logger) : IConsumer<PartyEmailUpdated>
{
private readonly IBCProviderClient client;
private readonly PidpDbContext context;
private readonly string bcProviderClientId;
private readonly ILogger<PartyEmailUpdatedBcProviderConsumer> logger;

public PartyEmailUpdatedBcProviderConsumer(
IBCProviderClient client,
PidpConfiguration config,
PidpDbContext context,
ILogger<PartyEmailUpdatedBcProviderConsumer> logger)
{
this.client = client;
this.context = context;
this.bcProviderClientId = config.BCProviderClient.ClientId;
this.logger = logger;
}
private readonly IBCProviderClient client = client;
private readonly PidpDbContext context = context;
private readonly string bcProviderClientId = config.BCProviderClient.ClientId;
private readonly ILogger<PartyEmailUpdatedBcProviderConsumer> logger = logger;

public async Task Consume(ConsumeContext<PartyEmailUpdated> context)
{
Expand Down

0 comments on commit aa37dd8

Please sign in to comment.