From 67a2106ec932b2ce9279dafbb6daf72059be7b14 Mon Sep 17 00:00:00 2001
From: Yoan VALLET <ext.sopra.yvallet@grandlyon.com>
Date: Mon, 9 May 2022 16:20:56 +0200
Subject: [PATCH] fix(orientation): wrong result with address on 2 postcodes

---
 .../orientation-form.component.ts             | 20 +++++++++++--------
 src/app/models/address.model.ts               | 13 ++++++------
 .../address-autocomplete.component.ts         |  1 +
 3 files changed, 20 insertions(+), 14 deletions(-)

diff --git a/src/app/form/orientation-form/orientation-form.component.ts b/src/app/form/orientation-form/orientation-form.component.ts
index 31b2c36d0..50b648f25 100644
--- a/src/app/form/orientation-form/orientation-form.component.ts
+++ b/src/app/form/orientation-form/orientation-form.component.ts
@@ -1,8 +1,6 @@
-import { stringify } from '@angular/compiler/src/util';
-import { Component, EventEmitter, HostListener, OnInit, Output } from '@angular/core';
-import { AbstractControl, Form, FormArray, FormControl, FormGroup, Validators } from '@angular/forms';
+import { Component, HostListener, OnInit } from '@angular/core';
+import { AbstractControl, FormArray, FormControl, FormGroup, Validators } from '@angular/forms';
 import { Meta } from '@angular/platform-browser';
-import { GeoJson } from '../../map/models/geojson.model';
 import { Address } from '../../models/address.model';
 import { OrientationFormFilters } from '../../models/orientation-filter.object';
 import { Structure } from '../../models/structure.model';
@@ -10,14 +8,13 @@ import { GeojsonService } from '../../services/geojson.service';
 import { RouterListenerService } from '../../services/routerListener.service';
 import { StructureService } from '../../services/structure.service';
 import { CategoryEnum } from '../../shared/enum/category.enum';
-import * as _ from 'lodash';
 import { Category } from '../../structure-list/models/category.model';
 import { Filter } from '../../structure-list/models/filter.model';
 import { Module } from '../../structure-list/models/module.model';
 import { SearchService } from '../../structure-list/services/search.service';
-import { PageTypeEnum } from './pageType.enum';
-import { Utils } from '../../utils/utils';
 import { CustomRegExp } from '../../utils/CustomRegExp';
+import { Utils } from '../../utils/utils';
+import { PageTypeEnum } from './pageType.enum';
 
 @Component({
   selector: 'app-orientation-form',
@@ -163,6 +160,7 @@ export class OrientationFormComponent implements OnInit {
         numero: new FormControl(''),
         street: new FormControl('', Validators.required),
         commune: new FormControl('', Validators.required),
+        postcode: new FormControl(''),
       }),
       structureAccompaniment: new FormControl(orientationFormFilters.structureAccompaniment, Validators.required),
       contactAccompanimentPhone: new FormControl(
@@ -344,6 +342,7 @@ export class OrientationFormComponent implements OnInit {
       this.getOrientationControl('address').get('numero').setValue(address.numero);
       this.getOrientationControl('address').get('street').setValue(address.street);
       this.getOrientationControl('address').get('commune').setValue(address.commune);
+      this.getOrientationControl('address').get('postcode').setValue(address.postcode);
       this.userLocation = address.coordinates;
     } else {
       this.orientationForm.get('address').reset();
@@ -441,7 +440,11 @@ export class OrientationFormComponent implements OnInit {
 
   private setStructuresAndCoord(): void {
     this.geoJsonService
-      .getCoord(this.orientationForm.value.address.numero, this.orientationForm.value.address.street, '69000')
+      .getCoord(
+        this.orientationForm.value.address.numero,
+        this.orientationForm.value.address.street,
+        this.orientationForm.value.address.postcode ? this.orientationForm.value.address.postcode : '69000'
+      )
       .subscribe((res) => {
         this.structureService.getStructures(this.filters.filter((elem) => elem.checked == true)).subscribe((data) => {
           data.map((structure) => {
@@ -518,6 +521,7 @@ export class OrientationFormComponent implements OnInit {
           }
           this.getOrientationControl('address').get('street').setValue(location.properties.street);
           this.getOrientationControl('address').get('commune').setValue(location.properties.city);
+          this.getOrientationControl('address').get('postcode').setValue(location.properties.postcode);
           this.setValidationsForm();
           this.isLoading = false;
         },
diff --git a/src/app/models/address.model.ts b/src/app/models/address.model.ts
index 0b30ddc1c..8ef6bc349 100644
--- a/src/app/models/address.model.ts
+++ b/src/app/models/address.model.ts
@@ -1,6 +1,7 @@
-export class Address {
-  numero: string = null;
-  street: string = null;
-  commune: string = null;
-  coordinates? = [];
-}
+export class Address {
+  numero: string = null;
+  street: string = null;
+  commune: string = null;
+  postcode?: number = null;
+  coordinates? = [];
+}
diff --git a/src/app/shared/components/address-autocomplete/address-autocomplete.component.ts b/src/app/shared/components/address-autocomplete/address-autocomplete.component.ts
index 45710320a..f079a7934 100644
--- a/src/app/shared/components/address-autocomplete/address-autocomplete.component.ts
+++ b/src/app/shared/components/address-autocomplete/address-autocomplete.component.ts
@@ -59,6 +59,7 @@ export class AddressAutocompleteComponent implements OnInit {
     address.numero = hit.properties.housenumber ? hit.properties.housenumber : null;
     address.commune = hit.properties.city;
     address.coordinates = hit.geometry.coordinates;
+    address.postcode = hit.properties.postcode;
     if (hit.properties.street) {
       address.street = hit.properties.street;
     } else {
-- 
GitLab