<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 services.adjustmentsmanagement

import model.Commands.Candidate
import model.Exceptions.ApplicationNotFound
import model.events.EventTypes.Events
import model.events.{ AuditEvents, DataStoreEvents, EmailEvents }
import model.persisted.ContactDetails
import model.{ AdjustmentDetail, Adjustments, AdjustmentsComment }
import play.api.mvc.RequestHeader
import repositories._
import repositories.application.GeneralApplicationRepository
import repositories.contactdetails.ContactDetailsRepository
import services.events.{ EventService, EventSink }
import uk.gov.hmrc.play.http.HeaderCarrier

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

object AdjustmentsManagementService extends AdjustmentsManagementService {
  val appRepository = applicationRepository
  val eventService = EventService
  val cdRepository = faststreamContactDetailsRepository
}

trait AdjustmentsManagementService extends EventSink {
  val appRepository: GeneralApplicationRepository
  val cdRepository: ContactDetailsRepository

  def confirmAdjustment(applicationId: String, adjustmentInformation: Adjustments)
                       (implicit hc: HeaderCarrier, rh: RequestHeader): Future[Unit] = {

    val adjustmentsDataStoreAndAuditEvents: Events =
      DataStoreEvents.ManageAdjustmentsUpdated(applicationId) ::
      AuditEvents.AdjustmentsConfirmed(Map("applicationId" -> applicationId, "adjustments" -> adjustmentInformation.toString)) ::
      Nil

    appRepository.find(applicationId).flatMap {
      case Some(candidate) => eventSink {
        for {
          cd <- cdRepository.find(candidate.userId)
          previousAdjustments <- appRepository.findAdjustments(applicationId)
          _ <- appRepository.confirmAdjustments(applicationId, adjustmentInformation)
        } yield {
          val hasNewAdjustments = adjustmentInformation.adjustments.exists(_.nonEmpty)
          val hasPreviousAdjustments = previousAdjustments.nonEmpty

          val events = if (hasNewAdjustments || hasPreviousAdjustments) {
            createEmailEvents(candidate, adjustmentInformation, hasPreviousAdjustments, cd) :: adjustmentsDataStoreAndAuditEvents
          } else {
            adjustmentsDataStoreAndAuditEvents
          }

          events
        }
      }
      case None => throw ApplicationNotFound(applicationId)<commit_msg>[FSEST-1510] Edit candidates' adjustments Bug fix. previousAdjustment was always not empty, the key to find out if there were adjustment before was if adjustmentConfirmed == true<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 services.adjustmentsmanagement

import model.Commands.Candidate
import model.Exceptions.ApplicationNotFound
import model.events.EventTypes.Events
import model.events.{ AuditEvents, DataStoreEvents, EmailEvents }
import model.persisted.ContactDetails
import model.{ AdjustmentDetail, Adjustments, AdjustmentsComment }
import play.api.mvc.RequestHeader
import repositories._
import repositories.application.GeneralApplicationRepository
import repositories.contactdetails.ContactDetailsRepository
import services.events.{ EventService, EventSink }
import uk.gov.hmrc.play.http.HeaderCarrier

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

object AdjustmentsManagementService extends AdjustmentsManagementService {
  val appRepository = applicationRepository
  val eventService = EventService
  val cdRepository = faststreamContactDetailsRepository
}

trait AdjustmentsManagementService extends EventSink {
  val appRepository: GeneralApplicationRepository
  val cdRepository: ContactDetailsRepository

  def confirmAdjustment(applicationId: String, adjustmentInformation: Adjustments)
                       (implicit hc: HeaderCarrier, rh: RequestHeader): Future[Unit] = {

    val adjustmentsDataStoreAndAuditEvents: Events =
      DataStoreEvents.ManageAdjustmentsUpdated(applicationId) ::
      AuditEvents.AdjustmentsConfirmed(Map("applicationId" -> applicationId, "adjustments" -> adjustmentInformation.toString)) ::
      Nil

    appRepository.find(applicationId).flatMap {
      case Some(candidate) => eventSink {
        for {
          cd <- cdRepository.find(candidate.userId)
          previousAdjustments <- appRepository.findAdjustments(applicationId)
          _ <- appRepository.confirmAdjustments(applicationId, adjustmentInformation)
        } yield {
          val hasNewAdjustments = adjustmentInformation.adjustments.exists(_.nonEmpty)
          val hasPreviousAdjustments = previousAdjustments.flatMap(_.adjustmentsConfirmed).getOrElse(false)

          val events = if (hasNewAdjustments || hasPreviousAdjustments) {
            createEmailEvents(candidate, adjustmentInformation, hasPreviousAdjustments, cd) :: adjustmentsDataStoreAndAuditEvents
          } else {
            adjustmentsDataStoreAndAuditEvents
          }

          events
        }
      }
      case None => throw ApplicationNotFound(applicationId)
{'fraction_non_alphanumeric': 0.06607287449392713, 'fraction_numerical': 0.0032388663967611335, 'mean_word_length': 4.342560553633218, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5396877', 'n_tokens_mistral': 1513, 'n_tokens_neox': 1442, 'n_words': 423}