From a0ae7ee539ff045d22a6990f71d8ce2197ddc355 Mon Sep 17 00:00:00 2001
From: Guillaume Heurtier <guillaume.heurtier@maarch.org>
Date: Mon, 27 Jan 2020 16:33:08 +0100
Subject: [PATCH] FEAT #12091 TIME 0:15 split pre process for
 interrupt/reset/reject visa actions

---
 rest/index.php                                |  3 +-
 .../PreProcessActionController.php            | 51 ++++++++++++++++++-
 .../interrupt-visa-action.component.ts        |  2 +-
 ...-visa-back-to-previous-action.component.ts |  2 +-
 .../reset-visa-action.component.ts            |  2 +-
 5 files changed, 55 insertions(+), 5 deletions(-)

diff --git a/rest/index.php b/rest/index.php
index 679495cac76..4994e5d6859 100755
--- a/rest/index.php
+++ b/rest/index.php
@@ -394,7 +394,8 @@ $app->post('/resourcesList/users/{userId}/groups/{groupId}/baskets/{basketId}/ac
 $app->post('/resourcesList/users/{userId}/groups/{groupId}/baskets/{basketId}/actions/{actionId}/checkValidateParallelOpinion', \Action\controllers\PreProcessActionController::class . ':checkValidateParallelOpinion');
 $app->post('/resourcesList/users/{userId}/groups/{groupId}/baskets/{basketId}/actions/{actionId}/checkContinueOpinionCircuit', \Action\controllers\PreProcessActionController::class . ':checkContinueOpinionCircuit');
 $app->post('/resourcesList/users/{userId}/groups/{groupId}/baskets/{basketId}/actions/{actionId}/checkGiveParallelOpinion', \Action\controllers\PreProcessActionController::class . ':checkGiveParallelOpinion');
-$app->post('/resourcesList/users/{userId}/groups/{groupId}/baskets/{basketId}/actions/{actionId}/checkInterruptRejectResetVisa', \Action\controllers\PreProcessActionController::class . ':checkInterruptRejectResetVisa');
+$app->post('/resourcesList/users/{userId}/groups/{groupId}/baskets/{basketId}/actions/{actionId}/checkRejectVisa', \Action\controllers\PreProcessActionController::class . ':checkRejectVisa');
+$app->post('/resourcesList/users/{userId}/groups/{groupId}/baskets/{basketId}/actions/{actionId}/checkInterruptResetVisa', \Action\controllers\PreProcessActionController::class . ':checkInterruptResetVisa');
 
 //Search
 $app->get('/search', \Search\controllers\SearchController::class . ':get');
diff --git a/src/app/action/controllers/PreProcessActionController.php b/src/app/action/controllers/PreProcessActionController.php
index 31851fd86b4..067a1894612 100755
--- a/src/app/action/controllers/PreProcessActionController.php
+++ b/src/app/action/controllers/PreProcessActionController.php
@@ -985,7 +985,7 @@ class PreProcessActionController
         return $response->withJson(['resourcesInformations' => $resourcesInformations]);
     }
 
-    public function checkInterruptRejectResetVisa(Request $request, Response $response, array $args)
+    public function checkRejectVisa(Request $request, Response $response, array $args)
     {
         $body = $request->getParsedBody();
 
@@ -1045,6 +1045,55 @@ class PreProcessActionController
         return $response->withJson(['resourcesInformations' => $resourcesInformation]);
     }
 
+    public function checkInterruptResetVisa(Request $request, Response $response, array $args)
+    {
+        $body = $request->getParsedBody();
+
+        if (!Validator::arrayType()->notEmpty()->validate($body['resources'])) {
+            return $response->withStatus(400)->withJson(['errors' => 'Body resources is empty or not an array']);
+        }
+
+        $errors = ResourceListController::listControl(['groupId' => $args['groupId'], 'userId' => $args['userId'], 'basketId' => $args['basketId'], 'currentUserId' => $GLOBALS['id']]);
+        if (!empty($errors['errors'])) {
+            return $response->withStatus($errors['code'])->withJson(['errors' => $errors['errors']]);
+        }
+
+        $body['resources'] = array_slice($body['resources'], 0, 500);
+        if (!ResController::hasRightByResId(['resId' => $body['resources'], 'userId' => $GLOBALS['id']])) {
+            return $response->withStatus(403)->withJson(['errors' => 'Document out of perimeter']);
+        }
+
+        $signableAttachmentsTypes = [];
+        $attachmentsTypes = AttachmentModel::getAttachmentsTypesByXML();
+        foreach ($attachmentsTypes as $key => $type) {
+            if ($type['sign']) {
+                $signableAttachmentsTypes[] = $key;
+            }
+        }
+
+        $resourcesInformation = [];
+        foreach ($body['resources'] as $resId) {
+            $resource = ResModel::getById(['resId' => $resId, 'select' => ['alt_identifier']]);
+            if (empty($resource['alt_identifier'])) {
+                $resource['alt_identifier'] = _UNDEFINED;
+            }
+
+            $isSignatory = ListInstanceModel::get(['select' => ['signatory', 'requested_signature'], 'where' => ['res_id = ?', 'difflist_type = ?', 'process_date is null'], 'data' => [$resId, 'VISA_CIRCUIT'], 'orderBy' => ['listinstance_id'], 'limit' => 1]);
+            if (empty($isSignatory[0])) {
+                $hasCircuit = ListInstanceModel::get(['select' => [1], 'where' => ['res_id = ?', 'difflist_type = ?'], 'data' => [$resId, 'VISA_CIRCUIT']]);
+                if (!empty($hasCircuit)) {
+                    $resourcesInformation['error'][] = ['alt_identifier' => $resource['alt_identifier'], 'res_id' => $resId, 'reason' => 'endedCircuit'];
+                } else {
+                    $resourcesInformation['error'][] = ['alt_identifier' => $resource['alt_identifier'], 'res_id' => $resId, 'reason' => 'noCircuitAvailable'];
+                }
+            } else {
+                $resourcesInformation['success'][] = ['alt_identifier' => $resource['alt_identifier'], 'res_id' => $resId];
+            }
+        }
+
+        return $response->withJson(['resourcesInformations' => $resourcesInformation]);
+    }
+
     public function checkValidateParallelOpinion(Request $request, Response $response, array $args)
     {
         $body = $request->getParsedBody();
diff --git a/src/frontend/app/actions/visa-interrupt-action/interrupt-visa-action.component.ts b/src/frontend/app/actions/visa-interrupt-action/interrupt-visa-action.component.ts
index f5ea201140f..09790da7be2 100644
--- a/src/frontend/app/actions/visa-interrupt-action/interrupt-visa-action.component.ts
+++ b/src/frontend/app/actions/visa-interrupt-action/interrupt-visa-action.component.ts
@@ -44,7 +44,7 @@ export class InterruptVisaActionComponent implements OnInit {
         this.resourcesWarnings = [];
 
         return new Promise((resolve, reject) => {
-            this.http.post('../../rest/resourcesList/users/' + this.data.userId + '/groups/' + this.data.groupId + '/baskets/' + this.data.basketId + '/actions/' + this.data.action.id + '/checkInterruptRejectResetVisa', { resources: this.data.resIds })
+            this.http.post('../../rest/resourcesList/users/' + this.data.userId + '/groups/' + this.data.groupId + '/baskets/' + this.data.basketId + '/actions/' + this.data.action.id + '/checkInterruptResetVisa', { resources: this.data.resIds })
                 .subscribe((data: any) => {
                     if (!this.functions.empty(data.resourcesInformations.warning)) {
                         this.resourcesWarnings = data.resourcesInformations.warning;
diff --git a/src/frontend/app/actions/visa-reject-back-to-previous-action/reject-visa-back-to-previous-action.component.ts b/src/frontend/app/actions/visa-reject-back-to-previous-action/reject-visa-back-to-previous-action.component.ts
index c43cba03b8e..759d2fb3c6f 100644
--- a/src/frontend/app/actions/visa-reject-back-to-previous-action/reject-visa-back-to-previous-action.component.ts
+++ b/src/frontend/app/actions/visa-reject-back-to-previous-action/reject-visa-back-to-previous-action.component.ts
@@ -46,7 +46,7 @@ export class RejectVisaBackToPrevousActionComponent implements OnInit {
         this.resourcesWarnings = [];
 
         return new Promise((resolve, reject) => {
-            this.http.post('../../rest/resourcesList/users/' + this.data.userId + '/groups/' + this.data.groupId + '/baskets/' + this.data.basketId + '/actions/' + this.data.action.id + '/checkInterruptRejectResetVisa', { resources: this.data.resIds })
+            this.http.post('../../rest/resourcesList/users/' + this.data.userId + '/groups/' + this.data.groupId + '/baskets/' + this.data.basketId + '/actions/' + this.data.action.id + '/checkRejectVisa', { resources: this.data.resIds })
                 .subscribe((data: any) => {
                     if (!this.functions.empty(data.resourcesInformations.warning)) {
                         this.resourcesWarnings = data.resourcesInformations.warning;
diff --git a/src/frontend/app/actions/visa-reset-action/reset-visa-action.component.ts b/src/frontend/app/actions/visa-reset-action/reset-visa-action.component.ts
index 1046030de42..4ee02073899 100644
--- a/src/frontend/app/actions/visa-reset-action/reset-visa-action.component.ts
+++ b/src/frontend/app/actions/visa-reset-action/reset-visa-action.component.ts
@@ -45,7 +45,7 @@ export class ResetVisaActionComponent implements OnInit {
         this.resourcesWarnings = [];
 
         return new Promise((resolve, reject) => {
-            this.http.post('../../rest/resourcesList/users/' + this.data.userId + '/groups/' + this.data.groupId + '/baskets/' + this.data.basketId + '/actions/' + this.data.action.id + '/checkInterruptRejectResetVisa', { resources: this.data.resIds })
+            this.http.post('../../rest/resourcesList/users/' + this.data.userId + '/groups/' + this.data.groupId + '/baskets/' + this.data.basketId + '/actions/' + this.data.action.id + '/checkInterruptResetVisa', { resources: this.data.resIds })
                 .subscribe((data: any) => {
                     if (!this.functions.empty(data.resourcesInformations.warning)) {
                         this.resourcesWarnings = data.resourcesInformations.warning;
-- 
GitLab