diff --git a/packages/api/src/business/check-prolongations.ts b/packages/api/src/business/check-prolongations.ts index f649597231c150f5882c7b67feb4077be7804b8a..8f378675dcc3fd1b5da6e529837e824d795da4f0 100644 --- a/packages/api/src/business/check-prolongations.ts +++ b/packages/api/src/business/check-prolongations.ts @@ -141,40 +141,46 @@ const checkOneProlongation = (pool: Pool, demarcheId: DemarcheId): Effect.Effect const titreIdVideError = "Pas d'id de titre" as const const upsertFailError = "Échec de mise à jour de l'étape" as const -const titreGetError = "Impossible de fetch le titre" as const -const titreGetIsNullError = "Titre introuvable" as const -const etapeIntrouvableError = "Étape introuvable dans le titre" as const -const demarcheListIsEmptyError = "Liste des démarches héritables vide" as const +const titreGetError = 'Impossible de fetch le titre' as const +const titreGetIsNullError = 'Titre introuvable' as const +const etapeIntrouvableError = 'Étape introuvable dans le titre' as const +const demarcheListIsEmptyError = 'Liste des démarches héritables vide' as const type FixProlongationErrors = typeof titreIdVideError | typeof upsertFailError | typeof titreGetError | typeof titreGetIsNullError | typeof demarcheListIsEmptyError | typeof etapeIntrouvableError const fixProlongation = (pool: Pool, checkedProlongation: CheckedProlongation): Effect.Effect<null, CaminoError<FixProlongationErrors>> => { return Effect.Do.pipe( - Effect.bind('titre', () => pipe( - Effect.succeed(checkedProlongation.titre_id), - Effect.filterOrFail( - (titreId) => isNotNullNorUndefined(titreId), - () => ({ message: titreIdVideError }) - ), - Effect.flatMap((titreId) => Effect.tryPromise({ - try: () => getTitre(pool, userSuper, titreId), - catch: (error) => ({ message: titreGetError, extra: error }) - })), - Effect.filterOrFail( - (titre): titre is NonNullable<typeof titre> => isNotNullNorUndefined(titre), - (error) => ({ message: titreGetIsNullError, extra: error }) + Effect.bind('titre', () => + pipe( + Effect.succeed(checkedProlongation.titre_id), + Effect.filterOrFail( + titreId => isNotNullNorUndefined(titreId), + () => ({ message: titreIdVideError }) + ), + Effect.flatMap(titreId => + Effect.tryPromise({ + try: () => getTitre(pool, userSuper, titreId), + catch: error => ({ message: titreGetError, extra: error }), + }) + ), + Effect.filterOrFail( + (titre): titre is NonNullable<typeof titre> => isNotNullNorUndefined(titre), + error => ({ message: titreGetIsNullError, extra: error }) + ) ) - )), - Effect.bind('etape', ({ titre }) => pipe( - Effect.succeed(titre.demarches.find(({ id }) => id === checkedProlongation.demarche_id)), - Effect.filterOrFail( - (demarche): demarche is NonNullable<typeof demarche> => isNotNullNorUndefined(checkedProlongation.demarche_id) && isNotNullNorUndefined(demarche), - () => ({ message: etapeIntrouvableError, extra: 'Démarche introuvable' }) - ), - Effect.map((demarche) => demarche.etapes.find(({ id }) => id === checkedProlongation.etape_id)), - Effect.filterOrFail( - (etape): etape is NonNullable<typeof etape> => isNotNullNorUndefined(checkedProlongation.etape_id) && isNotNullNorUndefined(etape), - () => ({ message: etapeIntrouvableError, extra: 'Étape introuvable' }) + ), + Effect.bind('etape', ({ titre }) => + pipe( + Effect.succeed(titre.demarches.find(({ id }) => id === checkedProlongation.demarche_id)), + Effect.filterOrFail( + (demarche): demarche is NonNullable<typeof demarche> => isNotNullNorUndefined(checkedProlongation.demarche_id) && isNotNullNorUndefined(demarche), + () => ({ message: etapeIntrouvableError, extra: 'Démarche introuvable' }) + ), + Effect.map(demarche => demarche.etapes.find(({ id }) => id === checkedProlongation.etape_id)), + Effect.filterOrFail( + (etape): etape is NonNullable<typeof etape> => isNotNullNorUndefined(checkedProlongation.etape_id) && isNotNullNorUndefined(etape), + () => ({ message: etapeIntrouvableError, extra: 'Étape introuvable' }) + ) ) - )), + ), Effect.bind('propsHeritees', ({ titre }) => Effect.Do.pipe( Effect.bind('demarches', () => { @@ -201,7 +207,7 @@ const fixProlongation = (pool: Pool, checkedProlongation: CheckedProlongation): Effect.flatMap(({ titre, etape, propsHeritees }) => Effect.tryPromise({ try: () => { - console.log(`Trying to fix http://localhost:4180/etapes/${etape.id}`) + console.info(`Trying to fix http://localhost:4180/etapes/${etape.id}`) return titreEtapeUpsert( { id: etape.id, @@ -210,9 +216,18 @@ const fixProlongation = (pool: Pool, checkedProlongation: CheckedProlongation): substances: 'fondamentale' in etape && isNotNullNorUndefinedNorEmpty(etape.fondamentale.substances) ? etape.fondamentale.substances : propsHeritees.substances, titulaireIds: 'fondamentale' in etape && isNotNullNorUndefinedNorEmpty(etape.fondamentale.titulaireIds) ? etape.fondamentale.titulaireIds : propsHeritees.titulaireIds, surface: 'fondamentale' in etape && isNotNullNorUndefined(etape.fondamentale.perimetre?.surface) ? etape.fondamentale.perimetre.surface : propsHeritees.perimetre?.surface, - geojson4326Perimetre: 'fondamentale' in etape && isNotNullNorUndefined(etape.fondamentale.perimetre?.geojson4326_perimetre) ? etape.fondamentale.perimetre.geojson4326_perimetre : propsHeritees.perimetre?.geojson4326_perimetre, - geojsonOriginePerimetre: 'fondamentale' in etape && isNotNullNorUndefined(etape.fondamentale.perimetre?.geojson_origine_perimetre) ? etape.fondamentale.perimetre.geojson_origine_perimetre : propsHeritees.perimetre?.geojson_origine_perimetre, - geojsonOrigineGeoSystemeId: 'fondamentale' in etape && isNotNullNorUndefined(etape.fondamentale.perimetre?.geojson_origine_geo_systeme_id) ? etape.fondamentale.perimetre.geojson_origine_geo_systeme_id : propsHeritees.perimetre?.geojson_origine_geo_systeme_id, + geojson4326Perimetre: + 'fondamentale' in etape && isNotNullNorUndefined(etape.fondamentale.perimetre?.geojson4326_perimetre) + ? etape.fondamentale.perimetre.geojson4326_perimetre + : propsHeritees.perimetre?.geojson4326_perimetre, + geojsonOriginePerimetre: + 'fondamentale' in etape && isNotNullNorUndefined(etape.fondamentale.perimetre?.geojson_origine_perimetre) + ? etape.fondamentale.perimetre.geojson_origine_perimetre + : propsHeritees.perimetre?.geojson_origine_perimetre, + geojsonOrigineGeoSystemeId: + 'fondamentale' in etape && isNotNullNorUndefined(etape.fondamentale.perimetre?.geojson_origine_geo_systeme_id) + ? etape.fondamentale.perimetre.geojson_origine_geo_systeme_id + : propsHeritees.perimetre?.geojson_origine_geo_systeme_id, }, userSuper, titre.id @@ -222,8 +237,8 @@ const fixProlongation = (pool: Pool, checkedProlongation: CheckedProlongation): }) ), Effect.map(() => null), - Effect.catchAll((error) => { - console.log(error) + Effect.catchAll(error => { + console.info(error) return Effect.succeed(null) }) ) @@ -256,7 +271,7 @@ export const checkProlongations = (pool: Pool): Effect.Effect<CheckedProlongatio // const link = isNotNullNorUndefined(checkedProlongation.titre_slug) // ? `http://localhost:4180/titres/${checkedProlongation.titre_slug}` // : `http://localhost:4180/demarches/${checkedProlongation.demarche_id}` - // console.log(`${link} : ${checkedProlongation.errors.join(' | ')}`) + // console.info(`${link} : ${checkedProlongation.errors.join(' | ')}`) // } return [...acc, checkedProlongation] @@ -271,7 +286,7 @@ export const checkProlongations = (pool: Pool): Effect.Effect<CheckedProlongatio const prolongationsAvecDureeManquante = prolongations.filter(({ errors }) => errors.some(error => error.includes('la durée est obligatoire'))) const prolongationsParType = prolongationsEnVrac.reduce<{ [key: string]: number }>((acc, prolongation) => { - const domaine = isNotNullNorUndefined(prolongation.titre_slug) ? `${prolongation.titre_slug.slice(2, 4)}${ prolongation.titre_slug.slice(0, 1)}` : 'inconnu' + const domaine = isNotNullNorUndefined(prolongation.titre_slug) ? `${prolongation.titre_slug.slice(2, 4)}${prolongation.titre_slug.slice(0, 1)}` : 'inconnu' if (isNullOrUndefined(acc[domaine])) { acc[domaine] = 1 } else { @@ -280,9 +295,9 @@ export const checkProlongations = (pool: Pool): Effect.Effect<CheckedProlongatio return acc }, {}) - console.log(`Prolongations en vrac : ${prolongationsEnVrac.length} (${toPercentage(prolongationsEnVrac.length / prolongations.length)})`) - console.log(JSON.stringify(prolongationsParType, null, 2)) - console.log(`Prolongations sans durée : ${prolongationsAvecDureeManquante.length} (${toPercentage(prolongationsAvecDureeManquante.length / prolongationsEnVrac.length)})`) + console.info(`Prolongations en vrac : ${prolongationsEnVrac.length} (${toPercentage(prolongationsEnVrac.length / prolongations.length)})`) + console.info(JSON.stringify(prolongationsParType, null, 2)) + console.info(`Prolongations sans durée : ${prolongationsAvecDureeManquante.length} (${toPercentage(prolongationsAvecDureeManquante.length / prolongationsEnVrac.length)})`) }) ) }