Skip to content

Commit

Permalink
feat: add commune infos endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
MaGOs92 committed Dec 22, 2023
1 parent 4802101 commit 625e00f
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 5 deletions.
8 changes: 5 additions & 3 deletions server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ const next = require('next')
require('dotenv').config()
const mongoClient = require('./utils/mongo-client')
const {routeGuard} = require('./route-guard')
const {apiDepotProxy} = require('./proxy-api-depot')
const {apiMoissonneurProxy} = require('./proxy-api-moissonneur-bal')

function setDemoClient(req, res, next) {
req.demo = 1
Expand All @@ -30,9 +32,9 @@ async function main() {
}))

// Proxy routes are protected by routeGuard
server.use('/api/proxy-api-depot', routeGuard, require('./proxy-api-depot'))
server.use('/api/proxy-api-depot-demo', routeGuard, setDemoClient, require('./proxy-api-depot'))
server.use('/api/proxy-api-moissonneur-bal', routeGuard, require('./proxy-api-moissonneur-bal'))
server.use('/api/proxy-api-depot', routeGuard, apiDepotProxy)
server.use('/api/proxy-api-depot-demo', routeGuard, setDemoClient, apiDepotProxy)
server.use('/api/proxy-api-moissonneur-bal', routeGuard, apiMoissonneurProxy)
server.use('/api/proxy-mes-adresses-api', routeGuard, require('./proxy-mes-adresses-api'))

// Some Partenaire de la charte routes are public, others are protected by routeGuard
Expand Down
11 changes: 11 additions & 0 deletions server/lib/bal-widget/controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,15 @@ BALWidgetRoutes.post("/send-mail", async (req, res) => {
}
});

BALWidgetRoutes.get("/commune/:code", async (req, res) => {
try {
const { code } = req.params;
const response = await BALWidgetService.getCommuneInfos(code);
res.json(response);
} catch (err) {
console.error(err);
res.status(500).json({ error: err.message });
}
});

module.exports = BALWidgetRoutes;
23 changes: 23 additions & 0 deletions server/lib/bal-widget/service.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
const mongoClient = require("../../utils/mongo-client");
const {apiDepotClient} = require("../../proxy-api-depot");
const {apiMoissonneurClient} = require("../../proxy-api-moissonneur-bal");

const NEXT_PUBLIC_API_MES_ADRESSES = process.env.NEXT_PUBLIC_API_MES_ADRESSES || 'https://api-bal.adresse.data.gouv.fr/v1'

const collectionName = "bal-widget";

Expand Down Expand Up @@ -28,7 +32,26 @@ async function setConfig(payload) {
return update;
}

async function getCommuneInfos(code) {
const mesAdressesResponse = await fetch(`${NEXT_PUBLIC_API_MES_ADRESSES}/bases-locales/search?commune=${code}`)
const mesAdressesResponseData = await mesAdressesResponse.json()
const balsMesAdresses = mesAdressesResponseData.results.filter(({status}) => {
return status === 'published' || status === 'replaced'
})

const apiDepotRevisionsResponse = await apiDepotClient.get(`communes/${code}/revisions?status=all`)

const apiMoissonneurRevisionsResponse = await apiMoissonneurClient.get(`communes/${code}/revisions`)

return {
balsMesAdresses,
apiDepotRevisions: apiDepotRevisionsResponse.body,
apiMoissonneurRevisions: apiMoissonneurRevisionsResponse.body,
};
}

module.exports = {
getConfig,
setConfig,
getCommuneInfos,
};
5 changes: 4 additions & 1 deletion server/proxy-api-depot.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,5 +156,8 @@ app.get('/stats/publications', w(getStatPublications))

app.get('/communes/:codeCommune/revisions', w(getAllRevisionsByCommune))

module.exports = app
module.exports = {
apiDepotProxy: app,
apiDepotClient: client
}

5 changes: 4 additions & 1 deletion server/proxy-api-moissonneur-bal.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,7 @@ app.put('/sources/:sourceId', w(updateSource))
app.post('/revisions/:revisionId/publish', w(publishRevision))
app.get('/communes/:codeCommune/revisions', w(getRevisionsByCommune))

module.exports = app
module.exports = {
apiMoissonneurProxy: app,
apiMoissonneurClient: client
}

0 comments on commit 625e00f

Please sign in to comment.