diff --git a/app/audit/Logging.scala b/app/audit/Logging.scala index 27fd3b6c..05dae440 100644 --- a/app/audit/Logging.scala +++ b/app/audit/Logging.scala @@ -105,18 +105,23 @@ class Logging @Inject()(application: Application, object Logging { - val auditRegistrationTxName: String = "Registration" val splunkString = "SPLUNK AUDIT:\n" - final val eventTypeRequest: String = "Request" - final val eventTypeSuccess: String = "Success" - final val eventTypeFailure: String = "Failure" - final val eventTypeBadRequest: String = "BadRequest" - final val eventTypeConflict: String = "Conflict" - final val eventTypeNotFound: String = "NotFound" - final val eventTypeInternalServerError: String = "InternalServerError" - final val eventTypeServerUnavailable: String = "ServerUnavailable" - final val eventTypeUnexpectedError: String = "UnexpectedError" + val auditType = "CustomerSubscribedToSendQtrlySAIncomeTaxReports" + val transactionName = "Customer-subscribed-to-send-quarterly-SA-income-tax-reports" + + +// val auditRegistrationTxName: String = "Registration" + + val eventTypeRequest: String = "Request" + val eventTypeSuccess: String = "Success" + val eventTypeFailure: String = "Failure" + val eventTypeBadRequest: String = "BadRequest" + val eventTypeConflict: String = "Conflict" + val eventTypeNotFound: String = "NotFound" + val eventTypeInternalServerError: String = "InternalServerError" + val eventTypeServerUnavailable: String = "ServerUnavailable" + val eventTypeUnexpectedError: String = "UnexpectedError" } diff --git a/app/connectors/GGAdminConnector.scala b/app/connectors/GGAdminConnector.scala index b9730106..c235bafd 100644 --- a/app/connectors/GGAdminConnector.scala +++ b/app/connectors/GGAdminConnector.scala @@ -56,8 +56,7 @@ class GGAdminConnector @Inject()(config: Configuration, val updatedHc = createHeaderCarrierPost(hc) lazy val requestDetails: Map[String, String] = Map("knownFacts" -> (knownFacts: JsValue).toString) - lazy val auditRequest = logging.auditFor(auditAddKnownFactsName, requestDetails)(updatedHc) - auditRequest(eventTypeRequest) + logging.debug(s"Request:\n$requestDetails") httpPost.POST[KnownFactsRequest, HttpResponse](addKnownFactsUrl, knownFacts)( implicitly[Writes[KnownFactsRequest]], implicitly[HttpReads[HttpResponse]], updatedHc).map { response => diff --git a/app/connectors/GGConnector.scala b/app/connectors/GGConnector.scala index ada3537f..6c4baea6 100644 --- a/app/connectors/GGConnector.scala +++ b/app/connectors/GGConnector.scala @@ -52,11 +52,6 @@ class GGConnector @Inject() lazy val requestDetails: Map[String, String] = Map("enrolRequest" -> (enrolmentRequest: JsValue).toString) val updatedHc = createHeaderCarrierPost(hc) - - lazy val auditRequest = logging.auditFor(auditEnrolName, requestDetails)(updatedHc) - auditRequest(eventTypeRequest) - - logging.debug(s"Request:\n$requestDetails") httpPost.POST[EnrolRequest, HttpResponse](enrolUrl, enrolmentRequest)( diff --git a/app/connectors/RegistrationConnector.scala b/app/connectors/RegistrationConnector.scala index 0a994d34..018349df 100644 --- a/app/connectors/RegistrationConnector.scala +++ b/app/connectors/RegistrationConnector.scala @@ -48,13 +48,13 @@ class RegistrationConnector @Inject()( appConfig: AppConfig, // API 1(b) [API 1 (b)] val getRegistrationUrl: String => String = (nino: String) => s"${appConfig.desURL}/registration/details?nino=$nino" - def createHeaderCarrierPost: HeaderCarrier = - HeaderCarrier(extraHeaders = Seq("Environment" -> appConfig.desEnvironment, "Content-Type" -> "application/json"), - authorization = Some(Authorization(urlHeaderAuthorization))) + def createHeaderCarrierPost(headerCarrier: HeaderCarrier): HeaderCarrier = + headerCarrier.withExtraHeaders("Environment" -> appConfig.desEnvironment, "Content-Type" -> "application/json") + .copy(authorization = Some(Authorization(urlHeaderAuthorization))) - def createHeaderCarrierGet: HeaderCarrier = - HeaderCarrier(extraHeaders = Seq("Environment" -> appConfig.desEnvironment), - authorization = Some(Authorization(urlHeaderAuthorization))) + def createHeaderCarrierGet(headerCarrier: HeaderCarrier): HeaderCarrier = + headerCarrier.withExtraHeaders("Environment" -> appConfig.desEnvironment) + .copy(authorization = Some(Authorization(urlHeaderAuthorization))) def register(nino: String, registration: RegistrationRequestModel)(implicit hc: HeaderCarrier): Future[NewRegistrationUtil.Response] = { import NewRegistrationUtil._ @@ -62,10 +62,7 @@ class RegistrationConnector @Inject()( appConfig: AppConfig, implicit val loggingConfig = RegistrationConnector.registerLoggingConfig lazy val requestDetails: Map[String, String] = Map("nino" -> nino, "requestJson" -> (registration: JsValue).toString) - val updatedHc = createHeaderCarrierPost - - lazy val auditRequest = logging.auditFor(auditRegisterName, requestDetails)(updatedHc) - auditRequest(eventTypeRequest) + val updatedHc = createHeaderCarrierPost(hc) logging.debug(s"Request:\n$requestDetails\n\nRequest Headers:\n$updatedHc") httpPost.POST[RegistrationRequestModel, HttpResponse](newRegistrationUrl(nino), registration)( @@ -112,7 +109,7 @@ class RegistrationConnector @Inject()( appConfig: AppConfig, implicit val loggingConfig = RegistrationConnector.getRegistrationLoggingConfig lazy val requestDetails: Map[String, String] = Map("nino" -> nino) - val updatedHc = createHeaderCarrierGet + val updatedHc = createHeaderCarrierGet(hc) lazy val auditRequest = logging.auditFor(auditGetRegistrationName, requestDetails)(updatedHc) auditRequest(eventTypeRequest) diff --git a/app/connectors/SubscriptionConnector.scala b/app/connectors/SubscriptionConnector.scala index f1cffc66..73fe2b5f 100644 --- a/app/connectors/SubscriptionConnector.scala +++ b/app/connectors/SubscriptionConnector.scala @@ -24,7 +24,6 @@ import config.AppConfig import connectors.utils.ConnectorUtils import models.subscription.business._ import models.subscription.property.{PropertySubscriptionFailureModel, PropertySubscriptionResponseModel} -import play.api.Configuration import play.api.http.Status._ import play.api.libs.json.{JsValue, Writes} import uk.gov.hmrc.play.config.ServicesConfig @@ -45,13 +44,9 @@ class SubscriptionConnector @Inject() lazy val urlHeaderAuthorization: String = s"Bearer ${applicationConfig.desToken}" - def createHeaderCarrierPost(hc: HeaderCarrier): HeaderCarrier = - HeaderCarrier(extraHeaders = Seq("Environment" -> applicationConfig.desEnvironment, "Content-Type" -> "application/json"), - authorization = Some(Authorization(urlHeaderAuthorization))) - -// def createHeaderCarrierPostEmpty(headerCarrier: HeaderCarrier): HeaderCarrier = -// HeaderCarrier(extraHeaders = Seq("Environment" -> applicationConfig.desEnvironment), -// authorization = Some(Authorization(urlHeaderAuthorization))) + def createHeaderCarrierPost(headerCarrier: HeaderCarrier): HeaderCarrier = + headerCarrier.copy(authorization = Some(Authorization(urlHeaderAuthorization))) + .withExtraHeaders("Environment" -> applicationConfig.desEnvironment, "Content-Type" -> "application/json") def businessSubscribe(nino: String, businessSubscriptionPayload: BusinessSubscriptionRequestModel) (implicit hc: HeaderCarrier, ec: ExecutionContext): Future[BusinessConnectorUtil.Response] = { @@ -64,7 +59,7 @@ class SubscriptionConnector @Inject() lazy val auditRequest = logging.auditFor(auditBusinessSubscribeName, requestDetails)(updatedHc) auditRequest(eventTypeRequest) - logging.debug(s"Request:\n$requestDetails") + logging.debug(s"Request:\n$requestDetails\n\nHeader Carrier:\n$updatedHc") httpPost.POST[BusinessSubscriptionRequestModel, HttpResponse](businessSubscribeUrl(nino), businessSubscriptionPayload)( implicitly[Writes[BusinessSubscriptionRequestModel]], HttpReads.readRaw, createHeaderCarrierPost(hc) ).map { response => @@ -86,12 +81,10 @@ class SubscriptionConnector @Inject() import SubscriptionConnector._ implicit val loggingConfig = SubscriptionConnector.propertySubscribeLoggingConfig lazy val requestDetails: Map[String, String] = Map("nino" -> nino) - val updatedHc = createHeaderCarrierPost(hc) - lazy val auditRequest = logging.auditFor(auditPropertySubscribeName, requestDetails)(updatedHc) - auditRequest(eventTypeRequest) - - logging.debug(s"Request:\n$requestDetails") + + val updatedHc = createHeaderCarrierPost(hc) + logging.debug(s"Request:\n$requestDetails\n\nHeader Carrier:\n$updatedHc") httpPost.POST[JsValue, HttpResponse](propertySubscribeUrl(nino),"{}": JsValue)(implicitly[Writes[JsValue]], HttpReads.readRaw, updatedHc).map { response => diff --git a/app/services/RosmAndEnrolManagerService.scala b/app/services/RosmAndEnrolManagerService.scala index a74458cf..e8d5fc06 100644 --- a/app/services/RosmAndEnrolManagerService.scala +++ b/app/services/RosmAndEnrolManagerService.scala @@ -19,21 +19,23 @@ package services import javax.inject.{Inject, Singleton} import audit.Logging +import config.AppConfig import connectors.AuthenticatorConnector import models.ErrorModel import models.authenticator.{RefreshFailure, RefreshSuccessful} -import models.frontend.{Both, Business, FERequest, FESuccessResponse, Property} +import models.frontend._ import models.subscription.business.BusinessSubscriptionSuccessResponseModel import models.subscription.property.PropertySubscriptionResponseModel +import play.api.http.Status._ import uk.gov.hmrc.play.http.{HeaderCarrier, HttpResponse} import utils.Implicits._ -import play.api.http.Status._ import scala.concurrent.{ExecutionContext, Future} @Singleton class RosmAndEnrolManagerService @Inject() ( + appConfig: AppConfig, logging: Logging, registrationService: RegistrationService, subscriptionService: SubscriptionService, @@ -41,8 +43,26 @@ class RosmAndEnrolManagerService @Inject() authenticatorConnector: AuthenticatorConnector ) { - //TODO add logging + lazy val urlHeaderAuthorization: String = s"Bearer ${appConfig.desToken}" + + val feRequestToAuditMap: FERequest => Map[String, String] = (feRequest: FERequest) => + Map( + "nino" -> feRequest.nino, + "sourceOfIncome" -> feRequest.incomeSource.toString, + "acccountingPeriodStartDate" -> feRequest.accountingPeriodStart.fold("-")(x => x.toDesDateFormat), + "acccountingPeriodEndDate" -> feRequest.accountingPeriodEnd.fold("-")(x => x.toDesDateFormat), + "tradingName" -> feRequest.tradingName.fold("-")(x => x), + "cashOrAccruals" -> feRequest.cashOrAccruals.fold("-")(x => x.toLowerCase), + "Authorization" -> urlHeaderAuthorization + ) + def rosmAndEnrol(request: FERequest)(implicit hc: HeaderCarrier, ec: ExecutionContext): Future[Either[ErrorModel, FESuccessResponse]] = { + + lazy val requestDetails: Map[String, String] = feRequestToAuditMap(request) + + lazy val auditRequest = logging.auditFor(Logging.transactionName, requestDetails) + auditRequest(Logging.auditType) + orchestrateROSM(request).flatMap { case Right(rosmSuccess) => orchestrateEnrolment(request.nino, rosmSuccess.mtditId).flatMap { @@ -68,7 +88,7 @@ class RosmAndEnrolManagerService @Inject() case (_, Some(Left(err))) => err case (Some(Right(x)), _) => FESuccessResponse(x.mtditId) // As long as there's no error reported then case (_, Some(Right(x))) => FESuccessResponse(x.mtditId) // We only need the response of one of the calls - case (_,_) => ErrorModel(INTERNAL_SERVER_ERROR, "Unexpected Error") // this error is impossible but included for exhaustive match + case (_, _) => ErrorModel(INTERNAL_SERVER_ERROR, "Unexpected Error") // this error is impossible but included for exhaustive match } } case Left(failure) => Future.successful(failure) diff --git a/test/unit/connectors/RegistrationConnectorSpec.scala b/test/unit/connectors/RegistrationConnectorSpec.scala index bd2268c2..adec6b56 100644 --- a/test/unit/connectors/RegistrationConnectorSpec.scala +++ b/test/unit/connectors/RegistrationConnectorSpec.scala @@ -34,7 +34,7 @@ class RegistrationConnectorSpec extends MockRegistrationConnector { "RegistrationConnector.register" should { "Put in the correct headers" in { - val rHc = TestRegistrationConnector.createHeaderCarrierPost + val rHc = TestRegistrationConnector.createHeaderCarrierPost(hc) rHc.headers.contains("Authorization" -> s"Bearer $authToken") shouldBe true rHc.headers.contains("Content-Type" -> "application/json") shouldBe true rHc.headers.contains("Environment" -> env) shouldBe true @@ -86,7 +86,7 @@ class RegistrationConnectorSpec extends MockRegistrationConnector { "RegistrationConnector.getRegistration" should { "Put in the correct headers" in { - val rHc = TestRegistrationConnector.createHeaderCarrierGet + val rHc = TestRegistrationConnector.createHeaderCarrierGet(hc) rHc.headers.contains("Authorization" -> s"Bearer $authToken") shouldBe true rHc.headers.contains("Environment" -> env) shouldBe true } diff --git a/test/unit/services/mocks/MockSubscriptionManagerService.scala b/test/unit/services/mocks/MockSubscriptionManagerService.scala index 16e36431..781b0b90 100644 --- a/test/unit/services/mocks/MockSubscriptionManagerService.scala +++ b/test/unit/services/mocks/MockSubscriptionManagerService.scala @@ -32,6 +32,7 @@ trait MockSubscriptionManagerService extends MockRegistrationService with MockSu override lazy val httpGet: HttpGet = mockHttpGet object TestSubscriptionManagerService extends RosmAndEnrolManagerService( + appConfig, logging, TestRegistrationService, TestSubscriptionService,