diff --git a/src/main/scala/it/pagopa/interop/catalogprocess/api/impl/ProcessApiServiceImpl.scala b/src/main/scala/it/pagopa/interop/catalogprocess/api/impl/ProcessApiServiceImpl.scala index 04fd8ef8..b5df83b5 100644 --- a/src/main/scala/it/pagopa/interop/catalogprocess/api/impl/ProcessApiServiceImpl.scala +++ b/src/main/scala/it/pagopa/interop/catalogprocess/api/impl/ProcessApiServiceImpl.scala @@ -728,12 +728,22 @@ final case class ProcessApiServiceImpl( val operationLabel = s"Deleting EService $eServiceId" logger.info(operationLabel) + def isDeletable(catalogItem: CatalogItem): Either[Throwable, Option[CatalogDescriptor]] = Either + .cond( + catalogItem.descriptors.isEmpty || + (catalogItem.descriptors.length == 1 && catalogItem.descriptors.exists(_.state == Draft)), + catalogItem.descriptors.headOption, + EServiceWithDescriptorsNotDeletable(catalogItem.id.toString) + ) + val result: Future[Unit] = for { organizationId <- getOrganizationIdFutureUUID(contexts) eServiceUuid <- eServiceId.toFutureUUID catalogItem <- catalogManagementService.getEServiceById(eServiceUuid) _ <- assertRequesterAllowed(catalogItem.producerId)(organizationId) - result <- catalogManagementService.deleteEService(eServiceId) + descriptor <- isDeletable(catalogItem).toFuture + _ <- descriptor.fold(Future.unit)(d => catalogManagementService.deleteDraft(eServiceId, d.id.toString)) + result <- catalogManagementService.deleteEService(eServiceId) } yield result onComplete(result) { diff --git a/src/main/scala/it/pagopa/interop/catalogprocess/service/impl/CatalogManagementServiceImpl.scala b/src/main/scala/it/pagopa/interop/catalogprocess/service/impl/CatalogManagementServiceImpl.scala index 1cef23fd..3a257477 100644 --- a/src/main/scala/it/pagopa/interop/catalogprocess/service/impl/CatalogManagementServiceImpl.scala +++ b/src/main/scala/it/pagopa/interop/catalogprocess/service/impl/CatalogManagementServiceImpl.scala @@ -24,8 +24,7 @@ import it.pagopa.interop.catalogprocess.errors.CatalogProcessErrors.{ DescriptorDocumentNotFound, EServiceDescriptorNotFound, EServiceNotFound, - EServiceRiskAnalysisNotFound, - EServiceWithDescriptorsNotDeletable + EServiceRiskAnalysisNotFound } import it.pagopa.interop.commons.logging.{CanLogContextFields, ContextFieldsToLog} @@ -78,7 +77,6 @@ final case class CatalogManagementServiceImpl(invoker: CatalogManagementInvoker, invoker.invoke(request, s"E-Service $eServiceId deleted").recoverWith { case err: ApiError[_] if err.code == 404 => Future.failed(EServiceNotFound(eServiceId)) - case err: ApiError[_] if err.code == 409 => Future.failed(EServiceWithDescriptorsNotDeletable(eServiceId)) } } diff --git a/src/test/scala/it/pagopa/interop/catalogprocess/CatalogProcessSpec.scala b/src/test/scala/it/pagopa/interop/catalogprocess/CatalogProcessSpec.scala index d83941c1..7da7ef78 100644 --- a/src/test/scala/it/pagopa/interop/catalogprocess/CatalogProcessSpec.scala +++ b/src/test/scala/it/pagopa/interop/catalogprocess/CatalogProcessSpec.scala @@ -18,8 +18,7 @@ import it.pagopa.interop.catalogprocess.errors.CatalogProcessErrors.{ DescriptorDocumentNotFound, EServiceNotFound, EServiceRiskAnalysisNotFound, - AttributeNotFound, - EServiceWithDescriptorsNotDeletable + AttributeNotFound } import it.pagopa.interop.catalogprocess.model._ import it.pagopa.interop.commons.cqrs.service.ReadModelService @@ -3282,14 +3281,11 @@ class CatalogProcessSpec extends SpecHelper with AnyWordSpecLike with ScalatestR .once() .returns(Future.successful(eService)) - (mockCatalogManagementService - .deleteEService(_: String)(_: Seq[(String, String)])) - .expects(eService.id.toString, *) - .returning(Future.failed(EServiceWithDescriptorsNotDeletable(SpecData.catalogItem.id.toString))) - .once() - Delete() ~> service.deleteEService(SpecData.catalogItem.id.toString) ~> check { status shouldEqual StatusCodes.Conflict + val problem = responseAs[Problem] + problem.status shouldBe StatusCodes.Conflict.intValue + problem.errors.head.code shouldBe "009-0021" } } "fail if requester is not the Producer" in {