From 3ec7f61d19e47d13acf10e4dd4f5aa04f4997173 Mon Sep 17 00:00:00 2001
From: "florian.azizian" <florian.azizian@maarch.org>
Date: Mon, 7 Jun 2021 18:38:22 +0200
Subject: [PATCH] FEAT #17191 TIME 1 Display delegate user in notes list

---
 sql/structure.sql                                   |  1 +
 src/app/document/controllers/DocumentController.php | 13 ++++++++++++-
 src/frontend/app/document/document.component.html   |  2 +-
 3 files changed, 14 insertions(+), 2 deletions(-)

diff --git a/sql/structure.sql b/sql/structure.sql
index 7e64bba36c..463970a9f0 100755
--- a/sql/structure.sql
+++ b/sql/structure.sql
@@ -260,6 +260,7 @@ CREATE TABLE workflows
     note text DEFAULT NULL,
     process_date timestamp without time zone DEFAULT NULL,
     digital_signature_id text,
+    delegate INTEGER,
     CONSTRAINT workflow_pkey PRIMARY KEY (id)
 )
 WITH (OIDS=FALSE);
diff --git a/src/app/document/controllers/DocumentController.php b/src/app/document/controllers/DocumentController.php
index 508468180c..095f5208af 100755
--- a/src/app/document/controllers/DocumentController.php
+++ b/src/app/document/controllers/DocumentController.php
@@ -176,7 +176,7 @@ class DocumentController
             }
         }
 
-        $workflow = WorkflowModel::getByDocumentId(['select' => ['id', 'user_id', 'mode', 'process_date', 'signature_mode', 'status', 'note', 'signature_positions', 'date_positions'], 'documentId' => $args['id'], 'orderBy' => ['"order"']]);
+        $workflow = WorkflowModel::getByDocumentId(['select' => ['id', 'user_id', 'mode', 'process_date', 'signature_mode', 'status', 'note', 'signature_positions', 'date_positions', 'delegate'], 'documentId' => $args['id'], 'orderBy' => ['"order"']]);
         $currentFound = false;
         $currentId = null;
         foreach ($workflow as $value) {
@@ -197,6 +197,12 @@ class DocumentController
                 unset($workflowExternalInformations['informations']['yousignFileId'], $workflowExternalInformations['informations']['yousignProcedureId']);
             }
 
+            $noteCreator = null;
+            if (!empty($value['note'])) {
+                $noteCreatorId = !empty($value['delegate']) ? $value['delegate'] : $value['user_id'];
+                $noteCreator   = UserModel::getLabelledUserById(['id' => $noteCreatorId]);
+            }
+
             $formattedDocument['workflow'][] = [
                 'userId'                => $value['user_id'],
                 'userDisplay'           => $userLabel,
@@ -208,6 +214,7 @@ class DocumentController
                 'datePositions'         => json_decode($value['date_positions'], true),
                 'userSignatureModes'    => !empty($userSignaturesModes['signature_modes']) ? json_decode($userSignaturesModes['signature_modes'], true) : [],
                 'note'                  => $value['note'],
+                'noteCreator'           => $noteCreator,
                 'status'                => $value['status'],
                 'externalInformations'  => $workflowExternalInformations
             ];
@@ -1069,6 +1076,10 @@ class DocumentController
         $set = ['process_date' => 'CURRENT_TIMESTAMP', 'status' => $args['status']];
         if (!empty($args['note'])) {
             $set['note'] = $args['note'];
+            $currentWorkflow = WorkflowModel::getCurrentStep(['select' => ['user_id'], 'documentId' => $args['id']]);
+            if ($currentWorkflow['user_id'] != $GLOBALS['id']) {
+                $set['delegate'] = $GLOBALS['id'];
+            }
         }
         WorkflowModel::update([
             'set'   => $set,
diff --git a/src/frontend/app/document/document.component.html b/src/frontend/app/document/document.component.html
index c2f3726183..3c866fa1be 100755
--- a/src/frontend/app/document/document.component.html
+++ b/src/frontend/app/document/document.component.html
@@ -74,7 +74,7 @@
             <ng-container *ngFor="let step of mainDocument.workflow">
                 <div *ngIf="step.note">
                     <div class="note-creator">
-                        {{step.userDisplay}} ({{step.processDate}}) :
+                        {{step.noteCreator}} ({{step.processDate}}) :
                     </div>
                     <div class="note-content">
                         {{step.note}}
-- 
GitLab