From 6aceeba220f2267ef14db13a0c3ffa9300a782ef Mon Sep 17 00:00:00 2001
From: Quentin RIBAC <quentin.ribac@xelians.fr>
Date: Mon, 31 Oct 2022 17:33:41 +0100
Subject: [PATCH] FEAT #17636 TIME 3:00 FastParapheur: saving email & name to
 external_id; all main documents are signable

---
 .../controllers/FastParapheurController.php   | 22 ++++++++++++-------
 .../controllers/AutoCompleteController.php    | 12 +++++-----
 2 files changed, 20 insertions(+), 14 deletions(-)

diff --git a/src/app/external/externalSignatoryBook/controllers/FastParapheurController.php b/src/app/external/externalSignatoryBook/controllers/FastParapheurController.php
index 540a5801086..fbb31dd7799 100755
--- a/src/app/external/externalSignatoryBook/controllers/FastParapheurController.php
+++ b/src/app/external/externalSignatoryBook/controllers/FastParapheurController.php
@@ -38,7 +38,7 @@ use Slim\Http\Response;
 use SrcCore\models\TextFormatModel;
 
 /**
-    * @codeCoverageIgnore
+* @codeCoverageIgnore
 */
 class FastParapheurController
 {
@@ -48,6 +48,9 @@ class FastParapheurController
         if (!Validator::notEmpty()->email()->validate($body['fastParapheurUserEmail'] ?? null)) {
             return $response->withStatus(400)->withJson(['errors' => 'body fastParapheurUserEmail is not a valid email address']);
         }
+        if (!Validator::notEmpty()->stringType()->validate($body['fastParapheurUserName'] ?? null)) {
+            return $response->withStatus(400)->withJson(['errors' => 'body fastParapheurUserName is empty or not a string']);
+        }
         if (!Validator::notEmpty()->intVal()->validate($args['id'])) {
             return $response->withStatus(400)->withJson(['errors' => 'args id is not an integer']);
         }
@@ -60,7 +63,7 @@ class FastParapheurController
 
         $alreadyLinked = UserModel::get([
             'select' => [1],
-            'where'  => ['external_id->>\'fastParapheur\' = ?'],
+            'where'  => ['external_id#>>\'{fastParapheur,email}\' = ?'],
             'data'   => [$body['fastParapheurUserEmail']]
         ]);
         if (!empty($alreadyLinked)) {
@@ -70,7 +73,10 @@ class FastParapheurController
         $userInfo = UserModel::getById(['select' => ['external_id', 'firstname', 'lastname'], 'id' => $args['id']]);
 
         $externalId = json_decode($userInfo['external_id'], true);
-        $externalId['fastParapheur'] = $body['fastParapheurUserEmail'];
+        $externalId['fastParapheur'] = [
+            'name'  => $body['fastParapheurUserName'],
+            'email' => $body['fastParapheurUserEmail']
+        ];
 
         UserModel::updateExternalId(['id' => $args['id'], 'externalId' => json_encode($externalId)]);
 
@@ -127,12 +133,12 @@ class FastParapheurController
             return $response->withStatus(403)->withJson(['errors' => 'fastParapheur is not enabled']);
         }
 
-        $user = UserModel::getById(['id' => $args['id'], 'select' => ['external_id->>\'fastParapheur\' as "fastParapheurEmail"']]);
-        if (empty($user['fastParapheurEmail'])) {
-            return $response->withStatus(403)->withJson(['errors' => 'user does not have Fast Parapheur email']);
+        $user = UserModel::getById(['id' => $args['id'], 'select' => ['external_id->>\'fastParapheur\' as "fastParapheurId"']]);
+        if (empty($user['fastParapheurId'])) {
+            return $response->withStatus(403)->withJson(['errors' => 'user does not have a Fast Parapheur email']);
         }
 
-        return $response->withJson(['link' => $user['fastParapheurEmail'], 'errors' => '']);
+        return $response->withJson(['link' => json_decode($user['fastParapheurId'], true), 'errors' => '']);
     }
 
     public function getWorkflow(Request $request, Response $response, array $args)
@@ -639,7 +645,7 @@ class FastParapheurController
         if ($resource['integrations']['inSignatureBook']) {
             $sentMainDocument = [
                 'comment'  => $resource['subject'],
-                'signable' => $resource['category_id'] == 'outgoing',
+                'signable' => true,
                 'path'     => $docservers[$resource['docserver_id']] . $resource['path'] . $resource['filename']
             ];
         }
diff --git a/src/core/controllers/AutoCompleteController.php b/src/core/controllers/AutoCompleteController.php
index cb5bd0f3d69..45b66072d51 100755
--- a/src/core/controllers/AutoCompleteController.php
+++ b/src/core/controllers/AutoCompleteController.php
@@ -180,13 +180,13 @@ class AutoCompleteController
         }
         $search = $queryParams['search'];
 
-        $excludedUsers = [];
+        $excludedEmails = [];
         if (!empty($queryParams['excludeAlreadyConnected'])) {
             $usersAlreadyConnected = UserModel::get([
-                'select' => ['external_id->>\'fastParapheur\' as external_id'],
-                'where'  => ['external_id->>\'fastParapheur\' is not null']
+                'select' => ['external_id#>>\'{fastParapheur,email}\' as external_email'],
+                'where'  => ['external_id#>>\'{fastParapheur,email}\' is not null']
             ]);
-            $excludedUsers = array_column($usersAlreadyConnected, 'external_id');
+            $excludedEmails = array_column($usersAlreadyConnected, 'external_email');
         }
 
         $loadedXml = CoreConfigModel::getXmlLoaded(['path' => 'modules/visa/xml/remoteSignatoryBooks.xml']);
@@ -236,8 +236,8 @@ class AutoCompleteController
                 unset($users[$userKey]);
             }
         }
-        $users = array_filter($users, function ($user) use ($excludedUsers) {
-            return !in_array($user['email'], $excludedUsers);
+        $users = array_filter($users, function ($user) use ($excludedEmails) {
+            return !in_array($user['email'], $excludedEmails);
         });
         $users = array_filter($users, function ($user) use ($search) {
             return mb_stripos($user['email'], $search) > -1 || mb_stripos($user['idToDisplay'], $search) > -1;
-- 
GitLab