Skip to content

Commit

Permalink
Merge pull request #1196 from cabinetoffice/DP-1060-PPON-Consortium-Code
Browse files Browse the repository at this point in the history
DP-1060 - PPON - Consortium Code
  • Loading branch information
dbgoaco authored Jan 30, 2025
2 parents 99c1d43 + dc92004 commit 8b07dd1
Show file tree
Hide file tree
Showing 10 changed files with 45 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ public static OrganisationRegistered GivenOrganisationRegisteredEvent(
Uri = null
}
],
Roles = ["supplier"]
Roles = ["supplier"],
Type = OrganisationInformation.OrganisationType.Organisation
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public async Task Action_GeneratesPponIdAndPersistsAndPublishes()
var publisher = new Mock<IPublisher>();
var generatedPpon = "92be415e5985421087bc8fee8c97d338";

pponService.Setup(x => x.GeneratePponId()).Returns(generatedPpon);
pponService.Setup(x => x.GeneratePponId(OrganisationInformation.OrganisationType.Organisation)).Returns(generatedPpon);

var handler = new OrganisationRegisteredSubscriber(pponService.Object, pponRepository.Object, publisher.Object);
var orgId = Guid.NewGuid();
Expand Down
17 changes: 15 additions & 2 deletions Services/CO.CDP.EntityVerification.Tests/Ppon/PponServiceTests.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,31 @@
using CO.CDP.EntityVerification.Ppon;
using CO.CDP.OrganisationInformation;
using System.Text.RegularExpressions;

namespace CO.CDP.EntityVerification.Tests.Ppon;

public class PponServiceTests
{
[Fact]
public void GeneratePponId_CreatesNewPponId_ReturnsPponIdWithValidLength()
public void GeneratePponId_CreatesNewPponIdForNonConsortium_ReturnsPponIdWithValidLength()
{
PponService ppon = new PponService();
string pponId = ppon.GeneratePponId();
string pponId = ppon.GeneratePponId(OrganisationType.Organisation);
string pattern = @"^P[A-Z]{3}-\d{4}-[A-Z]{4}$";
bool isMatch = Regex.IsMatch(pponId, pattern);

Assert.True(isMatch);
}

[Fact]
public void GeneratePponId_CreatesNewPponIdForConsortium_ReturnsPponIdWithValidLengthWithLastCharAsC()
{
PponService ppon = new PponService();
string pponId = ppon.GeneratePponId(OrganisationType.InformalConsortium);
string pattern = @"^P[A-Z]{3}-\d{4}-[A-Z]{3}C$";
bool isMatch = Regex.IsMatch(pponId, pattern);

Assert.True(isMatch);
}

}
4 changes: 4 additions & 0 deletions Services/CO.CDP.EntityVerification/Events/InboundEvents.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using CO.CDP.OrganisationInformation;

namespace CO.CDP.EntityVerification.Events;

public interface IEvent;
Expand All @@ -17,6 +19,8 @@ public record OrganisationRegistered : IEvent
public IEnumerable<Identifier> AllIdentifiers() => AdditionalIdentifiers.Prepend(Identifier);

public required List<string> Roles { get; init; }

public required OrganisationType Type { get; init; }
}

public record OrganisationUpdated : IEvent
Expand Down
4 changes: 3 additions & 1 deletion Services/CO.CDP.EntityVerification/Ppon/IPponService.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using CO.CDP.OrganisationInformation;

namespace CO.CDP.EntityVerification.Ppon;

public interface IPponService
{
string GeneratePponId();
string GeneratePponId(OrganisationType type);
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public async Task Handle(OrganisationRegistered @event)
{
Persistence.Ppon newPpon = new()
{
IdentifierId = pponService.GeneratePponId(),
IdentifierId = pponService.GeneratePponId(@event.Type),
Name = @event.Name,
OrganisationId = @event.Id,
Identifiers = Identifier.GetPersistenceIdentifiers(@event.AllIdentifiers())
Expand Down
19 changes: 12 additions & 7 deletions Services/CO.CDP.EntityVerification/Ppon/PponService.cs
Original file line number Diff line number Diff line change
@@ -1,31 +1,36 @@
using CO.CDP.OrganisationInformation;
using System.Text;

namespace CO.CDP.EntityVerification.Ppon;

public class PponService : IPponService
{
private readonly char[] ValidAlphaChars = "BCDGHJLMNPQRTVWXYZ".ToCharArray();
private readonly char[] ValidLastAlphaChars = "DGHJLMNPQRTVWXYZ".ToCharArray();
private readonly char[] ValidLastAlphaCharsForNonConsortium = "DGHJLMNPQRTVWXYZ".ToCharArray();
private readonly char[] ValidNumericChars = "123456789".ToCharArray();
private readonly Random RandomGenerator = new();

public string GeneratePponId()
public string GeneratePponId(OrganisationType type)
{
// TODO: requires new param to indicate type of org e.g. Consortium will have a C as the last character in future.

return $"P{GenerateCodePattern().Substring(1)}";
return $"P{GenerateCodePattern(type).Substring(1)}";
}

private string GenerateCodePattern()
private string GenerateCodePattern(OrganisationType type)
{
var builder = new StringBuilder();
for (int i = 0; i < 12; i++)
{
if (i == 11)
{
char nextChar = ValidLastAlphaChars[RandomGenerator.Next(ValidLastAlphaChars.Length)];
char nextChar = ValidLastAlphaCharsForNonConsortium[RandomGenerator.Next(ValidLastAlphaCharsForNonConsortium.Length)];

if (type == OrganisationType.InformalConsortium)
{
nextChar = 'C';
}

builder.Append(nextChar);

}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,8 @@ public async Task ItPublishesOrganisationRegisteredEvent()
AdditionalIdentifiers = command.AdditionalIdentifiers.AsEventValue(),
Addresses = command.Addresses.AsEventValue(),
ContactPoint = command.ContactPoint.AsEventValue(),
Roles = command.Roles.AsEventValue()
Roles = command.Roles.AsEventValue(),
Type = OrganisationType.Organisation
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,7 @@ private void OrganisationEventsMapping()
.ForMember(m => m.AdditionalIdentifiers, o => o.MapFrom(m => m.Identifiers.Where(i => !i.Primary)))
.ForMember(m => m.ContactPoint, o => o.MapFrom(m => m.ContactPoints.FirstOrDefault() ?? new Persistence.Organisation.ContactPoint()))
.ForMember(m => m.Addresses, o => o.MapFrom(m => m.Addresses))
.ForMember(m => m.Type, o => o.MapFrom(m => m.Type))
.ForMember(m => m.Roles, o => o.MapFrom(m => m.Roles.Select(r => r.AsCode())));
CreateMap<Persistence.Organisation, OrganisationUpdated>()
.ForMember(m => m.Id, o => o.MapFrom(m => m.Guid))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using CO.CDP.OrganisationInformation;

namespace CO.CDP.Organisation.WebApi.Events;

public record OrganisationRegistered
Expand All @@ -17,4 +19,6 @@ public record OrganisationRegistered
public required ContactPoint ContactPoint { get; init; }

public required List<string> Roles { get; init; }

public required OrganisationType Type { get; init; }
}

0 comments on commit 8b07dd1

Please sign in to comment.