From 7ae2e17eb10c685b70971193dfa03e69ea4b1a46 Mon Sep 17 00:00:00 2001
From: Alex ORLUC <alex.orluc@maarch.org>
Date: Fri, 20 Nov 2020 18:53:08 +0100
Subject: [PATCH] FEAT #14568 TIME 1:45 front proof action

---
 lang/fr.json                                  |  3 +-
 src/frontend/app/search/search.component.html |  2 +-
 src/frontend/app/search/search.component.ts   | 51 +++++++++++++------
 3 files changed, 38 insertions(+), 18 deletions(-)

diff --git a/lang/fr.json b/lang/fr.json
index 70c3c3fd7b..de6c88b169 100755
--- a/lang/fr.json
+++ b/lang/fr.json
@@ -349,6 +349,7 @@
 		"workflowUserstMandatory": "Veuillez choisir des utilisateurs pour le circuit",
 		"accessDocument": "Accéder au document",
 		"warnInterrupt": "Vous êtes sur le point d'interrompre le traitement de ce document.",
-		"documentInterrupted": "Circuit interrompu"
+		"documentInterrupted": "Circuit interrompu",
+		"download": "Télécharger"
 	}
 }
diff --git a/src/frontend/app/search/search.component.html b/src/frontend/app/search/search.component.html
index e7870ac116..32d11bcf80 100644
--- a/src/frontend/app/search/search.component.html
+++ b/src/frontend/app/search/search.component.html
@@ -83,7 +83,7 @@
                             </ion-buttons>
                         </ion-item>
                         <ion-item-options>
-                            <ion-item-option color="primary" (click)="openPromptProof(element)">
+                            <ion-item-option *ngIf="element.state !== 'PROG'" color="primary" (click)="openPromptProof(element)">
                                 <ion-icon slot="bottom" name="ribbon-sharp"></ion-icon>
                                 {{'lang.proof' | translate}}
                             </ion-item-option>
diff --git a/src/frontend/app/search/search.component.ts b/src/frontend/app/search/search.component.ts
index fc5f55c5c8..73d0a68055 100644
--- a/src/frontend/app/search/search.component.ts
+++ b/src/frontend/app/search/search.component.ts
@@ -6,7 +6,7 @@ import { Router } from '@angular/router';
 import { ActionSheetController, AlertController, LoadingController, MenuController } from '@ionic/angular';
 import { TranslateService } from '@ngx-translate/core';
 import { of } from 'rxjs';
-import { catchError, finalize, tap } from 'rxjs/operators';
+import { catchError, exhaustMap, finalize, tap } from 'rxjs/operators';
 import { VisaWorkflowComponent } from '../document/visa-workflow/visa-workflow.component';
 import { AuthService } from '../service/auth.service';
 import { NotificationService } from '../service/notification.service';
@@ -15,6 +15,7 @@ import { SignaturesContentService } from '../service/signatures.service';
 @Component({
     templateUrl: 'search.component.html',
     styleUrls: ['search.component.scss'],
+    providers: [DatePipe]
 })
 export class SearchComponent implements OnInit {
 
@@ -97,7 +98,8 @@ export class SearchComponent implements OnInit {
         public authService: AuthService,
         public loadingController: LoadingController,
         public alertController: AlertController,
-        public actionSheetController: ActionSheetController
+        public actionSheetController: ActionSheetController,
+        public datePipe: DatePipe,
     ) { }
 
     ngOnInit(): void { }
@@ -289,7 +291,7 @@ export class SearchComponent implements OnInit {
                 .pipe(
                     tap(() => {
                         this.notificationService.success('lang.documentInterrupted'),
-                        resolve(true);
+                            resolve(true);
                     }),
                     catchError((err: any) => {
                         this.notificationService.handleErrors(err);
@@ -309,20 +311,23 @@ export class SearchComponent implements OnInit {
 
     async openPromptProof(item: any) {
         const alert = await this.alertController.create({
-            header: this.translate.instant('lang.proof'),
+            cssClass : 'promptProof',
+            header: this.translate.instant('lang.download'),
             inputs: [
                 {
                     name: 'option1',
-                    type: 'checkbox',
-                    label: 'Option 1',
-                    value: 'value1',
+                    type: 'radio',
+                    label: 'Faisceau de preuve',
+                    value: 'onlyProof',
+                    checked: true
                 },
                 {
-                    name: 'option2',
-                    type: 'checkbox',
-                    label: 'Option 2',
-                    value: 'value2'
+                    name: 'option1',
+                    type: 'radio',
+                    label: 'Dossier complet',
+                    value: 'all',
                 },
+
             ],
             buttons: [
                 {
@@ -333,8 +338,8 @@ export class SearchComponent implements OnInit {
                 },
                 {
                     text: this.translate.instant('lang.validate'),
-                    handler: async () => {
-                        await this.downloadProof(item);
+                    handler: async (mode) => {
+                        await this.downloadProof(item, mode);
                         alert.dismiss();
                     }
                 }
@@ -343,11 +348,25 @@ export class SearchComponent implements OnInit {
         await alert.present();
     }
 
-    downloadProof(item: any) {
+    downloadProof(item: any, mode: string) {
+        let format = null;
+        const onlyProof = mode === 'onlyProof' ? '&onlyProof=true' : '';
+
         return new Promise((resolve) => {
-            this.http.get(`../rest/documents/${item.id}/proof`)
+            this.http.get(`../rest/documents/${item.id}/proof?mode=base64${onlyProof}`)
                 .pipe(
-                    tap(() => {
+                    tap((data: any) => {
+                        format = data.format;
+                    }),
+                    exhaustMap(() => this.http.get(`../rest/documents/${item.id}/proof?mode=stream${onlyProof}`, { responseType: 'blob' })),
+                    tap((data: any) => {
+                        const today = new Date();
+                        const filename = 'proof_' + item.id + '_' + this.datePipe.transform(today, 'dd-MM-y') + '.' + format;
+                        const downloadLink = document.createElement('a');
+                        downloadLink.href = window.URL.createObjectURL(data);
+                        downloadLink.setAttribute('download', filename);
+                        document.body.appendChild(downloadLink);
+                        downloadLink.click();
                         resolve(true);
                     }),
                     catchError((err: any) => {
-- 
GitLab