From 8db1acd905ae03676b2a84b83bf96ae3dcbcee2a Mon Sep 17 00:00:00 2001
From: Thomas Gratier <thomas_gratier@yahoo.fr>
Date: Mon, 20 Nov 2023 19:05:16 +0100
Subject: [PATCH] Update to support anciens INSEE codes

---
 build/communes.js |  1 +
 definition.yml    | 10 ++++++++++
 lib/communes.js   |  3 ++-
 server.js         |  2 +-
 4 files changed, 14 insertions(+), 2 deletions(-)

diff --git a/build/communes.js b/build/communes.js
index 9d40162..9e584cd 100644
--- a/build/communes.js
+++ b/build/communes.js
@@ -70,6 +70,7 @@ async function buildCommunes() {
         codeRegion: commune.region,
         codesPostaux: [...(commune.codesPostaux || [])].sort(),
         population: commune.population,
+        anciensCodes: commune.anciensCodes || [],
         zone: commune.zone
       }
 
diff --git a/definition.yml b/definition.yml
index 50806d6..4692d6d 100644
--- a/definition.yml
+++ b/definition.yml
@@ -33,6 +33,7 @@ parameters:
         - codeRegion
         - region
         - population
+        - anciensCodes
         - deleguees
         - associees
         - zone
@@ -254,6 +255,10 @@ paths:
           in: query
           description: Code de la commune si on a un arrondissement
           type: string
+        - name: ancienCode
+          in: query
+          description: Code INSEE ancien de la commune
+          type: string
         - $ref: '#/parameters/zoneParam'
         - $ref: '#/parameters/typeCommune'
         - $ref: '#/parameters/communeFieldsParam'
@@ -719,6 +724,11 @@ definitions:
       population:
         type: integer
         description: Population municipale
+      anciensCodes:
+        type: array
+        description: Liste des anciens codes INSEE associés à la commune
+        items:
+          type: string
       surface:
         type: number
         format: float
diff --git a/lib/communes.js b/lib/communes.js
index 2efff24..e47d122 100644
--- a/lib/communes.js
+++ b/lib/communes.js
@@ -24,7 +24,8 @@ const schema = {
   codeDepartement: {type: 'token', queryWith: 'codeDepartement'},
   codeRegion: {type: 'token', queryWith: 'codeRegion'},
   zone: {type: 'token', queryWith: 'zone', multiple: 'OR'},
-  contour: {type: 'geo', queryWith: 'pointInContour'}
+  contour: {type: 'geo', queryWith: 'pointInContour'},
+  anciensCodes: {type: 'tokenList', queryWith: 'ancienCode'}
 }
 
 function getIndexedDb(options = {}) {
diff --git a/server.js b/server.js
index 71551fe..e94d155 100644
--- a/server.js
+++ b/server.js
@@ -111,7 +111,7 @@ if (process.env.COMMUNES_ASSOCIEES_DELEGUEES) {
 
 /* Communes */
 app.get('/communes', initLimit(), initCommuneFields, initCommuneFormat, (req, res) => {
-  const query = pick(req.query, 'type', 'code', 'codePostal', 'nom', 'siren', 'deleguees', 'associees', 'codeEpci', 'codeDepartement', 'codeRegion', 'boost', 'zone', 'codeParent')
+  const query = pick(req.query, 'type', 'code', 'codePostal', 'nom', 'siren', 'deleguees', 'associees', 'codeEpci', 'codeDepartement', 'codeRegion', 'boost', 'zone', 'codeParent', 'ancienCode')
   if (req.query.lat && req.query.lon) {
     const lat = parseFloat(req.query.lat)
     const lon = parseFloat(req.query.lon)