Skip to content

Commit

Permalink
PIN-4504 Resolved PR issue
Browse files Browse the repository at this point in the history
  • Loading branch information
nttdata-rtorsoli committed Feb 6, 2024
1 parent fd614db commit 06bfe84
Showing 1 changed file with 24 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import it.pagopa.interop.tenantmanagement.model.tenant.PersistentTenant
import it.pagopa.interop.tenantmanagement.model.persistence.JsonFormats._
import org.mongodb.scala.Document
import org.mongodb.scala.bson.conversions.Bson
import org.mongodb.scala.model.Aggregates.{`match`, count, lookup, project, sort, unwind}
import org.mongodb.scala.model.Filters
import org.mongodb.scala.model.Aggregates.{`match`, count, lookup, project, sort, unwind, addFields}
import org.mongodb.scala.model.{Filters, Field}
import org.mongodb.scala.model.Projections.{computed, fields, include, excludeId}
import org.mongodb.scala.model.Sorts.ascending

Expand All @@ -30,7 +30,25 @@ object ReadModelTenantQueries extends ReadModelQuery {
`match`(query),
lookup(from = "tenants", localField = "data.id", foreignField = "data.attributes.id", as = "tenants"),
unwind("$tenants"),
`match`(Filters.not(Filters.exists("tenants.data.attributes.revocationTimestamp")))
unwind("$tenants.data.attributes"),
addFields(
Field(
"notRevoked",
Document(s"""{
| $$cond: {
| if: {
| $$and: [
| { $$eq: [ "$$tenants.data.attributes.id", "$$data.id" ] },
| { $$not: [ "$$tenants.data.attributes.revocationTimestamp"] }
| ],
| },
| then: true,
| else: false,
| },
|}""".stripMargin)
)
),
`match`(Filters.eq("notRevoked", true))
)

val projection: Bson = project(
Expand All @@ -47,7 +65,7 @@ object ReadModelTenantQueries extends ReadModelQuery {
for {
// Using aggregate to perform case insensitive sorting
// N.B.: Required because DocumentDB does not support collation
consumers <- readModel.aggregateRaw[CertifiedAttribute](
attributes <- readModel.aggregateRaw[CertifiedAttribute](
"attributes",
filterPipeline ++
Seq(projection, sort(ascending("lowerName"))),
Expand All @@ -57,15 +75,15 @@ object ReadModelTenantQueries extends ReadModelQuery {
)
// Note: This could be obtained using $facet function (avoiding to execute the query twice),
// but it is not supported by DocumentDB
count <- readModel.aggregate[TotalCountResult](
count <- readModel.aggregate[TotalCountResult](
"attributes",
filterPipeline ++
Seq(count("totalCount"), project(computed("data", Document("""{ "totalCount" : "$totalCount" }""")))),
offset = 0,
limit = Int.MaxValue,
allowDiskUse = true
)
} yield PaginatedResult(results = consumers, totalCount = count.headOption.map(_.totalCount).getOrElse(0))
} yield PaginatedResult(results = attributes, totalCount = count.headOption.map(_.totalCount).getOrElse(0))
}

def getTenantBySelfcareId(
Expand Down

0 comments on commit 06bfe84

Please sign in to comment.