Skip to content

Commit

Permalink
Increment CallbackBookingQuota NumberOfBookings when creating PhoneCall
Browse files Browse the repository at this point in the history
When we create a `PhoneCall` entity we need to find the applicable
`CallbackBookingQuota` (if one exists) and increment the `NumberOfBookings` so
that the quota will no longer be available once full.
  • Loading branch information
ethax-ross committed Sep 17, 2020
1 parent 09a72ea commit 936be9c
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 1 deletion.
20 changes: 20 additions & 0 deletions GetIntoTeachingApi/Jobs/UpsertCandidateJob.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public void Run(Candidate candidate, PerformContext context)
SaveCandidate(candidate);
SaveTeachingEventRegistrations(registrations, candidate);
SavePhoneCall(phoneCall, candidate);
IncrementCallbackBookingQuotaNumberOfBookings(phoneCall);

_logger.LogInformation("UpsertCandidateJob - Succeeded");
}
Expand Down Expand Up @@ -91,6 +92,25 @@ private void SaveTeachingEventRegistrations(IEnumerable<TeachingEventRegistratio
}
}

private void IncrementCallbackBookingQuotaNumberOfBookings(PhoneCall phoneCall)
{
if (phoneCall == null)
{
return;
}

var quota = _crm.GetCallbackBookingQuota(phoneCall.ScheduledAt);

if (quota == null || !quota.IsAvailable)
{
return;
}

quota.NumberOfBookings += 1;

_crm.Save(quota);
}

private void SavePhoneCall(PhoneCall phoneCall, Candidate candidate)
{
if (phoneCall == null)
Expand Down
10 changes: 9 additions & 1 deletion GetIntoTeachingApi/Services/CrmService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,15 @@ public IEnumerable<CallbackBookingQuota> GetCallbackBookingQuotas()
.OrderBy((entity) => entity.GetAttributeValue<DateTime>("dfe_starttime"))
.Select((entity) => new CallbackBookingQuota(entity, this))
.ToList()
.Where((quota) => quota.NumberOfBookings < quota.Quota); // Doing this in the Dynamics query throws an exception, though I'm not sure why.
.Where((quota) => quota.IsAvailable); // Doing this in the Dynamics query throws an exception, though I'm not sure why.
}

public CallbackBookingQuota GetCallbackBookingQuota(DateTime scheduledAt)
{
return _service.CreateQuery("dfe_callbackbookingquota", Context())
.Where((entity) => entity.GetAttributeValue<DateTime>("dfe_starttime") == scheduledAt)
.Select((entity) => new CallbackBookingQuota(entity, this))
.FirstOrDefault();
}

public IEnumerable<PrivacyPolicy> GetPrivacyPolicies()
Expand Down
1 change: 1 addition & 0 deletions GetIntoTeachingApi/Services/ICrmService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public interface ICrmService
Candidate GetCandidate(Guid id);
IEnumerable<TeachingEvent> GetTeachingEvents();
IEnumerable<CallbackBookingQuota> GetCallbackBookingQuotas();
CallbackBookingQuota GetCallbackBookingQuota(DateTime scheduledAt);
bool CandidateYetToAcceptPrivacyPolicy(Guid candidateId, Guid privacyPolicyId);
bool CandidateYetToRegisterForTeachingEvent(Guid candidateId, Guid teachingEventId);
void Save(BaseModel model);
Expand Down
36 changes: 36 additions & 0 deletions GetIntoTeachingApiTests/Jobs/UpsertCandidateJobTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,42 @@ public void Run_WithPhoneCallOnSuccess_SavesPhoneCall()
phoneCall.CandidateId.Should().Be(candidateId.ToString());
}

[Fact]
public void Run_WithPhoneCallOnSuccess_IncrementsCallbackBookingQuotaNumberOfBookings()
{
var candidateId = Guid.NewGuid();
var scheduledAt = DateTime.UtcNow.AddDays(3);
var phoneCall = new PhoneCall() { ScheduledAt = scheduledAt };
var quota = new CallbackBookingQuota() { StartAt = scheduledAt, NumberOfBookings = 5, Quota = 10 };
_candidate.PhoneCall = phoneCall;
_mockContext.Setup(m => m.GetRetryCount(null)).Returns(0);
_mockCrm.Setup(mock => mock.Save(_candidate)).Callback(() => _candidate.Id = candidateId);
_mockCrm.Setup(mock => mock.GetCallbackBookingQuota(scheduledAt)).Returns(quota);

_job.Run(_candidate, null);

_mockCrm.Verify(mock => mock.Save(quota), Times.Once);
quota.NumberOfBookings.Should().Be(6);
}

[Fact]
public void Run_WithPhoneCallOnSuccessButMatchingQuotaIsAlreadyFull_DoesNotIncrementsCallbackBookingQuotaNumberOfBookings()
{
var candidateId = Guid.NewGuid();
var scheduledAt = DateTime.UtcNow.AddDays(3);
var phoneCall = new PhoneCall() { ScheduledAt = scheduledAt };
var quota = new CallbackBookingQuota() { StartAt = scheduledAt, NumberOfBookings = 5, Quota = 5 };
_candidate.PhoneCall = phoneCall;
_mockContext.Setup(m => m.GetRetryCount(null)).Returns(0);
_mockCrm.Setup(mock => mock.Save(_candidate)).Callback(() => _candidate.Id = candidateId);
_mockCrm.Setup(mock => mock.GetCallbackBookingQuota(scheduledAt)).Returns(quota);

_job.Run(_candidate, null);

_mockCrm.Verify(mock => mock.Save(quota), Times.Never);
quota.NumberOfBookings.Should().Be(5);
}

[Fact]
public void Run_OnFailure_EmailsCandidate()
{
Expand Down
14 changes: 14 additions & 0 deletions GetIntoTeachingApiTests/Services/CrmServiceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,20 @@ public void GetCallbackBookingQuotas_ReturnsFutureQuotasUpTo14DaysInAdvanceExclu
options => options.WithStrictOrdering());
}

[Fact]
public void GetCallbackBookingQuota_ReturnsQuotaMatchingScheduledAt()
{
var queryableQuotas = MockCallbackBookingQuotas();
_mockService.Setup(mock => mock.CreateQuery("dfe_callbackbookingquota", _context))
.Returns(queryableQuotas);
var quota = queryableQuotas.ToArray()[3];
var startAt = quota.GetAttributeValue<DateTime>("dfe_starttime");

var result = _crm.GetCallbackBookingQuota(startAt);

result.StartAt.Should().Be(startAt);
}

[Fact]
public void GetPrivacyPolicies_Returns3MostRecentActiveWebPrivacyPolicies()
{
Expand Down

0 comments on commit 936be9c

Please sign in to comment.