From bfca4a17f76aa36d9f19bcb3047bcc8afb1e691b Mon Sep 17 00:00:00 2001
From: Hugo SUBTIL <ext.sopra.husubtil@grandlyon.com>
Date: Thu, 26 Nov 2020 08:44:28 +0100
Subject: [PATCH] feat: move structure location call to backend

---
 proxy.conf.json                          |  6 ----
 src/app/home/home.component.spec.ts      | 14 --------
 src/app/home/home.component.ts           | 44 ++++++++----------------
 src/app/map/components/map.component.ts  | 38 ++++++++------------
 src/app/models/structure.model.ts        |  9 +++++
 src/app/services/geojson.service.spec.ts | 15 --------
 src/app/services/geojson.service.ts      | 10 ------
 7 files changed, 37 insertions(+), 99 deletions(-)

diff --git a/proxy.conf.json b/proxy.conf.json
index 70c21305f..cc19a8115 100644
--- a/proxy.conf.json
+++ b/proxy.conf.json
@@ -14,12 +14,6 @@
     "changeOrigin": true,
     "logLevel": "info"
   },
-  "/geocoding/photon/api": {
-    "target": "https://download.data.grandlyon.com",
-    "secure": false,
-    "changeOrigin": true,
-    "logLevel": "info"
-  },
   "/wfs/grandlyon": {
     "target": "https://download.data.grandlyon.com",
     "secure": false,
diff --git a/src/app/home/home.component.spec.ts b/src/app/home/home.component.spec.ts
index d389cc50d..4bc5950f6 100644
--- a/src/app/home/home.component.spec.ts
+++ b/src/app/home/home.component.spec.ts
@@ -31,20 +31,6 @@ describe('HomeComponent', () => {
   it('should create', () => {
     expect(component).toBeTruthy();
   });
-
-  it('getCoord(): should get coord', async () => {
-    await new Promise((resolve) => {
-      component.getCoord('Rue de la Mairie ', 'Feyzin').subscribe(
-        (val) => {
-          expect(val.geometry.getLat()).toEqual(4.8591584);
-          expect(val.geometry.getLon()).toEqual(45.6727968);
-          resolve();
-        },
-        (err) => {
-          resolve();
-        }
-      );
-    });
   });
 
   it('getAddress(): should getAddress', () => {
diff --git a/src/app/home/home.component.ts b/src/app/home/home.component.ts
index 0f968db5b..265131817 100644
--- a/src/app/home/home.component.ts
+++ b/src/app/home/home.component.ts
@@ -38,12 +38,9 @@ export class HomeComponent implements OnInit {
         Promise.all(
           structures.map((structure) => {
             if (this.geolocation) {
-              return this.getStructurePosition(structure).then((val) => {
-                return this.structureService.updateOpeningStructure(val, DateTime.local());
-              });
-            } else {
-              return this.structureService.updateOpeningStructure(structure, DateTime.local());
+              structure = this.getStructurePosition(structure);
             }
+            return this.structureService.updateOpeningStructure(structure, DateTime.local());
           })
         ).then((structureList) => {
           structureList = _.sortBy(structureList, ['distance']);
@@ -58,31 +55,18 @@ export class HomeComponent implements OnInit {
   /**
    * Get structures positions and add marker corresponding to those positons on the map
    */
-  private getStructurePosition(structure: Structure): Promise<Structure> {
-    return new Promise((resolve, reject) => {
-      this.getCoord(structure.n, structure.voie, structure.commune).subscribe((coord: GeoJson) => {
-        structure.address = structure.voie + ' - ' + coord.properties.postcode + ' ' + coord.properties.city;
-        structure.distance = parseInt(
-          this.geoJsonService.getDistance(
-            coord.geometry.getLon(),
-            coord.geometry.getLat(),
-            this.currentLocation.geometry.getLon(),
-            this.currentLocation.geometry.getLat(),
-            'M'
-          ),
-          10
-        );
-        resolve(structure);
-      });
-    });
-  }
-
-  /**
-   * Get coord with a street reference
-   * @param idVoie Street reference
-   */
-  public getCoord(numero: string, voie: string, zipcode: string): Observable<GeoJson> {
-    return this.geoJsonService.getCoord(numero, voie, zipcode);
+  private getStructurePosition(structure: Structure): Structure {
+    structure.distance = parseInt(
+      this.geoJsonService.getDistance(
+        structure.getLon(),
+        structure.getLat(),
+        this.currentLocation.geometry.getLon(),
+        this.currentLocation.geometry.getLat(),
+        'M'
+      ),
+      10
+    );
+    return structure;
   }
 
   public getLocation(): void {
diff --git a/src/app/map/components/map.component.ts b/src/app/map/components/map.component.ts
index 84a68b693..6bccd2d44 100644
--- a/src/app/map/components/map.component.ts
+++ b/src/app/map/components/map.component.ts
@@ -108,22 +108,20 @@ export class MapComponent implements OnChanges {
   }
 
   private getStructuresPositions(structureListe: Structure[]): void {
-    structureListe.forEach((element: Structure) => {
-      this.getCoord(element.n, element.voie, element.commune).subscribe((coord: GeoJson) => {
-        this.mapService
-          .createMarker(
-            coord.geometry.getLon(),
-            coord.geometry.getLat(),
-            MarkerType.structure,
-            element.id,
-            this.buildToolTip(element)
-          )
-          .addTo(this.map)
-          // store structure before user click on button
-          .on('popupopen', () => {
-            this.currentStructure = element;
-          });
-      });
+    structureListe.forEach((structure: Structure) => {
+      this.mapService
+        .createMarker(
+          structure.getLon(),
+          structure.getLat(),
+          MarkerType.structure,
+          structure.id,
+          this.buildToolTip(structure)
+        )
+        .addTo(this.map)
+        // store structure before user click on button
+        .on('popupopen', () => {
+          this.currentStructure = structure;
+        });
     });
   }
 
@@ -159,14 +157,6 @@ export class MapComponent implements OnChanges {
     return `<h1>${mdmProperties.nom}</h1><p>${mdmProperties.adresse}</p>`;
   }
 
-  /**
-   * Get coord with a street reference
-   * @param idVoie Street reference
-   */
-  public getCoord(numero: string, voie: string, zipcode: string): Observable<GeoJson> {
-    return this.geoJsonService.getCoord(numero, voie, zipcode);
-  }
-
   /**
    * Add marker when map is ready to be showned
    * @param map map
diff --git a/src/app/models/structure.model.ts b/src/app/models/structure.model.ts
index 221dd15cf..bcc5b4a08 100644
--- a/src/app/models/structure.model.ts
+++ b/src/app/models/structure.model.ts
@@ -40,6 +40,7 @@ export class Structure {
   public accesAuxDroits: string[];
   public distance?: number;
   public address?: string;
+  public coord?: number[];
 
   constructor(obj?: any) {
     Object.assign(this, obj, {
@@ -109,4 +110,12 @@ export class Structure {
       }
     }
   }
+
+  public getLat(): number {
+    return this.coord[0];
+  }
+
+  public getLon(): number {
+    return this.coord[1];
+  }
 }
diff --git a/src/app/services/geojson.service.spec.ts b/src/app/services/geojson.service.spec.ts
index a947152a8..71045a64d 100644
--- a/src/app/services/geojson.service.spec.ts
+++ b/src/app/services/geojson.service.spec.ts
@@ -21,19 +21,4 @@ describe('GeojsonService', () => {
   it('should be created', () => {
     expect(service).toBeTruthy();
   });
-
-  it('should get coord with query string Rue de la Mairie Feyzin ', async () => {
-    await new Promise((resolve) => {
-      service.getCoord('Rue de la Mairie', 'Feyzin').subscribe(
-        (val) => {
-          expect(val.geometry.getLat()).toEqual(4.8591584);
-          expect(val.geometry.getLon()).toEqual(45.6727968);
-          resolve();
-        },
-        (err) => {
-          resolve();
-        }
-      );
-    });
-  });
 });
diff --git a/src/app/services/geojson.service.ts b/src/app/services/geojson.service.ts
index 5f15ae6d8..befafee6c 100644
--- a/src/app/services/geojson.service.ts
+++ b/src/app/services/geojson.service.ts
@@ -43,16 +43,6 @@ export class GeojsonService {
       .pipe(map((data: { features: any[] }) => _.map(data.features, this.parseToGeoJson)));
   }
 
-  /**
-   * Get GeoLocation with an address
-   * @param address Address
-   */
-  public getCoord(numero: string, address: string, zipcode: string): Observable<GeoJson> {
-    return this.http
-      .get('/geocoding/photon/api' + '?q=' + numero + ' ' + address + ' ' + zipcode)
-      .pipe(map((data: { features: any[]; type: string }) => new GeoJson(data.features[0])));
-  }
-
   // :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
   // :::                                                                         :::
   // :::  This routine calculates the distance between two points (given the     :::
-- 
GitLab