Skip to content

Commit

Permalink
Merge pull request #504 from SkillsFundingAgency/CV-134-prevent-dupli…
Browse files Browse the repository at this point in the history
…cate-reservations

CV-134 Prevent duplicate reservations & publish CreatedDraftApprenticeshipEvent
  • Loading branch information
CThomp2018 authored May 15, 2019
2 parents fbdf2c4 + f8edcf8 commit 2180398
Show file tree
Hide file tree
Showing 11 changed files with 84 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Threading.Tasks;
using NServiceBus;
using SFA.DAS.CommitmentsV2.Messages.Events;
using SFA.DAS.EmployerAccounts.Messages.Events;
using SFA.DAS.EmployerAccounts.Types.Models;

Expand Down Expand Up @@ -68,6 +69,11 @@ await _publisher.Publish(new AddedLegalEntityEvent { AccountId = accountId, Crea
Console.WriteLine();
Console.WriteLine($"Published RemovedLegalEntityEvent");
break;
case "f":
await _publisher.Publish(new DraftApprenticeshipCreatedEvent(111111, 222222, "AAA111", Guid.NewGuid(), DateTime.UtcNow));
Console.WriteLine();
Console.WriteLine($"Published {nameof(DraftApprenticeshipCreatedEvent)}");
break;
}
}
catch (Exception e)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System;

namespace SFA.DAS.CommitmentsV2.Messages.Events
{
public class DraftApprenticeshipCreatedEvent
{
public long DraftApprenticeshipId { get; }
public long CohortId { get; }
public string Uln { get; }
public Guid ReservationId { get; }
public DateTime CreatedOn { get; }

public DraftApprenticeshipCreatedEvent(long draftApprenticeshipId, long cohortId, string uln, Guid reservationId, DateTime createdOn)
{
DraftApprenticeshipId = draftApprenticeshipId;
CohortId = cohortId;
Uln = uln;
ReservationId = reservationId;
CreatedOn = createdOn;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,12 @@ public Task Handle(PaymentOrderChangedEvent message, IMessageHandlerContext cont
return Log(message, context);
}
}

class DraftApprenticeshipCreatedEventHandler : GenericHandler, IHandleMessages<SFA.DAS.CommitmentsV2.Messages.Events.DraftApprenticeshipCreatedEvent>
{
public Task Handle(DraftApprenticeshipCreatedEvent message, IMessageHandlerContext context)
{
return Log(message, context);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using SFA.DAS.CommitmentsV2.Domain.Interfaces;
using SFA.DAS.CommitmentsV2.Services;
using ProgrammeType = SFA.DAS.CommitmentsV2.Types.ProgrammeType;
using SFA.DAS.UnitOfWork;

namespace SFA.DAS.CommitmentsV2.UnitTests.Models
{
Expand Down Expand Up @@ -123,13 +124,15 @@ public void StartDate_CheckTrainingProgrammeActive_BeforeOrAfterDas_Validation(D

public class AddDraftApprenticeshipValidationTestsFixture
{
public UnitOfWorkContext UnitOfWorkContext;
public DraftApprenticeshipDetails DraftApprenticeshipDetails;
public Cohort Cohort;
public ICurrentDateTime CurrentDateTime;
public IAcademicYearDateProvider AcademicYearDateProvider;

public AddDraftApprenticeshipValidationTestsFixture()
{
UnitOfWorkContext = new UnitOfWorkContext();
DraftApprenticeshipDetails = new DraftApprenticeshipDetails
{
TrainingProgramme = new TrainingProgramme("TEST", "TEST", ProgrammeType.Framework, DateTime.MinValue, DateTime.MaxValue)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
using System;
using System.Linq;
using NUnit.Framework;
using AutoFixture;
using Moq;
using FluentAssertions;
using NUnit.Framework;
using SFA.DAS.CommitmentsV2.Domain.Entities;
using SFA.DAS.CommitmentsV2.Types;
using SFA.DAS.CommitmentsV2.Messages.Events;
using SFA.DAS.CommitmentsV2.Models;
using SFA.DAS.UnitOfWork;

namespace SFA.DAS.CommitmentsV2.UnitTests.Domain.Provider
namespace SFA.DAS.CommitmentsV2.UnitTests.Models.Provider
{
[TestFixture]
public class WhenProviderCreatesCohort
Expand Down Expand Up @@ -77,16 +79,32 @@ public void TheCohortIsProviderOriginated()
Assert.AreEqual(Originator.Provider, result.Originator);
}

[Test]
public void TheDraftApprenticeshipCreatedEventIsPublished()
{
var result = _fixture.CreateCohort();

_fixture.UnitOfWorkContext.GetEvents().Should().HaveCount(1)
.And.Subject.Cast<DraftApprenticeshipCreatedEvent>().Single().Should().BeEquivalentTo(new DraftApprenticeshipCreatedEvent(
draftApprenticeshipId: 0,
cohortId: 0,
uln: _fixture.DraftApprenticeshipDetails.Uln,
reservationId: _fixture.DraftApprenticeshipDetails.ReservationId.Value,
createdOn: result.CreatedOn.Value));
}

private class ProviderCreatesCohortTestFixture
{
public UnitOfWorkContext UnitOfWorkContext { get; private set; }
public CommitmentsV2.Models.Provider Provider { get; private set; }
public AccountLegalEntity AccountLegalEntity { get; private set; }
private DraftApprenticeshipDetails DraftApprenticeshipDetails { get; set; }
public DraftApprenticeshipDetails DraftApprenticeshipDetails { get; set; }

public ProviderCreatesCohortTestFixture()
{
var fixture = new Fixture();

UnitOfWorkContext = new UnitOfWorkContext();
Provider = new CommitmentsV2.Models.Provider(fixture.Create<long>(), fixture.Create<string>(), fixture.Create<DateTime>(), fixture.Create<DateTime>());

var account = new Account(fixture.Create<long>(), fixture.Create<string>(), fixture.Create<string>(), fixture.Create<string>(), fixture.Create<DateTime>());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite.Core" Version="2.2.2" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.9.0" />
<PackageReference Include="Moq" Version="4.10.1" />
<PackageReference Include="morelinq" Version="3.1.0" />
<PackageReference Include="NUnit" Version="3.11.0" />
<PackageReference Include="NUnit3TestAdapter" Version="3.12.0" />
<PackageReference Include="SFA.DAS.HashingService" Version="1.0.183" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ public void Configure(EntityTypeBuilder<Apprenticeship> builder)
.WithMany(p => p.Apprenticeship)
.HasPrincipalKey(p => p.EpaOrgId)
.HasForeignKey(d => d.EpaOrgId);

builder.Property(e => e.ProgrammeType).HasColumnName("TrainingType");
}

private void SetTablePerHierarchy(EntityTypeBuilder<Apprenticeship> builder)
Expand Down
4 changes: 3 additions & 1 deletion src/CommitmentsV2/SFA.DAS.CommitmentsV2/Models/Cohort.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@
using SFA.DAS.CommitmentsV2.Domain;
using SFA.DAS.CommitmentsV2.Domain.Entities;
using SFA.DAS.CommitmentsV2.Domain.Exceptions;
using SFA.DAS.CommitmentsV2.Messages.Events;
using TrainingProgrammeStatus = SFA.DAS.Apprenticeships.Api.Types.TrainingProgrammeStatus;

namespace SFA.DAS.CommitmentsV2.Models
{
public class Cohort
public class Cohort : Entity
{
public Cohort()
{
Expand Down Expand Up @@ -52,6 +53,7 @@ public virtual void AddDraftApprenticeship(DraftApprenticeshipDetails draftAppre
ValidateDraftApprenticeshipDetails(draftApprenticeshipDetails);
var draftApprenticeship = new DraftApprenticeship(draftApprenticeshipDetails, Originator);
Apprenticeship.Add(draftApprenticeship);
Publish(() => new DraftApprenticeshipCreatedEvent(draftApprenticeship.Id, Id, draftApprenticeship.Uln, draftApprenticeship.ReservationId.Value, CreatedOn.Value));
}

private void ValidateDraftApprenticeshipDetails(DraftApprenticeshipDetails draftApprenticeshipDetails)
Expand Down
13 changes: 13 additions & 0 deletions src/CommitmentsV2/SFA.DAS.CommitmentsV2/Models/Entity.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System;
using SFA.DAS.UnitOfWork;

namespace SFA.DAS.CommitmentsV2.Models
{
public abstract class Entity
{
protected void Publish<T>(Func<T> action) where T : class
{
UnitOfWorkContext.AddEvent(action);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
<ProjectReference Include="..\..\SFA.DAS.Reservations.Api.Client\SFA.DAS.Reservations.Api.Client.csproj" />
<ProjectReference Include="..\SFA.DAS.CommitmentsV2.Api.Types\SFA.DAS.CommitmentsV2.Api.Types.csproj" />
<ProjectReference Include="..\SFA.DAS.CommitmentsV2.Types\SFA.DAS.CommitmentsV2.Types.csproj" />
<ProjectReference Include="..\SFA.DAS.CommitmentsV2.Messages\SFA.DAS.CommitmentsV2.Messages.csproj" />
</ItemGroup>

</Project>
5 changes: 3 additions & 2 deletions src/SFA.DAS.Commitments.Database/Tables/Apprenticeship.sql
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
[PendingUpdateOriginator] TINYINT NULL,
[EPAOrgId] CHAR(7) NULL,
[CloneOf] BIGINT NULL,
[ReservationId] UNIQUEIDENTIFIER,
[ReservationId] UNIQUEIDENTIFIER NULL,
[IsApproved] AS (case when [PaymentStatus]>(0) then CONVERT([bit],(1)) else CONVERT([bit],(0)) end) PERSISTED,
CONSTRAINT [FK_Apprenticeship_Commitment] FOREIGN KEY ([CommitmentId]) REFERENCES [Commitment]([Id]),
CONSTRAINT [FK_Apprenticeship_AssessmentOrganisation] FOREIGN KEY ([EPAOrgId]) REFERENCES [AssessmentOrganisation]([EPAOrgId])
Expand All @@ -45,4 +45,5 @@ CREATE NONCLUSTERED INDEX [IX_Apprenticeship_AgreedOn] ON [dbo].[Apprenticeship]
GO
CREATE NONCLUSTERED INDEX [IX_Apprenticeship_Uln_PaymentStatus] ON [dbo].[Apprenticeship] ([PaymentStatus], [ULN]) INCLUDE ([AgreedOn], [CommitmentId], [StartDate], [StopDate]) WITH (ONLINE = ON)
GO

CREATE UNIQUE NONCLUSTERED INDEX [UK_Apprenticeship_ReservationId] ON [dbo].[Apprenticeship] ([ReservationId] ASC) WHERE [ReservationId] IS NOT NULL
GO

0 comments on commit 2180398

Please sign in to comment.