From fe3b7c003dc2397cc3bed380466b4a59a1b3f951 Mon Sep 17 00:00:00 2001 From: nttdata-rtorsoli Date: Wed, 17 Jan 2024 13:37:17 +0100 Subject: [PATCH] PIN-4414 BKE - catalog-process: missing descriptor archiving --- .../api/impl/ProcessApiServiceImpl.scala | 43 ++++++++++++++----- .../readmodel/ReadModelAgreementQueries.scala | 14 +++--- .../service/AgreementManagementService.scala | 1 + .../impl/AgreementManagementServiceImpl.scala | 16 ++++++- .../catalogprocess/CatalogProcessSpec.scala | 8 ++-- .../util/FakeDependencies.scala | 1 + 6 files changed, 61 insertions(+), 22 deletions(-) diff --git a/src/main/scala/it/pagopa/interop/catalogprocess/api/impl/ProcessApiServiceImpl.scala b/src/main/scala/it/pagopa/interop/catalogprocess/api/impl/ProcessApiServiceImpl.scala index 59498d53..d62af775 100644 --- a/src/main/scala/it/pagopa/interop/catalogprocess/api/impl/ProcessApiServiceImpl.scala +++ b/src/main/scala/it/pagopa/interop/catalogprocess/api/impl/ProcessApiServiceImpl.scala @@ -35,10 +35,14 @@ import it.pagopa.interop.commons.utils.AkkaUtils._ import it.pagopa.interop.commons.utils.OpenapiUtils.parseArrayParameters import it.pagopa.interop.commons.utils.TypeConversions._ import it.pagopa.interop.commons.utils.errors.GenericComponentErrors +import it.pagopa.interop.catalogmanagement.model.CatalogItemMode +import it.pagopa.interop.agreementmanagement.model.agreement.{ + Active => AgreementActive, + Suspended => AgreementSuspended +} import java.util.UUID import scala.concurrent.{ExecutionContext, Future} -import it.pagopa.interop.catalogmanagement.model.CatalogItemMode final case class ProcessApiServiceImpl( catalogManagementService: CatalogManagementService, @@ -162,6 +166,7 @@ final case class ProcessApiServiceImpl( eServicesIds = eServicesIds, producersIds = Nil, consumersIds = Seq(organizationId), + descriptorsIds = Nil, states = agreementStates ) .map(_.map(_.eserviceId)) @@ -249,10 +254,11 @@ final case class ProcessApiServiceImpl( _ <- catalogManagementService.publishDescriptor(eServiceId, descriptorId) _ <- currentActiveDescriptor .map(oldDescriptor => - deprecateDescriptorOrCancelPublication( - eServiceId = eServiceId, - descriptorIdToDeprecate = oldDescriptor.id.toString, - descriptorIdToCancel = descriptorId + deprecateOrArchiveDescriptorOrCancelPublication( + eServiceId = eServiceUuid, + oldDescriptorId = oldDescriptor.id, + descriptorId = descriptorUuid, + validStates = List(AgreementActive, AgreementSuspended) ) ) .sequence @@ -451,12 +457,27 @@ final case class ProcessApiServiceImpl( EServiceCannotBeUpdated(eService.id.toString) ) - private[this] def deprecateDescriptorOrCancelPublication( - eServiceId: String, - descriptorIdToDeprecate: String, - descriptorIdToCancel: String - )(implicit contexts: Seq[(String, String)]): Future[Unit] = deprecateDescriptor(descriptorIdToDeprecate, eServiceId) - .recoverWith(error => resetDescriptorToDraft(eServiceId, descriptorIdToCancel).flatMap(_ => Future.failed(error))) + private[this] def deprecateOrArchiveDescriptorOrCancelPublication( + eServiceId: UUID, + oldDescriptorId: UUID, + descriptorId: UUID, + validStates: Seq[PersistentAgreementState] + )(implicit contexts: Seq[(String, String)]): Future[Unit] = for { + validAgreements <- agreementManagementService.getAgreements( + List(eServiceId), + Nil, + Nil, + List(oldDescriptorId), + validStates + ) + _ <- validAgreements.headOption match { + case Some(_) => + deprecateDescriptor(oldDescriptorId.toString, eServiceId.toString).recoverWith(error => + resetDescriptorToDraft(eServiceId.toString, descriptorId.toString).flatMap(_ => Future.failed(error)) + ) + case None => catalogManagementService.archiveDescriptor(eServiceId.toString, oldDescriptorId.toString) + } + } yield () private[this] def deprecateDescriptor(descriptorId: String, eServiceId: String)(implicit contexts: Seq[(String, String)] diff --git a/src/main/scala/it/pagopa/interop/catalogprocess/common/readmodel/ReadModelAgreementQueries.scala b/src/main/scala/it/pagopa/interop/catalogprocess/common/readmodel/ReadModelAgreementQueries.scala index 36ac8cc6..f0eea339 100644 --- a/src/main/scala/it/pagopa/interop/catalogprocess/common/readmodel/ReadModelAgreementQueries.scala +++ b/src/main/scala/it/pagopa/interop/catalogprocess/common/readmodel/ReadModelAgreementQueries.scala @@ -17,13 +17,14 @@ object ReadModelAgreementQueries extends ReadModelQuery { eServicesIds: Seq[UUID], consumersIds: Seq[UUID], producersIds: Seq[UUID], + descriptorsIds: Seq[UUID], states: Seq[PersistentAgreementState], offset: Int, limit: Int )(implicit ec: ExecutionContext, readModel: ReadModelService): Future[Seq[PersistentAgreement]] = { val query: Bson = - getAgreementsFilters(eServicesIds, consumersIds, producersIds, states) + getAgreementsFilters(eServicesIds, consumersIds, producersIds, descriptorsIds, states) for { agreements <- readModel.aggregate[PersistentAgreement]( @@ -40,20 +41,23 @@ object ReadModelAgreementQueries extends ReadModelQuery { eServicesIds: Seq[UUID], consumersIds: Seq[UUID], producersIds: Seq[UUID], + descriptorsIds: Seq[UUID], states: Seq[PersistentAgreementState] ): Bson = { val statesFilter = listStatesFilter(states) - val eServicesIdsFilter = + val eServicesIdsFilter = mapToVarArgs(eServicesIds.map(id => Filters.eq("data.eserviceId", id.toString)))(Filters.or) - val consumersIdsFilter = + val consumersIdsFilter = mapToVarArgs(consumersIds.map(id => Filters.eq("data.consumerId", id.toString)))(Filters.or) - val producersIdsFilter = + val producersIdsFilter = mapToVarArgs(producersIds.map(id => Filters.eq("data.producerId", id.toString)))(Filters.or) + val descriptorsIdsFilter = + mapToVarArgs(descriptorsIds.map(id => Filters.eq("data.descriptorId", id.toString)))(Filters.or) mapToVarArgs( - eServicesIdsFilter.toList ++ consumersIdsFilter.toList ++ producersIdsFilter.toList ++ statesFilter.toList + eServicesIdsFilter.toList ++ consumersIdsFilter.toList ++ producersIdsFilter.toList ++ descriptorsIdsFilter.toList ++ statesFilter.toList )(Filters.and).getOrElse(Filters.empty()) } diff --git a/src/main/scala/it/pagopa/interop/catalogprocess/service/AgreementManagementService.scala b/src/main/scala/it/pagopa/interop/catalogprocess/service/AgreementManagementService.scala index d5466636..c4552445 100644 --- a/src/main/scala/it/pagopa/interop/catalogprocess/service/AgreementManagementService.scala +++ b/src/main/scala/it/pagopa/interop/catalogprocess/service/AgreementManagementService.scala @@ -11,6 +11,7 @@ trait AgreementManagementService { eServicesIds: Seq[UUID], consumersIds: Seq[UUID], producersIds: Seq[UUID], + descriptorsIds: Seq[UUID], states: Seq[PersistentAgreementState] )(implicit ec: ExecutionContext, readModel: ReadModelService): Future[Seq[PersistentAgreement]] } diff --git a/src/main/scala/it/pagopa/interop/catalogprocess/service/impl/AgreementManagementServiceImpl.scala b/src/main/scala/it/pagopa/interop/catalogprocess/service/impl/AgreementManagementServiceImpl.scala index b5e601cc..bf891027 100644 --- a/src/main/scala/it/pagopa/interop/catalogprocess/service/impl/AgreementManagementServiceImpl.scala +++ b/src/main/scala/it/pagopa/interop/catalogprocess/service/impl/AgreementManagementServiceImpl.scala @@ -14,24 +14,35 @@ object AgreementManagementServiceImpl extends AgreementManagementService { eServicesIds: Seq[UUID], consumersIds: Seq[UUID], producersIds: Seq[UUID], + descriptorsIds: Seq[UUID], states: Seq[PersistentAgreementState] )(implicit ec: ExecutionContext, readModel: ReadModelService): Future[Seq[PersistentAgreement]] = - getAllAgreements(eServicesIds, consumersIds, producersIds, states) + getAllAgreements(eServicesIds, consumersIds, producersIds, descriptorsIds, states) private def getAgreements( eServicesIds: Seq[UUID], consumersIds: Seq[UUID], producersIds: Seq[UUID], + descriptorsIds: Seq[UUID], states: Seq[PersistentAgreementState], offset: Int, limit: Int )(implicit ec: ExecutionContext, readModel: ReadModelService): Future[Seq[PersistentAgreement]] = - ReadModelAgreementQueries.getAgreements(eServicesIds, consumersIds, producersIds, states, offset, limit) + ReadModelAgreementQueries.getAgreements( + eServicesIds, + consumersIds, + producersIds, + descriptorsIds, + states, + offset, + limit + ) private def getAllAgreements( eServicesIds: Seq[UUID], consumersIds: Seq[UUID], producersIds: Seq[UUID], + descriptorsIds: Seq[UUID], states: Seq[PersistentAgreementState] )(implicit ec: ExecutionContext, readModel: ReadModelService): Future[Seq[PersistentAgreement]] = { @@ -40,6 +51,7 @@ object AgreementManagementServiceImpl extends AgreementManagementService { eServicesIds = eServicesIds, consumersIds = consumersIds, producersIds = producersIds, + descriptorsIds = descriptorsIds, states = states, offset = offset, limit = 50 diff --git a/src/test/scala/it/pagopa/interop/catalogprocess/CatalogProcessSpec.scala b/src/test/scala/it/pagopa/interop/catalogprocess/CatalogProcessSpec.scala index 8b9bed4c..facc4205 100644 --- a/src/test/scala/it/pagopa/interop/catalogprocess/CatalogProcessSpec.scala +++ b/src/test/scala/it/pagopa/interop/catalogprocess/CatalogProcessSpec.scala @@ -135,11 +135,11 @@ class CatalogProcessSpec extends SpecHelper with AnyWordSpecLike with ScalatestR val limit = 50 (mockAgreementManagementService - .getAgreements(_: Seq[UUID], _: Seq[UUID], _: Seq[UUID], _: Seq[PersistentAgreementState])( + .getAgreements(_: Seq[UUID], _: Seq[UUID], _: Seq[UUID], _: Seq[UUID], _: Seq[PersistentAgreementState])( _: ExecutionContext, _: ReadModelService )) - .expects(eServicesIds, Seq(requesterId), producersIds, Seq(Active), *, *) + .expects(eServicesIds, Seq(requesterId), producersIds, Nil, Seq(Active), *, *) .once() .returns(Future.successful(Seq(SpecData.agreement))) @@ -204,11 +204,11 @@ class CatalogProcessSpec extends SpecHelper with AnyWordSpecLike with ScalatestR val limit = 50 (mockAgreementManagementService - .getAgreements(_: Seq[UUID], _: Seq[UUID], _: Seq[UUID], _: Seq[PersistentAgreementState])( + .getAgreements(_: Seq[UUID], _: Seq[UUID], _: Seq[UUID], _: Seq[UUID], _: Seq[PersistentAgreementState])( _: ExecutionContext, _: ReadModelService )) - .expects(eServicesIds, Seq(requesterId), producersIds, Seq(Active), *, *) + .expects(eServicesIds, Seq(requesterId), producersIds, Nil, Seq(Active), *, *) .once() .returns(Future.successful(Seq.empty)) diff --git a/src/test/scala/it/pagopa/interop/catalogprocess/util/FakeDependencies.scala b/src/test/scala/it/pagopa/interop/catalogprocess/util/FakeDependencies.scala index 711e8cf8..268ae9d1 100644 --- a/src/test/scala/it/pagopa/interop/catalogprocess/util/FakeDependencies.scala +++ b/src/test/scala/it/pagopa/interop/catalogprocess/util/FakeDependencies.scala @@ -335,6 +335,7 @@ object FakeDependencies { eServicesIds: Seq[UUID], consumersIds: Seq[UUID], producersIds: Seq[UUID], + descriptorsIds: Seq[UUID], states: Seq[PersistentAgreementState] )(implicit ec: ExecutionContext, readModel: ReadModelService): Future[Seq[PersistentAgreement]] = Future.successful(Seq.empty)