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-4845: Check duplicated purpose title on update #204

Merged
merged 3 commits into from
May 28, 2024
Merged
Show file tree
Hide file tree
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 @@ -103,10 +103,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 @@ -119,9 +124,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 @@ -143,7 +147,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 @@ -168,7 +172,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 @@ -231,6 +235,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 @@ -253,6 +258,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 @@ -267,11 +273,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
Loading