Skip to content

Commit

Permalink
TRELLO-2812 : fix engagement deletion (#1811)
Browse files Browse the repository at this point in the history
* TRELLO-2812 : fix engagement deletion
  • Loading branch information
ssedoudbgouv authored Dec 23, 2024
1 parent 7964b85 commit 8b0adf8
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 10 deletions.
3 changes: 2 additions & 1 deletion app/loader/SignalConsoApplicationLoader.scala
Original file line number Diff line number Diff line change
Expand Up @@ -638,7 +638,8 @@ class SignalConsoComponents(
companyAccessRepository,
reportAdminActionOrchestrator,
websiteRepository,
eventRepository
eventRepository,
engagementRepository
)(
actorSystem
)
Expand Down
10 changes: 7 additions & 3 deletions app/models/report/sampledata/SampleDataService.scala
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import play.api.Logging
import repositories.accesstoken.AccessTokenRepositoryInterface
import repositories.company.CompanyRepositoryInterface
import repositories.companyaccess.CompanyAccessRepositoryInterface
import repositories.engagement.EngagementRepositoryInterface
import repositories.event.EventRepositoryInterface
import repositories.report.ReportRepositoryInterface
import repositories.user.UserRepositoryInterface
Expand All @@ -40,7 +41,8 @@ class SampleDataService(
companyAccessRepository: CompanyAccessRepositoryInterface,
reportAdminActionOrchestrator: ReportAdminActionOrchestrator,
websiteRepository: WebsiteRepositoryInterface,
eventRepository: EventRepositoryInterface
eventRepository: EventRepositoryInterface,
engagementRepository: EngagementRepositoryInterface
)(implicit system: ActorSystem)
extends Logging {

Expand Down Expand Up @@ -272,8 +274,10 @@ class SampleDataService(
companyIds = companies.map(c => c.company.id)
reportList <- companyIds.flatTraverse(c => reportRepository.getReports(c))
_ = logger.info(s"Looking for reports link to company user ${predefinedUser.id}, found: ${reportList.size}")
_ <- reportList.traverse(r => reportAdminActionOrchestrator.deleteReport(r.id))
_ <- maybeUser.traverse(user => eventRepository.deleteByUserId(user.id))
_ <- reportList.traverse(r => reportAdminActionOrchestrator.deleteReport(r.id))
_ <- maybeUser.traverse { user =>
engagementRepository.removeByUserId(user.id).flatMap(_ => eventRepository.deleteByUserId(user.id))
}
websites <- websiteRepository.searchByCompaniesId(companies.map(_.company.id))
_ = logger.info(s"Looking for websites link to company user ${predefinedUser.id}, found: ${reportList.size}")
_ <- websites.map(_.id).traverse(websiteRepository.delete)
Expand Down
12 changes: 6 additions & 6 deletions app/orchestrators/ReportAdminActionOrchestrator.scala
Original file line number Diff line number Diff line change
Expand Up @@ -223,14 +223,14 @@ class ReportAdminActionOrchestrator(
}

def deleteReport(
id: UUID
reportId: UUID
) =
for {
_ <- engagementOrchestrator.removeEngagement(id)
_ <- eventRepository.deleteByReportId(id)
_ <- reportFileOrchestrator.removeFromReportId(id)
_ <- reportConsumerReviewOrchestrator.remove(id)
_ <- reportRepository.delete(id)
_ <- engagementOrchestrator.removeEngagement(reportId)
_ <- eventRepository.deleteByReportId(reportId)
_ <- reportFileOrchestrator.removeFromReportId(reportId)
_ <- reportConsumerReviewOrchestrator.remove(reportId)
_ <- reportRepository.delete(reportId)
} yield ()

private def getCompanyWithUsers(report: Report): Future[(Option[Company], Option[List[User]])] = for {
Expand Down
21 changes: 21 additions & 0 deletions app/repositories/engagement/EngagementRepository.scala
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,25 @@ class EngagementRepository(override val dbConfig: DatabaseConfig[JdbcProfile])(i
.filter(_.reportId === reportId)
.delete
)

def removeByUserId(userId: UUID): Future[Int] = {
val idsToDelete = db.run(
table
.join(EventTable.table)
.on { case (engagement, promiseEvent) => engagement.promiseEventId === promiseEvent.id }
.joinLeft(EventTable.table)
.on { case ((engagement, _), resolutionEvent) => engagement.resolutionEventId === resolutionEvent.id }
.filter { case ((_, promiseEvent), resolutionEvent) =>
promiseEvent.userId === userId || resolutionEvent.flatMap(_.userId) === userId
}
.map(_._1._1.id) // Extract IDs of the rows to delete
.result
)

idsToDelete.flatMap { ids =>
db.run(
table.filter(_.id inSet ids).delete // Delete rows with those IDs
)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@ trait EngagementRepositoryInterface extends TypedCRUDRepositoryInterface[Engagem
def uncheck(engagementId: EngagementId): Future[Int]
def remove(reportId: UUID): Future[Int]
def listEngagementsExpiringAt(date: LocalDate): Future[Seq[(((Engagement, Report), Event), Option[Event])]]
def removeByUserId(userId: UUID): Future[Int]
}

0 comments on commit 8b0adf8

Please sign in to comment.