diff --git a/app/orchestrators/EngagementOrchestrator.scala b/app/orchestrators/EngagementOrchestrator.scala index f38ca552..7869ded0 100644 --- a/app/orchestrators/EngagementOrchestrator.scala +++ b/app/orchestrators/EngagementOrchestrator.scala @@ -1,10 +1,10 @@ package orchestrators import cats.implicits.catsSyntaxOption +import cats.implicits.catsSyntaxOptionId import controllers.error.AppError.CannotReviewReportResponse import controllers.error.AppError.EngagementNotFound import controllers.error.AppError.ReportNotFound -import controllers.error.AppError.ServerError import models.User import models.engagement.EngagementApi import models.engagement.EngagementId @@ -122,7 +122,13 @@ class EngagementOrchestrator( logger.info(s"No engagement review found for report $reportId") None case review :: Nil => Some(review) - case _ => throw ServerError(s"More than one engagement review for report id $reportId") + case engagementReviews => + io.sentry.Sentry.captureException( + new Exception( + s"More than one engagement review for report id $reportId, this is not and expected behavior it should be investigated" + ) + ) + engagementReviews.maxBy(_.creationDate).some } def findEngagementReviews(reportIds: Seq[UUID]): Future[Map[UUID, Option[EngagementReview]]] = diff --git a/conf/db/migration/default/V54__unique_review_constraint.sql b/conf/db/migration/default/V54__unique_review_constraint.sql new file mode 100644 index 00000000..b8a9cd42 --- /dev/null +++ b/conf/db/migration/default/V54__unique_review_constraint.sql @@ -0,0 +1,12 @@ +-- Removing duplicates before applying constraint +DELETE +FROM report_consumer_review r + USING (SELECT rr.report_id, MAX(rr.creation_date) AS max_creation + FROM report_consumer_review rr + GROUP BY rr.report_id + HAVING COUNT(1) > 1) agg +WHERE agg.report_id = r.report_id + AND agg.max_creation <> r.creation_date; + +ALTER TABLE report_consumer_review + ADD CONSTRAINT unique_report_id UNIQUE (report_id); \ No newline at end of file