Skip to content

Commit

Permalink
MMCA-5089 Add new endpoint for eori history without eori in url (#58)
Browse files Browse the repository at this point in the history
  • Loading branch information
magicaljellybeans authored Jan 15, 2025
1 parent 5912139 commit 450b472
Show file tree
Hide file tree
Showing 3 changed files with 132 additions and 7 deletions.
14 changes: 13 additions & 1 deletion app/controllers/EoriHistoryController.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package controllers

import actionbuilders.{AuthorisedRequest, RequestWithEori}
import connectors.Sub21Connector
import models.EoriPeriod
import play.api.libs.json.{Json, OFormat}
Expand All @@ -30,7 +31,8 @@ import scala.concurrent.{ExecutionContext, Future}
class EoriHistoryController @Inject() (
historicEoriRepository: HistoricEoriRepository,
eoriHistoryConnector: Sub21Connector,
cc: ControllerComponents
cc: ControllerComponents,
authorisedRequest: AuthorisedRequest
)(implicit executionContext: ExecutionContext)
extends BackendController(cc) {

Expand All @@ -44,6 +46,16 @@ class EoriHistoryController @Inject() (
}
}

def getEoriHistoryV2: Action[AnyContent] = authorisedRequest async { implicit request: RequestWithEori[AnyContent] =>
val eori = request.eori.value

historicEoriRepository.get(eori).flatMap {
case Right(eoriPeriods) if eoriPeriods.headOption.exists(_.definedDates) =>
Future.successful(Ok(Json.toJson(EoriHistoryResponse(eoriPeriods))))
case _ => retrieveAndStoreHistoricEoris(eori)
}
}

def updateEoriHistory(): Action[EoriPeriod] = Action.async(parse.json[EoriPeriod]) { implicit request =>
(for {
eoriHistory <- eoriHistoryConnector.getEoriHistory(request.body.eori)
Expand Down
2 changes: 2 additions & 0 deletions conf/app.routes
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ GET /eori/:eori/company-information controllers.CompanyInfor
GET /eori/company-information controllers.CompanyInformationController.getCompanyInformationV2

GET /eori/:eori/eori-history controllers.EoriHistoryController.getEoriHistory(eori: String)
GET /eori/eori-history controllers.EoriHistoryController.getEoriHistoryV2()

GET /eori/:eori/xieori-information controllers.XiEoriController.getXiEoriInformation(eori: String)

POST /update-email controllers.VerifiedEmailController.updateVerifiedEmail()
Expand Down
123 changes: 117 additions & 6 deletions test/controllers/EoriHistoryControllerSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,25 @@

package controllers

import actionbuilders.CustomAuthConnector
import org.mockito.ArgumentMatchers.any
import org.mockito.Mockito.when
import play.api.{Application, inject}
import play.api.libs.json.{JsObject, Json}
import play.api.test.FakeRequest
import play.api.test.Helpers._
import play.api.test.Helpers.*
import connectors.Sub21Connector
import models.EoriPeriod
import play.api.mvc.{AnyContentAsEmpty, AnyContentAsJson}
import repositories.{
FailedToRetrieveHistoricEori, FailedToUpdateHistoricEori, HistoricEoriRepository, HistoricEoriSuccessful
}
import utils.SpecBase
import utils.{MockAuthConnector, SpecBase}

import java.time.LocalDate
import scala.concurrent.Future

class EoriHistoryControllerSpec extends SpecBase {
class EoriHistoryControllerSpec extends SpecBase with MockAuthConnector {

"getEoriHistory" should {

Expand Down Expand Up @@ -143,6 +144,114 @@ class EoriHistoryControllerSpec extends SpecBase {
}
}

"getEoriHistoryV2" should {

"return historic EORI's and not call SUB21 if the trader data has eori history defined" in new Setup {
val eoriPeriods: Seq[EoriPeriod] = Seq(EoriPeriod("testEori", Some(date), Some(date)))

when(mockHistoricEoriRepository.get(any())).thenReturn(Future.successful(Right(eoriPeriods)))

running(app) {
val request = FakeRequest(GET, getRouteV2)

val result = route(app, request).value

status(result) mustBe OK

contentAsJson(result) mustBe Json.obj(
"eoriHistory" -> Json.arr(
Json.obj("eori" -> "testEori", "validFrom" -> date, "validUntil" -> date)
)
)
}
}

"return historic EORI's and not call SUB21 if the trader data has eori history defined no from date" in new Setup {
val eoriPeriods: Seq[EoriPeriod] = Seq(EoriPeriod("testEori", None, Some(date)))

when(mockHistoricEoriRepository.get(any())).thenReturn(Future.successful(Right(eoriPeriods)))

running(app) {
val request = FakeRequest(GET, getRouteV2)

val result = route(app, request).value

status(result) mustBe OK

contentAsJson(result) mustBe Json.obj(
"eoriHistory" -> Json.arr(
Json.obj("eori" -> "testEori", "validUntil" -> date)
)
)
}
}

"return historic EORI's and call SUB21 if the trader data has no eori history" in new Setup {
val eoriPeriods: Seq[EoriPeriod] = Seq(EoriPeriod("testEori", None, Some(date)))

when(mockHistoricEoriRepository.get(any()))
.thenReturn(Future.successful(Left(FailedToRetrieveHistoricEori)), Future.successful(Right(eoriPeriods)))

when(mockHistoricEoriRepository.set(any())).thenReturn(Future.successful(HistoricEoriSuccessful))

when(mockHistoryService.getEoriHistory(any())).thenReturn(Future.successful(Seq.empty))

val request: FakeRequest[AnyContentAsEmpty.type] = FakeRequest(GET, getRouteV2)

running(app) {
val result = route(app, request).value

status(result) mustBe OK

contentAsJson(result) mustBe Json.obj(
"eoriHistory" -> Json.arr(
Json.obj("eori" -> "testEori", "validUntil" -> date)
)
)
}
}

"return internal server error if the update to historic eori's failed" in new Setup {
val eoriPeriods: Seq[EoriPeriod] = Seq(EoriPeriod("testEori", Some(date), Some(date)))

when(mockHistoricEoriRepository.get(any()))
.thenReturn(Future.successful(Left(FailedToRetrieveHistoricEori)), Future.successful(Right(eoriPeriods)))

when(mockHistoricEoriRepository.set(any())).thenReturn(Future.successful(FailedToUpdateHistoricEori))

when(mockHistoryService.getEoriHistory(any())).thenReturn(Future.successful(Seq.empty))

val request: FakeRequest[AnyContentAsEmpty.type] = FakeRequest(GET, getRouteV2)

running(app) {
val result = route(app, request).value

status(result) mustBe INTERNAL_SERVER_ERROR
}
}

"return internal server error if the trader cannot be found after updating the historic eori's" in new Setup {
when(mockHistoricEoriRepository.get(any()))
.thenReturn(
Future.successful(Left(FailedToRetrieveHistoricEori)),
Future.successful(Left(FailedToRetrieveHistoricEori))
)

when(mockHistoricEoriRepository.set(any()))
.thenReturn(Future.successful(HistoricEoriSuccessful))

when(mockHistoryService.getEoriHistory(any())).thenReturn(Future.successful(Seq.empty))

val request: FakeRequest[AnyContentAsEmpty.type] = FakeRequest(GET, getRouteV2)

running(app) {
val result = route(app, request).value

status(result) mustBe INTERNAL_SERVER_ERROR
}
}
}

"updateEoriHistory" should {
"return 204 if the update to historic EORI was successful" in new Setup {
when(mockHistoryService.getEoriHistory(any())).thenReturn(Future.successful(Seq.empty))
Expand Down Expand Up @@ -241,16 +350,18 @@ class EoriHistoryControllerSpec extends SpecBase {
val date: String = LocalDate.now().toString
val eori = "test_eori"

val getRoute: String = routes.EoriHistoryController.getEoriHistory(testEori).url
val postRoute: String = routes.EoriHistoryController.updateEoriHistory().url
val getRoute: String = routes.EoriHistoryController.getEoriHistory(testEori).url
val getRouteV2: String = routes.EoriHistoryController.getEoriHistoryV2().url
val postRoute: String = routes.EoriHistoryController.updateEoriHistory().url

val mockHistoricEoriRepository: HistoricEoriRepository = mock[HistoricEoriRepository]
val mockHistoryService: Sub21Connector = mock[Sub21Connector]

val app: Application = application
.overrides(
inject.bind[HistoricEoriRepository].toInstance(mockHistoricEoriRepository),
inject.bind[Sub21Connector].toInstance(mockHistoryService)
inject.bind[Sub21Connector].toInstance(mockHistoryService),
inject.bind[CustomAuthConnector].toInstance(mockAuthConnector)
)
.build()
}
Expand Down

0 comments on commit 450b472

Please sign in to comment.