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-4560 Check voucher Lifespan and daily Calls on creation and update descriptor #240

Merged
merged 5 commits into from
Feb 20, 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
2 changes: 2 additions & 0 deletions src/main/resources/interface-specification.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1215,6 +1215,8 @@ components:
voucherLifespan:
type: integer
format: int32
minimum: 60
maximum: 86400
dailyCallsPerConsumer:
type: integer
format: int32
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,10 @@ final case class ProcessApiServiceImpl(
.find(_.id.toString == descriptorId)
.toRight(EServiceDescriptorNotFound(eService.id.toString, descriptorId))

private def getLatestDescriptor(eService: CatalogItem): Option[CatalogDescriptor] = eService.descriptors
.sortBy(_.version.toInt)(Ordering.Int.reverse)
.headOption

override def getEServices(
name: Option[String],
eServicesIds: String,
Expand Down Expand Up @@ -423,6 +427,22 @@ final case class ProcessApiServiceImpl(
catalogItem <- catalogManagementService.getEServiceById(eServiceUuid)
_ <- assertRequesterAllowed(catalogItem.producerId)(organizationId)
_ <- hasNotDraftDescriptor(catalogItem).toFuture
_ <- checkDailyCalls(
eServiceDescriptorSeed.dailyCallsPerConsumer,
eServiceDescriptorSeed.dailyCallsTotal
).toFuture
_ <- getLatestDescriptor(catalogItem).fold(Future.unit)(descriptor =>
checkIfDailyCallsIsGreaterThanExisting(
eServiceDescriptorSeed.dailyCallsPerConsumer,
descriptor.dailyCallsPerConsumer
).toFuture
)
_ <- getLatestDescriptor(catalogItem).fold(Future.unit)(descriptor =>
checkIfDailyCallsIsGreaterThanExisting(
eServiceDescriptorSeed.dailyCallsTotal,
descriptor.dailyCallsTotal
).toFuture
)
_ <- eServiceDescriptorSeed.attributes.certified.traverse(
_.traverse(attr => attributeRegistryManagementService.getAttributeById(attr.id))
)
Expand Down Expand Up @@ -463,6 +483,18 @@ final case class ProcessApiServiceImpl(
_ <- assertRequesterAllowed(catalogItem.producerId)(organizationId)
descriptor <- assertDescriptorExists(catalogItem, descriptorUuid)
_ <- isDraftDescriptor(descriptor)
_ <- checkDailyCalls(
updateEServiceDescriptorSeed.dailyCallsPerConsumer,
updateEServiceDescriptorSeed.dailyCallsTotal
).toFuture
_ <- checkIfDailyCallsIsGreaterThanExisting(
updateEServiceDescriptorSeed.dailyCallsPerConsumer,
descriptor.dailyCallsPerConsumer
).toFuture
_ <- checkIfDailyCallsIsGreaterThanExisting(
updateEServiceDescriptorSeed.dailyCallsTotal,
descriptor.dailyCallsTotal
).toFuture
updatedEService <- catalogManagementService.updateDescriptor(
eServiceId,
descriptorId,
Expand All @@ -485,13 +517,19 @@ final case class ProcessApiServiceImpl(
logger.info(operationLabel)

val result: Future[EService] = for {
organizationId <- getOrganizationIdFutureUUID(contexts)
eServiceUuid <- eServiceId.toFutureUUID
descriptorUuid <- descriptorId.toFutureUUID
catalogItem <- catalogManagementService.getEServiceById(eServiceUuid)
_ <- assertRequesterAllowed(catalogItem.producerId)(organizationId)
descriptor <- assertDescriptorExists(catalogItem, descriptorUuid)
_ <- descriptorCanBeUpdated(descriptor)
organizationId <- getOrganizationIdFutureUUID(contexts)
eServiceUuid <- eServiceId.toFutureUUID
descriptorUuid <- descriptorId.toFutureUUID
catalogItem <- catalogManagementService.getEServiceById(eServiceUuid)
_ <- assertRequesterAllowed(catalogItem.producerId)(organizationId)
descriptor <- assertDescriptorExists(catalogItem, descriptorUuid)
_ <- descriptorCanBeUpdated(descriptor)
_ <- checkDailyCalls(seed.dailyCallsPerConsumer, seed.dailyCallsTotal).toFuture
_ <- checkIfDailyCallsIsGreaterThanExisting(
seed.dailyCallsPerConsumer,
descriptor.dailyCallsPerConsumer
).toFuture
_ <- checkIfDailyCallsIsGreaterThanExisting(seed.dailyCallsTotal, descriptor.dailyCallsTotal).toFuture
updatedEService <- catalogManagementService.updateDescriptor(
eServiceId,
descriptorId,
Expand Down Expand Up @@ -961,4 +999,10 @@ object ProcessApiServiceImpl {
case Some(_) => Future.failed(InterfaceAlreadyExists(descriptor.id))
case None => Future.unit
}

def checkDailyCalls(dailyCallsPerConsumer: Int, dailyCallsTotal: Int): Either[Throwable, Unit] = Either
.cond(dailyCallsPerConsumer <= dailyCallsTotal, (), DailyCallsIncongruent(dailyCallsPerConsumer, dailyCallsTotal))

def checkIfDailyCallsIsGreaterThanExisting(dailyCalls: Int, previousDailyCalls: Int): Either[Throwable, Unit] = Either
.cond(previousDailyCalls <= dailyCalls, (), DailyCallsAreNotGreaterThanBefore(dailyCalls, previousDailyCalls))
}
Original file line number Diff line number Diff line change
Expand Up @@ -149,36 +149,42 @@ object ResponseHandlers extends AkkaResponses {
success: T => Route
)(result: Try[T])(implicit contexts: Seq[(String, String)], logger: LoggerTakingImplicit[ContextFieldsToLog]): Route =
result match {
case Success(s) => success(s)
case Failure(ex: OperationForbidden.type) => forbidden(ex, logMessage)
case Failure(ex: EServiceNotFound) => notFound(ex, logMessage)
case Failure(ex: AttributeNotFound) => badRequest(ex, logMessage)
case Failure(ex: DraftDescriptorAlreadyExists) => badRequest(ex, logMessage)
case Failure(ex) => internalServerError(ex, logMessage)
case Success(s) => success(s)
case Failure(ex: OperationForbidden.type) => forbidden(ex, logMessage)
case Failure(ex: EServiceNotFound) => notFound(ex, logMessage)
case Failure(ex: AttributeNotFound) => badRequest(ex, logMessage)
case Failure(ex: DraftDescriptorAlreadyExists) => badRequest(ex, logMessage)
case Failure(ex: DailyCallsIncongruent) => badRequest(ex, logMessage)
case Failure(ex: DailyCallsAreNotGreaterThanBefore) => badRequest(ex, logMessage)
case Failure(ex) => internalServerError(ex, logMessage)
}

def updateDraftDescriptorResponse[T](logMessage: String)(
success: T => Route
)(result: Try[T])(implicit contexts: Seq[(String, String)], logger: LoggerTakingImplicit[ContextFieldsToLog]): Route =
result match {
case Success(s) => success(s)
case Failure(ex: OperationForbidden.type) => forbidden(ex, logMessage)
case Failure(ex: EServiceNotFound) => notFound(ex, logMessage)
case Failure(ex: EServiceDescriptorNotFound) => notFound(ex, logMessage)
case Failure(ex: NotValidDescriptor) => badRequest(ex, logMessage)
case Failure(ex) => internalServerError(ex, logMessage)
case Success(s) => success(s)
case Failure(ex: OperationForbidden.type) => forbidden(ex, logMessage)
case Failure(ex: EServiceNotFound) => notFound(ex, logMessage)
case Failure(ex: EServiceDescriptorNotFound) => notFound(ex, logMessage)
case Failure(ex: NotValidDescriptor) => badRequest(ex, logMessage)
case Failure(ex: DailyCallsIncongruent) => badRequest(ex, logMessage)
case Failure(ex: DailyCallsAreNotGreaterThanBefore) => badRequest(ex, logMessage)
case Failure(ex) => internalServerError(ex, logMessage)
}

def updateDescriptorResponse[T](logMessage: String)(
success: T => Route
)(result: Try[T])(implicit contexts: Seq[(String, String)], logger: LoggerTakingImplicit[ContextFieldsToLog]): Route =
result match {
case Success(s) => success(s)
case Failure(ex: OperationForbidden.type) => forbidden(ex, logMessage)
case Failure(ex: EServiceNotFound) => notFound(ex, logMessage)
case Failure(ex: EServiceDescriptorNotFound) => notFound(ex, logMessage)
case Failure(ex: NotValidDescriptor) => badRequest(ex, logMessage)
case Failure(ex) => internalServerError(ex, logMessage)
case Success(s) => success(s)
case Failure(ex: OperationForbidden.type) => forbidden(ex, logMessage)
case Failure(ex: EServiceNotFound) => notFound(ex, logMessage)
case Failure(ex: EServiceDescriptorNotFound) => notFound(ex, logMessage)
case Failure(ex: NotValidDescriptor) => badRequest(ex, logMessage)
case Failure(ex: DailyCallsIncongruent) => badRequest(ex, logMessage)
case Failure(ex: DailyCallsAreNotGreaterThanBefore) => badRequest(ex, logMessage)
case Failure(ex) => internalServerError(ex, logMessage)
}

def deleteDraftResponse[T](logMessage: String)(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,16 @@ object CatalogProcessErrors {

final case class InterfaceAlreadyExists(descriptorId: UUID)
extends ComponentError("0022", s"Descriptor ${descriptorId.toString} already has an interface")

final case class DailyCallsIncongruent(dailyCallsPerConsumer: Int, dailyCallsTotal: Int)
extends ComponentError(
"0023",
s"Daily calls per consumer ($dailyCallsPerConsumer) is greater than daily calls ($dailyCallsTotal)"
)

final case class DailyCallsAreNotGreaterThanBefore(dailyCalls: Int, previousDailyCalls: Int)
extends ComponentError(
"0024",
s"Daily calls ($dailyCalls) must be greater than previous daily calls ($previousDailyCalls)"
)
}
Loading
Loading