From 26fe97a3d940e507277661ee21449fac462880c0 Mon Sep 17 00:00:00 2001
From: Guillaume Heurtier <guillaume.heurtier@maarch.org>
Date: Tue, 4 Feb 2020 17:59:24 +0100
Subject: [PATCH] FEAT #12346 TIME 0:30 refactor set in integrations + refactor
 shipping

---
 .../PreProcessActionController.php            |  4 +-
 src/app/action/controllers/ShippingTrait.php  | 10 ++---
 .../resource/controllers/ResController.php    | 38 ++++++++-----------
 .../ShippingTemplateController.php            |  2 +-
 4 files changed, 24 insertions(+), 30 deletions(-)

diff --git a/src/app/action/controllers/PreProcessActionController.php b/src/app/action/controllers/PreProcessActionController.php
index a70ca1ea07f..08807c08e37 100755
--- a/src/app/action/controllers/PreProcessActionController.php
+++ b/src/app/action/controllers/PreProcessActionController.php
@@ -735,7 +735,7 @@ class PreProcessActionController
                             break;
                         }
 
-                        $attachment['collId'] = 'attachments_coll';
+                        $attachment['type'] = 'attachments_coll';
                         $resources[] = $attachment;
                         unset($aAttachments[$key]);
                     }
@@ -791,7 +791,7 @@ class PreProcessActionController
                                 }
                             }
                         }
-                        $resInfo['collId'] = 'letterbox_coll';
+                        $resInfo['type'] = 'letterbox_coll';
                         $resources[] = $resInfo;
                     }
                 }
diff --git a/src/app/action/controllers/ShippingTrait.php b/src/app/action/controllers/ShippingTrait.php
index b54273b8433..d28195b9562 100644
--- a/src/app/action/controllers/ShippingTrait.php
+++ b/src/app/action/controllers/ShippingTrait.php
@@ -99,7 +99,7 @@ trait ShippingTrait
             }
             $contacts[] = $afnorAddress;
 
-            $attachment['collId'] = 'attachments_coll';
+            $attachment['type'] = 'attachments_coll';
             $resourcesList[] = $attachment;
         }
 
@@ -136,7 +136,7 @@ trait ShippingTrait
                 }
                 $contactsResource[] = $afnorAddress;
             }
-            $resource['collId'] = 'letterbox_coll';
+            $resource['type'] = 'letterbox_coll';
             $resourcesList[] = $resource;
         }
 
@@ -184,7 +184,7 @@ trait ShippingTrait
                 continue;
             }
 
-            $convertedDocument = ConvertPdfController::getConvertedPdfById(['resId' => $resId, 'collId' => $resource['collId']]);
+            $convertedDocument = ConvertPdfController::getConvertedPdfById(['resId' => $resId, 'collId' => $resource['type']]);
             $docserver = DocserverModel::getByDocserverId(['docserverId' => $convertedDocument['docserver_id'], 'select' => ['path_template']]);
             if (empty($docserver['path_template']) || !file_exists($docserver['path_template'])) {
                 $errors[] = "Docserver does not exist for resource {$resId}";
@@ -207,7 +207,7 @@ trait ShippingTrait
                 continue;
             }
 
-            if ($resource['collId'] == 'attachments_coll') {
+            if ($resource['type'] == 'attachments_coll') {
                 $createRecipient = CurlModel::execSimple([
                     'url'           => $mailevaConfig['uri'] . "/mail/v1/sendings/{$sendingId}/recipients",
                     'bearerAuth'    => ['token' => $token],
@@ -288,7 +288,7 @@ trait ShippingTrait
                 'resources' => [$resource]
             ]);
 
-            $documentType = $resource['collId'] == 'attachments_coll' ? 'attachment' : 'resource';
+            $documentType = $resource['type'] == 'attachments_coll' ? 'attachment' : 'resource';
             ShippingModel::create([
                 'userId'            => $currentUser['id'],
                 'documentId'        => $resId,
diff --git a/src/app/resource/controllers/ResController.php b/src/app/resource/controllers/ResController.php
index 88db020288b..2269ecd37d8 100755
--- a/src/app/resource/controllers/ResController.php
+++ b/src/app/resource/controllers/ResController.php
@@ -697,33 +697,27 @@ class ResController extends ResourceControlController
             return $response->withStatus(400)->withJson(['errors' => 'Body param integrations is missing or not an array']);
         }
 
-        $resources = ResModel::get([
-            'select' => ['res_id', 'integrations'],
-            'where'  => ['res_id in (?)'],
-            'data'   => [$body['resources']]
-        ]);
-
-        foreach ($resources as $resource) {
-            $integrations = json_decode($resource['integrations'], true);
+        if (isset($body['integrations']['inSignatureBook']) && Validator::boolType()->validate($body['integrations']['inSignatureBook'])) {
+            $inSignatureBook = $body['integrations']['inSignatureBook'] ? 'true' : 'false';
 
-            if (Validator::boolType()->validate($body['integrations']['inSignatureBook'])) {
-                $integrations['inSignatureBook'] = $body['integrations']['inSignatureBook'];
-            } else {
-                $integrations['inSignatureBook'] = $integrations['inSignatureBook'] ?? false;
-            }
+            ResModel::update([
+                'postSet'   => [
+                    'integrations' => "jsonb_set(integrations, '{inSignatureBook}', '".$inSignatureBook."')",
+                ],
+                'where' => ['res_id in (?)'],
+                'data'  => [$body['resources']]
+            ]);
+        }
 
-            if (Validator::boolType()->validate($body['integrations']['inShipping'])) {
-                $integrations['inShipping'] = $body['integrations']['inShipping'];
-            } else {
-                $integrations['inShipping'] = $integrations['inShipping'] ?? false;
-            }
+        if (isset($body['integrations']['inShipping']) && Validator::boolType()->validate($body['integrations']['inShipping'])) {
+            $inShipping = $body['integrations']['inShipping'] ? 'true' : 'false';
 
             ResModel::update([
-                'set' => [
-                    'integrations' => json_encode($integrations)
+                'postSet'   => [
+                    'integrations' => "jsonb_set(integrations, '{inShipping}', '".$inShipping."')",
                 ],
-                'where' => ['res_id = ?'],
-                'data'  => [$resource['res_id']]
+                'where' => ['res_id in (?)'],
+                'data'  => [$body['resources']]
             ]);
         }
 
diff --git a/src/app/shipping/controllers/ShippingTemplateController.php b/src/app/shipping/controllers/ShippingTemplateController.php
index d3b0964d790..c9616c36c56 100755
--- a/src/app/shipping/controllers/ShippingTemplateController.php
+++ b/src/app/shipping/controllers/ShippingTemplateController.php
@@ -291,7 +291,7 @@ class ShippingTemplateController
         foreach ($aArgs['resources'] as $value) {
             $resourceId = $value['res_id'];
 
-            $convertedResource = ConvertPdfController::getConvertedPdfById(['select' => ['docserver_id', 'path', 'filename'], 'resId' => $resourceId, 'collId' => $value['collId']]);
+            $convertedResource = ConvertPdfController::getConvertedPdfById(['select' => ['docserver_id', 'path', 'filename'], 'resId' => $resourceId, 'collId' => $value['type']]);
             $docserver         = DocserverModel::getByDocserverId(['docserverId' => $convertedResource['docserver_id'], 'select' => ['path_template']]);
             $pathToDocument    = $docserver['path_template'] . str_replace('#', DIRECTORY_SEPARATOR, $convertedResource['path']) . $convertedResource['filename'];
 
-- 
GitLab