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
21 changes: 12 additions & 9 deletions app/controllers/IncomeSourceSummaryController.scala
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ package controllers
import cats.implicits.*
import controllers.auth.AuthJourney
import pages.benefits.EndCompanyBenefitsUpdateIncomePage
import play.api.mvc.{Action, AnyContent, MessagesControllerComponents, Result}
import play.api.mvc.{Action, AnyContent, MessagesControllerComponents}
import uk.gov.hmrc.http.UpstreamErrorResponse
import uk.gov.hmrc.play.audit.http.connector.AuditConnector
import uk.gov.hmrc.tai.model.TaxYear
import uk.gov.hmrc.tai.model.domain.{Available, Employment, IabdDetails, TemporarilyUnavailable}
import uk.gov.hmrc.tai.service.{EmploymentService, IabdService, RtiService, TaxAccountService}
import uk.gov.hmrc.tai.util.{EmpIdCheck, TaxAccountHelper}
import uk.gov.hmrc.tai.viewModels.IncomeSourceSummaryViewModel
import views.html.IncomeSourceSummaryView
import uk.gov.hmrc.tai.model.domain.{Available, Employment, IabdDetails}

import javax.inject.Inject
import scala.concurrent.{ExecutionContext, Future}
Expand Down Expand Up @@ -62,33 +62,36 @@ class IncomeSourceSummaryController @Inject() (
taxAccountService.taxCodeIncomes(nino, TaxYear()),
taxAccountService.taxAccountSummary(nino, TaxYear()).value,
rtiService.getPaymentsForEmploymentAndYear(nino, TaxYear(), empId).value,
rtiService.getAllPaymentsForYear(nino, TaxYear()).value,
cacheUpdatedIncomeAmountFuture,
iabdService.getIabds(nino, TaxYear()).value
).mapN {
case (
Some(employment),
taxCodeIncomes,
taxAccountSummary,
payments,
paymentsForEmp,
paymentsForYear,
cacheUpdatedIncomeAmount,
Right(iabds)
) =>
val TaxAccountSummaryDate = taxAccountSummary.fold(_ => None, _.date)
val estimatedPayOverrides =
TaxAccountHelper.getIabdLatestEstimatedIncome(iabds, TaxAccountSummaryDate, Some(empId))

val rtiUnavailableMarkerPresent: Boolean =
paymentsForYear.exists(_.exists(a => a.sequenceNumber == 0 && a.realTimeStatus == TemporarilyUnavailable))

val rtiAvailableCalculated: Boolean = paymentsForEmp.exists(_.exists(_.realTimeStatus == Available))

val vm = IncomeSourceSummaryViewModel.apply(
empId = empId,
displayName = request.fullName,
optTaxCodeIncome =
taxCodeIncomes.fold(_ => None, _.find(_.employmentId.contains(employment.sequenceNumber))),
employment = employment,
payments = payments.toOption.flatten,
// TODO: handle a failure vs no payment present
// The way the rti availability is implemented using a stub Annual account is not compatible with None type
// So when no annual account found for an employment, assuming rti is down.
// The service also does not handle the case when there is no payments but assume the rti api not been available.
rtiAvailable = payments.fold(_ => false, _.fold(false)(_.realTimeStatus == Available)),
payments = paymentsForEmp.toOption.flatten,
rtiAvailable = if (rtiUnavailableMarkerPresent) false else rtiAvailableCalculated,
cacheUpdatedIncomeAmount = cacheUpdatedIncomeAmount,
estimatedPayOverrides = estimatedPayOverrides
)
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/IncomeTaxComparisonController.scala
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ class IncomeTaxComparisonController @Inject() (
val cyCodingComponents = CodingComponentForYear(currentTaxYear, codingComponentsCY)
val cyPlusOneTaxComponents = CodingComponentForYear(nextTaxYear, codingComponentsCYPlusOne)
val cyTaxSummary = TaxAccountSummaryForYear(currentTaxYear, taxAccountSummaryCY)
val cyPlusOneTaxSummary = TaxAccountSummaryForYear(currentTaxYear, taxAccountSummaryCYPlusOne)
val cyPlusOneTaxSummary = TaxAccountSummaryForYear(nextTaxYear, taxAccountSummaryCYPlusOne)

TaxFreeAmountComparisonViewModel(
Seq(cyCodingComponents, cyPlusOneTaxComponents),
Expand Down
4 changes: 1 addition & 3 deletions app/controllers/WhatDoYouWantToDoController.scala
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,13 @@ class WhatDoYouWantToDoController @Inject() (
nino: Nino
)(implicit hc: HeaderCarrier): EitherT[Future, UpstreamErrorResponse, Boolean] = {
val taxYears =
// start from the current year which is the most probable year to be present
(TaxYear().year to (TaxYear().year - applicationConfig.numberOfPreviousYearsToShowIncomeTaxHistory) by -1)
.map(TaxYear(_))
.toList

taxYears.foldLeft(EitherT.rightT[Future, UpstreamErrorResponse](false)) { (acc, year) =>
EitherT(acc.value.flatMap {
case Right(true) =>
// Short-circuit if we've already found an employment
Future.successful(Right[UpstreamErrorResponse, Boolean](true))
case _ =>
employmentService
Expand Down Expand Up @@ -122,7 +120,7 @@ class WhatDoYouWantToDoController @Inject() (
Right(None)
case Left(_) =>
// don't fail the page when we get an error for CY+1
Right(none)
Right(None)
}
} else {
EitherT.rightT[Future, UpstreamErrorResponse](None: Option[TaxAccountSummary])
Expand Down
34 changes: 31 additions & 3 deletions test/controllers/IncomeSourceSummaryControllerSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,18 @@ import builders.RequestBuilder
import cats.data.EitherT
import org.jsoup.Jsoup
import org.mockito.ArgumentMatchers.any
import org.mockito.Mockito
import org.mockito.Mockito.{times, verify, when}
import org.mockito.stubbing.OngoingStubbing
import org.mockito.Mockito
import org.scalatest.AppendedClues.convertToClueful
import pages.benefits.EndCompanyBenefitsUpdateIncomePage
import play.api.i18n.Messages
import play.api.mvc.Results.NotFound
import play.api.test.Helpers._
import play.api.test.Helpers.*
import uk.gov.hmrc.http.UpstreamErrorResponse
import uk.gov.hmrc.play.audit.http.connector.AuditConnector
import uk.gov.hmrc.tai.model.UserAnswers
import uk.gov.hmrc.tai.model.domain._
import uk.gov.hmrc.tai.model.domain.*
import uk.gov.hmrc.tai.model.domain.income.{Live, OtherBasisOfOperation, TaxCodeIncome, Week1Month1BasisOfOperation}
import uk.gov.hmrc.tai.service.{EmploymentService, IabdService, RtiService, TaxAccountService}
import uk.gov.hmrc.tai.util.TaxYearRangeUtil
Expand Down Expand Up @@ -109,6 +109,9 @@ class IncomeSourceSummaryControllerSpec extends BaseSpec {

when(mockIabdService.getIabds(any(), any())(any()))
.thenReturn(EitherT.rightT[Future, UpstreamErrorResponse](Seq.empty[IabdDetails]))

when(mockRtiService.getAllPaymentsForYear(any(), any())(any()))
.thenReturn(EitherT.rightT[Future, UpstreamErrorResponse](Seq.empty[AnnualAccount]))
}

private val employmentId = 1
Expand Down Expand Up @@ -308,6 +311,31 @@ class IncomeSourceSummaryControllerSpec extends BaseSpec {
verify(mockEmploymentService, times(1)).employment(any(), any(), any())(any())
}

"asked for pension details and NOT include RTI section where yearly RTI unavailable marker is present" in {
setUpPension(Right(Some(annualAccount)))

val unavailableMarker = annualAccount.copy(
sequenceNumber = 0,
realTimeStatus = TemporarilyUnavailable
)

when(mockRtiService.getAllPaymentsForYear(any(), any())(any()))
.thenReturn(EitherT.rightT[Future, UpstreamErrorResponse](Seq(unavailableMarker)))

val result = sut.onPageLoad(pensionId)(RequestBuilder.buildFakeRequestWithAuth("GET"))

status(result) mustBe OK

val doc = Jsoup.parse(contentAsString(result))

Option(doc.getElementById("incomeReceivedToDate"))
.map(_.text()) mustBe Some(
"Your income received to date is unavailable. Try again later"
) withClue "html id incomeReceivedToDate"

Option(doc.getElementById("updatePension")).isDefined mustBe false withClue "html id updatePension"
}

"failed to read tax code incomes" in {
when(mockTaxAccountService.taxCodeIncomes(any(), any())(any()))
.thenReturn(Future.successful(Left("Failed")))
Expand Down