Skip to content

Commit

Permalink
Merge pull request #158 from etalab/exposition-com-v2
Browse files Browse the repository at this point in the history
Exposition COM
  • Loading branch information
ThomasG77 authored Jun 22, 2022
2 parents f4cdf22 + 5d8ec3c commit ae61f0a
Show file tree
Hide file tree
Showing 14 changed files with 47 additions and 24 deletions.
3 changes: 2 additions & 1 deletion build/communes.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ async function buildCommunes() {
codeDepartement: commune.departement,
codeRegion: commune.region,
codesPostaux: commune.codesPostaux || [],
population: commune.population
population: commune.population,
zone: commune.zone
}

if (commune.code in communesFeaturesIndex) {
Expand Down
3 changes: 2 additions & 1 deletion build/departements.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ async function buildDepartements() {
return {
code: departement.code,
nom: departement.nom,
codeRegion: departement.region
codeRegion: departement.region,
zone: departement.zone
}
})

Expand Down
3 changes: 2 additions & 1 deletion build/regions.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ async function buildRegions() {
.map(region => {
return {
code: region.code,
nom: region.nom
nom: region.nom,
zone: region.zone
}
})

Expand Down
2 changes: 1 addition & 1 deletion lib/communeHelpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ const initCommuneFormat = initFormat({
defaultGeometry: 'centre'
})

const communesDefaultQuery = {type: ['commune-actuelle']}
const communesDefaultQuery = {type: ['commune-actuelle'], zone: ['metro', 'drom']}

module.exports = {initCommuneFields, initCommuneFormat, communesDefaultQuery}
1 change: 1 addition & 0 deletions lib/communes.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const schema = {
codesPostaux: {type: 'tokenList', queryWith: 'codePostal'},
codeDepartement: {type: 'token', queryWith: 'codeDepartement'},
codeRegion: {type: 'token', queryWith: 'codeRegion'},
zone: {type: 'token', queryWith: 'zone', multiple: 'OR'},
contour: {type: 'geo', queryWith: 'pointInContour'}
}

Expand Down
4 changes: 3 additions & 1 deletion lib/departementHelpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@ const initDepartementFields = initFields({
base: ['nom', 'code']
})

module.exports = {initDepartementFields}
const departementsDefaultQuery = {zone: ['metro', 'drom']}

module.exports = {initDepartementFields, departementsDefaultQuery}
3 changes: 2 additions & 1 deletion lib/departements.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ const schema = {
ref: 'code'
},
code: {type: 'token', queryWith: 'code'},
codeRegion: {type: 'token', queryWith: 'codeRegion'}
codeRegion: {type: 'token', queryWith: 'codeRegion'},
zone: {type: 'token', queryWith: 'zone', multiple: 'OR'}
}

function getIndexedDb(options = {}) {
Expand Down
4 changes: 3 additions & 1 deletion lib/regionHelpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@ const initRegionFields = initFields({
base: ['nom', 'code']
})

module.exports = {initRegionFields}
const regionsDefaultQuery = {zone: ['metro', 'drom']}

module.exports = {initRegionFields, regionsDefaultQuery}
3 changes: 2 additions & 1 deletion lib/regions.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ const schema = {
queryWith: 'nom',
ref: 'code'
},
code: {type: 'token', queryWith: 'code'}
code: {type: 'token', queryWith: 'code'},
zone: {type: 'token', queryWith: 'zone', multiple: 'OR'}
}

function getIndexedDb(options = {}) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"rbush": "^2.0.1"
},
"devDependencies": {
"@etalab/decoupage-administratif": "^2.0.0",
"@etalab/decoupage-administratif": "^2.1.0",
"@turf/area": "^6.0.1",
"@turf/point-on-feature": "^6.0.2",
"@turf/truncate": "^6.0.1",
Expand Down
26 changes: 19 additions & 7 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ const express = require('express')
const cors = require('cors')
const morgan = require('morgan')
const {initCommuneFields, initCommuneFormat, communesDefaultQuery} = require('./lib/communeHelpers')
const {initDepartementFields} = require('./lib/departementHelpers')
const {initRegionFields} = require('./lib/regionHelpers')
const {initDepartementFields, departementsDefaultQuery} = require('./lib/departementHelpers')
const {initRegionFields, regionsDefaultQuery} = require('./lib/regionHelpers')
const {formatOne, initLimit} = require('./lib/helpers')
const dbCommunes = require('./lib/communes').getIndexedDb()
const dbDepartements = require('./lib/departements').getIndexedDb()
Expand All @@ -29,7 +29,7 @@ app.use((req, res, next) => {

/* Communes */
app.get('/communes', initLimit(), initCommuneFields, initCommuneFormat, (req, res) => {
const query = pick(req.query, 'type', 'code', 'codePostal', 'nom', 'codeDepartement', 'codeRegion', 'boost')
const query = pick(req.query, 'type', 'code', 'codePostal', 'nom', 'codeDepartement', 'codeRegion', 'boost', 'zone')
if (req.query.lat && req.query.lon) {
const lat = parseFloat(req.query.lat)
const lon = parseFloat(req.query.lon)
Expand All @@ -56,6 +56,10 @@ app.get('/communes', initLimit(), initCommuneFields, initCommuneFormat, (req, re
query.type = query.type.split(',')
}

if (query.zone) {
query.zone = query.zone.split(',')
}

const result = req.applyLimit(dbCommunes.search({...communesDefaultQuery, ...query}))

if (req.outputFormat === 'geojson') {
Expand All @@ -79,14 +83,18 @@ app.get('/communes/:code', initCommuneFields, initCommuneFormat, (req, res) => {

/* Départements */
app.get('/departements', initLimit(), initDepartementFields, (req, res) => {
const query = pick(req.query, 'code', 'nom', 'codeRegion')
const query = pick(req.query, 'code', 'nom', 'codeRegion', 'zone')

if (query.nom) {
req.fields.add('_score')
}

if (query.zone) {
query.zone = query.zone.split(',')
}

res.send(
req.applyLimit(dbDepartements.search(query))
req.applyLimit(dbDepartements.search({...departementsDefaultQuery, ...query}))
.map(departement => formatOne(req, departement))
)
})
Expand Down Expand Up @@ -119,14 +127,18 @@ app.get('/departements/:code/communes', initLimit(), initCommuneFields, initComm

/* Régions */
app.get('/regions', initLimit(), initRegionFields, (req, res) => {
const query = pick(req.query, 'code', 'nom')
const query = pick(req.query, 'code', 'nom', 'zone')

if (query.nom) {
req.fields.add('_score')
}

if (query.zone) {
query.zone = query.zone.split(',')
}

res.send(
req.applyLimit(dbRegions.search(query))
req.applyLimit(dbRegions.search({...regionsDefaultQuery, ...query}))
.map(region => formatOne(req, region))
)
})
Expand Down
6 changes: 3 additions & 3 deletions test/communes.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,9 @@ describe('communes', () => {
})
})
describe('All criteria', () => {
it('should return an array with 1 commune', () => {
const query = {nom: 'efg', code: '67890', codeDepartement: '01', codeRegion: 'B', codePostal: '11111', pointInContour: [5, 5]}
expect(db.search(query).map(c => c.code)).to.eql(['67890'])
it('should return an array with no commune', () => {
const query = {nom: 'efg', code: '67890', codeDepartement: '01', codeRegion: 'B', codePostal: '11111', pointInContour: [5, 5], type: ['commune-actuelle']}
expect(db.search(query).map(c => c.code)).to.eql([])
})
})
})
Expand Down
3 changes: 2 additions & 1 deletion test/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,8 @@ describe('Test api', () => {
'region',
'centre',
'contour',
'surface'
'surface',
'zone'
]
request(server)
.get('/communes/54099?fields=' + fields.join(','))
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,10 @@
lodash "^4.17.11"
to-fast-properties "^2.0.0"

"@etalab/decoupage-administratif@^2.0.0":
version "2.0.0"
resolved "https://registry.yarnpkg.com/@etalab/decoupage-administratif/-/decoupage-administratif-2.0.0.tgz#24ee640070c47e3c62883873c35450197fa5f78c"
integrity sha512-X+pQDnm0Sk8T5KPhT5kU9cLyMKc+LagK8ObYxyzVDoQapjLNZl9/KY2VFr42aw4LEiTnUfqCgNZc9lhHfi6shQ==
"@etalab/decoupage-administratif@^2.1.0":
version "2.1.0"
resolved "https://registry.yarnpkg.com/@etalab/decoupage-administratif/-/decoupage-administratif-2.1.0.tgz#1e58329770d291009fa382f6514ba5b80eabd51c"
integrity sha512-ICOWiysBRNMd6HNY5yGFeia+AHqHLiXMrXDm4YPeEMPQP2i/vs38soCCFu8C789UT3o0efu79UK2hNTcHWBoXg==

"@mrmlnc/readdir-enhanced@^2.2.1":
version "2.2.1"
Expand Down

0 comments on commit ae61f0a

Please sign in to comment.