From 5a6ee7af8b7a7e56143eb406f5e25630bf7125bc Mon Sep 17 00:00:00 2001 From: To-om Date: Tue, 27 Oct 2020 13:43:17 +0100 Subject: [PATCH] #1557 Use the case returned by mergeInCase to prevent case retrieving for each alert --- .../thehive/controllers/v0/AlertCtrl.scala | 33 +++++++++++-------- 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/thehive/app/org/thp/thehive/controllers/v0/AlertCtrl.scala b/thehive/app/org/thp/thehive/controllers/v0/AlertCtrl.scala index daed87794e..37662eaaa4 100644 --- a/thehive/app/org/thp/thehive/controllers/v0/AlertCtrl.scala +++ b/thehive/app/org/thp/thehive/controllers/v0/AlertCtrl.scala @@ -190,21 +190,26 @@ class AlertCtrl @Inject() ( .authTransaction(db) { implicit request => implicit graph => val alertIds: Seq[String] = request.body("alertIds") val caseId: String = request.body("caseId") - for { - _ <- alertIds.toTry { alertId => - caseSrv.get(EntityIdOrName(caseId)) - .can(Permissions.manageCase) - .getOrFail("Case") - .flatMap(`case` => - alertSrv - .get(EntityIdOrName(alertId)) - .can(Permissions.manageAlert) - .getOrFail("Alert") - .flatMap(alertSrv.mergeInCase(_, `case`)) - ) + + val destinationCase = caseSrv + .get(EntityIdOrName(caseId)) + .can(Permissions.manageCase) + .getOrFail("Case") + + alertIds + .foldLeft(destinationCase) { (caseTry, alertId) => + for { + case0 <- caseTry + alert <- + alertSrv + .get(EntityIdOrName(alertId)) + .can(Permissions.manageAlert) + .getOrFail("Alert") + updatedCase <- alertSrv.mergeInCase(alert, case0) + } yield updatedCase } - richCase <- caseSrv.get(EntityIdOrName(caseId)).richCase.getOrFail("Case") - } yield Results.Ok(richCase.toJson) + .flatMap(c => caseSrv.get(c._id).richCase.getOrFail("Case")) + .map(rc => Results.Ok(rc.toJson)) } def markAsRead(alertId: String): Action[AnyContent] =