From 341e38c67888713efa70b9a3d7145e99b5b1ac27 Mon Sep 17 00:00:00 2001
From: Damien <damien.burel@maarch.org>
Date: Thu, 23 Jan 2020 09:48:51 +0100
Subject: [PATCH] FEAT #12073 TIME 0:15 Refactor batch history + system actions
 history

---
 rest/index.php                                |  4 +-
 .../controllers/BatchHistoryController.php    | 49 +++++++++++++------
 .../history/controllers/HistoryController.php | 17 ++++---
 .../models/BatchHistoryModelAbstract.php      | 23 ++++-----
 4 files changed, 58 insertions(+), 35 deletions(-)

diff --git a/rest/index.php b/rest/index.php
index 72c5561ac70..2493c04bc64 100755
--- a/rest/index.php
+++ b/rest/index.php
@@ -104,8 +104,8 @@ $app->delete('/baskets/{id}/groups/{groupId}', \Basket\controllers\BasketControl
 $app->get('/sortedBaskets', \Basket\controllers\BasketController::class . ':getSorted');
 $app->put('/sortedBaskets/{id}', \Basket\controllers\BasketController::class . ':updateSort');
 
-//BatchHistories
-$app->get('/batchHistories', \History\controllers\BatchHistoryController::class . ':get');
+//BatchHistory
+$app->get('/batchHistory', \History\controllers\BatchHistoryController::class . ':get');
 
 //Configurations
 $app->get('/configurations/{service}', \Configuration\controllers\ConfigurationController::class . ':getByService');
diff --git a/src/app/history/controllers/BatchHistoryController.php b/src/app/history/controllers/BatchHistoryController.php
index d9c58930ca9..20f4f864565 100755
--- a/src/app/history/controllers/BatchHistoryController.php
+++ b/src/app/history/controllers/BatchHistoryController.php
@@ -16,7 +16,6 @@ namespace History\controllers;
 
 use Group\controllers\PrivilegeController;
 use History\models\BatchHistoryModel;
-use Respect\Validation\Validator;
 use Slim\Http\Request;
 use Slim\Http\Response;
 
@@ -28,26 +27,46 @@ class BatchHistoryController
             return $response->withStatus(403)->withJson(['errors' => 'Service forbidden']);
         }
 
-        $data = $request->getQueryParams();
+        $queryParams = $request->getQueryParams();
 
-        $check = Validator::floatVal()->notEmpty()->validate($data['startDate']);
-        $check = $check && Validator::floatVal()->notEmpty()->validate($data['endDate']);
-        if (!$check) {
-            return $response->withStatus(400)->withJson(['errors' => 'Bad Request']);
+        $limit = 25;
+        if (!empty($queryParams['limit']) && is_numeric($queryParams['limit'])) {
+            $limit = (int)$queryParams['limit'];
+        }
+        $offset = 0;
+        if (!empty($queryParams['offset']) && is_numeric($queryParams['offset'])) {
+            $offset = (int)$queryParams['offset'];
+        }
+
+        $where = [];
+        $data = [];
+
+        if (!empty($queryParams['startDate'])) {
+            $where[] = 'event_date > ?';
+            $data[] = date('Y-m-d H:i:s', $queryParams['startDate']);
+        }
+        if (!empty($queryParams['endDate'])) {
+            $where[] = 'event_date < ?';
+            $data[] = date('Y-m-d H:i:s', $queryParams['endDate']);
         }
 
-        $maxRequestSize = 25000;
+        $order = !in_array($queryParams['order'], ['asc', 'desc']) ? '' : $queryParams['order'];
+        $orderBy = !in_array($queryParams['orderBy'], ['event_date', 'module_name', 'total_processed', 'total_errors', 'info']) ? ['event_date DESC'] : ["{$queryParams['orderBy']} {$order}"];
 
-        $batchHistories = BatchHistoryModel::get([
-            'select'    => ['event_date', 'module_name', 'total_processed', 'total_errors', 'info'],
-            'where'     => ['event_date > ?', 'event_date < ?'],
-            'data'      => [date('Y-m-d H:i:s', $data['startDate']), date('Y-m-d H:i:s', $data['endDate'])],
-            'orderBy'   => ['event_date DESC'],
-            'limit'     => $maxRequestSize
+        $history = BatchHistoryModel::get([
+            'select'    => ['event_date', 'module_name', 'total_processed', 'total_errors', 'info', 'count(1) OVER()'],
+            'where'     => $where,
+            'data'      => $data,
+            'orderBy'   => $orderBy,
+            'offset'    => $offset,
+            'limit'     => $limit
         ]);
 
-        $limitExceeded = (count($batchHistories) == $maxRequestSize);
+        $total = $history[0]['count'] ?? 0;
+        foreach ($history as $key => $value) {
+            unset($history[$key]['count']);
+        }
 
-        return $response->withJson(['batchHistories' => $batchHistories, 'limitExceeded' => $limitExceeded]);
+        return $response->withJson(['history' => $history, 'count' => $total]);
     }
 }
diff --git a/src/app/history/controllers/HistoryController.php b/src/app/history/controllers/HistoryController.php
index 095bb48a397..23841f72e3f 100755
--- a/src/app/history/controllers/HistoryController.php
+++ b/src/app/history/controllers/HistoryController.php
@@ -66,6 +66,7 @@ class HistoryController
             $where[] = 'user_id in (?)';
             $data[] = $users;
         }
+
         if (!empty($queryParams['startDate'])) {
             $where[] = 'event_date > ?';
             $data[] = date('Y-m-d H:i:s', $queryParams['startDate']);
@@ -74,17 +75,19 @@ class HistoryController
             $where[] = 'event_date < ?';
             $data[] = date('Y-m-d H:i:s', $queryParams['endDate']);
         }
+
+        $eventTypes = [];
         if (!empty($queryParams['actions']) && is_array($queryParams['actions'])) {
-            $actions = [];
             foreach ($queryParams['actions'] as $action) {
-                if (is_numeric($action)) {
-                    $actions[] = "ACTION#{$action}";
-                } else {
-                    $actions[] = $action;
-                }
+                $eventTypes[] = "ACTION#{$action}";
             }
+        }
+        if (!empty($queryParams['systemActions']) && is_array($queryParams['systemActions'])) {
+            $eventTypes = array_merge($eventTypes, $queryParams['systemActions']);
+        }
+        if (!empty($eventTypes)) {
             $where[] = 'event_type in (?)';
-            $data[] = $actions;
+            $data[] = $eventTypes;
         }
 
         $order = !in_array($queryParams['order'], ['asc', 'desc']) ? '' : $queryParams['order'];
diff --git a/src/app/history/models/BatchHistoryModelAbstract.php b/src/app/history/models/BatchHistoryModelAbstract.php
index 2d665a34c54..d1707a058c7 100755
--- a/src/app/history/models/BatchHistoryModelAbstract.php
+++ b/src/app/history/models/BatchHistoryModelAbstract.php
@@ -19,21 +19,22 @@ use SrcCore\models\DatabaseModel;
 
 abstract class BatchHistoryModelAbstract
 {
-    public static function get(array $aArgs)
+    public static function get(array $args)
     {
-        ValidatorModel::notEmpty($aArgs, ['select']);
-        ValidatorModel::arrayType($aArgs, ['select', 'where', 'data', 'orderBy']);
-        ValidatorModel::intVal($aArgs, ['limit']);
+        ValidatorModel::notEmpty($args, ['select']);
+        ValidatorModel::arrayType($args, ['select', 'where', 'data', 'orderBy']);
+        ValidatorModel::intVal($args, ['offset', 'limit']);
 
-        $aHistories = DatabaseModel::select([
-            'select'    => $aArgs['select'],
+        $history = DatabaseModel::select([
+            'select'    => $args['select'],
             'table'     => ['history_batch'],
-            'where'     => $aArgs['where'],
-            'data'      => $aArgs['data'],
-            'order_by'  => $aArgs['orderBy'],
-            'limit'     => $aArgs['limit']
+            'where'     => $args['where'] ?? [],
+            'data'      => $args['data'] ?? [],
+            'order_by'  => $args['orderBy'] ?? [],
+            'offset'    => $args['offset'] ?? 0,
+            'limit'     => $args['limit'] ?? 0
         ]);
 
-        return $aHistories;
+        return $history;
     }
 }
-- 
GitLab