From a63e03895e801ed20a398aa0b400fbe7be833cec Mon Sep 17 00:00:00 2001
From: Alex ORLUC <alex.orluc@maarch.org>
Date: Mon, 4 Oct 2021 16:12:48 +0200
Subject: [PATCH] FEAT #14839 TIME 1 add action page send multigest

---
 src/frontend/app/actions/actions.service.ts   | 24 +++++
 .../send-multigest-action.component.html      | 37 +++++++
 .../send-multigest-action.component.scss      | 27 ++++++
 .../send-multigest-action.component.ts        | 96 +++++++++++++++++++
 src/frontend/app/app.module.ts                |  3 +
 src/frontend/service/actionPages.service.ts   | 10 +-
 src/lang/lang-en.json                         |  3 +-
 src/lang/lang-fr.json                         |  3 +-
 8 files changed, 200 insertions(+), 3 deletions(-)
 create mode 100644 src/frontend/app/actions/send-multigest-action/send-multigest-action.component.html
 create mode 100644 src/frontend/app/actions/send-multigest-action/send-multigest-action.component.scss
 create mode 100644 src/frontend/app/actions/send-multigest-action/send-multigest-action.component.ts

diff --git a/src/frontend/app/actions/actions.service.ts b/src/frontend/app/actions/actions.service.ts
index ef3278dfc6d..6f1b9665b5c 100644
--- a/src/frontend/app/actions/actions.service.ts
+++ b/src/frontend/app/actions/actions.service.ts
@@ -36,6 +36,7 @@ import { HeaderService } from '@service/header.service';
 import { FunctionsService } from '@service/functions.service';
 import { ReconcileActionComponent } from './reconciliation-action/reconcile-action.component';
 import { SendAlfrescoActionComponent } from './send-alfresco-action/send-alfresco-action.component';
+import { SendMultigestActionComponent } from './send-multigest-action/send-multigest-action.component';
 import { SaveRegisteredMailActionComponent } from './save-registered-mail-action/save-registered-mail-action.component';
 import { SaveAndPrintRegisteredMailActionComponent } from './save-and-print-registered-mail-action/save-and-print-registered-mail-action.component';
 import { SaveAndIndexRegisteredMailActionComponent } from './save-and-index-registered-mail-action/save-and-index-registered-mail-action.component';
@@ -987,6 +988,29 @@ export class ActionsService implements OnDestroy {
         ).subscribe();
     }
 
+    sendMultigestAction(options: any = null) {
+        const dialogRef = this.dialog.open(SendMultigestActionComponent, {
+            panelClass: 'maarch-modal',
+            autoFocus: false,
+            disableClose: true,
+            data: this.setDatasActionToSend()
+        });
+        dialogRef.afterClosed().pipe(
+            tap((data: any) => {
+                this.unlockResourceAfterActionModal(data);
+            }),
+            filter((data: string) => data === 'success'),
+            tap((result: any) => {
+                this.endAction(result);
+            }),
+            finalize(() => this.loading = false),
+            catchError((err: any) => {
+                this.notify.handleErrors(err);
+                return of(false);
+            })
+        ).subscribe();
+    }
+
     saveRegisteredMailAction(options: any = null) {
 
         const dialogRef = this.dialog.open(SaveRegisteredMailActionComponent, {
diff --git a/src/frontend/app/actions/send-multigest-action/send-multigest-action.component.html b/src/frontend/app/actions/send-multigest-action/send-multigest-action.component.html
new file mode 100644
index 00000000000..19949169f1a
--- /dev/null
+++ b/src/frontend/app/actions/send-multigest-action/send-multigest-action.component.html
@@ -0,0 +1,37 @@
+<div class="mat-dialog-content-container">
+    <h1 mat-dialog-title>{{data.action.label}}</h1>
+    <div mat-dialog-content>
+        <div *ngIf="loading" class="loading" style="display:flex;height:100%;">
+            <mat-spinner style="margin:auto;"></mat-spinner>
+        </div>
+        <div class="row" style="margin: 0;">
+            <div class="col-md-12">
+                {{'lang.makeActionOn' | translate}}
+                <b *ngIf="data.resIds.length === 0" color="primary" class="highlight">{{'lang.currentIndexingMail' | translate}}</b>
+                <b *ngIf="data.resIds.length == 1" color="primary" class="highlight">{{data.resource.chrono}}</b>
+                <b *ngIf="data.resIds.length > 1" color="primary" class="highlight">{{data.resIds.length}}
+                    {{'lang.elements' | translate}}</b> ?
+            </div>
+            <br />
+            <div *ngIf="resourcesErrors.length > 0" class="alert-message alert-message-danger mailList" role="alert">
+                <p>
+                    {{'lang.canNotMakeAction' | translate}} :
+                </p>
+                <ul>
+                    <li *ngFor="let ressource of resourcesErrors">
+                        <b>{{ressource.alt_identifier}}</b> : {{'lang.' + ressource.reason | translate}}
+                    </li>
+                </ul>
+            </div>
+            <div class="col-md-12" style="padding-top: 10px;">
+                <app-note-editor #noteEditor [resIds]="data.resIds"></app-note-editor>
+            </div>
+        </div>
+    </div>
+    <span class="divider-modal"></span>
+    <div mat-dialog-actions class="actions">
+        <button mat-raised-button mat-button color="primary" [disabled]="loading || !isValidAction()"
+        (click)="onSubmit()">{{'lang.validate' | translate}}</button>
+    <button mat-raised-button mat-button [disabled]="loading" [mat-dialog-close]="">{{'lang.cancel' | translate}}</button>
+    </div>
+</div>
\ No newline at end of file
diff --git a/src/frontend/app/actions/send-multigest-action/send-multigest-action.component.scss b/src/frontend/app/actions/send-multigest-action/send-multigest-action.component.scss
new file mode 100644
index 00000000000..b43aef62f83
--- /dev/null
+++ b/src/frontend/app/actions/send-multigest-action/send-multigest-action.component.scss
@@ -0,0 +1,27 @@
+
+.loading {
+  display: flex;
+  position: absolute;
+  top: 0;
+  left: 0;
+  width: 100%;
+  height: 100%;
+  background: #ffffffb3;
+  z-index: 2;
+  overflow: hidden;
+}
+
+.smallInput {
+  font-size: 9px;
+  padding-left: 20px;
+  padding-right: 20px;
+
+  ::ng-deep.mat-form-field-flex {
+    display: flex;
+    align-items: center;
+  }
+
+  .mat-icon {
+    height: auto;
+  }
+}
\ No newline at end of file
diff --git a/src/frontend/app/actions/send-multigest-action/send-multigest-action.component.ts b/src/frontend/app/actions/send-multigest-action/send-multigest-action.component.ts
new file mode 100644
index 00000000000..385dce15966
--- /dev/null
+++ b/src/frontend/app/actions/send-multigest-action/send-multigest-action.component.ts
@@ -0,0 +1,96 @@
+import { Component, OnInit, Inject, ViewChild } from '@angular/core';
+import { TranslateService } from '@ngx-translate/core';
+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 { FunctionsService } from '@service/functions.service';
+import { of } from 'rxjs';
+
+
+@Component({
+    templateUrl: 'send-multigest-action.component.html',
+    styleUrls: ['send-multigest-action.component.scss'],
+})
+export class SendMultigestActionComponent implements OnInit {
+
+    @ViewChild('noteEditor', { static: true }) noteEditor: NoteEditorComponent;
+
+    loading: boolean = false;
+
+    errors: any;
+
+    resourcesErrors: any[] = [];
+    noResourceToProcess: boolean = null;
+
+    constructor(
+        public translate: TranslateService,
+        public http: HttpClient,
+        private notify: NotificationService,
+        public dialogRef: MatDialogRef<SendMultigestActionComponent>,
+        @Inject(MAT_DIALOG_DATA) public data: any,
+        public functions: FunctionsService
+    ) { }
+
+    async ngOnInit(): Promise<void> {
+        this.loading = true;
+        // await this.checkMultigest();
+        this.loading = false;
+    }
+
+    checkMultigest() {
+        this.resourcesErrors = [];
+
+        return new Promise((resolve, reject) => {
+            this.http.post('../rest/resourcesList/users/' + this.data.userId + '/groups/' + this.data.groupId + '/baskets/' + this.data.basketId + '/actions/' + this.data.action.id + '/checkMultigest', { resources: this.data.resIds })
+                .subscribe((data: any) => {
+                    if (!this.functions.empty(data.fatalError)) {
+                        this.notify.error(this.translate.instant('lang.' + data.reason));
+                        this.dialogRef.close();
+                    } else if (!this.functions.empty(data.resourcesInformations.error)) {
+                        this.resourcesErrors = data.resourcesInformations.error;
+                        this.noResourceToProcess = this.resourcesErrors.length === this.data.resIds.length;
+                    }
+                    resolve(true);
+                }, (err: any) => {
+                    this.notify.handleSoftErrors(err);
+                    this.dialogRef.close();
+                });
+        });
+    }
+
+    onSubmit() {
+        this.loading = true;
+
+        if (this.data.resIds.length > 0) {
+            this.executeAction();
+        }
+    }
+
+    executeAction() {
+
+        const realResSelected: number[] = this.data.resIds.filter((resId: any) => this.resourcesErrors.map(resErr => resErr.res_id).indexOf(resId) === -1);
+
+        this.http.put(this.data.processActionRoute, { resources: realResSelected, note: this.noteEditor.getNoteContent() }).pipe(
+            tap((data: any) => {
+                if (!data) {
+                    this.dialogRef.close('success');
+                }
+                if (data && data.errors != null) {
+                    this.notify.error(data.errors);
+                }
+            }),
+            finalize(() => this.loading = false),
+            catchError((err: any) => {
+                this.notify.handleErrors(err);
+                return of(false);
+            })
+        ).subscribe();
+    }
+
+    isValidAction() {
+        return !this.noResourceToProcess;
+    }
+
+}
diff --git a/src/frontend/app/app.module.ts b/src/frontend/app/app.module.ts
index 9f592d7f7c9..27467824db7 100755
--- a/src/frontend/app/app.module.ts
+++ b/src/frontend/app/app.module.ts
@@ -74,6 +74,7 @@ import { GiveAvisParallelActionComponent } from './actions/avis-give-parallel-ac
 import { ValidateAvisParallelComponent } from './actions/avis-parallel-validate-action/validate-avis-parallel-action.component';
 import { ReconcileActionComponent } from './actions/reconciliation-action/reconcile-action.component';
 import { SendAlfrescoActionComponent } from './actions/send-alfresco-action/send-alfresco-action.component';
+import { SendMultigestActionComponent } from './actions/send-multigest-action/send-multigest-action.component';
 import { SaveRegisteredMailActionComponent } from './actions/save-registered-mail-action/save-registered-mail-action.component';
 import { SaveAndIndexRegisteredMailActionComponent } from './actions/save-and-index-registered-mail-action/save-and-index-registered-mail-action.component';
 import { SaveAndPrintRegisteredMailActionComponent } from './actions/save-and-print-registered-mail-action/save-and-print-registered-mail-action.component';
@@ -275,6 +276,7 @@ export class MyHammerConfig extends HammerGestureConfig {
         GiveAvisParallelActionComponent,
         ValidateAvisParallelComponent,
         SendAlfrescoActionComponent,
+        SendMultigestActionComponent,
         SaveRegisteredMailActionComponent,
         SaveAndPrintRegisteredMailActionComponent,
         SaveAndIndexRegisteredMailActionComponent,
@@ -349,6 +351,7 @@ export class MyHammerConfig extends HammerGestureConfig {
         closeMailWithAttachmentsOrNotesActionComponent,
         SendSignatureBookActionComponent,
         SendAlfrescoActionComponent,
+        SendMultigestActionComponent,
         SaveRegisteredMailActionComponent,
         SaveAndPrintRegisteredMailActionComponent,
         SaveAndIndexRegisteredMailActionComponent,
diff --git a/src/frontend/service/actionPages.service.ts b/src/frontend/service/actionPages.service.ts
index be493f7d2df..f60aaad0198 100755
--- a/src/frontend/service/actionPages.service.ts
+++ b/src/frontend/service/actionPages.service.ts
@@ -6,7 +6,7 @@ interface actionPages {
     'label': string; // title
     'name': string; // name
     'component': string; // action service component
-    'category': 'application' | 'acknowledgementReceipt' | 'externalSignatoryBook' | 'visa' | 'avis' | 'maileva' | 'alfresco' | 'registeredMail' | 'recordManagement'; // category
+    'category': 'application' | 'acknowledgementReceipt' | 'externalSignatoryBook' | 'visa' | 'avis' | 'maileva' | 'alfresco' | 'multigest' | 'registeredMail' | 'recordManagement'; // category
     'description': string; // description
 }
 
@@ -238,6 +238,14 @@ export class ActionPagesService {
             'category': 'alfresco',
             'description': this.translate.instant('lang.sendAlfresco')
         },
+        {
+            'id': 'send_multigest',
+            'label': this.translate.instant('lang.sendMultigest'),
+            'name': 'send_multigest',
+            'component': 'sendMultigestAction',
+            'category': 'multigest',
+            'description': this.translate.instant('lang.sendMultigest')
+        },
         {
             'id': 'saveRegisteredMail',
             'label': this.translate.instant('lang.saveRegisteredMail'),
diff --git a/src/lang/lang-en.json b/src/lang/lang-en.json
index 188a29d575b..67895cf11d1 100644
--- a/src/lang/lang-en.json
+++ b/src/lang/lang-en.json
@@ -2544,5 +2544,6 @@
     "switchSae": "Configure Maarch RM",
     "configSaeInfo": "For more information on setting up an electronic filing system.Consult the documentation : <a href=\"{{url}}\" target=\"_blank\" class=\"link\"> Configure an SAE < / a>",
     "delegatedOpinion": "Opinion given instead of",
-    "requestedOpinion": "Opinion requested instead of"
+    "requestedOpinion": "Opinion requested instead of",
+    "sendMultigest": "Send to Multigest"
 }
diff --git a/src/lang/lang-fr.json b/src/lang/lang-fr.json
index 9a25135db54..db4f10f250a 100644
--- a/src/lang/lang-fr.json
+++ b/src/lang/lang-fr.json
@@ -2549,5 +2549,6 @@
     "multigestModification": "Modification d'un compte Multigest",
     "multigestAccount": "Compte Multigest",
     "multigestAPI": "API multigest",
-    "adminMultigestDesc": "Configurer les comptes Multigest"
+    "adminMultigestDesc": "Configurer les comptes Multigest",
+    "sendMultigest": "Envoyer vers Multigest"
 }
-- 
GitLab