Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ARS-430: Save submitted answers #29

Merged
merged 7 commits into from Mar 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -20,17 +20,20 @@ import javax.inject.{Inject, Singleton}

import scala.concurrent.ExecutionContext

import play.api.libs.json.{Json, JsValue}
import play.api.mvc.{Action, AnyContent, ControllerComponents}
import uk.gov.hmrc.advancevaluationrulings.logging.RequestAwareLogger
import uk.gov.hmrc.advancevaluationrulings.models.ValuationRulingsApplication
import uk.gov.hmrc.advancevaluationrulings.models.common.{AcknowledgementReference, EoriNumber}
import uk.gov.hmrc.advancevaluationrulings.models.common.Envelope._
import uk.gov.hmrc.advancevaluationrulings.services.TraderDetailsService
import uk.gov.hmrc.advancevaluationrulings.services.{TraderDetailsService, ValuationRulingsService}
import uk.gov.hmrc.play.bootstrap.backend.controller.BackendController

@Singleton()
class ValuationRulingsController @Inject() (
cc: ControllerComponents,
traderDetailsService: TraderDetailsService
traderDetailsService: TraderDetailsService,
valuationRulingsService: ValuationRulingsService
) extends BackendController(cc) {

protected lazy val logger: RequestAwareLogger = new RequestAwareLogger(this.getClass)
Expand All @@ -50,4 +53,16 @@ class ValuationRulingsController @Inject() (
)
.toResult
}

def submitAnswers(): Action[JsValue] =
Action.async(parse.json) {
implicit request =>
extractFromJson[ValuationRulingsApplication] {
rulingsApplication =>
logger.warn(s"User answers: ${Json.prettyPrint(Json.toJson(rulingsApplication))}")
valuationRulingsService
.submitApplication(rulingsApplication)
.toResult
}
}
}
62 changes: 62 additions & 0 deletions app/uk/gov/hmrc/advancevaluationrulings/controllers/package.scala
@@ -0,0 +1,62 @@
/*
* Copyright 2023 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 uk.gov.hmrc.advancevaluationrulings

import scala.concurrent.Future
import scala.util.{Failure, Success, Try}

import play.api.libs.json._
import play.api.mvc.{Request, Result, Results}
import uk.gov.hmrc.advancevaluationrulings.logging.RequestAwareLogger
import uk.gov.hmrc.advancevaluationrulings.models.common.Statuses
import uk.gov.hmrc.advancevaluationrulings.models.errors.{ErrorResponse, ValidationError, ValidationErrors}
import uk.gov.hmrc.http.HeaderCarrier

package object controllers {

protected lazy val logger: RequestAwareLogger = new RequestAwareLogger(this.getClass)

def extractFromJson[T](
func: T => Future[Result]
)(implicit request: Request[JsValue], reads: Reads[T], hc: HeaderCarrier): Future[Result] = {
logger.warn(s"Received json request: ${Json.prettyPrint(request.body)}")
Try(request.body.validate[T]) match {
case Success(JsSuccess(payload, _)) => func(payload)
case Success(JsError(errors)) =>
val failureReason = errors
.map { case (path, _) => s"field at path: [$path] missing or invalid" }
.mkString(", ")
toBadRequest(failureReason)
case Failure(e) =>
toBadRequest(e.getMessage)
}
}

private def toBadRequest(errorMessage: String)(implicit hc: HeaderCarrier) = {
logger.warn(s"Failed to extract case class from Json: $errorMessage")
Future.successful(
Results.BadRequest(
Json.toJson(
ErrorResponse(
Statuses.ValidationFailed,
ValidationErrors(Seq(ValidationError(errorMessage)))
)
)
)
)
}
}
@@ -0,0 +1,59 @@
/*
* Copyright 2023 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 uk.gov.hmrc.advancevaluationrulings.models

import java.time.Instant

import play.api.libs.json._
import uk.gov.hmrc.advancevaluationrulings.models.common.UserAnswers
import uk.gov.hmrc.mongo.play.json.formats.MongoJavatimeFormats

final case class ValuationRulingsApplication(
id: String,
data: UserAnswers,
applicationNumber: String,
lastUpdated: Instant
)

object ValuationRulingsApplication {

val reads: Reads[ValuationRulingsApplication] = {

import play.api.libs.functional.syntax._

(
(__ \ "_id").read[String] and
(__ \ "data").read[UserAnswers] and
(__ \ "applicationNumber").read[String] and
(__ \ "lastUpdated").read(MongoJavatimeFormats.instantFormat)
)(ValuationRulingsApplication.apply _)
}

val writes: OWrites[ValuationRulingsApplication] = {

import play.api.libs.functional.syntax._

(
(__ \ "_id").write[String] and
(__ \ "data").write[UserAnswers] and
(__ \ "applicationNumber").write[String] and
(__ \ "lastUpdated").write(MongoJavatimeFormats.instantFormat)
)(unlift(ValuationRulingsApplication.unapply))
}

implicit val format: OFormat[ValuationRulingsApplication] = OFormat(reads, writes)
}
@@ -0,0 +1,29 @@
/*
* Copyright 2023 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 uk.gov.hmrc.advancevaluationrulings.models.common

import play.api.libs.json.{Json, OFormat}

final case class ApplicationContactDetails(
name: String,
email: String,
phone: String
)

object ApplicationContactDetails {
implicit val format: OFormat[ApplicationContactDetails] = Json.format[ApplicationContactDetails]
}
Expand Up @@ -22,7 +22,6 @@ import play.api.libs.json.{Format, Json}
import play.api.libs.json.OFormat.oFormatFromReadsAndOWrites
import play.api.mvc.{Result, Results}
import uk.gov.hmrc.advancevaluationrulings.models.errors.BaseError
import uk.gov.hmrc.http.HeaderCarrier

import cats.data.EitherT
import cats.implicits._
Expand All @@ -31,16 +30,19 @@ object Envelope {

type Envelope[T] = EitherT[Future, BaseError, T]

def apply[T](future: Future[Either[BaseError, T]]): EitherT[Future, BaseError, T] = {
EitherT.apply(future)
}

implicit class EnvelopeExt[T](envelope: EitherT[Future, BaseError, T]) {
def toResult(implicit
ec: ExecutionContext,
hc: HeaderCarrier,
format: Format[T]
): Future[Result] =
envelope
.leftMap(_.toErrorResponse)
.fold(
error => Results.Status(error.statusCode)(Json.toJson(error)),
error => Results.Status(500)(Json.toJson(error)),
success => Results.Status(200)(Json.toJson(success))
)
}
Expand Down
@@ -0,0 +1,28 @@
/*
* Copyright 2023 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 uk.gov.hmrc.advancevaluationrulings.models.common

import play.api.libs.json.Format
import uk.gov.hmrc.mongo.play.json.formats.MongoJavatimeFormats

import java.time.Instant

final case class LastUpdatedTime(value: Instant)

object LastUpdatedTime {
implicit val format: Format[Instant] = MongoJavatimeFormats.instantFormat
}
@@ -0,0 +1,33 @@
/*
* Copyright 2023 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 uk.gov.hmrc.advancevaluationrulings.models.common

import play.api.libs.json.{Json, OFormat}

final case class RegisteredDetailsCheck(
value: Boolean,
eori: String,
name: String,
streetAndNumber: String,
city: String,
country: String,
postalCode: Option[String]
)

object RegisteredDetailsCheck {
implicit val format: OFormat[RegisteredDetailsCheck] = Json.format[RegisteredDetailsCheck]
}
@@ -0,0 +1,29 @@
/*
* Copyright 2023 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 uk.gov.hmrc.advancevaluationrulings.models.common

object Statuses {

val ApplicationSubmitted = "ApplicationSubmitted"
val SubmissionFailed = "SubmissionFailed"
val ConnectorError = "ConnectorError"
val UpstreamServiceError = "UpstreamServiceError"
val SerializationError = "SerializationError"
val ParseError = "ParseError"
val ValidationFailed = "ValidationFailed"

}
@@ -0,0 +1,28 @@
/*
* Copyright 2023 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 uk.gov.hmrc.advancevaluationrulings.models.common

import play.api.libs.json.{Json, OFormat}

final case class SubmissionSuccess(
acknowledged: Boolean,
status: String = Statuses.ApplicationSubmitted
)

object SubmissionSuccess {
implicit val format: OFormat[SubmissionSuccess] = Json.format[SubmissionSuccess]
}
@@ -0,0 +1,29 @@
/*
* Copyright 2023 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 uk.gov.hmrc.advancevaluationrulings.models.common

import play.api.libs.json.{Json, OFormat}

final case class SupportingDocument(
fileName: String,
downloadUrl: String,
isConfidential: Boolean
)

object SupportingDocument {
implicit val format: OFormat[SupportingDocument] = Json.format[SupportingDocument]
}