Skip to content

Commit

Permalink
Merge branch '1.0.x' into PIN-4844
Browse files Browse the repository at this point in the history
  • Loading branch information
nttdata-rtorsoli authored May 28, 2024
2 parents 0082efd + 195d0dd commit d815f43
Show file tree
Hide file tree
Showing 3 changed files with 191 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,15 @@ final case class PurposeApiServiceImpl(
tenantKind <- tenant.kind.toFuture(TenantKindNotFound(tenant.id))
} yield tenantKind

private def checkAgreements(eServiceId: UUID, consumerId: UUID, title: String): Future[Unit] = for {
agreements <- agreementManagementService.getAgreements(eServiceId, consumerId, OPERATIVE_AGREEMENT_STATES)
agreement <- agreements.headOption.toFuture(AgreementNotFound(eServiceId.toString, consumerId.toString))
maybePurpose <- purposeManagementService
private def checkAgreements(
eServiceId: UUID,
consumerId: UUID,
title: String,
purposeId: Option[UUID]
): Future[Unit] = for {
agreements <- agreementManagementService.getAgreements(eServiceId, consumerId, OPERATIVE_AGREEMENT_STATES)
agreement <- agreements.headOption.toFuture(AgreementNotFound(eServiceId.toString, consumerId.toString))
result <- purposeManagementService
.listPurposes(
consumerId,
title.some,
Expand All @@ -122,9 +127,8 @@ final case class PurposeApiServiceImpl(
limit = 1,
exactMatchOnTitle = true
)
.map(_.results.headOption)

_ <- maybePurpose.fold(Future.unit)(_ => Future.failed(DuplicatedPurposeName(title)))
purpose = purposeId.fold(result.results)(id => result.results.filterNot(_.id == id))
_ <- purpose.headOption.map(_.title).fold(Future.unit)(_ => Future.failed(DuplicatedPurposeName(title)))
} yield ()

override def createPurposeFromEService(seed: EServicePurposeSeed)(implicit
Expand All @@ -146,7 +150,7 @@ final case class PurposeApiServiceImpl(
_ <- checkFreeOfCharge(seed.isFreeOfCharge, seed.freeOfChargeReason)
tenantKind <- getTenantKind(eService.producerId)
purposeSeed = seed.toManagement(seed.eServiceId, riskAnalysis.riskAnalysisForm.toManagement(seed.riskAnalysisId))
_ <- checkAgreements(seed.eServiceId, seed.consumerId, seed.title)
_ <- checkAgreements(seed.eServiceId, seed.consumerId, seed.title, None)
purpose <- purposeManagementService.createPurpose(purposeSeed)
isValidRiskAnalysisForm = isRiskAnalysisFormValid(
riskAnalysisForm = purpose.riskAnalysisForm.map(_.toApi),
Expand All @@ -171,7 +175,7 @@ final case class PurposeApiServiceImpl(
_ <- checkFreeOfCharge(seed.isFreeOfCharge, seed.freeOfChargeReason)
tenantKind <- getTenantKind(requesterOrgId)
purposeSeed <- seed.toManagement(schemaOnlyValidation = true)(tenantKind).toFuture
_ <- checkAgreements(seed.eserviceId, seed.consumerId, seed.title)
_ <- checkAgreements(seed.eserviceId, seed.consumerId, seed.title, None)
purpose <- purposeManagementService.createPurpose(purposeSeed)
isValidRiskAnalysisForm = isRiskAnalysisFormValid(
riskAnalysisForm = purpose.riskAnalysisForm.map(_.toApi),
Expand Down Expand Up @@ -234,6 +238,7 @@ final case class PurposeApiServiceImpl(
eService => if (eService.mode == Deliver) Future.unit else Future.failed(EServiceNotInDeliverMode(eService.id)),
seed.isFreeOfCharge,
seed.freeOfChargeReason,
seed.title,
(_, tenantKind) =>
seed
.toManagement(schemaOnlyValidation = true)(tenantKind)
Expand All @@ -256,6 +261,7 @@ final case class PurposeApiServiceImpl(
eService => if (eService.mode == Receive) Future.unit else Future.failed(EServiceNotInReceiveMode(eService.id)),
seed.isFreeOfCharge,
seed.freeOfChargeReason,
seed.title,
(purpose, tenantKind) =>
seed
.toManagement(schemaOnlyValidation = true, purpose.riskAnalysisForm.map(_.toApi))(tenantKind)
Expand All @@ -270,11 +276,13 @@ final case class PurposeApiServiceImpl(
eServiceModeCheck: CatalogItem => Future[Unit],
isFreeOfCharge: Boolean,
freeOfChargeReason: Option[String],
title: String,
payload: (PersistentPurpose, PersistentTenantKind) => Future[PurposeManagementDependency.PurposeUpdateContent]
)(implicit contexts: Seq[(String, String)]): Future[Purpose] = for {
requesterOrgId <- getOrganizationIdFutureUUID(contexts)
purposeUUID <- purposeId.toFutureUUID
purpose <- purposeManagementService.getPurposeById(purposeUUID)
_ <- checkAgreements(purpose.eserviceId, purpose.consumerId, title, Some(purposeUUID))
_ <- assertOrganizationIsAConsumer(requesterOrgId, purpose.consumerId)
_ <- assertPurposeIsInDraftState(purpose)
eService <- catalogManagementService.getEServiceById(purpose.eserviceId)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,8 @@ object ResponseHandlers extends AkkaResponses {
case Failure(ex: OrganizationIsNotTheConsumer) => forbidden(ex, logMessage)
case Failure(ex: PurposeNotInDraftState) => forbidden(ex, logMessage)
case Failure(ex: PurposeNotFound) => notFound(ex, logMessage)

case Failure(ex) => internalServerError(ex, logMessage)
case Failure(ex: DuplicatedPurposeName) => conflict(ex, logMessage)
case Failure(ex) => internalServerError(ex, logMessage)
}

def updateReversePurposeResponse[T](logMessage: String)(
Expand All @@ -114,8 +114,8 @@ object ResponseHandlers extends AkkaResponses {
case Failure(ex: OrganizationIsNotTheConsumer) => forbidden(ex, logMessage)
case Failure(ex: PurposeNotInDraftState) => forbidden(ex, logMessage)
case Failure(ex: PurposeNotFound) => notFound(ex, logMessage)

case Failure(ex) => internalServerError(ex, logMessage)
case Failure(ex: DuplicatedPurposeName) => conflict(ex, logMessage)
case Failure(ex) => internalServerError(ex, logMessage)
}

def getPurposeResponse[T](logMessage: String)(
Expand Down
Loading

0 comments on commit d815f43

Please sign in to comment.