From a21fa62bc2cd7aa5ab6cdcc3d1f7bb4f829a1598 Mon Sep 17 00:00:00 2001
From: Guillaume Heurtier <guillaume.heurtier@maarch.org>
Date: Tue, 28 Jan 2020 17:00:38 +0100
Subject: [PATCH] FEAT #12091 TIME 0:15 added checks from preprocess in actions

---
 .../controllers/ActionMethodController.php    | 45 +++++++++++++++++--
 1 file changed, 42 insertions(+), 3 deletions(-)

diff --git a/src/app/action/controllers/ActionMethodController.php b/src/app/action/controllers/ActionMethodController.php
index d2d22d2e280..1c6d2221ee0 100644
--- a/src/app/action/controllers/ActionMethodController.php
+++ b/src/app/action/controllers/ActionMethodController.php
@@ -423,8 +423,24 @@ class ActionMethodController
             'limit'     => 1
         ]);
 
-        if (empty($listInstances)) {
-            return false;
+        if (empty($listInstances[0])) {
+            $hasCircuit = ListInstanceModel::get(['select' => [1], 'where' => ['res_id = ?', 'difflist_type = ?'], 'data' => [$args['resId'], 'VISA_CIRCUIT']]);
+            if (!empty($hasCircuit)) {
+                return ['errors' => ['Workflow has ended']];
+            } else {
+                return ['errors' => ['No workflow defined']];
+            }
+        } else {
+            $hasPrevious = ListInstanceModel::get([
+                'select'  => [1],
+                'where'   => ['res_id = ?', 'difflist_type = ?', 'process_date is not null'],
+                'data'    => [$args['resId'], 'VISA_CIRCUIT'],
+                'orderBy' => ['listinstance_id'],
+                'limit'   => 1
+            ]);
+            if (!empty($hasPrevious)) {
+                return ['errors' => ['Workflow not yet started']];
+            }
         }
 
         $listInstances = $listInstances[0];
@@ -443,6 +459,22 @@ class ActionMethodController
         ValidatorModel::notEmpty($args, ['resId']);
         ValidatorModel::intVal($args, ['resId']);
 
+        $inCircuit = ListInstanceModel::get([
+            'select'  => [1],
+            'where'   => ['res_id = ?', 'difflist_type = ?', 'process_date is null'],
+            'data'    => [$args['resId'], 'VISA_CIRCUIT'],
+            'orderBy' => ['listinstance_id'],
+            'limit'   => 1
+        ]);
+        if (empty($inCircuit[0])) {
+            $hasCircuit = ListInstanceModel::get(['select' => [1], 'where' => ['res_id = ?', 'difflist_type = ?'], 'data' => [$args['resId'], 'VISA_CIRCUIT']]);
+            if (!empty($hasCircuit)) {
+                return ['errors' => ['Workflow has ended']];
+            } else {
+                return ['errors' => ['No workflow defined']];
+            }
+        }
+
         ListInstanceModel::update([
             'set'   => ['process_date' => null],
             'where' => ['res_id = ?', 'difflist_type = ?'],
@@ -466,7 +498,7 @@ class ActionMethodController
             'limit'    => 1
         ]);
 
-        if (!empty($listInstances)) {
+        if (!empty($listInstances[0])) {
             $listInstances = $listInstances[0];
 
             ListInstanceModel::update([
@@ -477,6 +509,13 @@ class ActionMethodController
                 'where' => ['listinstance_id = ?'],
                 'data'  => [$listInstances['listinstance_id']]
             ]);
+        } else {
+            $hasCircuit = ListInstanceModel::get(['select' => [1], 'where' => ['res_id = ?', 'difflist_type = ?'], 'data' => [$args['resId'], 'VISA_CIRCUIT']]);
+            if (!empty($hasCircuit)) {
+                return ['errors' => ['Workflow has ended']];
+            } else {
+                return ['errors' => ['No workflow defined']];
+            }
         }
 
         ListInstanceModel::update([
-- 
GitLab