From b0f60d1d9ed416be4c025c3b559c27ab2ccd408e Mon Sep 17 00:00:00 2001
From: Jean-Laurent <jean-laurent.duzant@xelians.fr>
Date: Mon, 23 May 2022 16:56:03 +0200
Subject: [PATCH] FIX #20391 TIME 2:40 analyse, add function calcLimitDateToday
 && update processLimitDate if clock is active from processLimitDate

---
 .../controllers/IndexingController.php        |  5 ++
 .../indexing-form/indexing-form.component.ts  | 48 +++++++++++++++++--
 2 files changed, 50 insertions(+), 3 deletions(-)

diff --git a/src/app/resource/controllers/IndexingController.php b/src/app/resource/controllers/IndexingController.php
index fc4d01bb1cb..7c0f108a733 100755
--- a/src/app/resource/controllers/IndexingController.php
+++ b/src/app/resource/controllers/IndexingController.php
@@ -244,6 +244,8 @@ class IndexingController
     {
         $queryParams = $request->getQueryParams();
 
+        // if delay is 0, then the process limit date is today
+        $delay = 0;
         if (!empty($queryParams['doctype'])) {
             $doctype = DoctypeModel::getById(['id' => $queryParams['doctype'], 'select' => ['process_delay']]);
             if (empty($doctype)) {
@@ -258,6 +260,9 @@ class IndexingController
             }
             $delay = $priority['delays'];
         }
+        if (!empty($queryParams['today'])) {
+            $delay = 0;
+        }
         if (!Validator::intVal()->validate($delay)) {
             return $response->withStatus(400)->withJson(['errors' => 'Delay is not a numeric value']);
         }
diff --git a/src/frontend/app/indexation/indexing-form/indexing-form.component.ts b/src/frontend/app/indexation/indexing-form/indexing-form.component.ts
index b0f54260bea..1b9b9948132 100755
--- a/src/frontend/app/indexation/indexing-form/indexing-form.component.ts
+++ b/src/frontend/app/indexation/indexing-form/indexing-form.component.ts
@@ -266,6 +266,8 @@ export class IndexingFormComponent implements OnInit {
     indexingModelClone: any;
     resDataClone: any;
 
+    isProcessLimitDateToday: boolean = false;
+
     constructor(
         public translate: TranslateService,
         public http: HttpClient,
@@ -997,7 +999,16 @@ export class IndexingFormComponent implements OnInit {
                     this.indexingModelClone = JSON.parse(JSON.stringify(data.indexingModel));
                 }
 
-                await this.initElemForm(saveResourceState);
+                await this.initElemForm(saveResourceState).then(() => {
+                    // check if clock is active
+                    if (!this.functions.empty(this['indexingModels_process'])) {
+                        const processLimitDate = this['indexingModels_process'].find((element: any) => element.identifier === 'processLimitDate');
+                        if (!this.functions.empty(processLimitDate.today)) {
+                            this.isProcessLimitDateToday = true;
+                            this.calcLimitDateToday();
+                        }
+                    }
+                });
                 this.createForm();
 
             }),
@@ -1187,7 +1198,8 @@ export class IndexingFormComponent implements OnInit {
         let limitDate: any = null;
         const objToSend: any = {
             doctype: value,
-            priority: this.arrFormControl['priority'].value
+            priority: this.arrFormControl['priority'].value,
+            today: this.isProcessLimitDateToday
         };
         if (this.functions.empty(this.arrFormControl['priority'].value)) {
             delete objToSend.priority;
@@ -1212,11 +1224,41 @@ export class IndexingFormComponent implements OnInit {
         }
     }
 
+    /**
+     * @description calculate the limit date if the clock is active
+     * @returns void
+     */
+     calcLimitDateToday() {
+        let limitDate: any = null;
+        const objToSend: any = {
+            today: true
+        };
+        if (this.arrFormControl['processLimitDate'] !== undefined) {
+            this.http.get('../rest/indexing/processLimitDate', { params: objToSend }).pipe(
+                tap((data: any) => {
+                    limitDate = data.processLimitDate !== null ? new Date(data.processLimitDate) : '';
+                    this.arrFormControl['processLimitDate'].setValue(limitDate);
+                }),
+                filter((data) => this.arrFormControl['priority'] !== undefined && data.processLimitDate !== null),
+                exhaustMap(() => this.http.get('../rest/indexing/priority', { params: { 'processLimitDate': limitDate.toDateString() } })),
+                tap((data: any) => {
+                    this.arrFormControl['priority'].setValue(data.priority);
+                    this.setPriorityColor(null, data.priority);
+                }),
+                catchError((err: any) => {
+                    this.notify.handleErrors(err);
+                    return of(false);
+                })
+            ).subscribe();
+        }
+    }
+
     calcLimitDateByPriority(field: any, value: any) {
         let limitDate: any = null;
         const objToSend: any = {
             priority: value,
-            doctype: this.arrFormControl['doctype'].value
+            doctype: this.arrFormControl['doctype'].value,
+            today: this.isProcessLimitDateToday
         };
         if (this.functions.empty(this.arrFormControl['doctype'].value)) {
             delete objToSend.doctype;
-- 
GitLab