Skip to content

Commit

Permalink
PIN-4486 BKE - Catalog-Process - Endpoint /eservices/{eServiceId} - A…
Browse files Browse the repository at this point in the history
…pplied visibility restrictions to EServices
  • Loading branch information
nttdata-rtorsoli committed Feb 2, 2024
1 parent 9848a80 commit 353aa4e
Show file tree
Hide file tree
Showing 6 changed files with 131 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ final case class ProcessApiServiceImpl(
clientSeed = eServiceSeed.toDependency(organizationId)
maybeEservice <- catalogManagementService
.getEServices(
organizationId,
eServiceSeed.name.some,
Seq.empty,
Seq(clientSeed.producerId),
Expand Down Expand Up @@ -160,20 +161,23 @@ final case class ProcessApiServiceImpl(
states: Seq[CatalogDescriptorState],
agreementStates: Seq[PersistentAgreementState],
mode: Option[CatalogItemMode],
visibilityRestrictions: Boolean,
offset: Int,
limit: Int
): Future[PaginatedResult[CatalogItem]] = {

if (agreementStates.isEmpty)
catalogManagementService.getEServices(
name,
eServicesIds,
producersIds,
attributesIds,
states,
mode,
offset,
limit
requesterId = organizationId,
name = name,
eServicesIds = eServicesIds,
producersIds = producersIds,
attributesIds = attributesIds,
states = states,
mode = mode,
visibilityRestrictions = visibilityRestrictions,
offset = offset,
limit = limit
)
else
for {
Expand All @@ -191,14 +195,16 @@ final case class ProcessApiServiceImpl(
Future.successful(ReadModelCatalogQueries.emptyResults[CatalogItem])
else
catalogManagementService.getEServices(
name,
agreementEservicesIds,
producersIds,
attributesIds,
states,
mode,
offset,
limit
requesterId = organizationId,
name = name,
eServicesIds = agreementEservicesIds,
producersIds = producersIds,
attributesIds = attributesIds,
states = states,
mode = mode,
visibilityRestrictions = visibilityRestrictions,
offset = offset,
limit = limit
)
} yield result
}
Expand All @@ -213,7 +219,9 @@ final case class ProcessApiServiceImpl(
agreementStates <- parseArrayParameters(agreementStates)
.traverse(AgreementState.fromValue)
.toFuture
eServices <-
role <- getUserRolesFuture(contexts)
visibilityRestrictions = Seq(ADMIN_ROLE, API_ROLE).contains(role)
eServices <-
getEservicesInner(
organizationId,
name,
Expand All @@ -223,10 +231,15 @@ final case class ProcessApiServiceImpl(
states.map(_.toPersistent),
agreementStates.map(_.toPersistent),
mode.map(_.toPersistent),
visibilityRestrictions,
offset,
limit
)
} yield EServices(results = eServices.results.map(_.toApi), totalCount = eServices.totalCount)
results =
if (visibilityRestrictions)
eServices.results.map(ese => ese.copy(descriptors = ese.descriptors.filterNot(_.state == Draft)))
else eServices.results
} yield EServices(results = results.map(_.toApi), totalCount = eServices.totalCount)

onComplete(result) {
getEServicesResponse(operationLabel)(getEServices200)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import scala.concurrent.{ExecutionContext, Future}
import java.util.UUID
import it.pagopa.interop.catalogmanagement.model.{CatalogItemMode, CatalogDescriptorState}
import org.mongodb.scala.bson.BsonDocument
import it.pagopa.interop.catalogmanagement.model.Draft

object ReadModelCatalogQueries extends ReadModelQuery {

Expand Down Expand Up @@ -114,6 +115,7 @@ object ReadModelCatalogQueries extends ReadModelQuery {
}

def getEServices(
requesterId: UUID,
name: Option[String],
eServicesIds: Seq[UUID],
producersIds: Seq[UUID],
Expand All @@ -122,10 +124,21 @@ object ReadModelCatalogQueries extends ReadModelQuery {
mode: Option[CatalogItemMode],
offset: Int,
limit: Int,
exactMatchOnName: Boolean = false
exactMatchOnName: Boolean = false,
visibilityRestrictions: Boolean = false
)(implicit ec: ExecutionContext, readModel: ReadModelService): Future[PaginatedResult[CatalogItem]] = {

val query = listEServicesFilters(name, eServicesIds, producersIds, attributesIds, states, mode, exactMatchOnName)
val query = listEServicesFilters(
requesterId,
name,
eServicesIds,
producersIds,
attributesIds,
states,
mode,
exactMatchOnName,
visibilityRestrictions
)

for {
// Using aggregate to perform case insensitive sorting
Expand Down Expand Up @@ -157,13 +170,15 @@ object ReadModelCatalogQueries extends ReadModelQuery {
}

private def listEServicesFilters(
requesterId: UUID,
name: Option[String],
eServicesIds: Seq[UUID],
producersIds: Seq[UUID],
attributesIds: Seq[UUID],
states: Seq[CatalogDescriptorState],
mode: Option[CatalogItemMode],
exactMatchOnName: Boolean
exactMatchOnName: Boolean,
visibilityRestrictions: Boolean
): Bson = {
val statesPartialFilter = states
.map(_.toString)
Expand Down Expand Up @@ -193,8 +208,22 @@ object ReadModelCatalogQueries extends ReadModelQuery {

val modeFilter = mode.map(_.toString).map(Filters.eq("data.mode", _))

val visibilityRestrictionsFilter =
if (visibilityRestrictions)
Some(
Filters.nor(
Filters.and(Filters.ne("data.producerId", requesterId.toString), Filters.size("data.descriptors", 0)),
Filters.and(
Filters.ne("data.producerId", requesterId.toString),
Filters.size("data.descriptors", 1),
Filters.eq("data.descriptors.state", Draft.toString())
)
)
)
else Some(Filters.empty())

mapToVarArgs(
eServicesIdsFilter.toList ++ producersIdsFilter.toList ++ attributesIdsFilter.toList ++ statesFilter.toList ++ nameFilter.toList ++ modeFilter.toList
eServicesIdsFilter.toList ++ producersIdsFilter.toList ++ attributesIdsFilter.toList ++ statesFilter.toList ++ nameFilter.toList ++ modeFilter.toList ++ visibilityRestrictionsFilter.toList
)(Filters.and)
.getOrElse(Filters.empty())
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ trait CatalogManagementService {
): Future[PaginatedResult[Consumers]]

def getEServices(
requesterId: UUID,
name: Option[String],
eServicesIds: Seq[UUID],
producersIds: Seq[UUID],
Expand All @@ -72,7 +73,8 @@ trait CatalogManagementService {
mode: Option[CatalogItemMode],
offset: Int,
limit: Int,
exactMatchOnName: Boolean = false
exactMatchOnName: Boolean = false,
visibilityRestrictions: Boolean = false
)(implicit ec: ExecutionContext, readModel: ReadModelService): Future[PaginatedResult[CatalogItem]]

def createRiskAnalysis(eServiceId: UUID, riskAnalysisSeed: RiskAnalysisSeed)(implicit
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,7 @@ final case class CatalogManagementServiceImpl(invoker: CatalogManagementInvoker,
): Future[PaginatedResult[Consumers]] = ReadModelCatalogQueries.getConsumers(eServiceId, offset, limit)

override def getEServices(
requesterId: UUID,
name: Option[String],
eServicesIds: Seq[UUID],
producersIds: Seq[UUID],
Expand All @@ -267,9 +268,11 @@ final case class CatalogManagementServiceImpl(invoker: CatalogManagementInvoker,
mode: Option[CatalogItemMode],
offset: Int,
limit: Int,
exactMatchOnName: Boolean = false
exactMatchOnName: Boolean = false,
visibilityRestrictions: Boolean = false
)(implicit ec: ExecutionContext, readModel: ReadModelService): Future[PaginatedResult[CatalogItem]] =
ReadModelCatalogQueries.getEServices(
requesterId,
name,
eServicesIds,
producersIds,
Expand All @@ -278,7 +281,8 @@ final case class CatalogManagementServiceImpl(invoker: CatalogManagementInvoker,
mode,
offset,
limit,
exactMatchOnName
exactMatchOnName,
visibilityRestrictions
)

override def createRiskAnalysis(eServiceId: UUID, riskAnalysisSeed: RiskAnalysisSeed)(implicit
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ class CatalogProcessSpec extends SpecHelper with AnyWordSpecLike with ScalatestR

(mockCatalogManagementService
.getEServices(
_: UUID,
_: Option[String],
_: Seq[UUID],
_: Seq[UUID],
Expand All @@ -101,9 +102,24 @@ class CatalogProcessSpec extends SpecHelper with AnyWordSpecLike with ScalatestR
_: Option[CatalogItemMode],
_: Int,
_: Int,
_: Boolean,
_: Boolean
)(_: ExecutionContext, _: ReadModelService))
.expects(name, eServicesIds, producersIds, attributesIds, states, mode, offset, limit, false, *, *)
.expects(
requesterId,
name,
eServicesIds,
producersIds,
attributesIds,
states,
mode,
offset,
limit,
false,
true,
*,
*
)
.once()
.returns(Future.successful(PaginatedResult(results = Seq(SpecData.catalogItem), 1)))

Expand Down Expand Up @@ -150,6 +166,7 @@ class CatalogProcessSpec extends SpecHelper with AnyWordSpecLike with ScalatestR

(mockCatalogManagementService
.getEServices(
_: UUID,
_: Option[String],
_: Seq[UUID],
_: Seq[UUID],
Expand All @@ -158,9 +175,11 @@ class CatalogProcessSpec extends SpecHelper with AnyWordSpecLike with ScalatestR
_: Option[CatalogItemMode],
_: Int,
_: Int,
_: Boolean,
_: Boolean
)(_: ExecutionContext, _: ReadModelService))
.expects(
requesterId,
name,
Seq(SpecData.agreement.eserviceId),
producersIds,
Expand All @@ -170,6 +189,7 @@ class CatalogProcessSpec extends SpecHelper with AnyWordSpecLike with ScalatestR
offset,
limit,
false,
true,
*,
*
)
Expand Down Expand Up @@ -346,6 +366,7 @@ class CatalogProcessSpec extends SpecHelper with AnyWordSpecLike with ScalatestR

(mockCatalogManagementService
.getEServices(
_: UUID,
_: Option[String],
_: Seq[UUID],
_: Seq[UUID],
Expand All @@ -354,9 +375,24 @@ class CatalogProcessSpec extends SpecHelper with AnyWordSpecLike with ScalatestR
_: Option[CatalogItemMode],
_: Int,
_: Int,
_: Boolean,
_: Boolean
)(_: ExecutionContext, _: ReadModelService))
.expects(Some(seed.name), Seq.empty, Seq(seed.producerId), Seq.empty, Seq.empty, None, 0, 1, true, *, *)
.expects(
requesterId,
Some(seed.name),
Seq.empty,
Seq(seed.producerId),
Seq.empty,
Seq.empty,
None,
0,
1,
true,
false,
*,
*
)
.once()
.returns(Future.successful(PaginatedResult(results = catalogItems, catalogItems.size)))

Expand Down Expand Up @@ -409,6 +445,7 @@ class CatalogProcessSpec extends SpecHelper with AnyWordSpecLike with ScalatestR

(mockCatalogManagementService
.getEServices(
_: UUID,
_: Option[String],
_: Seq[UUID],
_: Seq[UUID],
Expand All @@ -417,9 +454,24 @@ class CatalogProcessSpec extends SpecHelper with AnyWordSpecLike with ScalatestR
_: Option[CatalogItemMode],
_: Int,
_: Int,
_: Boolean,
_: Boolean
)(_: ExecutionContext, _: ReadModelService))
.expects(Some(apiSeed.name), Seq.empty, Seq(requesterId), Seq.empty, Seq.empty, None, 0, 1, true, *, *)
.expects(
requesterId,
Some(apiSeed.name),
Seq.empty,
Seq(requesterId),
Seq.empty,
Seq.empty,
None,
0,
1,
true,
false,
*,
*
)
.once()
.returns(Future.successful(PaginatedResult(results = catalogItems, catalogItems.size)))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,7 @@ object FakeDependencies {
)

override def getEServices(
requesterId: UUID,
name: Option[String],
eServicesIds: Seq[UUID],
producersIds: Seq[UUID],
Expand All @@ -274,7 +275,8 @@ object FakeDependencies {
mode: Option[CatalogItemMode],
offset: Int,
limit: Int,
exactMatchOnName: Boolean = false
exactMatchOnName: Boolean = false,
visibilityRestrictions: Boolean = false
)(implicit ec: ExecutionContext, readModel: ReadModelService): Future[PaginatedResult[CatalogItem]] =
Future.successful(PaginatedResult(results = Seq.empty, totalCount = 0))

Expand Down

0 comments on commit 353aa4e

Please sign in to comment.