Skip to content

Commit

Permalink
DDCE-5000 Remove pureconfig (#523)
Browse files Browse the repository at this point in the history
* DDCE-5000 Remove pureconfig

* WIP
  • Loading branch information
gastonracui authored May 1, 2024
1 parent 0a49eef commit af9bd4b
Show file tree
Hide file tree
Showing 34 changed files with 249 additions and 224 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,11 @@

package uk.gov.hmrc.merchandiseinbaggage.config

import javax.inject.Inject
import play.api.libs.json.{JsObject, Json}

class AddressLookupConfig @Inject() {
object AddressLookupConfig {

def config(continueUrl: String): JsObject =
def configAddressLookup(continueUrl: String): JsObject =
Json
.parse(s"""{
| "version": 2,
Expand Down
59 changes: 28 additions & 31 deletions app/uk/gov/hmrc/merchandiseinbaggage/config/AppConfig.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,59 +17,56 @@
package uk.gov.hmrc.merchandiseinbaggage.config

import com.google.inject.Inject
import com.typesafe.config.ConfigFactory
import play.api.{Configuration, Environment}
import pureconfig.ConfigSource
import pureconfig.generic.auto._
import uk.gov.hmrc.merchandiseinbaggage.config.AppConfigSource.configSource
import uk.gov.hmrc.merchandiseinbaggage.model.api.tpspayments.TpsNavigation
import uk.gov.hmrc.play.bootstrap.config.ServicesConfig

import javax.inject.Singleton

@Singleton
class AppConfig @Inject() (val config: Configuration, val env: Environment)()
extends MibConfiguration
with IsAssistedDigitalConfiguration {
class AppConfig @Inject() (val config: Configuration, val env: Environment, servicesConfig: ServicesConfig)()
extends IsAssistedDigitalConfiguration {

private val serviceIdentifier = "mib"

private val contactHost: String = configSource("contact-frontend.host").loadOrThrow[String]
val contactUrl = s"$contactHost/contact/contact-hmrc-unauthenticated?service=$serviceIdentifier"

lazy val strideRoles: Seq[String] = config.get[Seq[String]]("stride.roles")
lazy val timeout: Int = configSource("timeout.timeout").loadOrThrow[Int]
lazy val countdown: Int = configSource("timeout.countdown").loadOrThrow[Int]
lazy val timeout: Int = config.get[Int]("timeout.timeout")
lazy val countdown: Int = config.get[Int]("timeout.countdown")

lazy val paymentsReturnUrl: String = configSource("payments.returnUrl").loadOrThrow[String]
lazy val paymentsBackUrl: String = configSource("payments.backUrl").loadOrThrow[String]
lazy val paymentsReturnUrl: String = config.get[String]("payments.returnUrl")
lazy val paymentsBackUrl: String = config.get[String]("payments.backUrl")

lazy val tpsNavigation: TpsNavigation = configSource("tps-navigation").loadOrThrow[TpsNavigation]
lazy val tpsNavigation: TpsNavigation = TpsNavigation(
back = config.get[String]("tps-navigation.back"),
reset = config.get[String]("tps-navigation.reset"),
finish = config.get[String]("tps-navigation.finish")
)

lazy val mongoTTL: Int = config.get[Int]("mongodb.timeToLiveInSeconds")

val feedbackUrl: String = {
val url = configSource("microservice.services.feedback-frontend.url").loadOrThrow[String]
val url = config.get[String]("microservice.services.feedback-frontend.url")
s"$url/$serviceIdentifier"
}

lazy val languageTranslationEnabled: Boolean = configSource("features.welsh-translation").loadOrThrow[Boolean]
lazy val languageTranslationEnabled: Boolean = config.get[Boolean]("features.welsh-translation")

}
private lazy val mibBaseUrl: String = "/declare-commercial-goods"
lazy val mibDeclarationsUrl: String = s"$mibBaseUrl/declarations"
lazy val mibCalculationsUrl: String = s"$mibBaseUrl/calculations"
lazy val mibAmendsPlusExistingCalculationsUrl: String = s"$mibBaseUrl/amend-calculations"
lazy val mibCheckEoriUrl: String = s"$mibBaseUrl/validate/eori/"

object AppConfigSource {
val configSource: String => ConfigSource = ConfigSource.default.at
lazy val paymentUrl: String = servicesConfig.baseUrl("payment")
lazy val tpsPaymentsBackendUrl: String = servicesConfig.baseUrl("tps-payments-backend")
lazy val merchandiseInBaggageUrl: String = servicesConfig.baseUrl("merchandise-in-baggage")
lazy val addressLookupFrontendUrl: String = servicesConfig.baseUrl("address-lookup-frontend")
lazy val addressLookupCallbackUrl: String =
config.get[String]("microservice.services.address-lookup-frontend.callback")
}

trait MibConfiguration {
lazy val mibConf: MIBConf = configSource("microservice.services.merchandise-in-baggage").loadOrThrow[MIBConf]
lazy val baseUrl: String = "/declare-commercial-goods"
lazy val declarationsUrl: String = s"$baseUrl/declarations"
lazy val calculationsUrl: String = s"$baseUrl/calculations"
lazy val amendsPlusExistingCalculationsUrl: String = s"$baseUrl/amend-calculations"
lazy val checkEoriUrl: String = s"$baseUrl/validate/eori/"
}

final case class MIBConf(protocol: String, host: String, port: Int)

trait IsAssistedDigitalConfiguration {
lazy val isAssistedDigital: Boolean = configSource("assistedDigital").loadOrThrow[Boolean]
// to avoid re writing the codebase, need to improve in the future to allow injection
lazy val isAssistedDigital: Boolean = ConfigFactory.load().getBoolean("assistedDigital")
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -16,30 +16,29 @@

package uk.gov.hmrc.merchandiseinbaggage.connectors

import javax.inject.{Inject, Named, Singleton}
import javax.inject.{Inject, Singleton}
import play.api.http.HeaderNames.LOCATION
import play.api.libs.json.{JsObject, JsValue, Json}
import play.api.mvc.Call
import uk.gov.hmrc.http.HttpReads.Implicits._
import uk.gov.hmrc.http.{HeaderCarrier, HttpClient, HttpResponse}
import uk.gov.hmrc.merchandiseinbaggage.config.AddressLookupConfig
import uk.gov.hmrc.merchandiseinbaggage.config.AddressLookupConfig._
import uk.gov.hmrc.merchandiseinbaggage.config.AppConfig
import uk.gov.hmrc.merchandiseinbaggage.model.api.addresslookup.Address

import scala.concurrent.{ExecutionContext, Future}

@Singleton
class AddressLookupFrontendConnector @Inject() (
http: HttpClient,
@Named("addressLookupFrontendBaseUrl") baseUrl: String,
@Named("addressLookupCallback") callback: String,
addressLookupConfig: AddressLookupConfig
) {
class AddressLookupFrontendConnector @Inject() (appConfig: AppConfig, http: HttpClient) {

private val baseUrl = appConfig.addressLookupFrontendUrl
private val callback = appConfig.addressLookupCallbackUrl

private lazy val initJourneyUrl = s"$baseUrl/api/v2/init"
private def confirmJourneyUrl(id: String) = s"$baseUrl/api/confirmed?id=$id"

def initJourney(call: Call)(implicit hc: HeaderCarrier, ec: ExecutionContext): Future[String] = {
val addressConfig = Json.toJson(addressLookupConfig.config(s"$callback${call.url}"))
val addressConfig = Json.toJson(configAddressLookup(s"$callback${call.url}"))

http.POST[JsValue, HttpResponse](initJourneyUrl, addressConfig) map { response =>
response.header(LOCATION).getOrElse {
Expand All @@ -50,5 +49,4 @@ class AddressLookupFrontendConnector @Inject() (

def getAddress(id: String)(implicit hc: HeaderCarrier, ec: ExecutionContext): Future[Address] =
http.GET[JsObject](confirmJourneyUrl(id)) map (json => (json \ "address").as[Address])

}
40 changes: 26 additions & 14 deletions app/uk/gov/hmrc/merchandiseinbaggage/connectors/MibConnector.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,32 +17,34 @@
package uk.gov.hmrc.merchandiseinbaggage.connectors

import cats.data.EitherT
import javax.inject.{Inject, Named, Singleton}

import javax.inject.{Inject, Singleton}
import play.api.Logging
import play.api.http.Status
import uk.gov.hmrc.http.HttpReads.Implicits.{readFromJson, readRaw}
import uk.gov.hmrc.http.{HeaderCarrier, HttpClient, HttpResponse}
import uk.gov.hmrc.merchandiseinbaggage.config.MibConfiguration
import uk.gov.hmrc.merchandiseinbaggage.config.AppConfig
import uk.gov.hmrc.merchandiseinbaggage.model.api.calculation.{CalculationAmendRequest, CalculationRequest, CalculationResponse}
import uk.gov.hmrc.merchandiseinbaggage.model.api.checkeori.CheckResponse
import uk.gov.hmrc.merchandiseinbaggage.model.api.{Declaration, DeclarationId, Eori, MibReference}

import scala.concurrent.{ExecutionContext, Future}

@Singleton
class MibConnector @Inject() (httpClient: HttpClient, @Named("mibBackendBaseUrl") base: String)(implicit
class MibConnector @Inject() (appConfig: AppConfig, httpClient: HttpClient)(implicit
ec: ExecutionContext
) extends MibConfiguration
with Logging {
) extends Logging {

private val baseUrl = appConfig.merchandiseInBaggageUrl

def persistDeclaration(declaration: Declaration)(implicit hc: HeaderCarrier): Future[DeclarationId] =
httpClient.POST[Declaration, DeclarationId](s"$base$declarationsUrl", declaration)
httpClient.POST[Declaration, DeclarationId](s"$baseUrl${appConfig.mibDeclarationsUrl}", declaration)

def amendDeclaration(declaration: Declaration)(implicit hc: HeaderCarrier): Future[DeclarationId] =
httpClient.PUT[Declaration, DeclarationId](s"$base$declarationsUrl", declaration)
httpClient.PUT[Declaration, DeclarationId](s"$baseUrl${appConfig.mibDeclarationsUrl}", declaration)

def findDeclaration(declarationId: DeclarationId)(implicit hc: HeaderCarrier): Future[Option[Declaration]] =
httpClient.GET[HttpResponse](s"$base$declarationsUrl/${declarationId.value}").map { response =>
httpClient.GET[HttpResponse](s"$baseUrl${appConfig.mibDeclarationsUrl}/${declarationId.value}").map { response =>
response.status match {
case Status.OK => response.json.asOpt[Declaration]
case other =>
Expand All @@ -55,8 +57,11 @@ class MibConnector @Inject() (httpClient: HttpClient, @Named("mibBackendBaseUrl"
hc: HeaderCarrier
): EitherT[Future, String, Option[Declaration]] =
EitherT(
httpClient.GET[HttpResponse](s"$base$declarationsUrl?mibReference=${mibReference.value}&eori=${eori.value}").map {
response =>
httpClient
.GET[HttpResponse](
s"$baseUrl${appConfig.mibDeclarationsUrl}?mibReference=${mibReference.value}&eori=${eori.value}"
)
.map { response =>
response.status match {
case Status.OK => Right(response.json.asOpt[Declaration])
case Status.NOT_FOUND => Right(None)
Expand All @@ -66,20 +71,27 @@ class MibConnector @Inject() (httpClient: HttpClient, @Named("mibBackendBaseUrl"
)
Left(s"unexpected status for findBy, status:$other")
}
}
}
)

def calculatePayments(calculationRequests: Seq[CalculationRequest])(implicit
hc: HeaderCarrier
): Future[CalculationResponse] =
httpClient.POST[Seq[CalculationRequest], CalculationResponse](s"$base$calculationsUrl", calculationRequests)
httpClient
.POST[Seq[CalculationRequest], CalculationResponse](
s"$baseUrl${appConfig.mibCalculationsUrl}",
calculationRequests
)

def calculatePaymentsAmendPlusExisting(
amendRequest: CalculationAmendRequest
)(implicit hc: HeaderCarrier): Future[CalculationResponse] =
httpClient
.POST[CalculationAmendRequest, CalculationResponse](s"$base$amendsPlusExistingCalculationsUrl", amendRequest)
.POST[CalculationAmendRequest, CalculationResponse](
s"$baseUrl${appConfig.mibAmendsPlusExistingCalculationsUrl}",
amendRequest
)

def checkEoriNumber(eori: String)(implicit hc: HeaderCarrier): Future[CheckResponse] =
httpClient.GET[CheckResponse](s"$base$checkEoriUrl$eori")
httpClient.GET[CheckResponse](s"$baseUrl${appConfig.mibCheckEoriUrl}$eori")
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,16 @@ import play.api.http.Status
import play.api.libs.json.Json
import uk.gov.hmrc.http.HttpReads.Implicits.readRaw
import uk.gov.hmrc.http.{HeaderCarrier, HttpClient, HttpResponse}
import uk.gov.hmrc.merchandiseinbaggage.connectors.PaymentApiUrls._
import uk.gov.hmrc.merchandiseinbaggage.config.AppConfig
import uk.gov.hmrc.merchandiseinbaggage.model.api.payapi.{PayApiRequest, PayApiResponse}

import javax.inject.{Inject, Named, Singleton}
import javax.inject.{Inject, Singleton}
import scala.concurrent.{ExecutionContext, Future}

case class PayApiException(message: String) extends RuntimeException(message)

@Singleton
class PaymentConnector @Inject() (httpClient: HttpClient, @Named("paymentBaseUrl") baseUrl: String) extends Logging {
class PaymentConnector @Inject() (appConfig: AppConfig, httpClient: HttpClient) extends Logging {

private val url = s"$baseUrl$payUrl"
private val url = s"${appConfig.paymentUrl}/pay-api/mib-frontend/mib/journey/start"

def sendPaymentRequest(
requestBody: PayApiRequest
Expand All @@ -43,15 +41,10 @@ class PaymentConnector @Inject() (httpClient: HttpClient, @Named("paymentBaseUrl
response.status match {
case Status.CREATED => response.json.as[PayApiResponse]
case other: Int =>
throw PayApiException(
throw new RuntimeException(
s"unexpected status from pay-api for reference:${requestBody.mibReference.value}, status:$other"
)
}
}
}
}

object PaymentApiUrls {
val payUrl = "/pay-api/mib-frontend/mib/journey/start"
val payInitiatedJourneyUrl = "/pay/initiate-journey"
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,17 @@ package uk.gov.hmrc.merchandiseinbaggage.connectors
import play.api.http.Status
import uk.gov.hmrc.http.HttpReads.Implicits._
import uk.gov.hmrc.http.{HeaderCarrier, HttpClient, HttpResponse}
import uk.gov.hmrc.merchandiseinbaggage.config.AppConfig
import uk.gov.hmrc.merchandiseinbaggage.model.api.payapi.PayApiResponse
import uk.gov.hmrc.merchandiseinbaggage.model.api.tpspayments.TpsPaymentsRequest

import javax.inject.{Inject, Named, Singleton}
import javax.inject.{Inject, Singleton}
import scala.concurrent.{ExecutionContext, Future}

case class TpsPaymentsException(message: String) extends RuntimeException(message)

@Singleton
class TpsPaymentsBackendConnector @Inject() (httpClient: HttpClient, @Named("tpsBackendBaseUrl") baseUrl: String) {
class TpsPaymentsBackendConnector @Inject() (appConfig: AppConfig, httpClient: HttpClient) {

private val baseUrl = appConfig.tpsPaymentsBackendUrl

def tpsPayments(
requestBody: TpsPaymentsRequest
Expand All @@ -39,7 +40,7 @@ class TpsPaymentsBackendConnector @Inject() (httpClient: HttpClient, @Named("tps
response.status match {
case Status.CREATED => response.json.as[PayApiResponse]
case other: Int =>
throw TpsPaymentsException(
throw new RuntimeException(
s"unexpected status from tps-payments-backend for reference: ${requestBody.mibReference}, status: $other"
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ package uk.gov.hmrc.merchandiseinbaggage.controllers

import javax.inject.{Inject, Singleton}
import play.api.mvc.{Action, AnyContent, MessagesControllerComponents}
import uk.gov.hmrc.merchandiseinbaggage.config.{AppConfig, IsAssistedDigitalConfiguration}
import uk.gov.hmrc.merchandiseinbaggage.config.AppConfig
import uk.gov.hmrc.merchandiseinbaggage.forms.EnterEmailForm._
import uk.gov.hmrc.merchandiseinbaggage.navigation._
import uk.gov.hmrc.merchandiseinbaggage.repositories.DeclarationJourneyRepository
Expand All @@ -36,15 +36,14 @@ class EnterEmailController @Inject() (
viewOptional: EnterOptionalEmailView,
navigator: Navigator
)(implicit ec: ExecutionContext, appConfig: AppConfig)
extends DeclarationJourneyUpdateController
with IsAssistedDigitalConfiguration {
extends DeclarationJourneyUpdateController {

private def backButtonUrl(implicit request: DeclarationJourneyRequest[_]) =
backToCheckYourAnswersIfCompleteElse(routes.TravellerDetailsController.onPageLoad)

val onPageLoad: Action[AnyContent] =
actionProvider.journeyAction { implicit request =>
if (isAssistedDigital) {
if (appConfig.isAssistedDigital) {
val preparedForm = optionalForm.fill(request.declarationJourney.maybeEmailAddress)
Ok(viewOptional(preparedForm, request.declarationType, backButtonUrl))
} else {
Expand All @@ -54,7 +53,7 @@ class EnterEmailController @Inject() (
}

val onSubmit: Action[AnyContent] = actionProvider.journeyAction.async { implicit request =>
if (isAssistedDigital) {
if (appConfig.isAssistedDigital) {
optionalForm
.bindFromRequest()
.fold(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,12 @@ import play.api.i18n.Messages
import play.api.libs.json.{JsObject, Json}
import uk.gov.hmrc.govukfrontend.views.Aliases.{Table, TableRow, Text}
import uk.gov.hmrc.govukfrontend.views.viewmodels.table.HeadCell
import uk.gov.hmrc.merchandiseinbaggage.config.IsAssistedDigitalConfiguration
import uk.gov.hmrc.merchandiseinbaggage.model.api.GoodsVatRates.Zero
import uk.gov.hmrc.merchandiseinbaggage.model.api.calculation.{CalculationRequest, CalculationResults}
import uk.gov.hmrc.merchandiseinbaggage.model.api.{Country, _}
import uk.gov.hmrc.merchandiseinbaggage.model.core.PurchaseDetailsInput

object DataModelEnriched extends IsAssistedDigitalConfiguration {
object DataModelEnriched {

implicit class PurchaseDetailsEnriched(details: PurchaseDetails) {
import details._
Expand Down
Loading

0 comments on commit af9bd4b

Please sign in to comment.