From 5d56c9b4bd2209ee1c9d622c5363e55c28badc6a Mon Sep 17 00:00:00 2001
From: Guillaume Heurtier <guillaume.heurtier@maarch.org>
Date: Mon, 7 Dec 2020 10:43:46 +0100
Subject: [PATCH] FIX #15570 TIME 0:35 fix check visa parameters before
 starting action

---
 .../controllers/ParameterController.php       |  2 +-
 .../send-signature-book-action.component.ts   | 25 +++++++++++++++++--
 2 files changed, 24 insertions(+), 3 deletions(-)

diff --git a/src/app/parameter/controllers/ParameterController.php b/src/app/parameter/controllers/ParameterController.php
index 3dab8759386..fe0ccfb5746 100755
--- a/src/app/parameter/controllers/ParameterController.php
+++ b/src/app/parameter/controllers/ParameterController.php
@@ -53,7 +53,7 @@ class ParameterController
 
     public function getById(Request $request, Response $response, array $aArgs)
     {
-        if (!PrivilegeController::hasPrivilege(['privilegeId' => 'admin_parameters', 'userId' => $GLOBALS['id']])) {
+        if (!in_array($aArgs['id'], ['minimumVisaRole', 'maximumSignRole']) && !PrivilegeController::hasPrivilege(['privilegeId' => 'admin_parameters', 'userId' => $GLOBALS['id']])) {
             return $response->withStatus(403)->withJson(['errors' => 'Service forbidden']);
         }
 
diff --git a/src/frontend/app/actions/visa-send-signature-book-action/send-signature-book-action.component.ts b/src/frontend/app/actions/visa-send-signature-book-action/send-signature-book-action.component.ts
index 20f547537e4..0f7d133a71f 100644
--- a/src/frontend/app/actions/visa-send-signature-book-action/send-signature-book-action.component.ts
+++ b/src/frontend/app/actions/visa-send-signature-book-action/send-signature-book-action.component.ts
@@ -4,7 +4,7 @@ import { NotificationService } from '@service/notification/notification.service'
 import { MAT_DIALOG_DATA, MatDialogRef } from '@angular/material/dialog';
 import { HttpClient } from '@angular/common/http';
 import { NoteEditorComponent } from '../../notes/note-editor.component';
-import { tap, finalize, catchError } from 'rxjs/operators';
+import {tap, finalize, catchError, exhaustMap} from 'rxjs/operators';
 import { of } from 'rxjs';
 import { FunctionsService } from '@service/functions.service';
 import { VisaWorkflowComponent } from '../../visa/visa-workflow.component';
@@ -57,6 +57,7 @@ export class SendSignatureBookActionComponent implements AfterViewInit {
             } else if (!this.functions.empty(this.data.resource.destination)) {
                 this.noResourceToProcess = false;
                 await this.appVisaWorkflow.loadListModel(this.data.resource.destination);
+                await this.loadMinMaxVisaSignParameters();
             }
             this.loading = false;
         } else if (this.data.resIds.length > 0) {
@@ -208,7 +209,27 @@ export class SendSignatureBookActionComponent implements AfterViewInit {
             });
 
             this.visaNumberCorrect = this.minimumVisaRole === 0 || nbVisaRole >= this.minimumVisaRole;
-            this.signNumberCorrect = this.maximumSignRole === 0 || nbSignRole <= this.maximumSignRole;
+            this.signNumberCorrect = this.maximumSignRole === 0 || nbSignRole <= this.maximumSignRole && nbSignRole >= 1;
         }
     }
+
+    async loadMinMaxVisaSignParameters() {
+        return new Promise((resolve) => {
+            this.http.get('../rest/parameters/minimumVisaRole').pipe(
+                tap((data: any) => {
+                    this.minimumVisaRole = data.parameter.param_value_int;
+                }),
+                exhaustMap(() => this.http.get('../rest/parameters/maximumSignRole')),
+                tap((data: any) => {
+                    this.maximumSignRole = data.parameter.param_value_int;
+                    resolve(true);
+                }),
+                catchError((err: any) => {
+                    this.notify.handleErrors(err);
+                    resolve(false);
+                    return of(false);
+                })
+            ).subscribe();
+        });
+    }
 }
-- 
GitLab