Skip to content

Commit

Permalink
feat(lba-2299): suppression collection siretdiffusibles (#1694)
Browse files Browse the repository at this point in the history
* feat: suppression code afférent à la collection siretdiffusiblestatuses

* feat: migration suppression siretdiffusiblestatuses
  • Loading branch information
alanlr authored Dec 12, 2024
1 parent 3457463 commit dca948a
Show file tree
Hide file tree
Showing 7 changed files with 11 additions and 237 deletions.
61 changes: 1 addition & 60 deletions server/src/common/apis/apiEntreprise/apiEntreprise.client.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import { ObjectId } from "mongodb"
import { EDiffusibleStatus } from "shared/constants/diffusibleStatus"
import { BusinessErrorCodes } from "shared/constants/errorCodes"
import { ISiretDiffusibleStatus, ZAdresseV3 } from "shared/models"
import { ZAdresseV3 } from "shared/models"
import { IEtablissementGouvData } from "shared/models/cacheInfosSiret.model"

import { getDbCollection } from "@/common/utils/mongodbUtils"
import { sentryCaptureException } from "@/common/utils/sentryUtils"
import config from "@/config"

Expand Down Expand Up @@ -47,60 +45,3 @@ export async function getEtablissementFromGouvSafe(siret: string): Promise<IEtab
throw error
}
}

const saveSiretDiffusionStatus = async (siret, diffusionStatus) => {
try {
const now = new Date()
const obj: ISiretDiffusibleStatus = {
_id: new ObjectId(),
siret,
status_diffusion: diffusionStatus,
created_at: now,
last_update_at: now,
}
await getDbCollection("siretdiffusiblestatuses").insertOne(obj)
} catch (err) {
// non blocking error
sentryCaptureException(err)
}
}

/**
* @description Get diffusion status from the ENTREPRISE API for a given SIRET
*/
export async function getEtablissementDiffusionStatus(siret: string): Promise<string> {
try {
if (config.entreprise.simulateError) {
throw new Error("API entreprise : simulation d'erreur")
}

const siretDiffusibleStatus = await getDbCollection("siretdiffusiblestatuses").findOne({ siret })
if (siretDiffusibleStatus) {
return siretDiffusibleStatus.status_diffusion
}

const { data } = await client.get<IEtablissementGouvData>(`${config.entreprise.baseUrl}/sirene/etablissements/diffusibles/${encodeURIComponent(siret)}/adresse`, {
params: apiParams,
})
await saveSiretDiffusionStatus(siret, data.data.status_diffusion)

return data.data.status_diffusion
} catch (error: any) {
if (error?.response?.status === 404) {
await saveSiretDiffusionStatus(siret, EDiffusibleStatus.NOT_FOUND)
return EDiffusibleStatus.NOT_FOUND
}
if (error?.response?.status === 451) {
await saveSiretDiffusionStatus(siret, EDiffusibleStatus.UNAVAILABLE)
return EDiffusibleStatus.UNAVAILABLE
}
if (error?.response?.status === 429 || error?.response?.status === 504) {
return "quota"
}
if (error?.code === "ECONNABORTED") {
return "quota"
}
sentryCaptureException(error)
throw error
}
}
133 changes: 0 additions & 133 deletions server/src/jobs/database/fixDiffusibleCompanies.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { Db } from "mongodb"

import { logger } from "@/common/logger"

export const up = async (db: Db) => {
await db.collection("siretdiffusiblestatuses").drop()

logger.info("20241209090516-remove_siretdiffusible_statuses")
}
20 changes: 1 addition & 19 deletions server/src/services/etablissement.service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { setTimeout } from "timers/promises"

import { badRequest, internal } from "@hapi/boom"
import { captureException } from "@sentry/node"
import { Filter as MongoDBFilter, ObjectId } from "mongodb"
Expand All @@ -17,7 +15,6 @@ import {
ZCfaReferentielData,
} from "shared"
import { CFA, ENTREPRISE, RECRUITER_STATUS } from "shared/constants"
import { EDiffusibleStatus } from "shared/constants/diffusibleStatus"
import { BusinessErrorCodes } from "shared/constants/errorCodes"
import { OPCOS_LABEL, VALIDATION_UTILISATEUR } from "shared/constants/recruteur"
import { IEtablissementGouvData } from "shared/models/cacheInfosSiret.model"
Expand All @@ -27,7 +24,7 @@ import { AccessEntityType, AccessStatus } from "shared/models/roleManagement.mod
import { IUserWithAccount } from "shared/models/userWithAccount.model"
import { getLastStatusEvent } from "shared/utils/getLastStatusEvent"

import { getEtablissementDiffusionStatus, getEtablissementFromGouvSafe } from "@/common/apis/apiEntreprise/apiEntreprise.client"
import { getEtablissementFromGouvSafe } from "@/common/apis/apiEntreprise/apiEntreprise.client"
import { FCGetOpcoInfos } from "@/common/apis/franceCompetences/franceCompetencesClient"
import { asyncForEach } from "@/common/utils/asyncUtils"
import { getStaticFilePath } from "@/common/utils/getStaticFilePath"
Expand Down Expand Up @@ -139,21 +136,6 @@ const getIdcc = async (siret: string): Promise<ISIRET2IDCC | null> => {
}
}

const MAX_RETRY = 100
const DELAY = 100

export const getDiffusionStatus = async (siret: string, count = 1) => {
const isDiffusible = await getEtablissementDiffusionStatus(siret)
if (isDiffusible === "quota") {
if (count > MAX_RETRY) throw internal(`Api entreprise or cache entreprise not availabe. Tried ${MAX_RETRY} times`)
await setTimeout(DELAY, "result")
return await getDiffusionStatus(siret, count++)
}
return isDiffusible
}

export const checkIsDiffusible = async (siret: string) => (await getDiffusionStatus(siret)) === EDiffusibleStatus.DIFFUSIBLE

/**
* @description Get the establishment information from the ENTREPRISE API for a given SIRET
* Throw an error if the data is private
Expand Down
1 change: 0 additions & 1 deletion shared/models/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ export * from "./referentielOpco.model"
export * from "./roleManagement.model"
export * from "./rome.model"
export * from "./session.model"
export * from "./siretDiffusibleStatus.model"
export * from "./trafficSources.model"
export * from "./unsubscribeOF.model"
export * from "./unsubscribedRecruteurLba.model"
Expand Down
2 changes: 0 additions & 2 deletions shared/models/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ import roleManagementModel from "./roleManagement.model"
import roleManagement360Model from "./roleManagement360.model"
import romeModel from "./rome.model"
import sessionModel from "./session.model"
import siretDiffusibleStatusModel from "./siretDiffusibleStatus.model"
import sitemapModel from "./sitemap.model"
import trafficSourcesModel from "./trafficSources.model"
import unsubscribedLbaCompanyModel from "./unsubscribedRecruteurLba.model"
Expand Down Expand Up @@ -83,7 +82,6 @@ const modelDescriptorMap = {
[roleManagementModel.collectionName]: roleManagementModel,
[roleManagement360Model.collectionName]: roleManagement360Model,
[sessionModel.collectionName]: sessionModel,
[siretDiffusibleStatusModel.collectionName]: siretDiffusibleStatusModel,
[unsubscribedLbaCompanyModel.collectionName]: unsubscribedLbaCompanyModel,
[unsubscribeOFModel.collectionName]: unsubscribeOFModel,
[userModel.collectionName]: userModel,
Expand Down
22 changes: 0 additions & 22 deletions shared/models/siretDiffusibleStatus.model.ts

This file was deleted.

0 comments on commit dca948a

Please sign in to comment.