From 732d986273744ff71c20ac53b6fcb7d161945ba7 Mon Sep 17 00:00:00 2001
From: Hamza HRAMCHI <hamza.hramchi@xelians.fr>
Date: Fri, 6 May 2022 11:10:54 +0200
Subject: [PATCH] FIX #17671 TIME 1:10 add document state filter

---
 lang/en.json                                  |  3 +-
 lang/fr.json                                  |  3 +-
 src/frontend/app/search/search.component.html |  9 +++++
 src/frontend/app/search/search.component.ts   | 34 ++++++++++++++++---
 4 files changed, 43 insertions(+), 6 deletions(-)

diff --git a/lang/en.json b/lang/en.json
index 1e5d8fc9b8..899c2fd9f3 100755
--- a/lang/en.json
+++ b/lang/en.json
@@ -653,6 +653,7 @@
         "downloadProofOnPurge": "You may not view the document after it is removed; its complete proof folder will be suggested for download afterwards.",
 		"manage_documents": "All-document access",
 		"indexation": "Workflow initiation",
-		"can_purge": "Document deletion"
+		"can_purge": "Document deletion",
+        "documentStateSearch": "Document state"
     }
 }
\ No newline at end of file
diff --git a/lang/fr.json b/lang/fr.json
index d9dbda6d0f..69f61d1eb4 100755
--- a/lang/fr.json
+++ b/lang/fr.json
@@ -652,6 +652,7 @@
 		"downloadProofOnPurge": "Après suppression, le document ne sera plus consultable ; son dossier complet vous sera ensuite proposé au téléchargement.",
 		"manage_documents": "Accès à tous les documents",
 		"indexation": "Initiation de circuit",
-		"can_purge": "Suppression de document"
+		"can_purge": "Suppression de document",
+		"documentStateSearch": "Etat du document"
 	}
 }
diff --git a/src/frontend/app/search/search.component.html b/src/frontend/app/search/search.component.html
index a4376e78d1..71a2ef092d 100644
--- a/src/frontend/app/search/search.component.html
+++ b/src/frontend/app/search/search.component.html
@@ -47,6 +47,15 @@
                             <ion-icon name="close-circle"></ion-icon>
                         </ion-chip>
                     </div>
+                    <div *ngIf="currentFilter.id === 'documentState' && currentFilter.val.length > 0">
+                        <ion-chip outline color="primary"
+                            style="background: white;" [title]="'lang.documentStateSearch' | translate"
+                            (click)="removeFilter(currentFilter, 'documentDeleted')">
+                            <ion-icon name="document-outline"></ion-icon>
+                            <ion-label>{{ 'lang.' + currentFilter.val[0] | translate }}</ion-label>
+                            <ion-icon name="close-circle"></ion-icon>
+                        </ion-chip>
+                    </div>
                 </div>
             </div>
         </div>
diff --git a/src/frontend/app/search/search.component.ts b/src/frontend/app/search/search.component.ts
index e070f9286b..6d5e53736d 100644
--- a/src/frontend/app/search/search.component.ts
+++ b/src/frontend/app/search/search.component.ts
@@ -105,6 +105,7 @@ export class SearchComponent implements OnInit {
     openedLine = '';
     reActiveInfinite: any;
     documentId: any;
+    canPurge: boolean = false;
 
     constructor(
         public http: HttpClient,
@@ -124,6 +125,22 @@ export class SearchComponent implements OnInit {
     ) { }
 
     ngOnInit(): void {
+        this.canPurge = this.authService.user.appPrivileges.find((privilege: any) => privilege.id === 'can_purge') !== undefined;
+        if (this.canPurge) {
+            this.filters.splice(3, 0,
+                {
+                    id: 'documentState',
+                    type: 'checkbox',
+                    val: [],
+                    values: [
+                        {
+                            id: 'documentDeleted',
+                            label: 'lang.documentDeleted',
+                            selected: false
+                        }
+                    ]
+                });
+        }
         this._activatedRoute.queryParamMap.subscribe((paramMap: ParamMap) => {
             if (!this.functionsService.empty(paramMap.get('documentId'))) {
                 this.documentId = paramMap.get('documentId');
@@ -178,6 +195,7 @@ export class SearchComponent implements OnInit {
             filter.val.splice(index, 1);
         } else {
             filter.val.push(item.id);
+            filter.val = [... new Set(filter.val)];
         }
     }
 
@@ -241,7 +259,7 @@ export class SearchComponent implements OnInit {
     }
 
     canShowButton(id: string, item: any) {
-        if (id === 'purgeDocument' && this.authService.user.appPrivileges.find((privilege: any) => privilege.id === 'can_purge') !== undefined && ['STOP', 'VAL', 'REF'].indexOf(item.state) > -1) {
+        if (id === 'purgeDocument' && this.canPurge && ['STOP', 'VAL', 'REF'].indexOf(item.state) > -1) {
             return true;
         } else if (id === 'interruptWorkflow' && item.canInterrupt) {
             return true;
@@ -269,7 +287,9 @@ export class SearchComponent implements OnInit {
         this.refreshCurrentFilter();
 
         return new Promise((resolve) => {
-            this.http.post('../rest/search/documents?limit=10&offset=0', this.formatDatas())
+            let uri: string = '../rest/search/documents?limit=10&offset=0';
+            uri = this.canPurge && this.filters.find((filter: any) => filter.id === 'documentState').values[0].selected ? uri.concat('&withDeleted=true') : uri;
+            this.http.post(`${uri}`, this.formatDatas())
                 .pipe(
                     tap((data: any) => {
                         this.ressources = this.formatListDatas(data.documents);
@@ -288,13 +308,16 @@ export class SearchComponent implements OnInit {
 
     refreshCurrentFilter() {
         this.currentFilters = JSON.parse(JSON.stringify(this.filters.filter((item: any) => !this.functionsService.empty(item.val))));
-
         if (this.currentFilters.filter((item: any) => item.id === 'workflowStates').length > 0) {
             this.currentFilters.filter((item: any) => item.id === 'workflowStates')[0].val = this.currentFilters.filter((item: any) => item.id === 'workflowStates')[0].values.filter((item: any) => item.selected);
             if (this.currentFilters.filter((item: any) => item.id === 'workflowStates')[0].val.length === 0) {
                 this.currentFilters = this.currentFilters.filter((item: any) => item.id !== 'workflowStates');
             }
         }
+
+        if (this.filters.find((el: any) => el.id === 'documentState').values[0].selected === false) {
+            this.filters.find((el: any) => el.id === 'documentState').val = [];
+        }
     }
 
     loadData(event: any) {
@@ -475,8 +498,11 @@ export class SearchComponent implements OnInit {
         if (!Array.isArray(filter.val)) {
             this.filters.find((element: any) => element.id === filter.id).val = '';
         } else {
-            if (filter.id === 'workflowStates') {
+            if (filter.id !== 'workflowUsers') {
                 this.filters.find((element: any) => element.id === filter.id).values.filter((element: any) => element.id === item)[0].selected = false;
+                if (!this.filters.find((el: any) => el.id === 'documentState').values[0].selected) {
+                    this.filters.find((el: any) => el.id === 'documentState').val = [];
+                }
             } else {
                 const index = filter.val.indexOf(item);
                 this.filters.filter((element: any) => element.id === filter.id)[0].val.splice(index, 1);
-- 
GitLab