Skip to content

Commit

Permalink
fix: risk analysis depends on eservice mode
Browse files Browse the repository at this point in the history
  • Loading branch information
beetlecrunch committed Nov 24, 2023
1 parent a5bcfd9 commit 9d1695e
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import it.pagopa.interop.purposemanagement.model.purpose.{
WaitingForApproval
}
import it.pagopa.interop.catalogmanagement.model.{CatalogItem, Deliver, Receive}
import it.pagopa.interop.tenantmanagement.model.tenant.PersistentTenantKind
import it.pagopa.interop.tenantmanagement.model.tenant.{PersistentTenant, PersistentTenantKind}
import it.pagopa.interop.purposeprocess.service.AgreementManagementService.OPERATIVE_AGREEMENT_STATES
import it.pagopa.interop.commons.riskanalysis.service.RiskAnalysisService
import it.pagopa.interop.commons.riskanalysis.api.impl.RiskAnalysisValidation
Expand Down Expand Up @@ -265,15 +265,15 @@ final case class PurposeApiServiceImpl(
freeOfChargeReason: Option[String],
payload: (PersistentPurpose, PersistentTenantKind) => Future[PurposeManagementDependency.PurposeUpdateContent]
)(implicit contexts: Seq[(String, String)]): Future[Purpose] = for {
organizationId <- getOrganizationIdFutureUUID(contexts)
requesterOrgId <- getOrganizationIdFutureUUID(contexts)
purposeUUID <- purposeId.toFutureUUID
purpose <- purposeManagementService.getPurposeById(purposeUUID)
_ <- assertOrganizationIsAConsumer(organizationId, purpose.consumerId)
_ <- assertOrganizationIsAConsumer(requesterOrgId, purpose.consumerId)
_ <- assertPurposeIsInDraftState(purpose)
eService <- catalogManagementService.getEServiceById(purpose.eserviceId)
_ <- eServiceModeCheck(eService)
_ <- checkFreeOfCharge(isFreeOfCharge, freeOfChargeReason)
tenant <- tenantManagementService.getTenantById(organizationId)
tenant <- getInvolvedTenantByEServiceMode(eService, requesterOrgId)
tenantKind <- tenant.kind.toFuture(TenantKindNotFound(tenant.id))
purposePayload <- payload(purpose, tenantKind)
updatedPurpose <- purposeManagementService.updatePurpose(purposeUUID, purposePayload)
Expand Down Expand Up @@ -633,17 +633,17 @@ final case class PurposeApiServiceImpl(
logger.info(operationLabel)

val result: Future[RiskAnalysisFormConfigResponse] = for {
organizationId <- getOrganizationIdFutureUUID(contexts)
tenant <- tenantManagementService.getTenantById(organizationId)
tenantKindParam <- tenantKind.traverse(TenantKind.fromValue).toFuture
kind <- tenantKindParam.fold(tenant.kind.toFuture(TenantKindNotFound(tenant.id)))(kind =>
organizationId <- getOrganizationIdFutureUUID(contexts)
tenant <- tenantManagementService.getTenantById(organizationId)
tenantKindParam <- tenantKind.traverse(TenantKind.fromValue).toFuture
kind <- tenantKindParam.fold(tenant.kind.toFuture(TenantKindNotFound(tenant.id)))(kind =>
Future.successful(kind.toPersistent)
)
kindConfig <- RiskAnalysisService
kindConfig <- RiskAnalysisService
.riskAnalysisForms()
.get(kind.toTemplate)
.toFuture(RiskAnalysisConfigForTenantKindNotFound(tenant.id))
(latest, riskAnalysisFormConfig) <- kindConfig
(_, riskAnalysisFormConfig) <- kindConfig
.maxByOption(_._1.toDouble)
.toFuture(RiskAnalysisConfigLatestVersionNotFound(kind))
} yield riskAnalysisFormConfig.toApi
Expand All @@ -664,13 +664,10 @@ final case class PurposeApiServiceImpl(
logger.info(operationLabel)

val result: Future[RiskAnalysisFormConfigResponse] = for {
organizationId <- getOrganizationIdFutureUUID(contexts)
requesterOrgId <- getOrganizationIdFutureUUID(contexts)
eserviceId <- eserviceId.toFutureUUID
eservice <- catalogManagementService.getEServiceById(eserviceId)
tenant <- eservice.mode match {
case Deliver => tenantManagementService.getTenantById(organizationId)
case Receive => tenantManagementService.getTenantById(eservice.producerId)
}
tenant <- getInvolvedTenantByEServiceMode(eservice, requesterOrgId)
kind <- tenant.kind.fold(tenant.kind.toFuture(TenantKindNotFound(tenant.id)))(kind => Future.successful(kind))
kindConfig <- RiskAnalysisService
.riskAnalysisForms()
Expand All @@ -688,6 +685,12 @@ final case class PurposeApiServiceImpl(
}
}

private def getInvolvedTenantByEServiceMode(eservice: CatalogItem, requesterId: UUID): Future[PersistentTenant] =
eservice.mode match {
case Deliver => tenantManagementService.getTenantById(requesterId)
case Receive => tenantManagementService.getTenantById(eservice.producerId)
}

private def assertOrganizationIsAConsumer(organizationId: UUID, consumerId: UUID): Future[Ownership] =
if (organizationId == consumerId) Future.successful(Ownership.CONSUMER)
else Future.failed(OrganizationIsNotTheConsumer(organizationId))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import akka.http.scaladsl.model.MediaTypes
import akka.http.scaladsl.server.directives.FileInfo
import cats.syntax.all._
import it.pagopa.interop.authorizationmanagement.client.model.ClientComponentState
import it.pagopa.interop.catalogmanagement.model.CatalogItem
import it.pagopa.interop.catalogmanagement.model.{CatalogItem, Deliver, Receive}
import it.pagopa.interop.commons.cqrs.service.ReadModelService
import it.pagopa.interop.commons.files.service.FileManager
import it.pagopa.interop.commons.utils.TypeConversions._
Expand Down Expand Up @@ -211,8 +211,11 @@ final case class PurposeVersionActivation(
consumerOrigin = consumer.externalId.origin,
consumerIPACode = consumer.externalId.value
)
consumerKind <- consumer.kind.toFuture(TenantKindNotFound(consumer.id))
path <- createRiskAnalysisDocument(documentId, purpose, version, eServiceInfo)(consumerKind)
tenantKind <- eService.mode match {
case Receive => producer.kind.toFuture(TenantKindNotFound(consumer.id))
case Deliver => consumer.kind.toFuture(TenantKindNotFound(consumer.id))
}
path <- createRiskAnalysisDocument(documentId, purpose, version, eServiceInfo)(tenantKind)
payload = ActivatePurposeVersionPayload(
riskAnalysis = Some(
PurposeVersionDocument(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1543,6 +1543,7 @@ class PurposeApiServiceSpec extends AnyWordSpecLike with SpecHelper with Scalate
val purposeId = UUID.randomUUID()
val eserviceId = UUID.randomUUID()
val consumerId = UUID.randomUUID()
val producerId = UUID.randomUUID()
val reversePurposeUpdateContent =
ReversePurposeUpdateContent(
title = "A title",
Expand Down Expand Up @@ -1575,9 +1576,9 @@ class PurposeApiServiceSpec extends AnyWordSpecLike with SpecHelper with Scalate

mockPurposeRetrieve(purposeId, purpose)

mockEServiceRetrieve(eserviceId, SpecData.eService.copy(id = eserviceId, mode = Receive))
mockEServiceRetrieve(eserviceId, SpecData.eService.copy(id = eserviceId, mode = Receive, producerId = producerId))

mockTenantRetrieve(consumerId, SpecData.tenant.copy(id = consumerId, kind = PersistentTenantKind.PRIVATE.some))
mockTenantRetrieve(producerId, SpecData.tenant.copy(id = producerId, kind = PersistentTenantKind.PA.some))

mockPurposeUpdate(purposeId, seed, SpecData.dependencyPurpose.copy(id = purpose.id))

Expand Down

0 comments on commit 9d1695e

Please sign in to comment.