From f6bcf5e95421b9adf4175c8ae6fbfa01a1dc515a Mon Sep 17 00:00:00 2001
From: Damien <damien.burel@maarch.org>
Date: Thu, 30 Jan 2020 15:03:50 +0100
Subject: [PATCH] FEAT #12346 TIME 1:00 signature book action

---
 .../controllers/ActionMethodController.php    |  6 +++++
 .../PreProcessActionController.php            | 25 +++++++++++--------
 .../controllers/IndexingController.php        |  7 ++++--
 .../resource/controllers/StoreController.php  |  7 ++++++
 .../send-signature-book-action.component.ts   |  4 +--
 5 files changed, 35 insertions(+), 14 deletions(-)

diff --git a/src/app/action/controllers/ActionMethodController.php b/src/app/action/controllers/ActionMethodController.php
index 1c6d2221ee0..fe92c1cf3f1 100644
--- a/src/app/action/controllers/ActionMethodController.php
+++ b/src/app/action/controllers/ActionMethodController.php
@@ -324,6 +324,12 @@ class ActionMethodController
             }
         }
 
+        $resource = ResModel::getById(['select' => ['integrations'], 'id' => $args['resId']]);
+        $integrations = json_decode($resource['integrations'], true);
+        if (!empty($integrations['inSignatureBook'])) {
+            return true;
+        }
+
         $attachments = AttachmentModel::get([
             'select'    => [1],
             'where'     => ['res_id_master = ?', 'attachment_type in (?)', 'in_signature_book = ?', 'status not in (?)'],
diff --git a/src/app/action/controllers/PreProcessActionController.php b/src/app/action/controllers/PreProcessActionController.php
index 5504987924d..03db6175f5e 100755
--- a/src/app/action/controllers/PreProcessActionController.php
+++ b/src/app/action/controllers/PreProcessActionController.php
@@ -936,21 +936,26 @@ class PreProcessActionController
 
         $resourcesInformations = [];
         foreach ($body['resources'] as $resId) {
-            $resource = ResModel::getById(['resId' => $resId, 'select' => ['alt_identifier']]);
+            $resource = ResModel::getById(['resId' => $resId, 'select' => ['alt_identifier', 'integrations']]);
             if (empty($resource['alt_identifier'])) {
                 $resource['alt_identifier'] = _UNDEFINED;
             }
 
-            $attachments = AttachmentModel::get([
-                'select'    => [1],
-                'where'     => ['res_id_master = ?', 'attachment_type in (?)', 'in_signature_book = ?', 'status not in (?)'],
-                'data'      => [$resId, $signableAttachmentsTypes, true, ['OBS', 'DEL', 'FRZ']]
-            ]);
-
-            if (empty($attachments)) {
-                $resourcesInformations['noAttachment'][] = ['alt_identifier' => $resource['alt_identifier'], 'res_id' => $resId, 'reason' => 'noAttachmentInSignatoryBook'];
+            $integrations = json_decode($resource['integrations'], true);
+            if (!empty($integrations['inSignatureBook'])) {
+                $resourcesInformations['success'][] = ['res_id' => $resId];
             } else {
-                $resourcesInformations['attachments'][] = ['res_id' => $resId];
+                $attachments = AttachmentModel::get([
+                    'select'    => [1],
+                    'where'     => ['res_id_master = ?', 'attachment_type in (?)', 'in_signature_book = ?', 'status not in (?)'],
+                    'data'      => [$resId, $signableAttachmentsTypes, true, ['OBS', 'DEL', 'FRZ']]
+                ]);
+
+                if (empty($attachments)) {
+                    $resourcesInformations['error'][] = ['alt_identifier' => $resource['alt_identifier'], 'res_id' => $resId, 'reason' => 'noAttachmentInSignatoryBook'];
+                } else {
+                    $resourcesInformations['success'][] = ['res_id' => $resId];
+                }
             }
         }
 
diff --git a/src/app/resource/controllers/IndexingController.php b/src/app/resource/controllers/IndexingController.php
index f45ba7b30a0..5a39996a909 100755
--- a/src/app/resource/controllers/IndexingController.php
+++ b/src/app/resource/controllers/IndexingController.php
@@ -99,12 +99,15 @@ class IndexingController
         if (!empty($method)) {
             $methodResponse = ActionMethodController::$method(['resId' => $body['resource'], 'data' => $body['data'], 'note' => $body['note']]);
         }
+        if (!empty($methodResponse['errors'])) {
+            return $response->withStatus(400)->withJson(['errors' => $methodResponse['errors'][0]]);
+        }
 
         $historic = empty($methodResponse['history']) ? '' : $methodResponse['history'];
         ActionMethodController::terminateAction(['id' => $args['actionId'], 'resources' => [$body['resource']], 'note' => $body['note'], 'history' => $historic]);
 
-        if (!empty($methodResponses['data']) || !empty($methodResponses['errors'])) {
-            return $response->withJson($methodResponses);
+        if (!empty($methodResponse['data'])) {
+            return $response->withJson($methodResponse['data']);
         }
 
         return $response->withStatus(204);
diff --git a/src/app/resource/controllers/StoreController.php b/src/app/resource/controllers/StoreController.php
index fe34e32989a..e833b7b1ca4 100755
--- a/src/app/resource/controllers/StoreController.php
+++ b/src/app/resource/controllers/StoreController.php
@@ -172,6 +172,12 @@ class StoreController
             $externalId = json_encode($args['externalId']);
         }
 
+        $integrations = ['inSignatureBook' => false, 'inShipping' => false];
+        if (!empty($args['integrations'])) {
+            $integrations['inSignatureBook'] = !empty($integrations['inSignatureBook']);
+            $integrations['inShipping'] = !empty($integrations['inShipping']);
+        }
+
         if (!empty($args['customFields'])) {
             foreach ($args['customFields'] as $key => $value) {
                 $customField = CustomFieldModel::getById(['id' => $key, 'select' => ['type']]);
@@ -204,6 +210,7 @@ class StoreController
             'barcode'               => $args['barcode'] ?? null,
             'origin'                => $args['origin'] ?? null,
             'custom_fields'         => !empty($args['customFields']) ? json_encode($args['customFields']) : null,
+            'integrations'          => json_encode($integrations),
             'linked_resources'      => !empty($args['linkedResources']) ? json_encode($args['linkedResources']) : '[]',
             'external_id'           => $externalId,
             'creation_date'         => 'CURRENT_TIMESTAMP'
diff --git a/src/frontend/app/actions/visa-send-signature-book-action/send-signature-book-action.component.ts b/src/frontend/app/actions/visa-send-signature-book-action/send-signature-book-action.component.ts
index 40f4cfa1f06..6b1f7e9004d 100644
--- a/src/frontend/app/actions/visa-send-signature-book-action/send-signature-book-action.component.ts
+++ b/src/frontend/app/actions/visa-send-signature-book-action/send-signature-book-action.component.ts
@@ -57,8 +57,8 @@ export class SendSignatureBookActionComponent implements AfterViewInit {
         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 + '/checkSignatureBook', { resources: this.data.resIds })
                 .subscribe((data: any) => {
-                    if (!this.functions.empty(data.resourcesInformations.noAttachment)) {
-                        this.resourcesError = data.resourcesInformations.noAttachment;
+                    if (!this.functions.empty(data.resourcesInformations.error)) {
+                        this.resourcesError = data.resourcesInformations.error;
                     }
                     this.noResourceToProcess = this.data.resIds.length === this.resourcesError.length;
                     resolve(true);
-- 
GitLab