From 02b0b0172f23563db0164fb4160840abcef6627d Mon Sep 17 00:00:00 2001
From: Alex ORLUC <alex.orluc@maarch.org>
Date: Tue, 17 Mar 2020 10:57:52 +0100
Subject: [PATCH] FEAT #12091 TIME 1:30 fix priv in signatureBook

---
 .../app/signature-book.component.html         |  2 +-
 .../app/visa/visa-workflow.component.html     | 12 +--
 .../app/visa/visa-workflow.component.ts       | 89 +++++++++++--------
 src/frontend/lang/lang-en.ts                  |  3 +-
 src/frontend/lang/lang-fr.ts                  |  3 +-
 src/frontend/lang/lang-nl.ts                  |  3 +-
 6 files changed, 63 insertions(+), 49 deletions(-)

diff --git a/src/frontend/app/signature-book.component.html b/src/frontend/app/signature-book.component.html
index cdbbe070527..94b979beca7 100755
--- a/src/frontend/app/signature-book.component.html
+++ b/src/frontend/app/signature-book.component.html
@@ -94,7 +94,7 @@
             </div>
             <div *ngIf="headerTab == 'visaCircuit'" class="contentShow" style="width:98%;overflow-x: hidden;">
                 <app-visa-workflow #appVisaWorkflow [resId]="resId"
-                    [adminMode]="privilegeService.hasCurrentUserPrivilege('config_visa_workflow')"></app-visa-workflow>
+                    [adminMode]="privilegeService.hasCurrentUserPrivilege('config_visa_workflow')" [target]="'signatureBook'"></app-visa-workflow>
                 <div style="position: sticky;bottom: 0px;text-align:right;">
                     <button mat-fab [title]="lang.saveModifications" *ngIf="appVisaWorkflow !== undefined && appVisaWorkflow.isModified()" (click)="saveVisaWorkflow()"
                         color="accent">
diff --git a/src/frontend/app/visa/visa-workflow.component.html b/src/frontend/app/visa/visa-workflow.component.html
index 9a6665422c3..02a84748f72 100644
--- a/src/frontend/app/visa/visa-workflow.component.html
+++ b/src/frontend/app/visa/visa-workflow.component.html
@@ -52,12 +52,12 @@
             {{lang.noPerson}}
         </div>
         <mat-list-item disableRipple *ngFor="let diffusion of visaWorkflow.items;let i=index" cdkDrag class="columns workflow"
-            [cdkDragDisabled]="!adminMode || !functions.empty(diffusion.process_date)"
-            [class.notDraggable]="!adminMode || !functions.empty(diffusion.process_date)"
+            [cdkDragDisabled]="!canManageUser(diffusion, i)"
+            [class.notDraggable]="!canManageUser(diffusion, i)"
             [class.notEditable]="!adminMode"
             [class.processed]="diffusion.process_date != null && [lang.visaWorkflowInterrupted, lang.hasInterruptedWorkflow].indexOf(diffusion.process_comment) === -1"
             [class.interrupt]="diffusion.process_date != null && [lang.visaWorkflowInterrupted, lang.hasInterruptedWorkflow].indexOf(diffusion.process_comment) > -1">
-            <mat-icon *ngIf="getCurrentVisaUserIndex() === i && !adminMode" class="fa fa-chevron-right fa-2x" mat-list-icon color="accent">
+            <mat-icon *ngIf="getCurrentVisaUserIndex() === i && (!adminMode || target === 'signatureBook')" class="fa fa-chevron-right fa-2x" mat-list-icon color="accent">
             </mat-icon>
             <mat-icon
                 [ngClass]="{'fa fa-user fa-2x': functions.empty(diffusion.picture),'avatar': !functions.empty(diffusion.picture)}"
@@ -89,7 +89,7 @@
                     </div>
                     <div class="workflowLineSubLabel" *ngIf="(showComment && ((!linkedToMaarchParapheur && adminMode && functions.empty(diffusion.process_date)) || (!linkedToMaarchParapheur && getCurrentVisaUserIndex() === i && !functions.empty(diffusion.process_comment)))) && diffusion.hasPrivilege">
                         <mat-form-field>
-                            <input matInput class="comment" [disabled]="!adminMode || diffusion.process_date != null" [placeholder]="lang.visaNote" [(ngModel)]="diffusion.process_comment">
+                            <input matInput class="comment" [disabled]="!adminMode || diffusion.process_date != null || (target === 'signatureBook' && getCurrentVisaUserIndex() === i)" [placeholder]="lang.visaNote" [(ngModel)]="diffusion.process_comment">
                         </mat-form-field>
                     </div>
                     <div *ngIf="diffusion.process_date != null && [lang.visaWorkflowInterrupted, lang.hasInterruptedWorkflow].indexOf(diffusion.process_comment) === -1" class="workflowLineProcessDate"
@@ -102,7 +102,7 @@
                 </div>
                 <div *ngIf="diffusion.hasPrivilege">
                     <button class="currentRoleButton" [color]="diffusion.requested_signature ? 'primary': '' "
-                        [disabled]="!adminMode || !functions.empty(diffusion.process_date)" mat-raised-button
+                        [disabled]="!canManageUser(diffusion, i)" mat-raised-button
                         title="{{lang[diffusion.currentRole+'User']}}"
                         (click)="changeRole(i)">{{diffusion.requested_signature ? lang.signUser : lang.visaUser}}</button>
                 </div>
@@ -110,7 +110,7 @@
                     {{lang.noPrivileges}}
                 </div>
             </div>
-            <button mat-icon-button *ngIf="adminMode && functions.empty(diffusion.process_date)"
+            <button mat-icon-button *ngIf="canManageUser(diffusion, i)"
                 (click)="deleteItem(i)">
                 <mat-icon class="fa fa-times" color="warn"></mat-icon>
             </button>
diff --git a/src/frontend/app/visa/visa-workflow.component.ts b/src/frontend/app/visa/visa-workflow.component.ts
index c37686d6e9a..e8cb7654d92 100644
--- a/src/frontend/app/visa/visa-workflow.component.ts
+++ b/src/frontend/app/visa/visa-workflow.component.ts
@@ -6,7 +6,7 @@ import { CdkDragDrop, moveItemInArray } from '@angular/cdk/drag-drop';
 import { FunctionsService } from '../../service/functions.service';
 import { tap, exhaustMap, map, startWith, catchError, finalize, filter } from 'rxjs/operators';
 import { FormControl } from '@angular/forms';
-import { LatinisePipe } from 'ngx-pipes';
+import { LatinisePipe, ScanPipe } from 'ngx-pipes';
 import { Observable, of } from 'rxjs';
 import { MatDialog } from '@angular/material';
 import { AddVisaModelModalComponent } from './addVisaModel/add-visa-model-modal.component';
@@ -15,7 +15,8 @@ import { ConfirmComponent } from '../../plugins/modal/confirm.component';
 @Component({
     selector: 'app-visa-workflow',
     templateUrl: 'visa-workflow.component.html',
-    styleUrls: ['visa-workflow.component.scss']
+    styleUrls: ['visa-workflow.component.scss'],
+    providers: [ScanPipe]
 })
 export class VisaWorkflowComponent implements OnInit {
 
@@ -40,6 +41,7 @@ export class VisaWorkflowComponent implements OnInit {
     data: any;
 
     @Input('injectDatas') injectDatas: any;
+    @Input('target') target: string = '';
     @Input('adminMode') adminMode: boolean;
     @Input('resId') resId: number = null;
 
@@ -57,7 +59,8 @@ export class VisaWorkflowComponent implements OnInit {
         private notify: NotificationService,
         public functions: FunctionsService,
         private latinisePipe: LatinisePipe,
-        public dialog: MatDialog
+        public dialog: MatDialog,
+        private scanPipe: ScanPipe
     ) { }
 
     ngOnInit(): void {
@@ -69,10 +72,10 @@ export class VisaWorkflowComponent implements OnInit {
 
     drop(event: CdkDragDrop<string[]>) {
         if (event.previousContainer === event.container) {
-            if (this.functions.empty(this.visaWorkflow.items[event.currentIndex].process_date)) {
+            if (this.canManageUser(this.visaWorkflow.items[event.previousIndex], event.currentIndex)) {
                 moveItemInArray(event.container.data, event.previousIndex, event.currentIndex);
             } else {
-                this.notify.error(`${this.lang.moveVisaUserErr1} <b>${this.visaWorkflow.items[event.previousIndex].labelToDisplay}</b> ${this.lang.moveVisaUserErr2}.`);
+                this.notify.error(`${this.scanPipe.transform(this.lang.moveVisaUserErr, [this.visaWorkflow.items[event.previousIndex].labelToDisplay])}`);
             }
         }
     }
@@ -86,25 +89,25 @@ export class VisaWorkflowComponent implements OnInit {
 
         return new Promise((resolve, reject) => {
             this.http.get(route)
-            .subscribe((data: any) => {
-                if (data.listTemplates[0]) {
-                    this.visaWorkflow.items = data.listTemplates[0].items.map((item: any) => {
-                        return {
-                            ...item,
-                            item_entity: item.descriptionToDisplay,
-                            requested_signature: item.item_mode !== 'visa'
+                .subscribe((data: any) => {
+                    if (data.listTemplates[0]) {
+                        this.visaWorkflow.items = data.listTemplates[0].items.map((item: any) => {
+                            return {
+                                ...item,
+                                item_entity: item.descriptionToDisplay,
+                                requested_signature: item.item_mode !== 'visa'
+                            }
+                        });
+                    }
+                    this.visaWorkflow.items.forEach((element: any, key: number) => {
+                        if (!this.functions.empty(element['externalId'])) {
+                            this.getMaarchParapheurUserAvatar(element.externalId.maarchParapheur, key);
                         }
                     });
-                }
-                this.visaWorkflow.items.forEach((element: any, key: number) => {
-                    if (!this.functions.empty(element['externalId'])) {
-                        this.getMaarchParapheurUserAvatar(element.externalId.maarchParapheur, key);
-                    }
+                    this.visaWorkflowClone = JSON.parse(JSON.stringify(this.visaWorkflow.items));
+                    this.loading = false;
+                    resolve(true);
                 });
-                this.visaWorkflowClone = JSON.parse(JSON.stringify(this.visaWorkflow.items));
-                this.loading = false;
-                resolve(true);
-            });
         });
     }
 
@@ -215,7 +218,7 @@ export class VisaWorkflowComponent implements OnInit {
                 if (this.showListModels) {
                     await this.loadVisaModelList();
                 }
-                
+
                 this.searchVisaSignUser.reset();
 
                 this.visaModelListNotLoaded = false;
@@ -308,7 +311,7 @@ export class VisaWorkflowComponent implements OnInit {
         this.visaWorkflow.items = [];
         this.http.get(`../../rest/attachments/${attachmentId}/maarchParapheurWorkflow`)
             .subscribe((data: any) => {
-                data.workflow.forEach((element: any, key:any) => {
+                data.workflow.forEach((element: any, key: any) => {
                     const user = {
                         'listinstance_id': key,
                         'id': element.userId,
@@ -349,10 +352,12 @@ export class VisaWorkflowComponent implements OnInit {
     }
 
     getCurrentVisaUserIndex() {
-
-        const index = this.visaWorkflow.items.map((item: any) => item.listinstance_id).indexOf(this.getLastVisaUser().listinstance_id);
-
-        return (index + 1);
+        if (this.getLastVisaUser().listinstance_id === undefined) {
+            return 0;
+        } else {
+            const index = this.visaWorkflow.items.map((item: any) => item.listinstance_id).indexOf(this.getLastVisaUser().listinstance_id);
+            return (index + 1);
+        }
     }
 
     getFirstVisaUser() {
@@ -422,7 +427,7 @@ export class VisaWorkflowComponent implements OnInit {
         });
     }
 
-    addItemToWorkflow(item: any, maarchParapheurMode = false) {        
+    addItemToWorkflow(item: any, maarchParapheurMode = false) {
         return new Promise((resolve, reject) => {
             if (maarchParapheurMode) {
                 this.visaWorkflow.items.push({
@@ -434,7 +439,7 @@ export class VisaWorkflowComponent implements OnInit {
                     difflist_type: 'VISA_CIRCUIT',
                     signatory: !this.functions.empty(item.signatory) ? item.signatory : false,
                     requested_signature: !this.functions.empty(item.requested_signature) ? item.requested_signature : false,
-                    hasPrivilege : true
+                    hasPrivilege: true
                 });
                 if (this.linkedToMaarchParapheur) {
                     this.getMaarchParapheurUserAvatar(item.externalId.maarchParapheur, this.visaWorkflow.items.length - 1);
@@ -451,9 +456,9 @@ export class VisaWorkflowComponent implements OnInit {
                     difflist_type: 'VISA_CIRCUIT',
                     signatory: !this.functions.empty(item.signatory) ? item.signatory : false,
                     requested_signature: !this.functions.empty(item.requested_signature) ? item.requested_signature : false,
-                    hasPrivilege : item.hasPrivilege
+                    hasPrivilege: item.hasPrivilege
                 });
-    
+
                 if (this.linkedToMaarchParapheur) {
                     this.getMaarchParapheurUserAvatar(item.externalId.maarchParapheur, this.visaWorkflow.items.length - 1);
                 }
@@ -464,7 +469,7 @@ export class VisaWorkflowComponent implements OnInit {
                 this.http.get(`../../rest/listTemplates/${item.id}`).pipe(
                     tap((data: any) => {
                         this.visaWorkflow.items = this.visaWorkflow.items.concat(
-    
+
                             data.listTemplate.items.map((itemTemplate: any) => {
                                 return {
                                     item_id: itemTemplate.item_id,
@@ -474,7 +479,7 @@ export class VisaWorkflowComponent implements OnInit {
                                     difflist_type: 'VISA_CIRCUIT',
                                     signatory: false,
                                     requested_signature: itemTemplate.item_mode === 'sign',
-                                    hasPrivilege : itemTemplate.hasPrivilege
+                                    hasPrivilege: itemTemplate.hasPrivilege
                                 }
                             })
                         );
@@ -484,7 +489,7 @@ export class VisaWorkflowComponent implements OnInit {
                     })
                 ).subscribe();
             }
-        });        
+        });
     }
 
     resetWorkflow() {
@@ -502,7 +507,7 @@ export class VisaWorkflowComponent implements OnInit {
     getError() {
         if (this.visaWorkflow.items.filter((item: any) => item.requested_signature).length === 0) {
             return this.lang.signUserRequired;
-        } else if(this.visaWorkflow.items.filter((item: any) => !item.hasPrivilege).length > 0) {
+        } else if (this.visaWorkflow.items.filter((item: any) => !item.hasPrivilege).length > 0) {
             return this.lang.mustDeleteUsersWithNoPrivileges;
         }
     }
@@ -570,7 +575,19 @@ export class VisaWorkflowComponent implements OnInit {
         }
     }
 
-    isModified() {        
+    isModified() {
         return !(this.loading || JSON.stringify(this.visaWorkflow.items) === JSON.stringify(this.visaWorkflowClone));
     }
+
+    canManageUser(item: any, i: number) {
+        if (this.adminMode) {
+            if (!this.functions.empty(item.process_date) || (this.target === 'signatureBook' && this.getCurrentVisaUserIndex() === i)) {
+                return false;
+            } else {
+                return true;
+            }
+        } else {
+            return false;
+        }
+    }
 }
diff --git a/src/frontend/lang/lang-en.ts b/src/frontend/lang/lang-en.ts
index 81a1438d2b4..6a73e5a98c4 100755
--- a/src/frontend/lang/lang-en.ts
+++ b/src/frontend/lang/lang-en.ts
@@ -1412,8 +1412,7 @@ export const LANG_EN = {
     "hasInterruptedWorkflow" : "Has interrupted the workflow",
     "publicModel" : "Public model",
     "privateModel" : "Private model",
-    "moveVisaUserErr1" : "You cannot move", 
-    "moveVisaUserErr2" : "with users who have already approved / signed",
+    "moveVisaUserErr" : "You cannot move <p>{0}</p> with users who have already approved / signed or sign / approval is in progress.", 
     "moveAvisUserErr1" : "You cannot move", 
     "moveAvisUserErr2" : "with users who have already given an opinion", 
     "avisWorkflowUpdated" : "Opinion workflow updated",
diff --git a/src/frontend/lang/lang-fr.ts b/src/frontend/lang/lang-fr.ts
index 53c05ad33fb..707369414d1 100755
--- a/src/frontend/lang/lang-fr.ts
+++ b/src/frontend/lang/lang-fr.ts
@@ -1452,8 +1452,7 @@ export const LANG_FR = {
     "hasInterruptedWorkflow" : "A terminé le circuit",
     "publicModel" : "Modèle public",
     "privateModel" : "Modèle privé", 
-    "moveVisaUserErr1" : "Vous ne pouvez pas déplacer", 
-    "moveVisaUserErr2" : "avec des personnes ayant déja visé / signé", 
+    "moveVisaUserErr" : "Vous ne pouvez pas déplacer <b>{0}</b> avec des personnes ayant déja visé / signé ou en cours de visa / signature.", 
     "moveAvisUserErr1" : "Vous ne pouvez pas déplacer", 
     "moveAvisUserErr2" : "avec des personnes ayant déja donné un avis", 
     "avisWorkflowUpdated" : "Circuit d'avis modifié",
diff --git a/src/frontend/lang/lang-nl.ts b/src/frontend/lang/lang-nl.ts
index 6b10f52e80a..43c04edaa7f 100755
--- a/src/frontend/lang/lang-nl.ts
+++ b/src/frontend/lang/lang-nl.ts
@@ -1435,8 +1435,7 @@ export const LANG_NL = {
     "visaWorkflowDeleted" : "Visa workflow deleted", //_TO_TRANSLATE
     "publicModel" : "Public model", //_TO_TRANSLATE
     "privateModel" : "Private model", //_TO_TRANSLATE
-    "moveVisaUserErr1" : "You cannot move", //_TO_TRANSLATE
-    "moveVisaUserErr2" : "with users who have already approved / signed", //_TO_TRANSLATE
+    "moveVisaUserErr" : "You cannot move <p>{0}</p> with users who have already approved / signed or sign / approval is in progress.", //_TO_TRANSLATE
     "moveAvisUserErr1" : "You cannot move", //_TO_TRANSLATE
     "moveAvisUserErr2" : "with users who have already given an opinion", //_TO_TRANSLATE
     "avisWorkflowUpdated" : "Opinion workflow updated", //_TO_TRANSLATE
-- 
GitLab