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

[NU-1772] Scenario activities - fragment, comment and attachment improvements #7036

Merged
merged 20 commits into from
Oct 22, 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 @@ -2,11 +2,16 @@ package pl.touk.nussknacker.engine.api

import io.circe.{Decoder, Encoder}

final case class Comment(content: String) extends AnyVal {
final case class Comment private (content: String) extends AnyVal {
override def toString: String = content
}

object Comment {

def from(content: String): Option[Comment] = {
if (content.isEmpty) None else Some(Comment(content))
}

implicit val encoder: Encoder[Comment] = Encoder.encodeString.contramap(_.content)
implicit val decoder: Decoder[Comment] = Decoder.decodeString.map(Comment.apply)
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,18 @@ import pl.touk.nussknacker.security.Permission
import pl.touk.nussknacker.security.Permission.Permission
import pl.touk.nussknacker.ui.api.description.scenarioActivity.Dtos.ScenarioActivityError.{
NoActivity,
NoAttachment,
NoComment,
NoPermission,
NoScenario
}
import pl.touk.nussknacker.ui.api.description.scenarioActivity.Dtos._
import pl.touk.nussknacker.ui.api.description.scenarioActivity.{Dtos, Endpoints}
import pl.touk.nussknacker.ui.process.ProcessService.GetScenarioWithDetailsOptions
import pl.touk.nussknacker.ui.process.deployment.DeploymentManagerDispatcher
import pl.touk.nussknacker.ui.process.repository.DBIOActionRunner
import pl.touk.nussknacker.ui.process.repository.activities.ScenarioActivityRepository
import pl.touk.nussknacker.ui.process.repository.activities.ScenarioActivityRepository.DeleteAttachmentError
import pl.touk.nussknacker.ui.process.{ProcessService, ScenarioAttachmentService}
import pl.touk.nussknacker.ui.security.api.{AuthManager, LoggedUser}
import pl.touk.nussknacker.ui.server.HeadersSupport.ContentDisposition
Expand Down Expand Up @@ -96,6 +99,18 @@ class ScenarioActivityApiHttpService(
}
}

expose {
endpoints.deleteAttachmentEndpoint
.serverSecurityLogic(authorizeKnownUser[ScenarioActivityError])
.serverLogicEitherT { implicit loggedUser => request: DeleteAttachmentRequest =>
for {
scenarioId <- getScenarioIdByName(request.scenarioName)
_ <- isAuthorized(scenarioId, Permission.Write)
_ <- markAttachmentAsDeleted(request, scenarioId)
} yield ()
}
}

expose {
endpoints.downloadAttachmentEndpoint
.serverSecurityLogic(authorizeKnownUser[ScenarioActivityError])
Expand Down Expand Up @@ -140,7 +155,14 @@ class ScenarioActivityApiHttpService(
for {
scenarioId <- getScenarioIdByName(scenarioName)
_ <- isAuthorized(scenarioId, Permission.Read)
metadata = ScenarioActivitiesMetadata.default
scenarioWithDetails <- EitherT.right(
scenarioService.getLatestProcessWithDetails(
ProcessIdWithName(scenarioId, scenarioName),
GetScenarioWithDetailsOptions.detailsOnly
)
)
scenarioType = if (scenarioWithDetails.isFragment) ScenarioType.Fragment else ScenarioType.Scenario
metadata = ScenarioActivitiesMetadata.default(scenarioType)
} yield metadata
}
}
Expand Down Expand Up @@ -246,21 +268,19 @@ class ScenarioActivityApiHttpService(
} yield sortedResult
}

private def toDto(scenarioComment: ScenarioComment): Dtos.ScenarioActivityComment = {
scenarioComment match {
case ScenarioComment.Available(comment, lastModifiedByUserName, lastModifiedAt) =>
Dtos.ScenarioActivityComment(
content = Dtos.ScenarioActivityCommentContent.Available(comment),
lastModifiedBy = lastModifiedByUserName.value,
lastModifiedAt = lastModifiedAt,
)
case ScenarioComment.Deleted(deletedByUserName, deletedAt) =>
Dtos.ScenarioActivityComment(
content = Dtos.ScenarioActivityCommentContent.Deleted,
lastModifiedBy = deletedByUserName.value,
lastModifiedAt = deletedAt,
)
}
private def toDto(scenarioComment: ScenarioComment): Dtos.ScenarioActivityComment = scenarioComment match {
case ScenarioComment.WithContent(comment, _, _) =>
Dtos.ScenarioActivityComment(
content = Dtos.ScenarioActivityCommentContent.Available(comment),
lastModifiedBy = scenarioComment.lastModifiedByUserName.value,
lastModifiedAt = scenarioComment.lastModifiedAt,
)
case ScenarioComment.WithoutContent(_, _) =>
Dtos.ScenarioActivityComment(
content = Dtos.ScenarioActivityCommentContent.NotAvailable,
lastModifiedBy = scenarioComment.lastModifiedByUserName.value,
lastModifiedAt = scenarioComment.lastModifiedAt,
)
}

private def toDto(attachment: ScenarioAttachment): Dtos.ScenarioActivityAttachment = {
Expand Down Expand Up @@ -558,6 +578,15 @@ class ScenarioActivityApiHttpService(
)
}

private def markAttachmentAsDeleted(request: DeleteAttachmentRequest, scenarioId: ProcessId)(
implicit loggedUser: LoggedUser
): EitherT[Future, ScenarioActivityError, Unit] =
EitherT(
dbioActionRunner.run(
scenarioActivityRepository.markAttachmentAsDeleted(scenarioId, request.attachmentId)
)
).leftMap { case DeleteAttachmentError.CouldNotDeleteAttachment => NoAttachment(request.attachmentId) }

private def buildResponse(maybeAttachment: Option[(String, Array[Byte])]): GetAttachmentResponse =
maybeAttachment match {
case Some((fileName, content)) =>
Expand Down
Loading
Loading