Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP -> Rewrite CreditService.getAllCredits to run single 1553 call with taxR… #2712

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
26 changes: 26 additions & 0 deletions app/connectors/FinancialDetailsConnector.scala
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,32 @@ class FinancialDetailsConnector @Inject()(
}
}

def getCreditsAndRefundV2(taxYearFrom: TaxYear, taxYearTo: TaxYear, nino: String)
(implicit headerCarrier: HeaderCarrier, mtdItUser: MtdItUser[_]): Future[ResponseModel[CreditsModel]] = {

val dateFrom: String = taxYearFrom.toFinancialYearStart.format(DateTimeFormatter.ISO_DATE)
val dateTo: String = taxYearTo.toFinancialYearEnd.format(DateTimeFormatter.ISO_DATE)

val url = getCreditAndRefundUrl(nino, dateFrom, dateTo)
Logger("application").debug(s"GET $url")

val hc = checkAndAddTestHeader(mtdItUser.path, headerCarrier, appConfig.poaAdjustmentOverrides(), "afterPoaAmountAdjusted")

val correlationId =
CorrelationId.fromHeaderCarrier(hc).getOrElse(CorrelationId())

httpV2
.get(url"$url")
.setHeader(correlationId.asHeader())
.execute[ResponseModel[CreditsModel]]
.recover {
case e =>
Logger("application").error(e.getMessage)
Left(UnexpectedError)
}
}


// TODO: MFA Credits
def getFinancialDetails(taxYear: Int, nino: String)
(implicit headerCarrier: HeaderCarrier, mtdItUser: MtdItUser[_]): Future[FinancialDetailsResponseModel] = {
Expand Down
30 changes: 15 additions & 15 deletions app/services/CreditService.scala
Original file line number Diff line number Diff line change
Expand Up @@ -43,20 +43,20 @@ class CreditService @Inject()(val financialDetailsConnector: FinancialDetailsCon
Logger("application").debug(
s"Requesting Financial Details for all periods for mtditid: ${user.mtditid}")

Future.sequence(
user.incomeSources.orderedTaxYearsByYearOfMigration.map { taxYearInt =>
Logger("application").debug(s"Getting financial details for TaxYear: ${taxYearInt}")
for {
taxYear <- Future.fromTry(Try(TaxYear.forYearEnd(taxYearInt)))
response <- financialDetailsConnector.getCreditsAndRefund(taxYear, user.nino)
} yield response match {
case Right(financialDetails: CreditsModel) => Some(financialDetails)
case Left(error: ErrorModel) if error.code != NOT_FOUND =>
throw new Exception("Error response while getting Unpaid financial details")
case _ => None
}
})
.map(_.flatten)
.map(_.reduceOption(mergeCreditAndRefundModels).getOrElse(CreditsModel(0, 0, Nil)))
val (from, to) = (user.incomeSources.orderedTaxYearsByYearOfMigration.min, user.incomeSources.orderedTaxYearsByYearOfMigration.max)
Logger("application").debug(s"Getting financial details for TaxYear: ${from} - ${to}")
val res = {
for {
taxYearFrom <- Future.fromTry(Try(TaxYear.forYearEnd(from)))
taxYearTo <- Future.fromTry(Try(TaxYear.forYearEnd(to)))
response <- financialDetailsConnector.getCreditsAndRefundV2(taxYearFrom, taxYearTo, user.nino)
} yield response match {
case Right(financialDetails: CreditsModel) => Some(financialDetails)
case Left(error: ErrorModel) if error.code != NOT_FOUND =>
throw new Exception("Error response while getting Unpaid financial details")
case _ => None
}
}
res.map(_.reduceOption(mergeCreditAndRefundModels).getOrElse(CreditsModel(0, 0, Nil)))
}
}