<commit_before>/*
 * Copyright 2019 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.businessmatching.updateservice.add

import cats.data.OptionT
import cats.implicits._
import connectors.DataCacheConnector
import controllers.DefaultBaseController
import controllers.businessmatching.updateservice.AddBusinessTypeHelper
import forms.EmptyForm
import javax.inject.{Inject, Singleton}
import models.flowmanagement.{AddBusinessTypeFlowModel, AddBusinessTypeSummaryPageId}
import models.responsiblepeople.ResponsiblePerson
import models.tradingpremises.TradingPremises
import services.businessmatching.BusinessMatchingService
import services.flowmanagement.Router
import services.{StatusService, TradingPremisesService}
import uk.gov.hmrc.http.HeaderCarrier
import utils.{AuthAction, RepeatingSection, StatusConstants}
import views.html.businessmatching.updateservice.add.update_services_summary

import scala.concurrent.Future

@Singleton
class AddBusinessTypeSummaryController @Inject()(
                                                  authAction: AuthAction,
                                                  implicit val dataCacheConnector: DataCacheConnector,
                                                  val statusService: StatusService,
                                                  val businessMatchingService: BusinessMatchingService,
                                                  val helper: AddBusinessTypeHelper,
                                                  val router: Router[AddBusinessTypeFlowModel],
                                                  val tradingPremisesService: TradingPremisesService
                                               ) extends DefaultBaseController with RepeatingSection {

  def get() = authAction.async {
    implicit request =>
      (for {
        flowModel <- OptionT(dataCacheConnector.fetch[AddBusinessTypeFlowModel](request.credId, AddBusinessTypeFlowModel.key))
        filteredTPs: Seq[(TradingPremises, Int)] <- helper.filteredTps(request.credId, flowModel)
        filteredRPs: Seq[(ResponsiblePerson, Int)] <- helper.filteredRps(request.credId, flowModel)
      } yield {
        Ok(update_services_summary(EmptyForm, flowModel, filteredTPs, filteredRPs))
      }) getOrElse Redirect(controllers.businessmatching.routes.SummaryController.get())
  }

  def post() = authAction.async {
      implicit request =>
        (for {
          model <- OptionT(dataCacheConnector.fetch[AddBusinessTypeFlowModel](request.credId, AddBusinessTypeFlowModel.key))
          activity <- OptionT.fromOption[Future](model.activity)
                  _ <- helper.updateTradingPremises(request.credId, model)
                  _ <- helper.updateResponsiblePeople(request.credId, model)
                  _ <- helper.updateSupervision(request.credId)
                  _ <- helper.updateBusinessMatching(request.credId, model)
                  _ <- helper.updateServicesRegister(request.credId, model)
                  _ <- helper.updateBusinessActivities(request.credId, model)
                  _ <- helper.updateHasAcceptedFlag(request.credId, model)
                  _ <- helper.clearFlowModel(request.credId)
          route <- OptionT.liftF(router.getRoute(request.credId, AddBusinessTypeSummaryPageId, model))
        } yield {
          route
        }) getOrElse InternalServerError("Could not fetch the flow model")
  }<commit_msg>AMLS-4789 | Fixed small bug relating to which CYA page would appear<commit_after>/*
 * Copyright 2019 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.businessmatching.updateservice.add

import cats.data.OptionT
import cats.implicits._
import connectors.DataCacheConnector
import controllers.DefaultBaseController
import controllers.businessmatching.updateservice.AddBusinessTypeHelper
import forms.EmptyForm
import javax.inject.{Inject, Singleton}
import models.flowmanagement.{AddBusinessTypeFlowModel, AddBusinessTypeSummaryPageId}
import models.responsiblepeople.ResponsiblePerson
import models.tradingpremises.TradingPremises
import services.businessmatching.BusinessMatchingService
import services.flowmanagement.Router
import services.{StatusService, TradingPremisesService}
import uk.gov.hmrc.http.HeaderCarrier
import utils.{AuthAction, RepeatingSection, StatusConstants}
import views.html.businessmatching.updateservice.add.update_services_summary

import scala.concurrent.Future

@Singleton
class AddBusinessTypeSummaryController @Inject()(
                                                  authAction: AuthAction,
                                                  implicit val dataCacheConnector: DataCacheConnector,
                                                  val statusService: StatusService,
                                                  val businessMatchingService: BusinessMatchingService,
                                                  val helper: AddBusinessTypeHelper,
                                                  val router: Router[AddBusinessTypeFlowModel],
                                                  val tradingPremisesService: TradingPremisesService
                                               ) extends DefaultBaseController with RepeatingSection {

  def get() = authAction.async {
    implicit request =>
      (for {
        flowModel <- OptionT(dataCacheConnector.fetch[AddBusinessTypeFlowModel](request.credId, AddBusinessTypeFlowModel.key))
        filteredTPs: Seq[(TradingPremises, Int)] <- helper.filteredTps(request.credId, flowModel) orElse OptionT.pure[Future, Seq[(TradingPremises, Int)]](Seq())
        filteredRPs: Seq[(ResponsiblePerson, Int)] <- helper.filteredRps(request.credId, flowModel) orElse OptionT.pure[Future, Seq[(ResponsiblePerson, Int)]](Seq())
      } yield {
        Ok(update_services_summary(EmptyForm, flowModel, filteredTPs, filteredRPs))
      }) getOrElse Redirect(controllers.businessmatching.routes.SummaryController.get())
  }

  def post() = authAction.async {
      implicit request =>
        (for {
          model <- OptionT(dataCacheConnector.fetch[AddBusinessTypeFlowModel](request.credId, AddBusinessTypeFlowModel.key))
          activity <- OptionT.fromOption[Future](model.activity)
                  _ <- helper.updateTradingPremises(request.credId, model)
                  _ <- helper.updateResponsiblePeople(request.credId, model)
                  _ <- helper.updateSupervision(request.credId)
                  _ <- helper.updateBusinessMatching(request.credId, model)
                  _ <- helper.updateServicesRegister(request.credId, model)
                  _ <- helper.updateBusinessActivities(request.credId, model)
                  _ <- helper.updateHasAcceptedFlag(request.credId, model)
                  _ <- helper.clearFlowModel(request.credId)
          route <- OptionT.liftF(router.getRoute(request.credId, AddBusinessTypeSummaryPageId, model))
        } yield {
          route
        }) getOrElse InternalServerError("Could not fetch the flow model")
  }
{'fraction_non_alphanumeric': 0.07462317766246603, 'fraction_numerical': 0.002470966147763776, 'mean_word_length': 2.735579141670512, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 32, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '834803', 'n_tokens_mistral': 2006, 'n_tokens_neox': 1877, 'n_words': 454}