From b35805143d0246c8ca9efbd1ea5c888a01eca784 Mon Sep 17 00:00:00 2001
From: Damien <damien.burel@maarch.org>
Date: Thu, 19 Dec 2019 16:48:05 +0100
Subject: [PATCH] FEAT #11887 TIME 0:30 Get Maarch parapheur workflow

---
 rest/index.php                                |  1 +
 .../controllers/MaarchParapheurController.php | 59 +++++++++++++++++++
 .../core/AutocompleteControllerTest.php       |  4 +-
 3 files changed, 62 insertions(+), 2 deletions(-)

diff --git a/rest/index.php b/rest/index.php
index 2e5a34793e7..5da4c1bb5d3 100755
--- a/rest/index.php
+++ b/rest/index.php
@@ -71,6 +71,7 @@ $app->get('/attachments/{id}/content', \Attachment\controllers\AttachmentControl
 $app->get('/attachments/{id}/originalContent', \Attachment\controllers\AttachmentController::class . ':getOriginalFileContent');
 $app->get('/attachments/{id}/thumbnail', \Attachment\controllers\AttachmentController::class . ':getThumbnailContent');
 $app->put('/attachments/{id}/inSendAttachment', \Attachment\controllers\AttachmentController::class . ':setInSendAttachment');
+$app->get('/attachments/{id}/maarchParapheurWorkflow', \ExternalSignatoryBook\controllers\MaarchParapheurController::class . ':getWorkflow');
 $app->get('/attachmentsTypes', \Attachment\controllers\AttachmentController::class . ':getAttachmentsTypes');
 
 //AutoComplete
diff --git a/src/app/external/externalSignatoryBook/controllers/MaarchParapheurController.php b/src/app/external/externalSignatoryBook/controllers/MaarchParapheurController.php
index 1a02e1c8b77..0348537cbd8 100755
--- a/src/app/external/externalSignatoryBook/controllers/MaarchParapheurController.php
+++ b/src/app/external/externalSignatoryBook/controllers/MaarchParapheurController.php
@@ -23,6 +23,7 @@ use Entity\models\ListInstanceModel;
 use History\controllers\HistoryController;
 use Note\models\NoteModel;
 use Priority\models\PriorityModel;
+use Resource\controllers\ResController;
 use Resource\controllers\SummarySheetController;
 use Resource\models\ResModel;
 use Respect\Validation\Validator;
@@ -846,4 +847,62 @@ class MaarchParapheurController
 
         return $response->withJson(['success' => 'success']);
     }
+
+    public function getWorkflow(Request $request, Response $response, array $args)
+    {
+        $attachment = AttachmentModel::getById(['id' => $args['id'], 'select' => ['res_id_master', 'status', 'external_id']]);
+        if (empty($attachment)) {
+            return $response->withStatus(400)->withJson(['errors' => 'Attachment does not exist']);
+        }
+        if (!ResController::hasRightByResId(['resId' => [$attachment['res_id_master']], 'userId' => $GLOBALS['id']])) {
+            return $response->withStatus(400)->withJson(['errors' => 'Attachment out of perimeter']);
+        }
+
+        $externalId = json_decode($attachment['external_id'], true);
+        if (empty($externalId['signatureBookId'])) {
+            return $response->withStatus(400)->withJson(['errors' => 'Attachment is not linked to Maarch Parapheur']);
+        }
+
+        $loadedXml = CoreConfigModel::getXmlLoaded(['path' => 'modules/visa/xml/remoteSignatoryBooks.xml']);
+        if (empty($loadedXml)) {
+            return $response->withStatus(400)->withJson(['errors' => 'SignatoryBooks configuration file missing']);
+        }
+
+        $url      = '';
+        $userId   = '';
+        $password = '';
+        foreach ($loadedXml->signatoryBook as $value) {
+            if ($value->id == "maarchParapheur") {
+                $url      = rtrim($value->url, '/');
+                $userId   = $value->userId;
+                $password = $value->password;
+                break;
+            }
+        }
+
+        if (empty($url)) {
+            return $response->withStatus(400)->withJson(['errors' => 'Maarch Parapheur configuration missing']);
+        }
+
+        $curlResponse = CurlModel::execSimple([
+            'url'           => rtrim($url, '/') . "/rest/documents/{$externalId['signatureBookId']}/workflow",
+            'basicAuth'     => ['user' => $userId, 'password' => $password],
+            'headers'       => ['content-type:application/json'],
+            'method'        => 'GET'
+        ]);
+
+        if ($curlResponse['code'] != '200') {
+            if (!empty($curlResponse['response']['errors'])) {
+                $errors =  $curlResponse['response']['errors'];
+            } else {
+                $errors =  $curlResponse['errors'];
+            }
+            if (empty($errors)) {
+                $errors = 'An error occured. Please check your configuration file.';
+            }
+            return $response->withStatus(400)->withJson(['errors' => $errors]);
+        }
+
+        return $response->withJson($curlResponse['response']);
+    }
 }
diff --git a/test/unitTests/core/AutocompleteControllerTest.php b/test/unitTests/core/AutocompleteControllerTest.php
index aeccf43cbd9..763ba9abbb1 100755
--- a/test/unitTests/core/AutocompleteControllerTest.php
+++ b/test/unitTests/core/AutocompleteControllerTest.php
@@ -128,7 +128,7 @@ class AutocompleteControllerTest extends TestCase
         }
     }
 
-    public function testGetUsersForVisa()
+    public function testGetUsersForCircuit()
     {
         $autocompleteController = new \SrcCore\controllers\AutoCompleteController();
 
@@ -141,7 +141,7 @@ class AutocompleteControllerTest extends TestCase
         ];
         $fullRequest = $request->withQueryParams($aArgs);
 
-        $response     = $autocompleteController->getUsersForVisa($fullRequest, new \Slim\Http\Response());
+        $response     = $autocompleteController->getUsersForCircuit($fullRequest, new \Slim\Http\Response());
         $responseBody = json_decode((string)$response->getBody());
 
         $this->assertInternalType('array', $responseBody);
-- 
GitLab