Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ namespace DigitalLearningSolutions.Data.Tests.Services
using DigitalLearningSolutions.Data.Exceptions;
using DigitalLearningSolutions.Data.Models.DelegateUpload;
using DigitalLearningSolutions.Data.Models.Register;
using DigitalLearningSolutions.Data.Models.Supervisor;
using DigitalLearningSolutions.Data.Services;
using DigitalLearningSolutions.Data.Tests.TestHelpers;
using FakeItEasy;
Expand All @@ -30,6 +31,7 @@ public class DelegateUploadFileServiceTests
private DelegateUploadFileService delegateUploadFileService = null!;
private IJobGroupsDataService jobGroupsDataService = null!;
private IRegistrationDataService registrationDataService = null!;
private ISupervisorDelegateService supervisorDelegateService = null!;
private IUserDataService userDataService = null!;
private IUserService userService = null!;

Expand All @@ -44,11 +46,17 @@ public void SetUp()
userDataService = A.Fake<IUserDataService>(x => x.Strict());
userService = A.Fake<IUserService>(x => x.Strict());
registrationDataService = A.Fake<IRegistrationDataService>(x => x.Strict());
supervisorDelegateService = A.Fake<ISupervisorDelegateService>();

A.CallTo(() => userDataService.GetDelegateUserByCandidateNumber(A<string>._, A<int>._))
.Returns(UserTestHelper.GetDefaultDelegateUser());


delegateUploadFileService = new DelegateUploadFileService(
jobGroupsDataService,
userDataService,
registrationDataService,
supervisorDelegateService,
userService
);
}
Expand Down Expand Up @@ -618,6 +626,104 @@ public void ProcessDelegateTable_calls_register_with_expected_values_when_welcom
result.RegisteredCount.Should().Be(1);
}

[Test]
[TestCase("-4")]
[TestCase("-3")]
[TestCase("-2")]
[TestCase("-1")]
public void ProcessDelegateTable_unsuccessful_register_does_not_update_supervisor_delegates(
string failureStatusCode
)
{
// Given
const string aliasId = "ALIAS";
var row = GetSampleDelegateDataRow(candidateNumber: string.Empty, aliasId: aliasId);
var table = CreateTableFromData(new[] { row });

A.CallTo(() => userDataService.GetDelegateUserByAliasId(aliasId, CentreId)).Returns(null);
A.CallTo(() => userService.IsDelegateEmailValidForCentre("email@test.com", CentreId)).Returns(true);
A.CallTo(() => registrationDataService.RegisterDelegateByCentre(A<DelegateRegistrationModel>._))
.Returns(failureStatusCode);

try
{
// When
delegateUploadFileService.ProcessDelegatesTable(table, CentreId);
}
catch (ArgumentOutOfRangeException ex)
{
// Then
ex.Message.Should().Be($"Unknown return value when creating delegate record. (Parameter 'errorCodeOrCandidateNumber')\r\nActual value was {failureStatusCode}.");
ex.ActualValue.Should().Be(failureStatusCode);
}
finally
{
// Then
A.CallTo(() => registrationDataService.RegisterDelegateByCentre(A<DelegateRegistrationModel>._))
.MustHaveHappened();
A.CallTo(
() => supervisorDelegateService.GetPendingSupervisorDelegateRecordsByEmailAndCentre(
A<int>._,
A<string>._
)
).MustNotHaveHappened();
A.CallTo(
() => supervisorDelegateService.AddDelegateIdToSupervisorDelegateRecords(
A<IEnumerable<int>>._,
A<int>._
)
).MustNotHaveHappened();
}
}

[Test]
public void ProcessDelegateTable_successful_register_updates_supervisor_delegates()
{
// Given
const string candidateNumber = "DELEGATE";
const string aliasId = "ALIAS";
const int newDelegateRecordId = 5;
var row = GetSampleDelegateDataRow(candidateNumber: string.Empty, aliasId: aliasId);
var table = CreateTableFromData(new[] { row });
var supervisorDelegates = new List<SupervisorDelegate>
{ new SupervisorDelegate { ID = 1 }, new SupervisorDelegate { ID = 2 } };
var supervisorDelegateIds = new List<int> { 1, 2 };

A.CallTo(() => userDataService.GetDelegateUserByAliasId(aliasId, CentreId)).Returns(null);
A.CallTo(() => userService.IsDelegateEmailValidForCentre("email@test.com", CentreId)).Returns(true);
A.CallTo(() => registrationDataService.RegisterDelegateByCentre(A<DelegateRegistrationModel>._))
.Returns(candidateNumber);
A.CallTo(() => userDataService.GetDelegateUserByCandidateNumber(candidateNumber, CentreId))
.Returns(UserTestHelper.GetDefaultDelegateUser(newDelegateRecordId));
A.CallTo(
() =>
supervisorDelegateService.GetPendingSupervisorDelegateRecordsByEmailAndCentre(A<int>._, A<string>._)
).Returns(supervisorDelegates);
A.CallTo(
() =>
supervisorDelegateService.AddDelegateIdToSupervisorDelegateRecords(A<IEnumerable<int>>._, A<int>._)
).DoesNothing();

// When
delegateUploadFileService.ProcessDelegatesTable(table, CentreId);

// Then
A.CallTo(() => registrationDataService.RegisterDelegateByCentre(A<DelegateRegistrationModel>._))
.MustHaveHappened();
A.CallTo(
() => supervisorDelegateService.GetPendingSupervisorDelegateRecordsByEmailAndCentre(
CentreId,
"email@test.com"
)
).MustHaveHappened();
A.CallTo(
() => supervisorDelegateService.AddDelegateIdToSupervisorDelegateRecords(
A<IEnumerable<int>>.That.IsSameSequenceAs(supervisorDelegateIds),
newDelegateRecordId
)
).MustHaveHappened();
}

[Test]
public void ProcessDelegateTable_counts_updated_correctly()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
{
using System;
using DigitalLearningSolutions.Data.DataServices;
using DigitalLearningSolutions.Data.DataServices.UserDataService;
using DigitalLearningSolutions.Data.Models.Supervisor;
using DigitalLearningSolutions.Data.Services;
using FakeItEasy;
Expand All @@ -13,14 +12,12 @@ public class SupervisorDelegateServiceTests
{
private ISupervisorDelegateDataService supervisorDelegateDataService = null!;
private ISupervisorDelegateService supervisorDelegateService = null!;
private IUserDataService userDataService = null!;

[SetUp]
public void SetUp()
{
supervisorDelegateDataService = A.Fake<ISupervisorDelegateDataService>();
userDataService = A.Fake<IUserDataService>();
supervisorDelegateService = new SupervisorDelegateService(supervisorDelegateDataService, userDataService);
supervisorDelegateService = new SupervisorDelegateService(supervisorDelegateDataService);
}

[Test]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,21 @@ public class DelegateUploadFileService : IDelegateUploadFileService
{
private readonly IJobGroupsDataService jobGroupsDataService;
private readonly IRegistrationDataService registrationDataService;
private readonly ISupervisorDelegateService supervisorDelegateService;
private readonly IUserDataService userDataService;
private readonly IUserService userService;

public DelegateUploadFileService(
IJobGroupsDataService jobGroupsDataService,
IUserDataService userDataService,
IRegistrationDataService registrationDataService,
ISupervisorDelegateService supervisorDelegateService,
IUserService userService
)
{
this.userDataService = userDataService;
this.registrationDataService = registrationDataService;
this.supervisorDelegateService = supervisorDelegateService;
this.jobGroupsDataService = jobGroupsDataService;
this.userService = userService;
}
Expand Down Expand Up @@ -184,8 +187,8 @@ private void UpdateDelegate(DelegateTableRow delegateRow, DelegateUser delegateU
private void RegisterDelegate(DelegateTableRow delegateRow, DateTime? welcomeEmailDate, int centreId)
{
var model = new DelegateRegistrationModel(delegateRow, centreId, welcomeEmailDate);
var status = registrationDataService.RegisterDelegateByCentre(model);
switch (status)
var errorCodeOrCandidateNumber = registrationDataService.RegisterDelegateByCentre(model);
switch (errorCodeOrCandidateNumber)
{
case "-1":
delegateRow.Error = BulkUploadResult.ErrorReason.UnexpectedErrorForCreate;
Expand All @@ -194,16 +197,37 @@ private void RegisterDelegate(DelegateTableRow delegateRow, DateTime? welcomeEma
case "-3":
case "-4":
throw new ArgumentOutOfRangeException(
nameof(status),
status,
nameof(errorCodeOrCandidateNumber),
errorCodeOrCandidateNumber,
"Unknown return value when creating delegate record."
);
default:
var newDelegateRecord = userDataService.GetDelegateUserByCandidateNumber(errorCodeOrCandidateNumber, centreId)!;
SetUpSupervisorDelegateRelations(delegateRow.Email!, centreId, newDelegateRecord.Id);
delegateRow.RowStatus = RowStatus.Registered;
break;
}
}

private void SetUpSupervisorDelegateRelations(string emailAddress, int centreId, int delegateId)
{
var pendingSupervisorDelegateIds =
supervisorDelegateService.GetPendingSupervisorDelegateRecordsByEmailAndCentre(
centreId,
emailAddress
).Select(supervisor => supervisor.ID).ToList();

if (!pendingSupervisorDelegateIds.Any())
{
return;
}

supervisorDelegateService.AddDelegateIdToSupervisorDelegateRecords(
pendingSupervisorDelegateIds,
delegateId
);
}

private static bool ValidateHeaders(IXLTable table)
{
var expectedHeaders = new List<string>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using System;
using System.Collections.Generic;
using DigitalLearningSolutions.Data.DataServices;
using DigitalLearningSolutions.Data.DataServices.UserDataService;
using DigitalLearningSolutions.Data.Models.Supervisor;

public interface ISupervisorDelegateService
Expand All @@ -20,23 +19,23 @@ public interface ISupervisorDelegateService
public class SupervisorDelegateService : ISupervisorDelegateService
{
private readonly ISupervisorDelegateDataService supervisorDelegateDataService;
private readonly IUserDataService userDataService;

public SupervisorDelegateService(
ISupervisorDelegateDataService supervisorDelegateDataService,
IUserDataService userDataService
ISupervisorDelegateDataService supervisorDelegateDataService
)
{
this.supervisorDelegateDataService = supervisorDelegateDataService;
this.userDataService = userDataService;
}

public SupervisorDelegate? GetSupervisorDelegateRecordByInviteHash(Guid inviteHash)
{
return supervisorDelegateDataService.GetSupervisorDelegateRecordByInviteHash(inviteHash);
}

public IEnumerable<SupervisorDelegate> GetPendingSupervisorDelegateRecordsByEmailAndCentre(int centreId, string email)
public IEnumerable<SupervisorDelegate> GetPendingSupervisorDelegateRecordsByEmailAndCentre(
int centreId,
string email
)
{
return supervisorDelegateDataService.GetPendingSupervisorDelegateRecordsByEmailAndCentre(centreId, email);
}
Expand Down