Skip to content

Commit

Permalink
Merge pull request #704 from nationalarchives/TDR-3427_update_series_…
Browse files Browse the repository at this point in the history
…name_for_consignment

TDR-3427 - Add series name as well while updating series id for the consignment.
  • Loading branch information
vimleshtna authored Oct 10, 2023
2 parents 24c0db4 + 8c98318 commit 7606052
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,11 @@ class ConsignmentRepository(db: Database, timeSource: TimeSource) {
db.run(query.result)
}

def updateSeriesIdOfConsignment(updateConsignmentSeriesIdInput: ConsignmentFields.UpdateConsignmentSeriesIdInput): Future[Int] = {
def updateSeriesOfConsignment(updateConsignmentSeriesIdInput: ConsignmentFields.UpdateConsignmentSeriesIdInput, seriesName: Option[String]): Future[Int] = {
val update = Consignment
.filter(_.consignmentid === updateConsignmentSeriesIdInput.consignmentId)
.map(t => t.seriesid)
.update(Some(updateConsignmentSeriesIdInput.seriesId))
.map(t => (t.seriesid, t.seriesname))
.update(Some(updateConsignmentSeriesIdInput.seriesId), seriesName)
db.run(update)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ object ConsignmentFields {
"updateConsignmentSeriesId",
OptionType(IntType),
arguments = UpdateConsignmentSeriesIdArg :: Nil,
resolve = ctx => ctx.ctx.consignmentService.updateSeriesIdOfConsignment(ctx.arg(UpdateConsignmentSeriesIdArg)),
resolve = ctx => ctx.ctx.consignmentService.updateSeriesOfConsignment(ctx.arg(UpdateConsignmentSeriesIdArg)),
tags = List(ValidateUserHasAccessToConsignment(UpdateConsignmentSeriesIdArg), ValidateUpdateConsignmentSeriesId)
)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,6 @@ class ConsignmentService(
for {
sequence <- consignmentRepository.getNextConsignmentSequence
body <- transferringBodyService.getBodyByCode(userBody)
series <- if (seriesId.isDefined) seriesRepository.getSeries(seriesId.get) else Future(Seq())
seriesName = if (series.nonEmpty) Some(series.head.name) else None
consignmentRef = ConsignmentReference.createConsignmentReference(yearNow, sequence)
consignmentId = uuidSource.uuid
consignmentRow = ConsignmentRow(
Expand All @@ -86,7 +84,7 @@ class ConsignmentService(
consignmentreference = consignmentRef,
consignmenttype = consignmentType,
bodyid = body.bodyId,
seriesname = seriesName,
seriesname = None,
transferringbodyname = Some(body.name)
)
descriptiveMetadataStatusRow = ConsignmentstatusRow(uuidSource.uuid, consignmentId, DescriptiveMetadata, NotEntered, timestampNow, Option(timestampNow))
Expand Down Expand Up @@ -123,9 +121,10 @@ class ConsignmentService(
consignment.map(rows => rows.headOption.map(series => Series(series.seriesid, series.bodyid, series.name, series.code, series.description)))
}

def updateSeriesIdOfConsignment(updateConsignmentSeriesIdInput: UpdateConsignmentSeriesIdInput): Future[Int] = {
def updateSeriesOfConsignment(updateConsignmentSeriesIdInput: UpdateConsignmentSeriesIdInput): Future[Int] = {
for {
result <- consignmentRepository.updateSeriesIdOfConsignment(updateConsignmentSeriesIdInput)
series <- seriesRepository.getSeries(updateConsignmentSeriesIdInput.seriesId)
result <- consignmentRepository.updateSeriesOfConsignment(updateConsignmentSeriesIdInput, series.headOption.map(_.name))
seriesStatus = if (result == 1) Completed else Failed
_ <- consignmentStatusRepository.updateConsignmentStatus(updateConsignmentSeriesIdInput.consignmentId, "Series", seriesStatus, Timestamp.from(timeSource.now))
} yield result
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import com.dimafeng.testcontainers.PostgreSQLContainer
import org.scalatest.concurrent.ScalaFutures
import org.scalatest.matchers.should.Matchers
import uk.gov.nationalarchives.Tables.ConsignmentstatusRow
import uk.gov.nationalarchives.tdr.api.graphql.fields.ConsignmentFields
import uk.gov.nationalarchives.tdr.api.graphql.fields.ConsignmentFields.{ConsignmentFilters, StartUploadInput, UpdateExportDataInput}
import uk.gov.nationalarchives.tdr.api.service.CurrentTimeSource
import uk.gov.nationalarchives.tdr.api.service.FileStatusService.{InProgress, Upload}
Expand Down Expand Up @@ -297,6 +298,28 @@ class ConsignmentRepositorySpec extends TestContainerUtils with ScalaFutures wit
consignmentReferences should equal(List("TDR-2021-B", "TDR-2021-A"))
}

"updateSeriesOfConsignment" should "update id and name of the consignment" in withContainers { case container: PostgreSQLContainer =>
val db = container.database
val consignmentRepository = new ConsignmentRepository(db, new CurrentTimeSource)
val utils = TestUtils(db)
val seriesId: UUID = UUID.fromString("20e88b3c-d063-4a6e-8b61-187d8c51d11d")
val seriesName: String = "Mock1"
val bodyId: UUID = UUID.fromString("8a72cc59-7f2f-4e55-a263-4a4cb9f677f5")

utils.createConsignment(consignmentIdOne, userId, consignmentRef = "TDR-2021-A")
utils.addTransferringBody(bodyId, "MOCK Department", "Code123")
utils.addSeries(seriesId, bodyId, "TDR-2020-XYZ", seriesName)

val input = ConsignmentFields.UpdateConsignmentSeriesIdInput(consignmentId = consignmentIdOne, seriesId = seriesId)

val response = consignmentRepository.updateSeriesOfConsignment(input, seriesName.some).futureValue

response should be(1)
val consignment = consignmentRepository.getConsignment(consignmentIdOne).futureValue.head
consignment.seriesid should be(seriesId.some)
consignment.seriesname should be(seriesName.some)
}

"totalConsignments" should "return total number of consignments" in withContainers { case container: PostgreSQLContainer =>
val db = container.database
val consignmentRepository = new ConsignmentRepository(db, new CurrentTimeSource)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class ConsignmentServiceSpec extends AnyFlatSpec with MockitoSugar with ResetMoc
consignmentreference = consignmentReference,
consignmenttype = "standard",
bodyid = bodyId,
seriesname = Some(seriesName),
seriesname = None,
transferringbodyname = Some(bodyName)
)

Expand Down Expand Up @@ -93,14 +93,12 @@ class ConsignmentServiceSpec extends AnyFlatSpec with MockitoSugar with ResetMoc

val result = consignmentService.addConsignment(AddConsignmentInput(Some(seriesId), "standard"), mockToken).futureValue

verify(seriesRepositoryMock).getSeries(seriesId)

result.consignmentid shouldBe consignmentId
result.seriesid shouldBe Some(seriesId)
result.userid shouldBe userId
result.consignmentType shouldBe "standard"
result.bodyId shouldBe bodyId
result.seriesName shouldBe Some(seriesName)
result.seriesName shouldBe None
result.transferringBodyName shouldBe Some(bodyName)
}

Expand Down Expand Up @@ -138,7 +136,6 @@ class ConsignmentServiceSpec extends AnyFlatSpec with MockitoSugar with ResetMoc
val mockToken = mock[Token]
val mockBody = mock[TransferringBody]
val consignmentStatusRow = mock[ConsignmentstatusRow]
when(seriesRepositoryMock.getSeries(seriesId)).thenReturn(Future.successful(Seq(mockSeries)))
when(consignmentStatusRepoMock.addConsignmentStatuses(any[Seq[ConsignmentstatusRow]])).thenReturn(Future.successful(Seq(consignmentStatusRow)))
when(consignmentRepoMock.getNextConsignmentSequence).thenReturn(Future.successful(consignmentSequence))
when(consignmentRepoMock.addConsignment(any[ConsignmentRow])).thenReturn(mockResponse)
Expand Down Expand Up @@ -311,38 +308,40 @@ class ConsignmentServiceSpec extends AnyFlatSpec with MockitoSugar with ResetMoc
series.description shouldBe mockSeries.head.description
}

"updateSeriesIdOfConsignment" should "update the seriesId and status for a given consignment" in {
"updateSeriesOfConsignment" should "update the seriesId, seriesName and status for a given consignment" in {
val updateConsignmentSeriesIdInput = UpdateConsignmentSeriesIdInput(consignmentId, seriesId)
val statusType = "Series"
val expectedSeriesStatus = Completed
val expectedResult = 1
when(consignmentRepoMock.updateSeriesIdOfConsignment(updateConsignmentSeriesIdInput))
when(consignmentRepoMock.updateSeriesOfConsignment(updateConsignmentSeriesIdInput, Some(seriesName)))
.thenReturn(Future.successful(1))
when(consignmentStatusRepoMock.updateConsignmentStatus(consignmentId, statusType, Completed, Timestamp.from(fixedTimeSource)))
.thenReturn(Future.successful(1))
when(seriesRepositoryMock.getSeries(updateConsignmentSeriesIdInput.seriesId)).thenReturn(Future.successful(Seq(mockSeries)))

val result = consignmentService.updateSeriesIdOfConsignment(updateConsignmentSeriesIdInput).futureValue
val result = consignmentService.updateSeriesOfConsignment(updateConsignmentSeriesIdInput).futureValue

verify(consignmentRepoMock).updateSeriesIdOfConsignment(updateConsignmentSeriesIdInput)
verify(consignmentRepoMock).updateSeriesOfConsignment(updateConsignmentSeriesIdInput, Some(seriesName))
verify(consignmentStatusRepoMock)
.updateConsignmentStatus(updateConsignmentSeriesIdInput.consignmentId, statusType, expectedSeriesStatus, Timestamp.from(fixedTimeSource))

result should equal(expectedResult)
}

"updateSeriesIdOfConsignment" should "update the status with 'Failed' if seriesId update fails for a given consignment" in {
"updateSeriesOfConsignment" should "update the status with 'Failed' if seriesId update fails for a given consignment" in {
val updateConsignmentSeriesIdInput = UpdateConsignmentSeriesIdInput(consignmentId, seriesId)
val statusType = "Series"
val expectedSeriesStatus = Failed
val expectedResult = 0
when(consignmentRepoMock.updateSeriesIdOfConsignment(updateConsignmentSeriesIdInput))
when(consignmentRepoMock.updateSeriesOfConsignment(updateConsignmentSeriesIdInput, Some(seriesName)))
.thenReturn(Future.successful(0))
when(consignmentStatusRepoMock.updateConsignmentStatus(consignmentId, statusType, Failed, Timestamp.from(fixedTimeSource)))
.thenReturn(Future.successful(1))
when(seriesRepositoryMock.getSeries(updateConsignmentSeriesIdInput.seriesId)).thenReturn(Future.successful(Seq(mockSeries)))

val result = consignmentService.updateSeriesIdOfConsignment(updateConsignmentSeriesIdInput).futureValue
val result = consignmentService.updateSeriesOfConsignment(updateConsignmentSeriesIdInput).futureValue

verify(consignmentRepoMock).updateSeriesIdOfConsignment(updateConsignmentSeriesIdInput)
verify(consignmentRepoMock).updateSeriesOfConsignment(updateConsignmentSeriesIdInput, Some(seriesName))
verify(consignmentStatusRepoMock)
.updateConsignmentStatus(updateConsignmentSeriesIdInput.consignmentId, statusType, expectedSeriesStatus, Timestamp.from(fixedTimeSource))

Expand Down

0 comments on commit 7606052

Please sign in to comment.