diff --git a/src/api/opendata/opendata.routes.js b/src/api/opendata/opendata.routes.js index 71c9c594..b91901ed 100644 --- a/src/api/opendata/opendata.routes.js +++ b/src/api/opendata/opendata.routes.js @@ -12,6 +12,7 @@ const FILTERS = { 'fr-esr-annelis-paysage-etablissements': { dataset: 'fr-esr-annelis-paysage-etablissements' }, 'fr-esr-paysage_structures_identifiants': { dataset: 'fr-esr-paysage_structures_identifiants' }, 'fr-esr-paysage_personnes_identifiants': { dataset: 'fr-esr-paysage_personnes_identifiants' }, + 'fr-esr-paysage_structures_websites': { dataset: 'fr-esr-paysage_structures_websites' }, }; router.route('/opendata/:datasetId') diff --git a/src/jobs/index.js b/src/jobs/index.js index a29a99ce..88a2f287 100644 --- a/src/jobs/index.js +++ b/src/jobs/index.js @@ -17,6 +17,7 @@ import { exportFrEsrAnnelisPaysageEtablissements, exportFrEsrStructureIdentifiers, exportFrEsrPersonIdentifiers, + exportFrEsrStructureWebsites, } from './opendata'; import synchronizeAnnuaireCollection from './synchronize/annuaire-collection'; import synchronizeCuriexploreActors from './synchronize/curiexplore-actors'; @@ -41,6 +42,7 @@ agenda.define('export fr-esr-paysage_structures_identifiants', { shouldSaveResul agenda.define('export fr-esr-paysage_personnes_identifiants', { shouldSaveResult: true }, exportFrEsrPersonIdentifiers); agenda.define('export fr-esr-paysage-fonctions-gourvernance', { shouldSaveResult: true }, exportFrEsrPaysageFonctionsGourvernance); agenda.define('export fr-esr-annelis-paysage-etablissements', { shouldSaveResult: true }, exportFrEsrAnnelisPaysageEtablissements); +agenda.define('export fr-esr-paysage_structures_websites', { shouldSaveResult: true }, exportFrEsrStructureWebsites); agenda.define('synchronize fr-esr-referentiel-geographique', { shouldSaveResult: true }, synchronizeFrEsrReferentielGeographique); agenda.define('synchronize curiexplore actors', { shouldSaveResult: true }, synchronizeCuriexploreActors); agenda.define('ask for email revalidation with otp', { shouldSaveResult: true }, askForEmailRevalidation); diff --git a/src/jobs/opendata/fr-esr-paysage_structures_websites.js b/src/jobs/opendata/fr-esr-paysage_structures_websites.js new file mode 100644 index 00000000..2b44164c --- /dev/null +++ b/src/jobs/opendata/fr-esr-paysage_structures_websites.js @@ -0,0 +1,42 @@ +import structuresLightQuery from '../../api/commons/queries/structures.light.query'; +import { client, db } from '../../services/mongo.service'; + +const dataset = 'fr-esr-paysage_structures_websites'; + +export default async function exportFrEsrStructureWebsites() { + const data = await db.collection('weblinks').aggregate([ + { + $lookup: { + from: 'structures', + localField: 'resourceId', + foreignField: 'id', + pipeline: structuresLightQuery, + as: 'structure', + }, + }, + { $set: { structure: { $arrayElemAt: ['$structure', 0] } } }, + ]).toArray(); + const json = data.map(({ structure, ...websites }) => { + if (!structure || !structure.id || !websites || !websites.id) { + return null; + } + const row = { + dataset, + url: websites.url, + internal_id: websites.id, + id_structure_paysage: structure.id, + type: websites.type, + language: websites.language, + }; + return row; + }).filter((row) => row !== null); + + const session = client.startSession(); + await session.withTransaction(async () => { + await db.collection('opendata').deleteMany({ dataset }); + await db.collection('opendata').insertMany(json); + await session.endSession(); + }); + + return { status: 'success', location: `/opendata/${dataset}` }; +} diff --git a/src/jobs/opendata/index.js b/src/jobs/opendata/index.js index a22dba54..607b9c47 100644 --- a/src/jobs/opendata/index.js +++ b/src/jobs/opendata/index.js @@ -2,3 +2,4 @@ export { default as exportFrEsrPaysageFonctionsGourvernance } from './fr-esr-pay export { default as exportFrEsrAnnelisPaysageEtablissements } from './fr-esr-annelis-paysage-etablissements'; export { default as exportFrEsrStructureIdentifiers } from './fr-esr-paysage_structures_identifiants'; export { default as exportFrEsrPersonIdentifiers } from './fr-esr-paysage_personnes_identifiants'; +export { default as exportFrEsrStructureWebsites } from './fr-esr-paysage_structures_websites';