From d81fc92c7c93e34ad799405aba4f48898fa46e28 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Marl=C3=A8ne=20SIMONDANT?= <msimondant@grandlyon.com>
Date: Thu, 16 Feb 2023 10:33:18 +0000
Subject: [PATCH] Fix(carto): search town names without hyphens or diacritics
 for a better match

---
 src/structures/structures.controller.ts | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)

diff --git a/src/structures/structures.controller.ts b/src/structures/structures.controller.ts
index 9385e2d52..19f13b6fb 100644
--- a/src/structures/structures.controller.ts
+++ b/src/structures/structures.controller.ts
@@ -37,6 +37,7 @@ import { QueryStructure } from './dto/query-structure.dto';
 import { UpdateStructureDto } from './dto/update-structure.dto';
 import { Structure, StructureDocument } from './schemas/structure.schema';
 import { StructuresService } from './services/structures.service';
+import * as _ from 'lodash';
 
 @ApiTags('structures')
 @Controller('structures')
@@ -66,8 +67,15 @@ export class StructuresController {
       .then((data) =>
         data.filter(
           (cityPoint) =>
+            /* check if query sting = postcode 
+                     OR query string = city (without hyphens and diacritics)
+                     OR query string = name (in 2 cases, Charly et Rochetaillée-sur-saône, the city name is in the name field and the city field is missing) */
             (cityPoint.properties.postcode == city ||
-              cityPoint.properties.city?.toLowerCase().includes(city.toLowerCase())) &&
+              _.deburr(cityPoint.properties.city?.toLowerCase().replace(/\-/g, ' ')).includes(
+                _.deburr(city).toLowerCase().replace(/\-/g, ' ')
+              ) ||
+              _.deburr(cityPoint.properties.name?.toLowerCase().replace(/\-/g, ' ')) ===
+                _.deburr(city).toLowerCase().replace(/\-/g, ' ')) &&
             cityPoint.properties.postcode.match(depRegex)
         )
       )
-- 
GitLab