From 5dd7ea066f2e24bfb2d12ec2ddcd12ee5425e717 Mon Sep 17 00:00:00 2001
From: Alex ORLUC <alex.orluc@maarch.org>
Date: Tue, 5 Nov 2019 11:45:22 +0100
Subject: [PATCH] FEAT #12074 TIME 0:35 add unlock gard

---
 src/frontend/app/actions/actions.service.ts   |  2 +
 src/frontend/app/app-routing.module.ts        |  4 +-
 .../app/indexation/indexation.component.ts    |  1 -
 src/frontend/app/process/process.component.ts | 70 +++++++++++++------
 src/frontend/service/app.guard.ts             | 26 +++++--
 5 files changed, 74 insertions(+), 29 deletions(-)

diff --git a/src/frontend/app/actions/actions.service.ts b/src/frontend/app/actions/actions.service.ts
index e05a91acbcf..5f198a22d25 100644
--- a/src/frontend/app/actions/actions.service.ts
+++ b/src/frontend/app/actions/actions.service.ts
@@ -511,6 +511,8 @@ export class ActionsService {
     }
 
     processDocument() {
+        this.stopRefreshResourceLock();
+        this.unlockResource();
         this.router.navigate([`/process/users/${this.currentUserId}/groups/${this.currentGroupId}/baskets/${this.currentBasketId}/resId/${this.currentResIds}`]);
     }
 }
diff --git a/src/frontend/app/app-routing.module.ts b/src/frontend/app/app-routing.module.ts
index 559d793c2aa..c8462ce8334 100755
--- a/src/frontend/app/app-routing.module.ts
+++ b/src/frontend/app/app-routing.module.ts
@@ -10,7 +10,7 @@ import { BasketListComponent }              from './list/basket-list.component';
 import { SignatureBookComponent }           from './signature-book.component';
 import { SaveNumericPackageComponent }      from './save-numeric-package.component';
 import { PrintSeparatorComponent }          from './separator/print-separator/print-separator.component';
-import { AppGuard }                         from '../service/app.guard';
+import { AppGuard, AfterProcessGuard }     from '../service/app.guard';
 import { FolderDocumentListComponent }      from './folder/document-list/folder-document-list.component';
 import { IndexationComponent }              from './indexation/indexation.component';
 import { ProcessComponent } from './process/process.component';
@@ -24,7 +24,7 @@ import { ProcessComponent } from './process/process.component';
             { path: 'about-us', canActivate: [AppGuard], component: AboutUsComponent },
             { path: 'home', canActivate: [AppGuard],  component: HomeComponent },
             { path: 'basketList/users/:userSerialId/groups/:groupSerialId/baskets/:basketId', canActivate: [AppGuard], component: BasketListComponent },
-            { path: 'process/users/:userSerialId/groups/:groupSerialId/baskets/:basketId/resId/:resId', canActivate: [AppGuard], component: ProcessComponent },
+            { path: 'process/users/:userSerialId/groups/:groupSerialId/baskets/:basketId/resId/:resId', canActivate: [AppGuard], canDeactivate: [AfterProcessGuard], component: ProcessComponent },
             { path: 'folders/:folderId', canActivate: [AppGuard], component: FolderDocumentListComponent },
             { path: 'saveNumericPackage', canActivate: [AppGuard], component: SaveNumericPackageComponent },
             { path: 'separators/print', canActivate: [AppGuard], component: PrintSeparatorComponent },
diff --git a/src/frontend/app/indexation/indexation.component.ts b/src/frontend/app/indexation/indexation.component.ts
index b30976fbb56..40076f8736f 100644
--- a/src/frontend/app/indexation/indexation.component.ts
+++ b/src/frontend/app/indexation/indexation.component.ts
@@ -18,7 +18,6 @@ import { DocumentViewerComponent } from '../viewer/document-viewer.component';
 import { ConfirmComponent } from '../../plugins/modal/confirm.component';
 import { AddPrivateIndexingModelModalComponent } from './private-indexing-model/add-private-indexing-model-modal.component';
 import { ActionsService } from '../actions/actions.service';
-import { AlertComponent } from '../../plugins/modal/alert.component';
 
 @Component({
     templateUrl: "indexation.component.html",
diff --git a/src/frontend/app/process/process.component.ts b/src/frontend/app/process/process.component.ts
index 05a744779cf..302f33f8d08 100644
--- a/src/frontend/app/process/process.component.ts
+++ b/src/frontend/app/process/process.component.ts
@@ -30,6 +30,8 @@ export class ProcessComponent implements OnInit {
 
     loading: boolean = false;
 
+    currentResourceLock: any = null;
+
     actionsList: any[] = [];
     currentUserId: number = null;
     currentBasketId: number = null;
@@ -50,53 +52,53 @@ export class ProcessComponent implements OnInit {
     processTool: any[] = [
         {
             id: 'dashboard',
-            icon : 'fas fa-columns',
-            label: this.lang.dashboard,   
+            icon: 'fas fa-columns',
+            label: this.lang.dashboard,
         },
         {
             id: 'history',
-            icon : 'fas fa-history',
+            icon: 'fas fa-history',
             label: this.lang.history,
         },
         {
             id: 'notes',
-            icon : 'fas fa-pen-square',
-            label: this.lang.notes,   
+            icon: 'fas fa-pen-square',
+            label: this.lang.notes,
         },
         {
             id: 'attachments',
-            icon : 'fas fa-paperclip',
-            label: this.lang.attachments,   
+            icon: 'fas fa-paperclip',
+            label: this.lang.attachments,
         },
         {
             id: 'link',
-            icon : 'fas fa-link',
-            label: this.lang.links,   
+            icon: 'fas fa-link',
+            label: this.lang.links,
         },
         {
             id: 'diffusionList',
-            icon : 'fas fa-share-alt',
-            label: this.lang.diffusionList,   
+            icon: 'fas fa-share-alt',
+            label: this.lang.diffusionList,
         },
         {
             id: 'mails',
-            icon : 'fas fa-envelope',
-            label: this.lang.mailsSent,   
+            icon: 'fas fa-envelope',
+            label: this.lang.mailsSent,
         },
         {
             id: 'visa',
-            icon : 'fas fa-list-ol',
-            label: this.lang.visaWorkflow,   
+            icon: 'fas fa-list-ol',
+            label: this.lang.visaWorkflow,
         },
         {
             id: 'avis',
-            icon : 'fas fa-comment-alt',
-            label: this.lang.avis,   
+            icon: 'fas fa-comment-alt',
+            label: this.lang.avis,
         },
         {
             id: 'info',
-            icon : 'fas fa-info-circle',
-            label: this.lang.informations,   
+            icon: 'fas fa-info-circle',
+            label: this.lang.informations,
         }
     ];
 
@@ -108,7 +110,7 @@ export class ProcessComponent implements OnInit {
     @ViewChild('snav2', { static: true }) sidenavRight: MatSidenav;
 
     @ViewChild('appDocumentViewer', { static: true }) appDocumentViewer: DocumentViewerComponent;
-
+    
     constructor(
         private route: ActivatedRoute,
         private _activatedRoute: ActivatedRoute,
@@ -134,13 +136,15 @@ export class ProcessComponent implements OnInit {
             this.currentUserId = params['userSerialId'];
             this.currentGroupId = params['groupSerialId'];
             this.currentBasketId = params['basketId'];
-            
+
             this.currentResourceInformations = {
                 resId: params['resId'],
                 category: 'outgoing',
                 mailtracking: false
             }
 
+            this.lockResource();
+
             this.loading = false;
 
             // TO : WAIT RIGHT ROUTE FOR PROCESS
@@ -187,8 +191,30 @@ export class ProcessComponent implements OnInit {
             });
     }
 
+    lockResource() {
+        this.currentResourceLock = setInterval(() => {
+            this.http.put(`../../rest/resourcesList/users/${this.currentUserId}/groups/${this.currentGroupId}/baskets/${this.currentBasketId}/lock`, { resources: [this.currentResourceInformations.resId] }).pipe(
+                catchError((err: any) => {
+                    this.notify.handleErrors(err);
+                    return of(false);
+                })
+            ).subscribe();
+        }, 50000);
+    }
+
+    unlockResource() {
+        clearInterval(this.currentResourceLock);
+
+        this.http.put(`../../rest/resourcesList/users/${this.currentUserId}/groups/${this.currentGroupId}/baskets/${this.currentBasketId}/unlock`, { resources: [this.currentResourceInformations.resId] }).pipe(
+            catchError((err: any) => {
+                this.notify.handleErrors(err);
+                return of(false);
+            })
+        ).subscribe();
+    }
+
     onSubmit() {
-        this.actionService.launchAction(this.selectedAction,  this.currentUserId, this.currentGroupId, this.currentBasketId, this.currentResourceInformations.resId, this.currentResourceInformations);
+        this.actionService.launchAction(this.selectedAction, this.currentUserId, this.currentGroupId, this.currentBasketId, this.currentResourceInformations.resId, this.currentResourceInformations);
     }
 
     showActionInCurrentCategory(action: any) {
diff --git a/src/frontend/service/app.guard.ts b/src/frontend/service/app.guard.ts
index fe568ebf84a..f1db35bb1e6 100644
--- a/src/frontend/service/app.guard.ts
+++ b/src/frontend/service/app.guard.ts
@@ -1,10 +1,11 @@
 
 import { Injectable } from '@angular/core';
-import { ActivatedRouteSnapshot, CanActivate, Router, RouterStateSnapshot } from '@angular/router';
+import { ActivatedRouteSnapshot, CanActivate, Router, RouterStateSnapshot, CanDeactivate } from '@angular/router';
 import { HttpClient } from '@angular/common/http';
 import { Observable } from 'rxjs';
 import { map } from 'rxjs/operators';
 import { HeaderService } from './header.service';
+import { ProcessComponent } from '../app/process/process.component';
 
 @Injectable({
     providedIn: 'root'
@@ -24,9 +25,9 @@ export class AppGuard implements CanActivate {
                 .pipe(
                     map((data: any) => {
                         this.headerService.user = {
-                            id : data.id,
-                            userId : data.user_id,
-                            firstname : data.firstname,
+                            id: data.id,
+                            userId: data.user_id,
+                            firstname: data.firstname,
                             lastname: data.lastname,
                             entities: data.entities,
                             groups: data.groups,
@@ -40,3 +41,20 @@ export class AppGuard implements CanActivate {
         }
     }
 }
+
+@Injectable({
+    providedIn: 'root'
+})
+export class AfterProcessGuard implements CanDeactivate<ProcessComponent> {
+    canDeactivate(component: ProcessComponent): boolean {
+        component.unlockResource();
+        /*if(component.hasUnsavedData()){
+            if (confirm("You have unsaved changes! If you leave, your changes will be lost.")) {
+                return true;
+            } else {
+                return false;
+            }
+        }*/
+        return true;
+    }
+}
\ No newline at end of file
-- 
GitLab