diff --git a/app/controllers/TraderReferenceController.scala b/app/controllers/TraderReferenceController.scala index f6e07f10..6699b1f3 100644 --- a/app/controllers/TraderReferenceController.scala +++ b/app/controllers/TraderReferenceController.scala @@ -72,7 +72,6 @@ class TraderReferenceController @Inject() ( case None => form case Some(value) => form.fill(value) } - auditService .auditStartUpdateGoodsRecord( request.eori, @@ -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) - ) + ) } - } ) } } diff --git a/test/controllers/TraderReferenceControllerSpec.scala b/test/controllers/TraderReferenceControllerSpec.scala index e8eebd2c..364f193c 100644 --- a/test/controllers/TraderReferenceControllerSpec.scala +++ b/test/controllers/TraderReferenceControllerSpec.scala @@ -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 @@ -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) @@ -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) @@ -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)), @@ -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()) - } } @@ -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) @@ -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( @@ -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) @@ -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) @@ -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 @@ -633,6 +667,5 @@ class TraderReferenceControllerSpec extends SpecBase with MockitoSugar { } } } - } } diff --git a/test/services/AuditServiceSpec.scala b/test/services/AuditServiceSpec.scala index f580025f..4982e44e 100644 --- a/test/services/AuditServiceSpec.scala +++ b/test/services/AuditServiceSpec.scala @@ -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}