UNPKG

@publidata/utils-data-manager

Version:

Collection of methods to extract data from publidata

368 lines (325 loc) 8.05 kB
const axios = require("axios"); const { search } = require("@publidata/utils-elasticsearch"); class DataSourcePublidata { constructor(instance) { this.endpoint = `${instance.custom_settings.url.api}/v2/search`; this.riskEndpoint = `${instance.custom_settings.url.api}/v2/risks`; this.accountEndpoint = `${instance.custom_settings.url.api}/v1/accounts`; this.bookingEndpoint = `${instance.custom_settings.url.booking}/v1/slots`; this.instanceId = instance.instance_id; } removeParam(params, key) { const newParams = Object.assign(params); delete newParams[key]; return newParams; } getRisks(params) { return new Promise((resolve, reject) => { search(this.riskEndpoint, params) .then(({ results }) => { const { data } = results; const object = { _source: data, _type: "risk" }; resolve({ data: [object] }); }) .catch(reject); }); } getObject(params) { return new Promise((resolve, reject) => { search(this.endpoint, params) .then(response => { resolve(response); }) .catch(err => reject(err)); }); } getData(params) { return this.getObject(params); } getInstance(params) { return this.getObject({ ...params, instances: [this.instanceId] }); } getPosts(params) { return this.getObject({ types: ["Post"], size: 100, instances: [this.instanceId], order: "settings.weight", ...params }); } getWasteCollections(params) { return this.getObject({ size: 100, types: ["Platform::Services::WasteCollection"], instances: [this.instanceId], ...params }); } getWasteCollection(params) { return this.getObject({ types: ["Platform::Services::WasteCollection"], size: 1, instances: [this.instanceId], ...params }); } getWasteCollectionById(params) { return this.getObject({ types: ["Platform::Services::WasteCollection"], instances: [this.instanceId], ...params }); } getWasteCollectionFacilities(params) { return this.getObject({ size: 1000, types: ["Platform::Facility"], ...params }); } getWasteCollectionFacility(params) { return this.getObject({ types: ["Platform::Facility"], size: 1, ...params }); } getRecyclingBins(params) { const newParams = this.removeParam(params, "garbageType"); return this.getObject(newParams); } getRecyclingBin(params) { const newParams = this.removeParam(params, "garbageType"); return this.getObject(newParams); } getRecyclingCenters(params) { return this.getObject({ size: 100, types: ["Platform::Facility"], facility_types: [71], order: "_geo_distance", ...params }); } getRecyclingCenter(params) { return this.getObject({ types: ["Platform::Facility"], facility_types: [71], order: "_geo_distance", size: 1, ...params }); } getFacilities(params) { return this.getObject({ size: 100000, types: ["Platform::Facility"], ...params }); } getFacilityById(params) { return this.getObject({ types: ["Platform::Facility"], ...params }); } getFacilitiesByType(params) { return this.getObject({ size: 10000, ...params }); } getFacilityByType(params) { return this.getObject({ ...params, size: 1 }); } getFacilityByTypeAndId(params) { const newParams = this.removeParam(params, "facility_types"); return this.getObject({ types: ["Platform::Facility"], ...newParams, size: 1 }); } getServiceFacilityAggregations(params) { return this.getObject({ types: ["Platform::Facility"], facility_status: ["opened"], facility_access: ["accessible"], aggregations: true, results: false, ...params }); } getCanteenMenuServices(params) { return this.getObject({ types: ["Platform::Services::CanteenMenu"], size: 1000, ...params }); } getCanteenMenuById(params) { return this.getObject({ types: ["Platform::Services::CanteenMenu"], size: 1, ...params }); } getMealsByMenuId(params) { return this.getObject({ types: ["CanteenMenus::Meal"], size: 1000, ...params }); } getDocumentsByInstanceId(params) { return this.getObject({ types: ["Media::Document"], size: 100, ...params }); } getDocumentsByIds(params) { return this.getObject({ types: ["Media::Document"], size: 100, ...params }); } getProceduresByIds(params) { return this.getObject({ types: ["Platform::Services::AdministrativeProcedure"], size: 100, ...params }); } getElected(params) { return this.getObject({ types: ["Elect"], instances: [this.instanceId], size: 100, ...params }); } getNews(params) { return this.getObject({ types: ["News"], instances: [this.instanceId], "order[desc]": "release_date", ...params }); } getNewsTotalPages(params) { return this.getObject({ types: ["News"], instances: [this.instanceId], results: false, ...params }).then(({ results }) => { const { hits } = results?.data || {}; const size = Number(params?.size || 10); const pages = hits?.total / size; return { data: Math.ceil(pages) }; }); } getCities(params) { return this.getObject({ size: 1000, types: ["city"], instances: [this.instanceId], select: ["full_name", "postal_codes", "insee_code", "address_count"], ...params }); } getSectors(params) { if (!params?.q) { return Promise.reject(new Error("Missing required parameter: q")); } return this.getObject({ types: ["sector"], ...params }); } getSlots(params) { return new Promise((resolve, reject) => { search(this.bookingEndpoint, params) .then(({ results }) => { const { data: rawData } = results; const data = rawData.map(slot => ({ _source: slot, _type: "slot" })); resolve({ data }); }) .catch(reject); }); } getItems(params) { return new Promise((resolve, reject) => { search(this.endpoint, params) .then(({ results }) => { const { data } = results; resolve({ data }); }) .catch(reject); }); } getAccountById(params) { return new Promise((resolve, reject) => { axios .get(`${this.accountEndpoint}/${params.id}`) .then(response => { const object = { _source: response.data, _type: "account" }; resolve({ data: [object] }); }) .catch(reject); }); } getAlerts(params) { return this.getObject({ types: ["Alert"], states: ["visible"], ...params }); } getAlert(params) { if (!params?.ids?.length) { return Promise.reject( new Error("Missing required parameter: ids[]='ALERT_ID'") ); } return this.getObject({ types: ["Alert"], states: ["visible"], ...params }); } getFacilitiesAlerts(params) { if (!params?.facilities?.length) { return Promise.reject( new Error("Missing required parameter: facilities<Array>") ); } return this.getObject({ types: ["Alert"], states: ["visible"], ...params }); } getWasteCollectionAlerts(params) { if (!params?.services?.length) { return Promise.reject( new Error("Missing required parameter: services<Array>") ); } return this.getObject({ types: ["Alert"], states: ["visible"], ...params }); } } module.exports = DataSourcePublidata;