Skip to content

Commit

Permalink
Merge pull request #755 from SkillsFundingAgency/CON-2087-DB-Changes
Browse files Browse the repository at this point in the history
CON-2087-DB-Changes new column MadeRedundant.
  • Loading branch information
Najamuddin-Muhammad authored Jul 29, 2020
2 parents 7705a9b + d1c67fb commit 312da9d
Show file tree
Hide file tree
Showing 16 changed files with 62 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,6 @@ public class Apprenticeship
public Guid? ReservationId { get; set; }
public DateTime? OriginalStartDate { get; set; }
public long? ContinuationOfId { get; set; }
public bool? MadeRedundant { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ public sealed class ApprenticeshipSubmission
public DateTime DateOfChange { get; set; }
public string UserId { get; set; }
public LastUpdateInfo LastUpdatedByInfo { get; set; }
public bool? MadeRedundant { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,23 @@ public async Task ThenAccountLegalEntityPublicHashedIdIsMapped(GetApprenticeship
Assert.AreEqual(accountLegalEntityPublicHashedId, result.Content.AccountLegalEntityPublicHashedId);
}

[Test, AutoData]
public async Task ThenMadeRedundantIsMapped(GetApprenticeshipResponse mediatorResponse)
{
_mockMediator.Setup(x => x.SendAsync(It.IsAny<GetApprenticeshipRequest>())).ReturnsAsync(mediatorResponse);

// for this unit test we want a controller where the employerOrchestrator contains a real ApprenticeshipMapper
_employerOrchestrator = new EmployerOrchestrator(_mockMediator.Object, Mock.Of<ICommitmentsLogger>(), new FacetMapper(Mock.Of<ICurrentDateTime>()), new ApprenticeshipFilterService(new FacetMapper(Mock.Of<ICurrentDateTime>())),
new ApprenticeshipMapper(), _commitmentMapper.Object, Mock.Of<ITransferRequestMapper>(), Mock.Of<IHashingService>());

_controller = new EmployerController(_employerOrchestrator, _apprenticeshipsOrchestrator);

var result = await _controller.GetApprenticeship(TestProviderId, TestApprenticeshipId) as OkNegotiatedContentResult<Apprenticeship.Apprenticeship>;

result.Content.Should().NotBeNull();
Assert.AreEqual(mediatorResponse.Data.MadeRedundant, result.Content.MadeRedundant);
}

[Test]
public async Task ThenTheMediatorIsCalledWithTheCommitmentIdApprenticeshipIdProviderId()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,8 @@ await _mediator.SendAsync(new StopApprenticeshipCommand
ApprenticeshipId = apprenticeshipId,
DateOfChange = apprenticeshipSubmission.DateOfChange,
UserId = apprenticeshipSubmission.UserId,
UserName = apprenticeshipSubmission.LastUpdatedByInfo?.Name
UserName = apprenticeshipSubmission.LastUpdatedByInfo?.Name,
MadeRedundant = apprenticeshipSubmission.MadeRedundant
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ public Apprenticeship MapFrom(Domain.Entities.Apprenticeship source, CallerType
HasHadDataLockSuccess = source.HasHadDataLockSuccess,
EndpointAssessorName = source.EndpointAssessorName,
ReservationId = source.ReservationId,
ContinuationOfId = source.ContinuationOfId
ContinuationOfId = source.ContinuationOfId,
MadeRedundant = source.MadeRedundant
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
using SFA.DAS.Commitments.Application.Exceptions;
using SFA.DAS.Commitments.Domain;
using SFA.DAS.Commitments.Domain.Entities;
using SFA.DAS.Commitments.Domain.Entities.AcademicYear;
using SFA.DAS.Commitments.Domain.Entities.DataLock;
using SFA.DAS.Commitments.Domain.Entities.History;

Expand Down Expand Up @@ -225,7 +224,21 @@ public async Task ThenShouldCallTheRepositoryToUpdateTheStatus()
MockApprenticeshipRespository.Verify(x => x.StopApprenticeship(
It.Is<long>(a => a == 123L),
It.Is<long>(a => a == ExampleValidRequest.ApprenticeshipId),
It.Is<DateTime>(a => a == ExampleValidRequest.DateOfChange)));
It.Is<DateTime>(a => a == ExampleValidRequest.DateOfChange),null));
}

[TestCase(true)]
[TestCase(false)]
public async Task ThenShouldCallTheRepositoryToUpdateTheStatus_WithRedundancyStatus(bool madeRedundant)
{
ExampleValidRequest.MadeRedundant = madeRedundant;

await Handler.Handle(ExampleValidRequest);

MockApprenticeshipRespository.Verify(x => x.StopApprenticeship(
It.Is<long>(a => a == 123L),
It.Is<long>(a => a == ExampleValidRequest.ApprenticeshipId),
It.Is<DateTime>(a => a == ExampleValidRequest.DateOfChange), madeRedundant));
}

[Test]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ public virtual void SetUp()
MockCommitmentsLogger.Object,
MockHistoryRepository.Object,
MockDataLockRepository.Object,
MockAcademicYearValidator.Object,
MockV2EventsPublisher.Object);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
using SFA.DAS.Commitments.Application.Exceptions;
using SFA.DAS.Commitments.Domain;
using SFA.DAS.Commitments.Domain.Entities;
using SFA.DAS.Commitments.Domain.Entities.AcademicYear;
using SFA.DAS.Commitments.Domain.Entities.DataLock;
using SFA.DAS.Commitments.Domain.Entities.History;

Expand Down Expand Up @@ -168,7 +167,8 @@ public async Task ThenShouldCallTheRepositoryToUpdateTheStatus()
MockApprenticeshipRespository.Verify(x => x.StopApprenticeship(
It.Is<long>(a => a == 123L),
It.Is<long>(a => a == ExampleValidRequest.ApprenticeshipId),
It.Is<DateTime>(a => a == ExampleValidRequest.DateOfChange)));
It.Is<DateTime>(a => a == ExampleValidRequest.DateOfChange),
It.IsAny<bool?>()));
}

[Test]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ public abstract class ApprenticeshipStatusChangeCommand : IAsyncRequest
{
public long AccountId { get; set; }
public long ApprenticeshipId { get; set; }

public Caller Caller { get; set; }
public DateTime DateOfChange { get; set; }
public string UserId { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
{
public sealed class StopApprenticeshipCommand : ApprenticeshipStatusChangeCommand
{
public bool? MadeRedundant { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
using System.Threading.Tasks;
using SFA.DAS.Commitments.Application.Interfaces;
using SFA.DAS.Commitments.Application.Services;
using SFA.DAS.Commitments.Domain.Entities.AcademicYear;
using SFA.DAS.Commitments.Domain.Entities.DataLock;
using SFA.DAS.Commitments.Domain.Entities.History;

Expand All @@ -26,7 +25,6 @@ public sealed class StopApprenticeshipCommandHandler : AsyncRequestHandler<StopA
private readonly IHistoryRepository _historyRepository;
private readonly IApprenticeshipEvents _eventsApi;
private readonly IDataLockRepository _dataLockRepository;
private readonly IAcademicYearValidator _academicYearValidator;
private readonly IV2EventsPublisher _v2EventsPublisher;

private const DataLockErrorCode CourseChangeErrors = DataLockErrorCode.Dlock03 | DataLockErrorCode.Dlock04 | DataLockErrorCode.Dlock05 | DataLockErrorCode.Dlock06;
Expand All @@ -40,7 +38,6 @@ public StopApprenticeshipCommandHandler(
ICommitmentsLogger logger,
IHistoryRepository historyRepository,
IDataLockRepository dataLockRepository,
IAcademicYearValidator academicYearValidator,
IV2EventsPublisher v2EventsPublisher
)
{
Expand All @@ -52,7 +49,6 @@ IV2EventsPublisher v2EventsPublisher
_logger = logger;
_historyRepository = historyRepository;
_dataLockRepository = dataLockRepository;
_academicYearValidator = academicYearValidator;
_v2EventsPublisher = v2EventsPublisher;
}

Expand Down Expand Up @@ -80,12 +76,12 @@ protected override async Task HandleCore(StopApprenticeshipCommand command)

private Task CreateEvent(StopApprenticeshipCommand command, Apprenticeship apprenticeship, Commitment commitment)
{
var tasks = new Task[]
var tasks = new[]
{
_eventsApi.PublishChangeApprenticeshipStatusEvent(
commitment, apprenticeship,
apprenticeship.PaymentStatus,
effectiveFrom: command.DateOfChange.Date),
command.DateOfChange.Date),

_v2EventsPublisher.PublishApprenticeshipStopped(commitment, apprenticeship)
};
Expand All @@ -100,8 +96,9 @@ private async Task SaveChange(StopApprenticeshipCommand command, Commitment comm
historyService.TrackUpdate(apprenticeship, ApprenticeshipChangeType.ChangeOfStatus.ToString(), null, apprenticeship.Id, CallerType.Employer, command.UserId, apprenticeship.ProviderId, apprenticeship.EmployerAccountId, command.UserName);
apprenticeship.PaymentStatus = PaymentStatus.Withdrawn;
apprenticeship.StopDate = command.DateOfChange;

await _apprenticeshipRepository.StopApprenticeship(commitment.Id, command.ApprenticeshipId, command.DateOfChange);
apprenticeship.MadeRedundant = command.MadeRedundant;

await _apprenticeshipRepository.StopApprenticeship(commitment.Id, command.ApprenticeshipId, command.DateOfChange, command.MadeRedundant);

if (command.DateOfChange == apprenticeship.StartDate)
{
Expand All @@ -111,7 +108,7 @@ private async Task SaveChange(StopApprenticeshipCommand command, Commitment comm
{
await ResolveAnyTriagedCourseDataLocks(command.ApprenticeshipId);
}

await historyService.Save();
}

Expand Down Expand Up @@ -149,7 +146,7 @@ private bool IsCourseChangeError(DataLockErrorCode errorCode)
private void ValidateChangeDateForStop(DateTime dateOfChange, Apprenticeship apprenticeship)
{
if (apprenticeship == null) throw new ArgumentException(nameof(apprenticeship));

if (apprenticeship.IsWaitingToStart(_currentDate))
{
if (dateOfChange.Date != apprenticeship.StartDate.Value.Date)
Expand All @@ -160,8 +157,8 @@ private void ValidateChangeDateForStop(DateTime dateOfChange, Apprenticeship app
if (dateOfChange.Date > _currentDate.Now.Date)
throw new ValidationException("Invalid Date of Change. Date cannot be in the future.");

if ( dateOfChange.Date < apprenticeship.StartDate.Value.Date)
throw new ValidationException("Invalid Date of Change. Date cannot be before the training start date.");
if (dateOfChange.Date < apprenticeship.StartDate.Value.Date)
throw new ValidationException("Invalid Date of Change. Date cannot be before the training start date.");
}
}

Expand Down
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 @@ -34,9 +34,10 @@
[IsApproved] AS (CASE WHEN [PaymentStatus] > (0) THEN CONVERT([BIT], (1)) ELSE CONVERT([BIT], (0)) END) PERSISTED,
[CompletionDate] DATETIME NULL,
[ContinuationOfId] BIGINT NULL,
[MadeRedundant] BIT NULL,
[OriginalStartDate] DATETIME NULL
CONSTRAINT [FK_Apprenticeship_Commitment] FOREIGN KEY ([CommitmentId]) REFERENCES [Commitment]([Id]),
CONSTRAINT [FK_Apprenticeship_AssessmentOrganisation] FOREIGN KEY ([EPAOrgId]) REFERENCES [AssessmentOrganisation]([EPAOrgId])
CONSTRAINT [FK_Apprenticeship_Commitment] FOREIGN KEY ([CommitmentId]) REFERENCES [Commitment]([Id]),
CONSTRAINT [FK_Apprenticeship_AssessmentOrganisation] FOREIGN KEY ([EPAOrgId]) REFERENCES [AssessmentOrganisation]([EPAOrgId])
)
GO
CREATE NONCLUSTERED INDEX [IX_Apprenticeship_CommitmentId] ON [dbo].[Apprenticeship] ([CommitmentId]) INCLUDE ([AgreedOn], [AgreementStatus], [Cost], [CreatedOn], [DateOfBirth], [EmployerRef], [EndDate], [FirstName], [LastName], [NINumber], [PaymentOrder], [PaymentStatus], [ProviderRef], [StartDate], [TrainingCode], [TrainingName], [TrainingType], [ULN], [StopDate], [PauseDate], [HasHadDataLockSuccess], [PendingUpdateOriginator]) WITH (ONLINE = ON)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ SELECT
END AS 'ProviderCanApproveApprenticeship',
ao.Name AS 'EndpointAssessorName',
a.ReservationId,
a.OriginalStartDate
a.OriginalStartDate,
a.MadeRedundant
FROM
Apprenticeship a
INNER JOIN
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public interface IApprenticeshipRepository
{
Task UpdateApprenticeship(Apprenticeship apprenticeship, Caller caller);

Task StopApprenticeship(long commitmentId, long apprenticeshipId, DateTime dateOfChange);
Task StopApprenticeship(long commitmentId, long apprenticeshipId, DateTime dateOfChange, bool? madeRedundant);

Task ResumeApprenticeship(long commitmentId, long apprenticeshipId);

Expand Down
1 change: 1 addition & 0 deletions src/SFA.DAS.Commitments.Domain/Entities/Apprenticeship.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ public Apprenticeship()
public DateTime? OriginalStartDate { get; set; }
public long? ContinuationOfId { get; set; }
public List<DataLockStatusSummary> DataLocks { get; set; }
public bool? MadeRedundant { get; set; }

public Apprenticeship Clone()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ await WithTransaction(async (connection, trans) =>
});
}

public async Task StopApprenticeship(long commitmentId, long apprenticeshipId, DateTime dateOfChange)
public async Task StopApprenticeship(long commitmentId, long apprenticeshipId, DateTime dateOfChange, bool? madeRedundant)
{
_logger.Debug($"Stopping apprenticeship {apprenticeshipId} for commitment {commitmentId}", commitmentId: commitmentId, apprenticeshipId: apprenticeshipId);

Expand All @@ -54,10 +54,13 @@ await WithTransaction(async (conn, tran) =>
parameters.Add("@id", apprenticeshipId, DbType.Int64);
parameters.Add("@paymentStatus", PaymentStatus.Withdrawn, DbType.Int16);
parameters.Add("@stopDate", dateOfChange, DbType.Date);
parameters.Add("@madeRedundant", madeRedundant, DbType.Byte);

var returnCode = await conn.ExecuteAsync(
sql:
"UPDATE [dbo].[Apprenticeship] SET PaymentStatus = @paymentStatus, StopDate = @stopDate " +
"UPDATE [dbo].[Apprenticeship] SET PaymentStatus = @paymentStatus, " +
"StopDate = @stopDate, " +
"MadeRedundant = @madeRedundant " +
"WHERE PaymentStatus != 4 AND Id = @id;",
transaction: tran,
param: parameters,
Expand Down

0 comments on commit 312da9d

Please sign in to comment.