<commit_before>/*
 * Copyright 2017 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 cats.data.OptionT
import cats.implicits._
import config.{AMLSAuthConnector, ApplicationConfig}
import connectors.{AmlsConnector, DataCacheConnector}
import models.notifications.ContactType._
import models.notifications._
import models.status.{SubmissionDecisionRejected, SubmissionStatus}
import models.notifications.StatusType.DeRegistered
import play.api.Play
import play.api.mvc.Request
import services.{AuthEnrolmentsService, NotificationService, StatusService}
import utils.{BusinessName, FeatureToggle}

import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.Future

trait NotificationController extends BaseController {

  protected[controllers] implicit val dataCacheConnector: DataCacheConnector

  protected[controllers] def authEnrolmentsService: AuthEnrolmentsService

  protected[controllers] def statusService: StatusService

  protected[controllers] lazy val amlsNotificationService: NotificationService = Play.current.injector.instanceOf[NotificationService]

  protected[controllers] implicit val amlsConnector: AmlsConnector

  def getMessages = FeatureToggle(ApplicationConfig.notificationsToggle) {
    Authorised.async {
      implicit authContext =>
        implicit request =>
          statusService.getReadStatus flatMap {
            case readStatus if readStatus.safeId.isDefined => {
              (for {
                businessName <- BusinessName.getName(readStatus.safeId)
                records <- OptionT.liftF(amlsNotificationService.getNotifications(readStatus.safeId.get))
              } yield {
                Ok(views.html.notifications.your_messages(businessName, records))
              }) getOrElse (throw new Exception("Cannot retrieve business name"))
            }
            case _ => throw new Exception("Unable to retrieve SafeID")
          }
    }
  }

  def messageDetails(id: String, contactType: ContactType, amlsRegNo: String) = FeatureToggle(ApplicationConfig.notificationsToggle) {
    Authorised.async {
      implicit authContext =>
        implicit request =>
          statusService.getReadStatus flatMap {
            case readStatus if readStatus.safeId.isDefined =>
              (for {
                businessName <- BusinessName.getName(readStatus.safeId)
                details <- OptionT(amlsNotificationService.getMessageDetails(amlsRegNo, id, contactType))
                status <- OptionT.liftF(statusService.getStatus)
              } yield contactTypeToResponse(contactType, amlsRegNo, businessName, details, status)) getOrElse NotFound(notFoundView)
            case r if r.safeId.isEmpty => throw new Exception("Unable to retrieve SafeID")
            case _ => Future.successful(BadRequest)
          }
    }
  }

  private def contactTypeToResponse(contactType: ContactType, reference: String, businessName: String, details: NotificationDetails, status: SubmissionStatus)
                                   (implicit request: Request[_]) = {

    val msgText = details.messageText.getOrElse("")

    contactType match {
      case MindedToRevoke => Ok(views.html.notifications.minded_to_revoke(msgText, reference, businessName))
      case MindedToReject => Ok(views.html.notifications.minded_to_reject(msgText, reference, businessName))
      case RejectionReasons => Ok(views.html.notifications.rejection_reasons(msgText, reference, businessName, details.dateReceived))
      case RevocationReasons => Ok(views.html.notifications.revocation_reasons(msgText, reference, businessName, details.dateReceived))
      case NoLongerMindedToReject => Ok(views.html.notifications.no_longer_minded_to_reject(msgText, reference))
      case NoLongerMindedToRevoke => Ok(views.html.notifications.no_longer_minded_to_revoke(msgText, reference))
      case _ => {
        (status, contactType) match {
          case (SubmissionDecisionRejected, _) | (_, DeRegistrationEffectiveDateChange) =>
            Ok(views.html.notifications.message_details(details.subject, msgText, reference.some))
          case _ =>
            Ok(views.html.notifications.message_details(details.subject, msgText, None))
        }
      }
    }
  }
}

object NotificationController extends NotificationController {
  // $COVERAGE-OFF$
  override protected[controllers] val dataCacheConnector = DataCacheConnector
  override protected[controllers] val authEnrolmentsService = AuthEnrolmentsService
  override protected[controllers] val statusService = StatusService
  override protected val authConnector = AMLSAuthConnector
  override protected[controllers] lazy val amlsConnector = AmlsConnector
}
<commit_msg>Pass safeId to notifications preapproved<commit_after>/*
 * Copyright 2017 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 cats.data.OptionT
import cats.implicits._
import config.{AMLSAuthConnector, ApplicationConfig}
import connectors.{AmlsConnector, DataCacheConnector}
import models.notifications.ContactType._
import models.notifications._
import models.status.{SubmissionDecisionRejected, SubmissionStatus}
import models.notifications.StatusType.DeRegistered
import play.api.Play
import play.api.mvc.Request
import services.{AuthEnrolmentsService, NotificationService, StatusService}
import utils.{BusinessName, FeatureToggle}

import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.Future

trait NotificationController extends BaseController {

  protected[controllers] implicit val dataCacheConnector: DataCacheConnector

  protected[controllers] def authEnrolmentsService: AuthEnrolmentsService

  protected[controllers] def statusService: StatusService

  protected[controllers] lazy val amlsNotificationService: NotificationService = Play.current.injector.instanceOf[NotificationService]

  protected[controllers] implicit val amlsConnector: AmlsConnector

  def getMessages = FeatureToggle(ApplicationConfig.notificationsToggle) {
    Authorised.async {
      implicit authContext =>
        implicit request =>
          statusService.getReadStatus flatMap {
            case readStatus if readStatus.safeId.isDefined => {
              (for {
                businessName <- BusinessName.getName(readStatus.safeId)
                records <- OptionT.liftF(amlsNotificationService.getNotifications(readStatus.safeId.get))
              } yield {
                Ok(views.html.notifications.your_messages(businessName, records))
              }) getOrElse (throw new Exception("Cannot retrieve business name"))
            }
            case _ => throw new Exception("Unable to retrieve SafeID")
          }
    }
  }

  def messageDetails(id: String, contactType: ContactType, amlsRegNo: String) = FeatureToggle(ApplicationConfig.notificationsToggle) {
    Authorised.async {
      implicit authContext =>
        implicit request =>
          statusService.getReadStatus flatMap {
            case readStatus if readStatus.safeId.isDefined =>
              (for {
                safeId <- OptionT.fromOption[Future](readStatus.safeId)
                businessName <- BusinessName.getName(readStatus.safeId)
                details <- OptionT(amlsNotificationService.getMessageDetails(amlsRegNo, id, contactType))
                status <- OptionT.liftF(statusService.getStatus)
              } yield contactTypeToResponse(contactType, (amlsRegNo, safeId), businessName, details, status)) getOrElse NotFound(notFoundView)
            case r if r.safeId.isEmpty => throw new Exception("Unable to retrieve SafeID")
            case _ => Future.successful(BadRequest)
          }
    }
  }

  private def contactTypeToResponse(
                                     contactType: ContactType,
                                     reference: (String, String),
                                     businessName: String,
                                     details: NotificationDetails,
                                     status: SubmissionStatus)(implicit request: Request[_]) = {

    val msgText = details.messageText.getOrElse("")

    val (amlsRefNo, safeId) = reference

    contactType match {
      case MindedToRevoke => Ok(views.html.notifications.minded_to_revoke(msgText, amlsRefNo, businessName))
      case MindedToReject => Ok(views.html.notifications.minded_to_reject(msgText, safeId, businessName))
      case RejectionReasons => Ok(views.html.notifications.rejection_reasons(msgText, safeId, businessName, details.dateReceived))
      case RevocationReasons => Ok(views.html.notifications.revocation_reasons(msgText, amlsRefNo, businessName, details.dateReceived))
      case NoLongerMindedToReject => Ok(views.html.notifications.no_longer_minded_to_reject(msgText, safeId))
      case NoLongerMindedToRevoke => Ok(views.html.notifications.no_longer_minded_to_revoke(msgText, amlsRefNo))
      case _ => {
        (status, contactType) match {
          case (SubmissionDecisionRejected, _) | (_, DeRegistrationEffectiveDateChange) =>
            Ok(views.html.notifications.message_details(details.subject, msgText, safeId.some))
          case _ =>
            Ok(views.html.notifications.message_details(details.subject, msgText, None))
        }
      }
    }
  }
}

object NotificationController extends NotificationController {
  // $COVERAGE-OFF$
  override protected[controllers] val dataCacheConnector = DataCacheConnector
  override protected[controllers] val authEnrolmentsService = AuthEnrolmentsService
  override protected[controllers] val statusService = StatusService
  override protected val authConnector = AMLSAuthConnector
  override protected[controllers] lazy val amlsConnector = AmlsConnector
}

{'fraction_non_alphanumeric': 0.07723728184181211, 'fraction_numerical': 0.0014853323431117712, 'mean_word_length': 3.6216216216216215, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 39, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2357074', 'n_tokens_mistral': 2798, 'n_tokens_neox': 2732, 'n_words': 666}