From 67c91345d4ffdc090f157363f018ac0d8ea009f1 Mon Sep 17 00:00:00 2001
From: Nestor <npegane@hotmail.com>
Date: Thu, 5 Apr 2018 11:21:25 +0200
Subject: [PATCH] FEAT #7475 update status with array of res_id or Chrono +
 unit test adaptation

---
 core/Test/ResControllerTest.php               |  8 +---
 .../resource/controllers/ResController.php    | 44 +++++--------------
 2 files changed, 12 insertions(+), 40 deletions(-)

diff --git a/core/Test/ResControllerTest.php b/core/Test/ResControllerTest.php
index 8625db950f9..538082855cb 100755
--- a/core/Test/ResControllerTest.php
+++ b/core/Test/ResControllerTest.php
@@ -55,9 +55,7 @@ class ResControllerTest extends TestCase
 
         $response     = $resController->create($fullRequest, new \Slim\Http\Response());
         $responseBody = json_decode((string)$response->getBody());
-
         self::$id = $responseBody->resId;
-
         $this->assertInternalType('int', self::$id);
 
         //  READ
@@ -84,15 +82,13 @@ class ResControllerTest extends TestCase
         $request        = \Slim\Http\Request::createFromEnvironment($environment);
 
         $aArgs = [
-            'resId'         => self::$id,
+            'resId'         => [self::$id],
             'status'        => 'EVIS'
         ];
-
         $fullRequest = \httpRequestCustom::addContentInBody($aArgs, $request);
 
         $response     = $resController->updateStatus($fullRequest, new \Slim\Http\Response());
         $responseBody = json_decode((string)$response->getBody());
-
         $this->assertSame('success', $responseBody->success);
 
         //  READ
@@ -105,7 +101,7 @@ class ResControllerTest extends TestCase
         $request        = \Slim\Http\Request::createFromEnvironment($environment);
 
         $aArgs = [
-            'resId'         => self::$id
+            'resId'         => [self::$id]
         ];
 
         $fullRequest = \httpRequestCustom::addContentInBody($aArgs, $request);
diff --git a/src/app/resource/controllers/ResController.php b/src/app/resource/controllers/ResController.php
index 32278dd1da3..81a1b34df2f 100755
--- a/src/app/resource/controllers/ResController.php
+++ b/src/app/resource/controllers/ResController.php
@@ -124,46 +124,19 @@ class ResController
             $data['historyMessage'] = _UPDATE_STATUS;
         }
 
-        $check = Validator::stringType()->notEmpty()->validate($data['chrono']) || Validator::intVal()->notEmpty()->validate($data['resId']) || Validator::stringType()->notEmpty()->validate($data['resId']);
+        $check = Validator::arrayType()->notEmpty()->validate($data['chrono']) || Validator::arrayType()->notEmpty()->validate($data['resId']);
         $check = $check && Validator::stringType()->notEmpty()->validate($data['status']);
         $check = $check && Validator::stringType()->notEmpty()->validate($data['historyMessage']);
         
         if (!$check) {
             return $response->withStatus(400)->withJson(['errors' => 'Bad Request']);
         }
-
-        if(strpos($data['resId'],',')!==false){
-            $resIds = explode(',', $data['resId']);
-            foreach($resIds as $resId){
-                if (!empty($data['chrono'])) {
-                    $document = ResModel::getResIdByAltIdentifier(['altIdentifier' => $data['chrono']]);
-                } else {
-                    $document = ResModel::getById(['resId' => $resId, 'select' => ['res_id']]);
-                }
-                if (empty($document)) {
-                    return $response->withStatus(400)->withJson(['errors' => _DOCUMENT_NOT_FOUND]);
-                }
-                if (!ResController::hasRightByResId(['resId' => $document['res_id'], 'userId' => $GLOBALS['userId']])) {
-                    return $response->withStatus(403)->withJson(['errors' => 'Document out of perimeter']);
-                }
-
-        
-                ResModel::update(['set' => ['status' => $data['status']], 'where' => ['res_id = ?'], 'data' => [$resId]]);
-        
-                HistoryController::add([
-                    'tableName' => 'res_letterbox',
-                    'recordId'  => $document['res_id'],
-                    'eventType' => 'UP',
-                    'info'      => $data['historyMessage'],
-                    'moduleId'  => 'apps',
-                    'eventId'   => 'resup',
-                ]);
-            }
-        } else {
+        $identifiers = !empty($data['chrono'])? $data['chrono']: $data['resId'] ;
+        foreach($identifiers as $id){
             if (!empty($data['chrono'])) {
-                $document = ResModel::getResIdByAltIdentifier(['altIdentifier' => $data['chrono']]);
+                $document = ResModel::getResIdByAltIdentifier(['altIdentifier' => $id]);
             } else {
-                $document = ResModel::getById(['resId' => $data['resId'], 'select' => ['res_id']]);
+                $document = ResModel::getById(['resId' => $id, 'select' => ['res_id']]);
             }
             if (empty($document)) {
                 return $response->withStatus(400)->withJson(['errors' => _DOCUMENT_NOT_FOUND]);
@@ -172,8 +145,9 @@ class ResController
                 return $response->withStatus(403)->withJson(['errors' => 'Document out of perimeter']);
             }
 
+    
             ResModel::update(['set' => ['status' => $data['status']], 'where' => ['res_id = ?'], 'data' => [$document['res_id']]]);
-
+    
             HistoryController::add([
                 'tableName' => 'res_letterbox',
                 'recordId'  => $document['res_id'],
@@ -182,11 +156,13 @@ class ResController
                 'moduleId'  => 'apps',
                 'eventId'   => 'resup',
             ]);
-        }       
+        }        
 
         return $response->withJson(['success' => 'success']);
     }
 
+    //EXTERNAL INFOS
+
     public function isLock(Request $request, Response $response, array $aArgs)
     {
         return $response->withJson(ResModel::isLock(['resId' => $aArgs['resId'], 'userId' => $GLOBALS['userId']]));
-- 
GitLab