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

fix: risk analysis depends on eservice mode #194

Merged
merged 8 commits into from
Nov 27, 2023
Merged
Show file tree
Hide file tree
Changes from 7 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
6 changes: 4 additions & 2 deletions src/main/resources/interface-specification.yml
Original file line number Diff line number Diff line change
Expand Up @@ -714,10 +714,12 @@ paths:
required: true
schema:
type: string
- name: tenantKind
- name: eserviceId
in: query
required: true
schema:
$ref: '#/components/schemas/TenantKind'
type: string
format: uuid
get:
tags:
- purpose
Expand Down
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 @@ -130,15 +130,15 @@ final case class PurposeApiServiceImpl(
s"Creating Purposes for EService ${seed.eServiceId}, Consumer ${seed.consumerId}"

val result: Future[Purpose] = for {
organizationId <- getOrganizationIdFutureUUID(contexts)
_ <- assertOrganizationIsAConsumer(organizationId, seed.consumerId)
requesterOrgId <- getOrganizationIdFutureUUID(contexts)
_ <- assertOrganizationIsAConsumer(requesterOrgId, seed.consumerId)
eService <- catalogManagementService.getEServiceById(seed.eServiceId)
_ <- if (eService.mode == Receive) Future.unit else Future.failed(EServiceNotInReceiveMode(eService.id))
riskAnalysis <- eService.riskAnalysis
.find(_.id == seed.riskAnalysisId)
.toFuture(RiskAnalysisNotFound(seed.eServiceId, seed.riskAnalysisId))
_ <- checkFreeOfCharge(seed.isFreeOfCharge, seed.freeOfChargeReason)
tenantKind <- getTenantKind(organizationId)
tenantKind <- getTenantKind(eService.producerId)
purposeSeed = seed.toManagement(seed.eServiceId, riskAnalysis.riskAnalysisForm.toManagement(seed.riskAnalysisId))
_ <- checkAgreements(seed.eServiceId, seed.consumerId, seed.title)
purpose <- purposeManagementService.createPurpose(purposeSeed)
Expand All @@ -160,10 +160,10 @@ final case class PurposeApiServiceImpl(
logger.info(operationLabel)

val result: Future[Purpose] = for {
organizationId <- getOrganizationIdFutureUUID(contexts)
_ <- assertOrganizationIsAConsumer(organizationId, seed.consumerId)
requesterOrgId <- getOrganizationIdFutureUUID(contexts)
_ <- assertOrganizationIsAConsumer(requesterOrgId, seed.consumerId)
_ <- checkFreeOfCharge(seed.isFreeOfCharge, seed.freeOfChargeReason)
tenantKind <- getTenantKind(organizationId)
tenantKind <- getTenantKind(requesterOrgId)
purposeSeed <- seed.toManagement(schemaOnlyValidation = true)(tenantKind).toFuture
_ <- checkAgreements(seed.eserviceId, seed.consumerId, seed.title)
purpose <- purposeManagementService.createPurpose(purposeSeed)
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, purpose.consumerId)
tenantKind <- tenant.kind.toFuture(TenantKindNotFound(tenant.id))
purposePayload <- payload(purpose, tenantKind)
updatedPurpose <- purposeManagementService.updatePurpose(purposeUUID, purposePayload)
Expand Down Expand Up @@ -435,10 +435,11 @@ final case class PurposeApiServiceImpl(
val result: Future[PurposeVersion] = for {
purposeUUID <- purposeId.toFutureUUID
versionUUID <- versionId.toFutureUUID
organizationId <- getOrganizationIdFutureUUID(contexts)
requesterOrgId <- getOrganizationIdFutureUUID(contexts)
purpose <- purposeManagementService.getPurposeById(purposeUUID)
consumer <- tenantManagementService.getTenantById(purpose.consumerId)
tenantKind <- consumer.kind.toFuture(TenantKindNotFound(consumer.id))
eService <- catalogManagementService.getEServiceById(purpose.eserviceId)
tenant <- getInvolvedTenantByEServiceMode(eService, purpose.consumerId)
tenantKind <- tenant.kind.toFuture(TenantKindNotFound(tenant.id))
version <- getVersion(purpose, versionUUID)
riskAnalysisForm <- purpose.riskAnalysisForm.toFuture(MissingRiskAnalysis(purposeUUID))
_ <-
Expand All @@ -448,15 +449,14 @@ final case class PurposeApiServiceImpl(
.toEither
.whenA(version.state == Draft)
.toFuture
eService <- catalogManagementService.getEServiceById(purpose.eserviceId)
ownership <- Ownership
.getOrganizationRole(organizationId, eService.producerId, purpose.consumerId)
.getOrganizationRole(requesterOrgId, eService.producerId, purpose.consumerId)
.toFuture
updatedVersion <- purposeVersionActivation.activateOrWaitForApproval(
eService,
purpose,
version,
organizationId,
requesterOrgId,
ownership
)
} yield updatedVersion.toApi
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 @@ -655,8 +655,7 @@ final case class PurposeApiServiceImpl(
}
}

override def retrieveRiskAnalysisConfigurationByVersion(tenantKind: Option[String], riskAnalysisVersion: String)(
implicit
override def retrieveRiskAnalysisConfigurationByVersion(eserviceId: String, riskAnalysisVersion: String)(implicit
contexts: Seq[(String, String)],
toEntityMarshallerRiskAnalysisFormConfigResponse: ToEntityMarshaller[RiskAnalysisFormConfigResponse],
toEntityMarshallerProblem: ToEntityMarshaller[Problem]
Expand All @@ -665,12 +664,11 @@ 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 =>
Future.successful(kind.toPersistent)
)
requesterOrgId <- getOrganizationIdFutureUUID(contexts)
eserviceId <- eserviceId.toFutureUUID
eservice <- catalogManagementService.getEServiceById(eserviceId)
tenant <- getInvolvedTenantByEServiceMode(eservice, requesterOrgId)
kind <- tenant.kind.fold(tenant.kind.toFuture(TenantKindNotFound(tenant.id)))(kind => Future.successful(kind))
kindConfig <- RiskAnalysisService
.riskAnalysisForms()
.get(kind.toTemplate)
Expand All @@ -687,6 +685,12 @@ final case class PurposeApiServiceImpl(
}
}

private def getInvolvedTenantByEServiceMode(eservice: CatalogItem, consumerId: UUID): Future[PersistentTenant] =
eservice.mode match {
case Deliver => tenantManagementService.getTenantById(consumerId)
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(producer.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 @@ -26,6 +26,7 @@ object ResponseHandlers extends AkkaResponses {
result match {
case Success(s) => success(s)
case Failure(ex: RiskAnalysisConfigVersionNotFound) => notFound(ex, logMessage)
case Failure(ex: EServiceNotFound) => notFound(ex, logMessage)
case Failure(ex) => internalServerError(ex, logMessage)
}

Expand Down
Loading
Loading