Skip to content

Commit

Permalink
PIN-4729: tenants-cert-attr-updater no longer creates tenants if they…
Browse files Browse the repository at this point in the history
… do not exist (#172)

Co-authored-by: nttdata-rtorsoli <[email protected]>
  • Loading branch information
nttdata-rtorsoli and nttdata-rtorsoli authored Apr 4, 2024
1 parent 1937c59 commit 5b30ab7
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import it.pagopa.interop.tenantmanagement.model.tenant.PersistentTenant
import it.pagopa.interop.tenantscertifiedattributesupdater.repository.{TenantRepository, extractData}
import it.pagopa.interop.tenantscertifiedattributesupdater.system.ApplicationConfiguration
import org.mongodb.scala.{Document, MongoClient, MongoCollection}
import org.mongodb.scala.model.Filters

import scala.concurrent.Future

Expand All @@ -16,6 +17,6 @@ final case class TenantRepositoryImpl(client: MongoClient) extends TenantReposit
.getCollection(ApplicationConfiguration.tenantsCollection)

def getTenants: Future[Seq[Either[Throwable, PersistentTenant]]] =
collection.find().map(extractData[PersistentTenant]).toFuture()
collection.find(Filters.ne("data.selfcareId", null)).map(extractData[PersistentTenant]).toFuture()

}
Original file line number Diff line number Diff line change
Expand Up @@ -65,22 +65,28 @@ object Utils {
attributesIndex: Map[UUID, AttributeInfo]
): TenantActions = {

val filteredTenant: List[PersistentTenant] = tenants.filter(_.selfcareId.isDefined)

val originsFromPartyRegistry: List[String] = institutions.map(_.origin).distinct

val filteredAttributesIndex: Map[UUID, AttributeInfo] =
attributesIndex.filter(a => originsFromPartyRegistry.contains(a._2.origin))

val fromRegistry: List[TenantSeed] = institutions.filter(_.id.nonEmpty).map(createTenantSeed)
val tenantsMap: Map[PersistentExternalId, String] = filteredTenant.map(t => (t.externalId, t.name)).toMap

val fromRegistry: List[TenantSeed] = institutions
.filter(institution =>
institution.id.nonEmpty && tenantsMap.contains(PersistentExternalId(institution.origin, institution.originId))
)
.map(prepareTenantSeedForUpdate)

val fromTenant: Map[PersistentExternalId, List[AttributeInfo]] =
tenants
filteredTenant
.map(tenant =>
tenant.externalId -> tenant.attributes.flatMap(AttributeInfo.addRevocationTimeStamp(filteredAttributesIndex))
)
.toMap

val tenantsMap: Map[PersistentExternalId, String] = tenants.map(t => (t.externalId, t.name)).toMap

val activations: List[InternalTenantSeed] =
fromRegistry
.map(extractActivable(fromTenant))
Expand Down Expand Up @@ -173,7 +179,7 @@ object Utils {
}
.map(_ => ())

private def createTenantSeed(institution: Institution): TenantSeed = {
private def prepareTenantSeedForUpdate(institution: Institution): TenantSeed = {
val attributesWithoutKind: List[AttributeInfo] = institution.classification match {
case Classification.AGENCY =>
List(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,47 +16,29 @@ import munit.FunSuite
import java.util.UUID

class CreateActionSpec extends FunSuite {
test("Create new Tenant with attributes if Tenant does not exist") {
val origin1 = "ORIGIN_1"
val origin2 = "ORIGIN_2"
val originId1 = "001"
val originId2 = "002"
val attributeCode1 = "CAT1"
val attributeCode2 = "CAT2"
val kind = "KIND"
val kindSha256 = Digester.toSha256(kind.getBytes())

test("Update the tenant with attributes if the tenant exists with non-empty selfcareId") {
val origin1 = "ORIGIN_1"
val origin2 = "ORIGIN_2"
val originId1 = "001"
val originId2 = "002"
val attributeCode1 = "CAT1"
val attributeCode2 = "CAT2"
val kind = "KIND"
val kindSha256 = Digester.toSha256(kind.getBytes())
val institutions: List[Institution] =
List(
institution(origin1, originId1, attributeCode1, kind),
institution(origin1, originId1, attributeCode2, kind),
institution(origin2, originId1, attributeCode2, kind),
institution(origin1, originId2, attributeCode2, kind)
)
val tenants: List[PersistentTenant] = List(persistentTenant("ORIG1", "VAL1"))
val tenants: List[PersistentTenant] =
List(persistentTenant(origin1, originId1))
val attributesIndex: Map[UUID, AttributeInfo] = Map.empty

val result = createAction(institutions, tenants, attributesIndex)

val expectedActivations = List(
InternalTenantSeed(
externalId = ExternalId(origin2, originId1),
certifiedAttributes = List(
InternalAttributeSeed(origin2, kindSha256),
InternalAttributeSeed(origin2, attributeCode2),
InternalAttributeSeed(origin2, originId1)
),
name = defaultName
),
InternalTenantSeed(
externalId = ExternalId(origin1, originId2),
certifiedAttributes = List(
InternalAttributeSeed(origin1, kindSha256),
InternalAttributeSeed(origin1, attributeCode2),
InternalAttributeSeed(origin1, originId2)
),
name = defaultName
),
InternalTenantSeed(
externalId = ExternalId(origin1, originId1),
certifiedAttributes = List(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ object SpecHelper {
): PersistentTenant = PersistentTenant(
id = UUID.randomUUID(),
kind = Some(PersistentTenantKind.PA),
selfcareId = None,
selfcareId = Some(UUID.randomUUID().toString()),
externalId = PersistentExternalId(origin, value),
features = Nil,
attributes = attributes,
Expand Down

0 comments on commit 5b30ab7

Please sign in to comment.