Skip to content

Commit

Permalink
PIN-4414 BKE - catalog-process: missing descriptor archiving
Browse files Browse the repository at this point in the history
  • Loading branch information
nttdata-rtorsoli committed Jan 17, 2024
1 parent ea9ce7c commit fe3b7c0
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -162,6 +166,7 @@ final case class ProcessApiServiceImpl(
eServicesIds = eServicesIds,
producersIds = Nil,
consumersIds = Seq(organizationId),
descriptorsIds = Nil,
states = agreementStates
)
.map(_.map(_.eserviceId))
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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](
Expand All @@ -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())
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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]]
}
Original file line number Diff line number Diff line change
Expand Up @@ -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]] = {

Expand All @@ -40,6 +51,7 @@ object AgreementManagementServiceImpl extends AgreementManagementService {
eServicesIds = eServicesIds,
consumersIds = consumersIds,
producersIds = producersIds,
descriptorsIds = descriptorsIds,
states = states,
offset = offset,
limit = 50
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)))

Expand Down Expand Up @@ -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))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit fe3b7c0

Please sign in to comment.