Skip to content

Commit

Permalink
Merge pull request #70 from hmrc/SAR-454
Browse files Browse the repository at this point in the history
Sar 454
  • Loading branch information
MJCallahanPage authored Mar 17, 2017
2 parents 353eb01 + 29b43ce commit e19a83e
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 48 deletions.
25 changes: 15 additions & 10 deletions app/audit/Logging.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}

3 changes: 1 addition & 2 deletions app/connectors/GGAdminConnector.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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 =>
Expand Down
5 changes: 0 additions & 5 deletions app/connectors/GGConnector.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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)(
Expand Down
19 changes: 8 additions & 11 deletions app/connectors/RegistrationConnector.scala
Original file line number Diff line number Diff line change
Expand Up @@ -48,24 +48,21 @@ 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._
import RegistrationConnector.auditRegisterName

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)(
Expand Down Expand Up @@ -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)
Expand Down
21 changes: 7 additions & 14 deletions app/connectors/SubscriptionConnector.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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] = {
Expand All @@ -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 =>
Expand All @@ -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 =>

Expand Down
28 changes: 24 additions & 4 deletions app/services/RosmAndEnrolManagerService.scala
Original file line number Diff line number Diff line change
Expand Up @@ -19,30 +19,50 @@ 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,
enrolmentService: EnrolmentService,
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 {
Expand All @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions test/unit/connectors/RegistrationConnectorSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ trait MockSubscriptionManagerService extends MockRegistrationService with MockSu
override lazy val httpGet: HttpGet = mockHttpGet

object TestSubscriptionManagerService extends RosmAndEnrolManagerService(
appConfig,
logging,
TestRegistrationService,
TestSubscriptionService,
Expand Down

0 comments on commit e19a83e

Please sign in to comment.