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 81898e90..61076bea 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 @@ -358,6 +358,9 @@ final case class ProcessApiServiceImpl( catalogItem <- catalogManagementService.getEServiceById(eServiceUuid) _ <- assertRequesterAllowed(catalogItem.producerId)(organizationId) descriptor <- assertDescriptorExists(catalogItem, descriptorUuid) + _ <- + if (documentSeed.kind == EServiceDocumentKind.INTERFACE) assertInterfaceDoesNotExists(descriptor) + else Future.unit updated <- catalogManagementService.createEServiceDocument(catalogItem.id, descriptor.id, managementSeed) } yield updated.toApi @@ -859,4 +862,9 @@ object ProcessApiServiceImpl { .find(_.id == descriptorId) .toFuture(EServiceDescriptorNotFound(eService.id.toString, descriptorId.toString)) + def assertInterfaceDoesNotExists(descriptor: CatalogDescriptor): Future[Unit] = + descriptor.interface match { + case Some(_) => Future.failed(InterfaceAlreadyExists(descriptor.id)) + case None => Future.unit + } } diff --git a/src/main/scala/it/pagopa/interop/catalogprocess/api/impl/ResponseHandlers.scala b/src/main/scala/it/pagopa/interop/catalogprocess/api/impl/ResponseHandlers.scala index cd1144b5..3f8d9e9b 100644 --- a/src/main/scala/it/pagopa/interop/catalogprocess/api/impl/ResponseHandlers.scala +++ b/src/main/scala/it/pagopa/interop/catalogprocess/api/impl/ResponseHandlers.scala @@ -200,6 +200,7 @@ object ResponseHandlers extends AkkaResponses { result match { case Success(s) => success(s) case Failure(ex: OperationForbidden.type) => forbidden(ex, logMessage) + case Failure(ex: InterfaceAlreadyExists) => badRequest(ex, logMessage) case Failure(ex: EServiceNotFound) => notFound(ex, logMessage) case Failure(ex: EServiceDescriptorNotFound) => notFound(ex, logMessage) case Failure(ex) => internalServerError(ex, logMessage) diff --git a/src/main/scala/it/pagopa/interop/catalogprocess/errors/CatalogProcessErrors.scala b/src/main/scala/it/pagopa/interop/catalogprocess/errors/CatalogProcessErrors.scala index 3025293d..484ce0db 100644 --- a/src/main/scala/it/pagopa/interop/catalogprocess/errors/CatalogProcessErrors.scala +++ b/src/main/scala/it/pagopa/interop/catalogprocess/errors/CatalogProcessErrors.scala @@ -76,4 +76,7 @@ object CatalogProcessErrors { final case class AttributeNotFound(attributeId: UUID) extends ComponentError("0020", s"Attribute ${attributeId.toString} not found") + final case class InterfaceAlreadyExists(descriptorId: UUID) + extends ComponentError("0021", s"Descriptor ${descriptorId.toString} already has an interface") + } diff --git a/src/test/scala/it/pagopa/interop/catalogprocess/CatalogProcessSpec.scala b/src/test/scala/it/pagopa/interop/catalogprocess/CatalogProcessSpec.scala index 70e77c8d..494531d0 100644 --- a/src/test/scala/it/pagopa/interop/catalogprocess/CatalogProcessSpec.scala +++ b/src/test/scala/it/pagopa/interop/catalogprocess/CatalogProcessSpec.scala @@ -2489,6 +2489,52 @@ class CatalogProcessSpec extends SpecHelper with AnyWordSpecLike with ScalatestR status shouldEqual StatusCodes.Forbidden } } + "fail if document is an interface and it already exists" in { + val requesterId = UUID.randomUUID() + val descriptorId = UUID.randomUUID() + implicit val context: Seq[(String, String)] = + Seq("bearer" -> bearerToken, USER_ROLES -> "admin", ORGANIZATION_ID_CLAIM -> requesterId.toString) + + val descriptor = SpecData.catalogDescriptor.copy( + id = descriptorId, + interface = Some( + CatalogDocument( + id = UUID.randomUUID(), + name = "name", + contentType = "application/yaml", + prettyName = "pretty", + path = "path", + checksum = "checksum", + uploadDate = OffsetDateTimeSupplier.get().minusDays(10) + ) + ) + ) + + val eService = SpecData.catalogItem.copy(descriptors = Seq(descriptor), producerId = requesterId) + + val documentId = UUID.randomUUID() + + val seed = CreateEServiceDescriptorDocumentSeed( + documentId = documentId, + kind = EServiceDocumentKind.INTERFACE, + prettyName = "prettyName", + filePath = "filePath", + fileName = "fileName", + contentType = "application/json", + checksum = "checksum", + serverUrls = Seq.empty + ) + + (mockCatalogManagementService + .getEServiceById(_: UUID)(_: ExecutionContext, _: ReadModelService)) + .expects(SpecData.catalogItem.id, *, *) + .once() + .returns(Future.successful(eService)) + + Post() ~> service.createEServiceDocument(SpecData.catalogItem.id.toString, descriptorId.toString, seed) ~> check { + status shouldEqual StatusCodes.BadRequest + } + } } "Document retrieve" should { "succeed" in {