Skip to content

Commit

Permalink
Merge pull request #491 from hmrc/TGP-2642/bug-fix-remove-skip-trader…
Browse files Browse the repository at this point in the history
…-ref-check

TGP-2642/bug-fix-remove-skip-trader-ref-check
  • Loading branch information
besscerule authored Oct 9, 2024
2 parents 0c88833 + 5086f0b commit c57c2ff
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 54 deletions.
69 changes: 29 additions & 40 deletions app/controllers/TraderReferenceController.scala
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ class TraderReferenceController @Inject() (
case None => form
case Some(value) => form.fill(value)
}

auditService
.auditStartUpdateGoodsRecord(
request.eori,
Expand Down Expand Up @@ -130,47 +129,37 @@ class TraderReferenceController @Inject() (
.bindFromRequest()
.fold(
formWithErrors => Future.successful(BadRequest(view(formWithErrors, onSubmitAction))),
value => {
val oldValueOpt = request.userAnswers.get(TraderReferenceUpdatePage(recordId))
val isValueChanged = oldValueOpt.exists(_ != value)
if (isValueChanged) {
goodsRecordConnector.filterRecordsByField(request.eori, value, "traderRef").flatMap {
case Some(traderRef) =>
for {
updatedAnswers <-
Future.fromTry(request.userAnswers.set(TraderReferenceUpdatePage(recordId), value))
_ <- sessionRepository.set(updatedAnswers)
} yield
if (traderRef.pagination.totalRecords == 0) {
Redirect(navigator.nextPage(TraderReferenceUpdatePage(recordId), mode, updatedAnswers))
.addingToSession(dataUpdated -> isValueChanged.toString)
.addingToSession(pageUpdated -> traderReference)
} else {
val formWithApiErrors =
form
.fill(value)
.copy(
errors =
Seq(elems = FormError("value", getMessage("traderReference.error.traderRefNotUnique")))
)
BadRequest(view(formWithApiErrors, onSubmitAction))
}
case None =>
Future.successful(
Redirect(
routes.GoodsRecordsLoadingController
.onPageLoad(Some(RedirectUrl(onSubmitAction.url)))
)
value =>
goodsRecordConnector.filterRecordsByField(request.eori, value, "traderRef").flatMap {
case Some(records) =>
for {
oldRecord <- goodsRecordConnector.getRecord(request.eori, recordId)
updatedAnswers <-
Future.fromTry(request.userAnswers.set(TraderReferenceUpdatePage(recordId), value))
_ <- sessionRepository.set(updatedAnswers)
} yield
if (records.pagination.totalRecords == 0 || oldRecord.traderRef == value) {
Redirect(navigator.nextPage(TraderReferenceUpdatePage(recordId), mode, updatedAnswers))
.addingToSession(dataUpdated -> (oldRecord.traderRef != value).toString)
.addingToSession(pageUpdated -> traderReference)
} else {
val formWithApiErrors =
form
.fill(value)
.copy(
errors =
Seq(elems = FormError("value", getMessage("traderReference.error.traderRefNotUnique")))
)
BadRequest(view(formWithApiErrors, onSubmitAction))
}
case None =>
Future.successful(
Redirect(
routes.GoodsRecordsLoadingController
.onPageLoad(Some(RedirectUrl(onSubmitAction.url)))
)
}
} else {
Future.successful(
Redirect(navigator.nextPage(TraderReferenceUpdatePage(recordId), mode, request.userAnswers))
.addingToSession(dataUpdated -> isValueChanged.toString)
.addingToSession(pageUpdated -> traderReference)
)
)
}
}
)
}
}
59 changes: 46 additions & 13 deletions test/controllers/TraderReferenceControllerSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import models.{GoodsRecordsPagination, NormalMode, UserAnswers}
import navigation.{FakeNavigator, Navigator}
import org.apache.pekko.Done
import org.mockito.ArgumentMatchers.{any, eq => eqTo}
import org.mockito.Mockito.{never, verify, when}
import org.mockito.Mockito.{verify, when}
import org.scalatestplus.mockito.MockitoSugar
import pages.{TraderReferencePage, TraderReferenceUpdatePage}
import play.api.data.FormError
Expand All @@ -54,12 +54,14 @@ class TraderReferenceControllerSpec extends SpecBase with MockitoSugar {
private val currentPage = firstPage
private val totalRecords = 23
private val numberOfPages = 3
private val record = goodsRecordResponse(
Instant.parse("2022-11-18T23:20:19Z"),
Instant.parse("2022-11-18T23:20:19Z")
)
private val records = Seq(
goodsRecordResponse(
Instant.parse("2022-11-18T23:20:19Z"),
Instant.parse("2022-11-18T23:20:19Z")
)
record
)

private val emptyResponse = GetRecordsResponse(
Seq.empty,
GoodsRecordsPagination(0, 0, 0, None, None)
Expand Down Expand Up @@ -360,6 +362,11 @@ class TraderReferenceControllerSpec extends SpecBase with MockitoSugar {
Some(emptyResponse)
)

when(mockGoodsRecordConnector.getRecord(any(), any())(any())) thenReturn Future
.successful(
record
)

val application =
applicationBuilder(userAnswers =
Some(emptyUserAnswers.set(TraderReferenceUpdatePage(recordId = testRecordId), "oldAnswer").success.value)
Expand Down Expand Up @@ -391,9 +398,19 @@ class TraderReferenceControllerSpec extends SpecBase with MockitoSugar {

val mockGoodsRecordConnector = mock[GoodsRecordConnector]

when(mockGoodsRecordConnector.getRecord(any(), any())(any())) thenReturn Future
.successful(
record
)

when(mockGoodsRecordConnector.filterRecordsByField(any(), any(), any())(any())) thenReturn Future
.successful(
Some(response)
)

val application =
applicationBuilder(userAnswers =
Some(emptyUserAnswers.set(TraderReferenceUpdatePage(recordId = testRecordId), "answer").success.value)
Some(emptyUserAnswers.set(TraderReferenceUpdatePage(recordId = testRecordId), "BAN0010011").success.value)
)
.overrides(
bind[Navigator].toInstance(new FakeNavigator(onwardRoute)),
Expand All @@ -405,15 +422,12 @@ class TraderReferenceControllerSpec extends SpecBase with MockitoSugar {
running(application) {
val request =
FakeRequest(POST, traderReferenceRoute)
.withFormUrlEncodedBody(("value", "answer"))
.withFormUrlEncodedBody(("value", "BAN0010011"))

val result = route(application, request).value

status(result) mustEqual SEE_OTHER
redirectLocation(result).value mustEqual onwardRoute.url

verify(mockGoodsRecordConnector, never()).filterRecordsByField(any(), any(), any())(any())

}
}

Expand All @@ -425,6 +439,11 @@ class TraderReferenceControllerSpec extends SpecBase with MockitoSugar {

val mockGoodsRecordConnector = mock[GoodsRecordConnector]

when(mockGoodsRecordConnector.getRecord(any(), any())(any())) thenReturn Future
.successful(
record
)

when(mockGoodsRecordConnector.filterRecordsByField(any(), any(), any())(any())) thenReturn Future
.successful(
Some(emptyResponse)
Expand Down Expand Up @@ -465,13 +484,18 @@ class TraderReferenceControllerSpec extends SpecBase with MockitoSugar {

val mockGoodsRecordConnector = mock[GoodsRecordConnector]

when(mockGoodsRecordConnector.getRecord(any(), any())(any())) thenReturn Future
.successful(
record
)

when(mockGoodsRecordConnector.filterRecordsByField(any(), any(), any())(any())) thenReturn Future
.successful(
Some(emptyResponse)
)

val userAnswers =
UserAnswers(userAnswersId).set(TraderReferenceUpdatePage(testRecordId), "oldValue").success.value
UserAnswers(userAnswersId).set(TraderReferenceUpdatePage(testRecordId), "BAN0010011").success.value
val application =
applicationBuilder(userAnswers = Some(userAnswers))
.overrides(
Expand All @@ -485,7 +509,7 @@ class TraderReferenceControllerSpec extends SpecBase with MockitoSugar {
val controller = application.injector.instanceOf[TraderReferenceController]
val request =
FakeRequest(POST, traderReferenceRoute)
.withFormUrlEncodedBody(("value", "oldValue"))
.withFormUrlEncodedBody(("value", "BAN0010011"))

val result: Future[Result] = controller.onSubmitUpdate(NormalMode, testRecordId)(request)

Expand Down Expand Up @@ -525,6 +549,11 @@ class TraderReferenceControllerSpec extends SpecBase with MockitoSugar {

val mockGoodsRecordConnector = mock[GoodsRecordConnector]

when(mockGoodsRecordConnector.getRecord(any(), any())(any())) thenReturn Future
.successful(
record
)

when(mockGoodsRecordConnector.filterRecordsByField(any(), any(), any())(any())) thenReturn Future
.successful(
Some(response)
Expand Down Expand Up @@ -571,6 +600,11 @@ class TraderReferenceControllerSpec extends SpecBase with MockitoSugar {

val mockGoodsRecordConnector = mock[GoodsRecordConnector]

when(mockGoodsRecordConnector.getRecord(any(), any())(any())) thenReturn Future
.successful(
record
)

when(mockGoodsRecordConnector.filterRecordsByField(any(), any(), any())(any())) thenReturn Future
.successful(
None
Expand Down Expand Up @@ -633,6 +667,5 @@ class TraderReferenceControllerSpec extends SpecBase with MockitoSugar {
}
}
}

}
}
2 changes: 1 addition & 1 deletion test/services/AuditServiceSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import factories.AuditEventFactory
import models.audits.{AuditGetCategorisationAssessment, AuditValidateCommodityCode, OttAuditData}
import models.helper._
import models.ott.response._
import models.{AdviceRequest, Category1Scenario, CategoryRecord, GoodsRecord, SupplementaryRequest, TraderProfile, UpdateGoodsRecord, UserAnswers}
import models.{AdviceRequest, Category1Scenario, CategoryRecord, GoodsRecord, SupplementaryRequest, TraderProfile, UpdateGoodsRecord}
import org.apache.pekko.Done
import org.mockito.ArgumentMatchers.{any, eq => eqTo}
import org.mockito.Mockito.{reset, times, verify, when}
Expand Down

0 comments on commit c57c2ff

Please sign in to comment.