Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PIN-4503 Fixed query certified attributes #94

Merged
merged 1 commit into from
Feb 1, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import it.pagopa.interop.tenantprocess.common.readmodel.CertifiedAttribute
import it.pagopa.interop.agreementmanagement.model.agreement.{Active, Suspended}
import it.pagopa.interop.attributeregistrymanagement.model.persistence.attribute.Certified
import it.pagopa.interop.commons.cqrs.service.ReadModelService
import it.pagopa.interop.tenantmanagement.model.tenant.{PersistentTenant, PersistentCertifiedAttribute}
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
Expand All @@ -23,27 +23,22 @@ object ReadModelTenantQueries extends ReadModelQuery {
readModel: ReadModelService
): Future[PaginatedResult[CertifiedAttribute]] = {

val query: Bson = Filters.eq("data.attributes.type", PersistentCertifiedAttribute.toString)
val query: Bson =
Filters.and(Filters.eq("data.origin", certifier.toString), Filters.eq("data.kind", Certified.toString))

val filterPipeline: Seq[Bson] = Seq(
`match`(query),
lookup(from = "attributes", localField = "data.id", foreignField = "data.attributes.id", as = "attributes"),
unwind("$attributes"),
`match`(
Filters.and(
Filters.eq("attributes.data.kind", Certified.toString),
Filters.eq("attributes.data.origin", certifier.toString)
)
)
lookup(from = "tenants", localField = "data.id", foreignField = "data.attributes.id", as = "tenants"),
unwind("$tenants")
)

val projection: Bson = project(
fields(
computed("id", "$data.id"),
computed("name", "$data.name"),
computed("attributeId", "$attributes.data.id"),
computed("attributeName", "$attributes.data.name"),
computed("lowerName", Document("""{ "$toLower" : "$data.name" }""")),
computed("id", "$tenants.data.id"),
computed("name", "$tenants.data.name"),
computed("attributeId", "$data.id"),
computed("attributeName", "$data.name"),
computed("lowerName", Document("""{ "$toLower" : "$tenants.data.name" }""")),
excludeId()
)
)
Expand All @@ -52,7 +47,7 @@ object ReadModelTenantQueries extends ReadModelQuery {
// Using aggregate to perform case insensitive sorting
// N.B.: Required because DocumentDB does not support collation
consumers <- readModel.aggregateRaw[CertifiedAttribute](
"tenants",
"attributes",
filterPipeline ++
Seq(projection, sort(ascending("lowerName"))),
offset = offset,
Expand All @@ -62,7 +57,7 @@ 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](
"tenants",
"attributes",
filterPipeline ++
Seq(count("totalCount"), project(computed("data", Document("""{ "totalCount" : "$totalCount" }""")))),
offset = 0,
Expand Down
Loading