Skip to content

Commit

Permalink
Merge pull request #67 from hmrc/MTDSA-9533
Browse files Browse the repository at this point in the history
[MTDSA-9533] -  Create marriage allowance Controller, Routes and Its
  • Loading branch information
BetaDraconis authored Aug 11, 2021
2 parents 56dc20c + 4f8c42a commit ddfa366
Show file tree
Hide file tree
Showing 16 changed files with 675 additions and 14 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ npm-debug.log
yarn-debug.log
yarn-error.log
./bsp
./bsp/sbt.json
13 changes: 13 additions & 0 deletions app/v1/connectors/BaseDownstreamConnector.scala
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import config.AppConfig
import play.api.Logger
import play.api.libs.json.Writes
import uk.gov.hmrc.http.{HeaderCarrier, HttpClient, HttpReads}
import v1.models.errors.DownstreamError

import scala.concurrent.{ExecutionContext, Future}

Expand Down Expand Up @@ -77,4 +78,16 @@ trait BaseDownstreamConnector {

doPut(downstreamHeaderCarrier(Seq("Content-Type")))
}

def post[Body: Writes, Resp](body: Body, uri: DesUri[Resp])(implicit ec: ExecutionContext,
hc: HeaderCarrier,
httpReads: HttpReads[DesOutcome[Resp]],
correlationId: String): Future[DesOutcome[Resp]] = {

def doPost(implicit hc: HeaderCarrier): Future[DesOutcome[Resp]] = {
http.POST(url = s"${appConfig.desBaseUrl}/${uri.value}", body)
}

doPost(downstreamHeaderCarrier(Seq("Content-Type")))
}
}
4 changes: 2 additions & 2 deletions app/v1/connectors/CreateMarriageAllowanceConnector.scala
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ class CreateMarriageAllowanceConnector @Inject()(val http: HttpClient,

val nino = request.nino.nino

put(
uri = DesUri[Unit](s"marriage-allowance/claim/nino/$nino"), body = request.body
post(
uri = DesUri[Unit](s"income-tax/marriage-allowance/claim/NINO/$nino"), body = request.body
)
}
}
102 changes: 102 additions & 0 deletions app/v1/controllers/CreateMarriageAllowanceController.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/*
* Copyright 2021 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 v1.controllers

import cats.data.EitherT
import cats.implicits._
import config.AppConfig
import javax.inject.Inject
import play.api.libs.json.{JsValue, Json}
import play.api.mvc.{Action, AnyContentAsJson, ControllerComponents}
import play.mvc.Http.MimeTypes
import utils.{IdGenerator, Logging}
import v1.controllers.requestParsers.CreateMarriageAllowanceRequestParser
import v1.models.errors._
import v1.models.request.marriageAllowance.CreateMarriageAllowanceRawData
import v1.services.{CreateMarriageAllowanceService, EnrolmentsAuthService, MtdIdLookupService}

import scala.concurrent.{ExecutionContext, Future}

class CreateMarriageAllowanceController @Inject()(val authService: EnrolmentsAuthService,
val lookupService: MtdIdLookupService,
requestParser: CreateMarriageAllowanceRequestParser,
service: CreateMarriageAllowanceService,
cc: ControllerComponents,
val idGenerator: IdGenerator)(implicit ec: ExecutionContext)
extends AuthorisedController(cc) with BaseController with Logging {

implicit val endpointLogContext: EndpointLogContext =
EndpointLogContext(
controllerName = "CreateMarriageAllowanceController",
endpointName = "createMarriageAllowance"
)

//noinspection ScalaStyle
def createMarriageAllowance(nino: String): Action[JsValue] =
authorisedAction(nino).async(parse.json) { implicit request =>

implicit val correlationId: String = idGenerator.generateCorrelationId
logger.info(
s"[${endpointLogContext.controllerName}][${endpointLogContext.endpointName}] " +
s"with CorrelationId: $correlationId")

val rawData: CreateMarriageAllowanceRawData = CreateMarriageAllowanceRawData(
nino = nino,
body = AnyContentAsJson(request.body)
)

val result =
for {
parsedRequest <- EitherT.fromEither[Future](requestParser.parseRequest(rawData))
serviceResponse <- EitherT(service.create(parsedRequest))
} yield {
logger.info(
s"[${endpointLogContext.controllerName}][${endpointLogContext.endpointName}] - " +
s"Success response received with CorrelationId: ${serviceResponse.correlationId}")

Created
.withApiHeaders(serviceResponse.correlationId)
.as(MimeTypes.JSON)
}

result.leftMap { errorWrapper =>
val resCorrelationId = errorWrapper.correlationId
val result = errorResult(errorWrapper).withApiHeaders(resCorrelationId)
logger.warn(
s"[${endpointLogContext.controllerName}][${endpointLogContext.endpointName}] - " +
s"Error response received with CorrelationId: $resCorrelationId")

result
}.merge
}

private def errorResult(errorWrapper: ErrorWrapper) = {
(errorWrapper.error: @unchecked) match {
case BadRequestError |
NinoFormatError |
PartnerFirstNameFormatError |
PartnerSurnameFormatError |
PartnerNinoFormatError |
PartnerDoBFormatError |
MtdErrorWithCustomMessage(RuleIncorrectOrEmptyBodyError.code)
=> BadRequest(Json.toJson(errorWrapper))
case RuleDeceasedRecipientError |
RuleActiveMarriageAllowanceClaimError => Forbidden(Json.toJson(errorWrapper))
case DownstreamError => InternalServerError(Json.toJson(errorWrapper))
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class CreateMarriageAllowanceValidator extends Validator[CreateMarriageAllowance
val body = data.body.json.as[CreateMarriageAllowanceBody]

List(Validator.flattenErrors(List(
Validator.validateOptional(body.spouseOrCivilPartnerNino)(NinoValidation.validate(_, PartnerNinoFormatError)),
NinoValidation.validate(body.spouseOrCivilPartnerNino, PartnerNinoFormatError),
SurnameValidation.validate(body.spouseOrCivilPartnerSurname, PartnerSurnameFormatError),
Validator.validateOptional(body.spouseOrCivilPartnerFirstName)(GivenNameValidation.validate(_, PartnerFirstNameFormatError)),
Validator.validateOptional(body.spouseOrCivilPartnerDateOfBirth)(DateFormatValidation.validate(_, PartnerDoBFormatError)),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import play.api.libs.json.{JsPath, Json, OWrites, Reads}
import utils.JsonUtils


case class CreateMarriageAllowanceBody(spouseOrCivilPartnerNino: Option[String],
case class CreateMarriageAllowanceBody(spouseOrCivilPartnerNino: String,
spouseOrCivilPartnerFirstName: Option[String],
spouseOrCivilPartnerSurname: String,
spouseOrCivilPartnerDateOfBirth: Option[String])
Expand All @@ -31,7 +31,7 @@ object CreateMarriageAllowanceBody extends JsonUtils {
implicit val reads: Reads[CreateMarriageAllowanceBody] = Json.reads[CreateMarriageAllowanceBody]

implicit val writes: OWrites[CreateMarriageAllowanceBody] = (
(JsPath \ "participant1Details" \ "nino").writeNullable[String] and
(JsPath \ "participant1Details" \ "nino").write[String] and
(JsPath \ "participant1Details" \ "firstForeName").writeNullable[String] and
(JsPath \ "participant1Details" \ "surname").write[String] and
(JsPath \ "participant1Details" \ "dateOfBirth").writeNullable[String]
Expand Down
3 changes: 3 additions & 0 deletions conf/v1.routes
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
POST /marriage-allowance/:nino v1.controllers.CreateMarriageAllowanceController.createMarriageAllowance(nino: String)

PUT /:nino/:taxYear v1.controllers.AmendDisclosuresController.amendDisclosures(nino: String, taxYear: String)

DELETE /:nino/:taxYear v1.controllers.DeleteDisclosuresController.deleteDisclosures(nino: String, taxYear: String)
GET /:nino/:taxYear v1.controllers.RetrieveDisclosuresController.retrieveDisclosures(nino: String, taxYear: String)
Loading

0 comments on commit ddfa366

Please sign in to comment.