Skip to content

Commit

Permalink
Merge pull request #1834 from betagouv/master
Browse files Browse the repository at this point in the history
MEP
  • Loading branch information
ssedoudbgouv authored Jan 13, 2025
2 parents 684d2bc + a8cd5da commit cde72f4
Show file tree
Hide file tree
Showing 9 changed files with 43 additions and 24 deletions.
2 changes: 1 addition & 1 deletion app/orchestrators/AlbertOrchestrator.scala
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class AlbertOrchestrator(
Future.successful(None)
case _ =>
albertService
.findProblems(companyId, descriptions)
.findProblems(descriptions)
.recover { err =>
logger.error(s"Didn't get a result from Albert for company $companyId", err)
None
Expand Down
8 changes: 4 additions & 4 deletions app/orchestrators/CompanyAccessOrchestrator.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ package orchestrators
import controllers.error.AppError.ActivationCodeAlreadyUsed
import controllers.error.AppError.CompanyActivationCodeExpired
import controllers.error.AppError.CompanyActivationSiretOrCodeInvalid
import controllers.error.AppError.ServerError
import controllers.error.AppError.TooMuchCompanyActivationAttempts
import models.AccessToken
import models.User
import models.access.ActivationLinkRequest

import java.time.OffsetDateTime.now
import cats.implicits.catsSyntaxApplicativeId
import cats.implicits.catsSyntaxOption
import cats.implicits.catsSyntaxOptionId
import cats.implicits.toTraverseOps
import models.UserRole.Admin
import models.UserRole.DGAL
Expand Down Expand Up @@ -146,9 +147,8 @@ class CompanyAccessOrchestrator(
"multiple_head_offices",
s"Multiple head offices for siret ${company.siret} company data ids ${companies.map(_.id)} "
)
Future.failed(
ServerError(s"Unexpected error when fetching head office for company with siret ${company.siret}")
)
// multiple_head_offices error should be investigated, but for now we are considering that last created company is the head office.
companies.maxBy(_.creationDate.toEpochSecond).some.pure[Future]
}

private def getHeadOfficeAccess(
Expand Down
19 changes: 19 additions & 0 deletions app/orchestrators/ReportOrchestrator.scala
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import models.report.reportmetadata.ReportWithMetadataAndBookmark
import models.token.TokenKind.CompanyInit
import models.website.Website
import orchestrators.ReportOrchestrator.ReportCompanyChangeThresholdInDays
import orchestrators.ReportOrchestrator.isGouvWebsite
import play.api.Logger
import play.api.i18n.MessagesApi
import play.api.libs.json.Json
Expand Down Expand Up @@ -65,6 +66,7 @@ import java.time.ZoneOffset
import java.time.temporal.TemporalAmount
import java.util.UUID
import java.util.concurrent.TimeUnit
import java.util.regex.Pattern
import scala.concurrent.ExecutionContext
import scala.concurrent.Future
import scala.concurrent.duration._
Expand Down Expand Up @@ -303,6 +305,7 @@ class ReportOrchestrator(
Future.failed(AppError.CannotReportPublicAdministration)
case _ => Future.unit
}
_ <- isGouvWebsite(reportDraft)
_ <- reportDraft.companySiret match {
case Some(siret) =>
// Try to check if siret exist in signal conso database
Expand Down Expand Up @@ -1112,4 +1115,20 @@ class ReportOrchestrator(

object ReportOrchestrator {
val ReportCompanyChangeThresholdInDays: Long = 90L

def isGouvWebsite(reportDraft: ReportDraft): Future[Unit] = {

def isAGouvWebsite(input: URL): Boolean = {
val regex = "^(?!.*\\.gouv\\.fr(?:[\\/\\?#]|$)).*$"
val pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE)
!pattern.matcher(input.value).matches()
}

reportDraft.websiteURL match {
case Some(websiteURL) if isAGouvWebsite(websiteURL) =>
Future.failed(AppError.CannotReportPublicAdministration)
case _ => Future.unit
}
}

}
1 change: 0 additions & 1 deletion app/services/AlbertService.scala
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,6 @@ class AlbertService(albertConfiguration: AlbertConfiguration)(implicit ec: Execu
}

def findProblems(
companyId: UUID,
selectedCompanyReportsDescriptions: Seq[String]
): Future[Option[AlbertProblemsResult]] = {
val prompt = AlbertPrompts.findProblems(selectedCompanyReportsDescriptions, maxPromptLength = 10000)
Expand Down
8 changes: 2 additions & 6 deletions app/tasks/company/CompanySyncService.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,11 @@ import sttp.model.Header
import utils.SIREN
import utils.SIRET

import java.time.OffsetDateTime
import java.time.format.DateTimeFormatter
import scala.concurrent.ExecutionContext
import scala.concurrent.Future

trait CompanySyncServiceInterface {
def syncCompanies(companies: Seq[Company], lastUpdated: OffsetDateTime): Future[List[CompanySearchResult]]
def syncCompanies(companies: Seq[Company]): Future[List[CompanySearchResult]]
def companyBySiret(siret: SIRET): Future[Option[CompanySearchResult]]
def companyBySiren(siren: SIREN, onlyHeadOffice: Boolean): Future[List[CompanySearchResult]]
def companiesBySirets(sirets: List[SIRET]): Future[List[CompanySearchResult]]
Expand All @@ -37,16 +35,14 @@ class CompanySyncService(
val SirenSearchEndpoint = "/api/companies/siren/search"

override def syncCompanies(
companies: Seq[Company],
lastUpdated: OffsetDateTime
companies: Seq[Company]
): Future[List[CompanySearchResult]] = {

val request = basicRequest
.headers(Header("X-Api-Key", companyUpdateConfiguration.etablissementApiKey))
.post(
uri"${companyUpdateConfiguration.etablissementApiUrl}"
.withWholePath(SearchEndpoint)
.addParam("lastUpdated", Some(lastUpdated.format(DateTimeFormatter.ISO_OFFSET_DATE_TIME)))
)
.body(companies.map(_.siret))
.response(asJson[List[CompanySearchResult]])
Expand Down
2 changes: 1 addition & 1 deletion app/tasks/company/CompanyUpdateTask.scala
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class CompanyUpdateTask(
"company_update_task_item",
s"Syncing ${companies.size} companies"
)
companySyncService.syncCompanies(companies, companySync.lastUpdated)
companySyncService.syncCompanies(companies)
}
.mapAsync(1)(updateSignalConsoCompaniesBySiret)
.map(_.flatMap(_.lastUpdated).maxOption.getOrElse(companySync.lastUpdated))
Expand Down
12 changes: 11 additions & 1 deletion test/orchestrators/ReportOrchestratorTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import org.specs2.mutable.Specification
import utils.AppSpec
import utils.Fixtures
import utils.TestApp

import utils.URL
import java.time.LocalDate
import java.time.OffsetDateTime
import java.time.ZoneOffset
Expand Down Expand Up @@ -242,6 +242,16 @@ class ReportOrchestratorTest(implicit ee: ExecutionEnv) extends Specification wi
res must throwA[CannotReportPublicAdministration.type].await
}

"fail when reporting gouv website " in {
val draftReportOnPublicCompany = aDraftReport.copy(
companyActivityCode = Some("90.10"),
websiteURL = Some(URL("http://totot.gouv.fr?titi=tr"))
)
val res =
components.reportOrchestrator.validateAndCreateReport(draftReportOnPublicCompany, ConsumerIp("0.0.0.0"))
res must throwA[CannotReportPublicAdministration.type].await
}

"succeed when reporting private company" in {
val draftReportOnPrivateCompany = aDraftReport.copy(
companyActivityCode = Some("90.10")
Expand Down
12 changes: 4 additions & 8 deletions test/tasks/company/CompanyUpdateTaskSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,7 @@ class CompanyUpdateTaskSpec(implicit ee: ExecutionEnv)
)

serviceMock.syncCompanies(
Seq(company),
companySync.lastUpdated
Seq(company)
) returns Future
.successful(
List(companySearchResult)
Expand Down Expand Up @@ -114,8 +113,7 @@ class CompanyUpdateTaskSpec(implicit ee: ExecutionEnv)
)

serviceMock.syncCompanies(
Seq(company),
CompanySync.default.lastUpdated
Seq(company)
) returns Future
.successful(
List(companySearchResult)
Expand Down Expand Up @@ -163,8 +161,7 @@ class CompanyUpdateTaskSpec(implicit ee: ExecutionEnv)
)

serviceMock.syncCompanies(
Seq(company),
companySync.lastUpdated
Seq(company)
) returns Future
.successful(
List(companySearchResult)
Expand Down Expand Up @@ -199,8 +196,7 @@ class CompanyUpdateTaskSpec(implicit ee: ExecutionEnv)
val companySync = CompanySync(UUID.randomUUID(), now.plus(1, ChronoUnit.DAYS))

serviceMock.syncCompanies(
org.mockito.ArgumentMatchers.eq(Seq(company)),
any[OffsetDateTime]
org.mockito.ArgumentMatchers.eq(Seq(company))
) returns Future
.successful(
List.empty
Expand Down
3 changes: 1 addition & 2 deletions test/utils/CompanySyncServiceMock.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@ import models.company.Company
import tasks.company.CompanySearchResult
import tasks.company.CompanySyncServiceInterface

import java.time.OffsetDateTime
import scala.concurrent.Future

class CompanySyncServiceMock extends CompanySyncServiceInterface {
override def syncCompanies(companies: Seq[Company], lastUpdated: OffsetDateTime): Future[List[CompanySearchResult]] =
override def syncCompanies(companies: Seq[Company]): Future[List[CompanySearchResult]] =
Future.successful(List.empty[CompanySearchResult])

override def companyBySiret(siret: SIRET): Future[Option[CompanySearchResult]] = Future.successful(
Expand Down

0 comments on commit cde72f4

Please sign in to comment.