-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathTaxCodeChangeController.scala
More file actions
110 lines (97 loc) · 4.75 KB
/
TaxCodeChangeController.scala
File metadata and controls
110 lines (97 loc) · 4.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/*
* Copyright 2023 HM Revenue & Customs
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package controllers
import controllers.auth.{AuthJourney, AuthedUser, AuthenticatedRequest}
import play.api.Logging
import play.api.mvc.{Action, AnyContent, MessagesControllerComponents, Result}
import uk.gov.hmrc.domain.Nino
import uk.gov.hmrc.http.UpstreamErrorResponse
import uk.gov.hmrc.tai.config.ApplicationConfig
import uk.gov.hmrc.tai.model.TaxYear
import uk.gov.hmrc.tai.service.*
import uk.gov.hmrc.tai.service.yourTaxFreeAmount.{DescribedYourTaxFreeAmountService, TaxCodeChangeReasonsService}
import uk.gov.hmrc.tai.util.yourTaxFreeAmount.{IabdTaxCodeChangeReasons, YourTaxFreeAmount}
import uk.gov.hmrc.tai.viewModels.taxCodeChange.TaxCodeChangeViewModel
import views.html.taxCodeChange.{TaxCodeComparisonView, WhatHappensNextView, YourTaxFreeAmountView}
import javax.inject.Inject
import scala.concurrent.{ExecutionContext, Future}
import uk.gov.hmrc.tai.util.EitherTExtensions.EitherTThrowableOps
class TaxCodeChangeController @Inject() (
taxCodeChangeService: TaxCodeChangeService,
taxAccountService: TaxAccountService,
describedYourTaxFreeAmountService: DescribedYourTaxFreeAmountService,
authenticate: AuthJourney,
yourTaxFreeAmountService: YourTaxFreeAmountService,
taxCodeChangeReasonsService: TaxCodeChangeReasonsService,
appConfig: ApplicationConfig,
mcc: MessagesControllerComponents,
taxCodeComparisonView: TaxCodeComparisonView,
yourTaxFreeAmountView: YourTaxFreeAmountView,
whatHappensNextView: WhatHappensNextView,
errorPagesHandler: ErrorPagesHandler
)(implicit val ec: ExecutionContext)
extends TaiBaseController(mcc)
with YourTaxFreeAmount
with Logging {
def taxCodeComparison: Action[AnyContent] = authenticate.authWithValidatePerson.async { implicit request =>
val nino = request.taiUser.nino
taxCodeChangeService
.hasTaxCodeChanged(nino)
.value
.flatMap {
case Right(false) => Future.successful(Redirect(controllers.routes.TaxAccountSummaryController.onPageLoad()))
case Right(true) => buildTaxCodeComparisonResult
case Left(error) => Future.successful(errorPagesHandler.internalServerError(error.message, Some(error)))
}
.recover { case exception =>
errorPagesHandler.internalServerError("Failed to build tax code comparison result", Some(exception))
}
}
private def buildTaxCodeComparisonResult(implicit request: AuthenticatedRequest[_]): Future[Result] = {
val nino: Nino = request.taiUser.nino
for {
taxCodeChange <- taxCodeChangeService.taxCodeChange(nino).toFutureOrThrow
yourTaxFreeAmountComparison <- yourTaxFreeAmountService.taxFreeAmountComparison(nino, taxCodeChange)
scottishTaxRateBands <- taxAccountService.scottishBandRates(nino, TaxYear(), taxCodeChange.uniqueTaxCodes)
} yield {
val iabdTaxCodeChangeReasons: IabdTaxCodeChangeReasons = new IabdTaxCodeChangeReasons
val taxCodeChangeReasons = taxCodeChangeReasonsService
.combineTaxCodeChangeReasons(iabdTaxCodeChangeReasons, yourTaxFreeAmountComparison.iabdPairs, taxCodeChange)
val isAGenericReason = taxCodeChangeReasonsService.isAGenericReason(taxCodeChangeReasons)
val maybeUserName = Some(request.fullName)
val viewModel =
TaxCodeChangeViewModel(
taxCodeChange,
scottishTaxRateBands,
taxCodeChangeReasons,
isAGenericReason,
maybeUserName
)
implicit val user: AuthedUser = request.taiUser
Ok(taxCodeComparisonView(viewModel, appConfig))
}
}
def yourTaxFreeAmount: Action[AnyContent] = authenticate.authWithValidatePerson.async { implicit request =>
val nino: Nino = request.taiUser.nino
val taxFreeAmountViewModel = describedYourTaxFreeAmountService.taxFreeAmountComparison(nino)
implicit val user: AuthedUser = request.taiUser
taxFreeAmountViewModel.map(viewModel => Ok(yourTaxFreeAmountView(viewModel)))
}
def whatHappensNext: Action[AnyContent] = authenticate.authWithValidatePerson.async { implicit request =>
implicit val user: AuthedUser = request.taiUser
Future.successful(Ok(whatHappensNextView()))
}
}