From 042e35e4a95bf2d4575694919ac0734a5e35f8e1 Mon Sep 17 00:00:00 2001
From: Alex ORLUC <alex.orluc@maarch.org>
Date: Thu, 16 Jan 2020 10:47:13 +0100
Subject: [PATCH] FEAT #12765 TIME 0:40 change route load models + fix update
 model list

---
 .../app/avis/avis-workflow.component.html     |  4 +-
 .../app/avis/avis-workflow.component.ts       | 60 ++++++++++++++-----
 .../app/visa/visa-workflow.component.ts       | 41 ++++++++++---
 3 files changed, 79 insertions(+), 26 deletions(-)

diff --git a/src/frontend/app/avis/avis-workflow.component.html b/src/frontend/app/avis/avis-workflow.component.html
index 350f7885c7a..9c9f734707e 100644
--- a/src/frontend/app/avis/avis-workflow.component.html
+++ b/src/frontend/app/avis/avis-workflow.component.html
@@ -1,8 +1,8 @@
 
 <mat-list *ngIf="!loading">
     <mat-form-field appearance="outline" *ngIf="adminMode && !linkedToMaarchParapheur">
-        <input type="text" matInput placeholder="Ajouter des personnes" id="searchAvisSignUserInput"
-            [formControl]="searchAvisSignUser" [matAutocomplete]="autoGroup">
+        <input type="text" matInput placeholder="Ajouter des personnes" id="searchAvisUserInput"
+            [formControl]="searchAvisUser" [matAutocomplete]="autoGroup">
         <mat-autocomplete #autoGroup="matAutocomplete" (optionSelected)="addItemToWorkflow($event.option.value)" (opened)="initFilterAvisModelList()">
             <mat-option disabled *ngIf="avisModelListNotLoaded">
                 <div style="display: flex;justify-content: center;">
diff --git a/src/frontend/app/avis/avis-workflow.component.ts b/src/frontend/app/avis/avis-workflow.component.ts
index 18414f9c2a6..362b06e1178 100644
--- a/src/frontend/app/avis/avis-workflow.component.ts
+++ b/src/frontend/app/avis/avis-workflow.component.ts
@@ -45,9 +45,9 @@ export class AvisWorkflowComponent implements OnInit {
     @Input('adminMode') adminMode: boolean;
     @Input('resId') resId: number = null;
 
-    @ViewChild('searchAvisSignUserInput', { static: true }) searchAvisSignUserInput: ElementRef;
+    @ViewChild('searchAvisUserInput', { static: true }) searchAvisUserInput: ElementRef;
 
-    searchAvisSignUser = new FormControl();
+    searchAvisUser = new FormControl();
 
     constructor(
         public http: HttpClient,
@@ -92,7 +92,7 @@ export class AvisWorkflowComponent implements OnInit {
             });
     }
 
-    loadAvisSignUsersList() {
+    loadAvisUsersList() {
         return new Promise((resolve, reject) => {
             this.http.get(`../../rest/autocomplete/users/circuit`).pipe(
                 map((data: any) => {
@@ -109,7 +109,7 @@ export class AvisWorkflowComponent implements OnInit {
                 }),
                 tap((data) => {
                     this.signAvisUsers = data;
-                    this.filteredSignAvisUsers = this.searchAvisSignUser.valueChanges
+                    this.filteredSignAvisUsers = this.searchAvisUser.valueChanges
                         .pipe(
                             startWith(''),
                             map(value => this._filter(value))
@@ -124,18 +124,20 @@ export class AvisWorkflowComponent implements OnInit {
         });
     }
 
-    loadAvisModelListByResource() {
+    async loadAvisModelList() {
+        await this.loadDefaultModel();
+
         return new Promise((resolve, reject) => {
-            this.http.get(`../../rest/resources/${this.resId}/availableCircuits?circuit=opinion`).pipe(
+            this.http.get(`../../rest/availableCircuits?circuit=opinion`).pipe(
                 tap((data: any) => {
-                    this.avisTemplates.public = data.circuits.filter((item: any) => !item.private).map((item: any) => {
+                    this.avisTemplates.public = this.avisTemplates.public.concat(data.circuits.filter((item: any) => !item.private).map((item: any) => {
                         return {
                             id: item.id,
                             title: item.title,
                             label: item.title,
                             type: 'entity'
                         }
-                    });
+                    }));
 
                     this.avisTemplates.private = data.circuits.filter((item: any) => item.private).map((item: any) => {
                         return {
@@ -145,29 +147,54 @@ export class AvisWorkflowComponent implements OnInit {
                             type: 'entity'
                         }
                     });
-                    this.filteredPublicModels = this.searchAvisSignUser.valueChanges
+                    this.filteredPublicModels = this.searchAvisUser.valueChanges
                         .pipe(
                             startWith(''),
                             map(value => this._filterPublicModel(value))
                         );
-                    this.filteredPrivateModels = this.searchAvisSignUser.valueChanges
+                    this.filteredPrivateModels = this.searchAvisUser.valueChanges
                         .pipe(
                             startWith(''),
                             map(value => this._filterPrivateModel(value))
                         );
                     resolve(true);
+                })
+            ).subscribe();
+        });
+    }
+
+    loadDefaultModel() {
+        this.avisTemplates.public = [];
+
+        return new Promise((resolve, reject) => {
+            this.http.get(`../../rest/resources/${this.resId}/defaultCircuit?circuit=opinion`).pipe(
+                filter((data: any) => !this.functions.empty(data.circuit)),
+                tap((data: any) => {
+                    if (!this.functions.empty(data.circuit)) {
+                        this.avisTemplates.public.push({
+                            id: data.circuit.id,
+                            title: data.circuit.title,
+                            label: data.circuit.title,
+                            type: 'entity'
+                        });
+                    }
                 }),
+                finalize(() => resolve(true)),
+                catchError((err: any) => {
+                    this.notify.handleSoftErrors(err);
+                    return of(false);
+                })
             ).subscribe();
         });
     }
 
     async initFilterAvisModelList() {
         if (this.avisModelListNotLoaded) {
-            await this.loadAvisSignUsersList();
+            await this.loadAvisUsersList();
 
-            await this.loadAvisModelListByResource();
+            await this.loadAvisModelList();
 
-            this.searchAvisSignUser.reset();
+            this.searchAvisUser.reset();
 
             this.avisModelListNotLoaded = false;
         }
@@ -278,7 +305,7 @@ export class AvisWorkflowComponent implements OnInit {
                 signatory: false,
                 requested_signature: false
             });
-            this.searchAvisSignUser.reset();
+            this.searchAvisUser.reset();
         } else if (item.type === 'entity') {
             this.http.get(`../../rest/listTemplates/${item.id}`).pipe(
                 tap((data: any) => {
@@ -295,7 +322,7 @@ export class AvisWorkflowComponent implements OnInit {
                             }
                         })
                     );
-                    this.searchAvisSignUser.reset();
+                    this.searchAvisUser.reset();
                 })
             ).subscribe();
         }
@@ -314,6 +341,7 @@ export class AvisWorkflowComponent implements OnInit {
                     label: data.title,
                     type: 'entity'
                 });
+                this.searchAvisUser.reset();
             }),
             catchError((err: any) => {
                 this.notify.handleSoftErrors(err);
@@ -330,7 +358,7 @@ export class AvisWorkflowComponent implements OnInit {
             exhaustMap(() => this.http.delete(`../../rest/listTemplates/${model.id}`)),
             tap(() => {
                 this.avisTemplates.private = this.avisTemplates.private.filter((template: any) => template.id !== model.id);
-                this.searchAvisSignUser.reset();
+                this.searchAvisUser.reset();
                 this.notify.success(this.lang.modelDeleted);
             }),
             catchError((err: any) => {
diff --git a/src/frontend/app/visa/visa-workflow.component.ts b/src/frontend/app/visa/visa-workflow.component.ts
index 96ffe7d4b4d..2d173b49b9d 100644
--- a/src/frontend/app/visa/visa-workflow.component.ts
+++ b/src/frontend/app/visa/visa-workflow.component.ts
@@ -62,7 +62,6 @@ export class VisaWorkflowComponent implements OnInit {
     ngOnInit(): void {
         if (this.resId !== null) {
             //this.initFilterVisaModelList();
-            //this.loadVisaModelListByResource();
             this.loadWorkflow(this.resId);
         }
     }
@@ -136,18 +135,20 @@ export class VisaWorkflowComponent implements OnInit {
         });
     }
 
-    loadVisaModelListByResource() {
+    async loadVisaModelList() {
+        await this.loadDefaultModel();
+
         return new Promise((resolve, reject) => {
-            this.http.get(`../../rest/resources/${this.resId}/availableCircuits?circuit=visa`).pipe(
+            this.http.get(`../../rest/availableCircuits?circuit=visa`).pipe(
                 tap((data: any) => {
-                    this.visaTemplates.public = data.circuits.filter((item: any) => !item.private).map((item: any) => {
+                    this.visaTemplates.public = this.visaTemplates.public.concat(data.circuits.filter((item: any) => !item.private).map((item: any) => {
                         return {
                             id: item.id,
                             title: item.title,
                             label: item.title,
                             type: 'entity'
                         }
-                    });
+                    }));
 
                     this.visaTemplates.private = data.circuits.filter((item: any) => item.private).map((item: any) => {
                         return {
@@ -168,7 +169,32 @@ export class VisaWorkflowComponent implements OnInit {
                             map(value => this._filterPrivateModel(value))
                         );
                     resolve(true);
+                })
+            ).subscribe();
+        });
+    }
+
+    loadDefaultModel() {
+        this.visaTemplates.public = [];
+
+        return new Promise((resolve, reject) => {
+            this.http.get(`../../rest/resources/${this.resId}/defaultCircuit?circuit=visa`).pipe(
+                filter((data: any) => !this.functions.empty(data.circuit)),
+                tap((data: any) => {
+                    if (!this.functions.empty(data.circuit)) {
+                        this.visaTemplates.public.push({
+                            id: data.circuit.id,
+                            title: data.circuit.title,
+                            label: data.circuit.title,
+                            type: 'entity'
+                        });
+                    }
                 }),
+                finalize(() => resolve(true)),
+                catchError((err: any) => {
+                    this.notify.handleSoftErrors(err);
+                    return of(false);
+                })
             ).subscribe();
         });
     }
@@ -178,9 +204,7 @@ export class VisaWorkflowComponent implements OnInit {
             if (this.visaModelListNotLoaded) {
                 await this.loadVisaSignUsersList();
 
-                if (this.resId) {
-                    await this.loadVisaModelListByResource();
-                }
+                await this.loadVisaModelList();
                 
                 this.searchVisaSignUser.reset();
 
@@ -467,6 +491,7 @@ export class VisaWorkflowComponent implements OnInit {
                     label: data.title,
                     type: 'entity'
                 });
+                this.searchVisaSignUser.reset();
             }),
             catchError((err: any) => {
                 this.notify.handleSoftErrors(err);
-- 
GitLab