Skip to content

Commit

Permalink
TRELLO-2895: fix double consumer review
Browse files Browse the repository at this point in the history
  • Loading branch information
ssedoudbgouv committed Feb 3, 2025
1 parent 5bd9e48 commit 14046a1
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
10 changes: 8 additions & 2 deletions app/orchestrators/EngagementOrchestrator.scala
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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]]] =
Expand Down
12 changes: 12 additions & 0 deletions conf/db/migration/default/V54__unique_review_constraint.sql
Original file line number Diff line number Diff line change
@@ -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);

0 comments on commit 14046a1

Please sign in to comment.