public async Task <ReadOnlyCollection <FundingSourcePaymentEvent> > HandleMonthEnd(ProcessLevyPaymentsOnMonthEndCommand command) { paymentLogger.LogVerbose($"Handling ProcessLevyPaymentsOnMonthEndCommand for {Id}, Job: {command.JobId}, Account: {command.AccountId}"); try { using (var operation = telemetry.StartOperation()) { var stopwatch = Stopwatch.StartNew(); var fundingSourceEvents = await fundingSourceService.HandleMonthEnd(command.AccountId, command.JobId); telemetry.StopOperation(operation); return(fundingSourceEvents); } } catch (Exception ex) { paymentLogger.LogError($"Failed to get levy or co-invested month end payments. Error: {ex.Message}", ex); throw; } }
public async Task TestProcessRequiredPayments() { // arrange var keys = new List <string> { "1" }; var requiredPaymentEvent = new CalculatedRequiredLevyAmount { EventId = Guid.NewGuid(), AmountDue = 100, SfaContributionPercentage = 11, OnProgrammeEarningType = OnProgrammeEarningType.Completion, Learner = new Learner(), }; var balance = 100m; var transferAllowance = 50; var levyPayment = new LevyPayment { AmountDue = 55, Type = FundingSourceType.Levy }; var employerCoInvestedPayment = new EmployerCoInvestedPayment { AmountDue = 44, Type = FundingSourceType.CoInvestedEmployer }; var sfaCoInvestedPayment = new SfaCoInvestedPayment { AmountDue = 33, Type = FundingSourceType.CoInvestedSfa }; var allPayments = new FundingSourcePayment[] { levyPayment, employerCoInvestedPayment, sfaCoInvestedPayment }; generateSortedPaymentKeysMock .Setup(x => x.GeyKeys()) .ReturnsAsync(keys) .Verifiable(); eventCacheMock.Setup(c => c.TryGet("1", CancellationToken.None)) .ReturnsAsync(() => new ConditionalValue <CalculatedRequiredLevyAmount>(true, requiredPaymentEvent)) .Verifiable(); levyAccountRepositoryMock.Setup(r => r.GetLevyAccount(666, CancellationToken.None)) .ReturnsAsync(() => new LevyAccountModel { AccountId = 666, Balance = balance, TransferAllowance = transferAllowance }) .Verifiable(); levyBalanceServiceMock.Setup(s => s.Initialise(balance, transferAllowance)).Verifiable(); processorMock.Setup(p => p.Process(It.IsAny <RequiredPayment>())).Returns(() => allPayments).Verifiable(); eventCacheMock.Setup(c => c.Clear("1", CancellationToken.None)).Returns(Task.CompletedTask).Verifiable(); refundSortKeysCacheMock.Setup(c => c.Clear(CacheKeys.RefundPaymentsKeyListKey, CancellationToken.None)).Returns(Task.CompletedTask).Verifiable(); transferPaymentSortKeysCacheMock.Setup(c => c.Clear(CacheKeys.SenderTransferKeyListKey, CancellationToken.None)).Returns(Task.CompletedTask).Verifiable(); requiredPaymentSortKeysCacheMock.Setup(c => c.Clear(CacheKeys.RequiredPaymentKeyListKey, CancellationToken.None)).Returns(Task.CompletedTask).Verifiable(); levyAccountCacheMock.Setup(c => c.AddOrReplace(It.Is <string>(key => key.Equals(CacheKeys.LevyBalanceKey)), It.Is <LevyAccountModel>(model => model.AccountId == 666), It.IsAny <CancellationToken>())) .Returns(Task.CompletedTask) .Verifiable(); // act var fundingSourcePayments = await service.HandleMonthEnd(666, 1); // assert fundingSourcePayments.Should().HaveCount(3); fundingSourcePayments[0].Should().BeOfType <LevyFundingSourcePaymentEvent>(); fundingSourcePayments[1].Should().BeOfType <EmployerCoInvestedFundingSourcePaymentEvent>(); fundingSourcePayments[2].Should().BeOfType <SfaCoInvestedFundingSourcePaymentEvent>(); fundingSourcePayments[0].AmountDue.Should().Be(55); fundingSourcePayments[1].AmountDue.Should().Be(44); fundingSourcePayments[2].AmountDue.Should().Be(33); }