From 9393ef0c43ba669a02dd44da5a478b47eda7b0d2 Mon Sep 17 00:00:00 2001
From: Damien <damien.burel@maarch.org>
Date: Wed, 4 Dec 2019 16:49:53 +0100
Subject: [PATCH] FEAT #12510 TIME 1:00 Save contacts in attachments

---
 .../controllers/AttachmentController.php      | 39 +++++++++++++++++--
 .../resource/controllers/ResController.php    |  2 +-
 .../resource/controllers/StoreController.php  |  4 ++
 src/app/user/models/UserModelAbstract.php     | 25 ++++++------
 4 files changed, 55 insertions(+), 15 deletions(-)

diff --git a/src/app/attachment/controllers/AttachmentController.php b/src/app/attachment/controllers/AttachmentController.php
index e8f48b0eb68..df0a9770721 100755
--- a/src/app/attachment/controllers/AttachmentController.php
+++ b/src/app/attachment/controllers/AttachmentController.php
@@ -15,6 +15,7 @@
 namespace Attachment\controllers;
 
 use Attachment\models\AttachmentModel;
+use Contact\models\ContactModel;
 use Convert\controllers\ConvertPdfController;
 use Convert\controllers\ConvertThumbnailController;
 use Convert\models\AdrModel;
@@ -87,8 +88,8 @@ class AttachmentController
             'id'        => $args['id'],
             'select'    => [
                 'res_id as "resId"', 'res_id_master as "resIdMaster"', 'status', 'title', 'identifier as chrono', 'typist', 'modified_by as "modifiedBy"', 'relation', 'attachment_type as type',
-                'origin_id as "originId"', 'creation_date as "creationDate"', 'modification_date as "modificationDate"', 'validation_date as "validationDate"',
-                'fulltext_result as "fulltextResult"', 'in_signature_book as "inSignatureBook"', 'in_send_attach as "inSendAttach"'
+                'recipient_id', 'recipient_type', 'origin_id as "originId"', 'creation_date as "creationDate"', 'modification_date as "modificationDate"',
+                'validation_date as "validationDate"', 'fulltext_result as "fulltextResult"', 'in_signature_book as "inSignatureBook"', 'in_send_attach as "inSendAttach"'
             ]
         ]);
         if (empty($attachment) || in_array($attachment['status'], ['DEL', 'OBS'])) {
@@ -177,7 +178,10 @@ class AttachmentController
         if (!empty($control['errors'])) {
             return $response->withStatus(400)->withJson(['errors' => $control['errors']]);
         }
-
+        $control = AttachmentController::controlRecipient(['body' => $body]);
+        if (!empty($control['errors'])) {
+            return $response->withStatus(400)->withJson(['errors' => $control['errors']]);
+        }
         $control = AttachmentController::controlDates(['body' => $body]);
         if (!empty($control['errors'])) {
             return $response->withStatus(400)->withJson(['errors' => $control['errors']]);
@@ -812,6 +816,11 @@ class AttachmentController
             return ['errors' => $control['errors']];
         }
 
+        $control = AttachmentController::controlRecipient(['body' => $body]);
+        if (!empty($control['errors'])) {
+            return ['errors' => $control['errors']];
+        }
+
         $control = AttachmentController::controlDates(['body' => $body]);
         if (!empty($control['errors'])) {
             return ['errors' => $control['errors']];
@@ -861,6 +870,30 @@ class AttachmentController
         return true;
     }
 
+    private static function controlRecipient(array $args)
+    {
+        $body = $args['body'];
+
+        if (!empty($body['recipientId'])) {
+            if (!Validator::intVal()->notEmpty()->validate($body['recipientId'])) {
+                return ['errors' => 'Body recipientId is not an integer'];
+            }
+            if (empty($body['recipientType']) || !in_array($body['recipientType'], ['user', 'contact'])) {
+                return ['errors' => 'Body recipientType is empty or not in [user, contact]'];
+            }
+            if ($body['recipientType'] == 'user') {
+                $recipient = UserModel::getById(['id' => $body['destUserId'], 'select' => [1], 'noDeleted' => true]);
+            } elseif ($body['recipientType'] == 'contact') {
+                $recipient = ContactModel::getById(['id' => $body['contactId'], 'select' => [1]]);
+            }
+            if (empty($recipient)) {
+                return ['errors' => 'Body recipientId does not exist'];
+            }
+        }
+
+        return true;
+    }
+
     private static function controlDates(array $args)
     {
         $body = $args['body'];
diff --git a/src/app/resource/controllers/ResController.php b/src/app/resource/controllers/ResController.php
index b8a2b1a8167..5bc4720a179 100755
--- a/src/app/resource/controllers/ResController.php
+++ b/src/app/resource/controllers/ResController.php
@@ -223,7 +223,7 @@ class ResController
 
         ResController::updateAdjacentData(['body' => $body, 'resId' => $args['resId']]);
 
-        $resource = ResModel::getById(['id' => $args['resId'], 'select' => ['alt_identifier']]);
+        $resource = ResModel::getById(['resId' => $args['resId'], 'select' => ['alt_identifier']]);
         if (!empty($body['encodedFile'])) {
             AdrModel::deleteDocumentAdr(['where' => ['res_id = ?'], 'data' => [$args['resId']]]);
             ConvertPdfController::convert([
diff --git a/src/app/resource/controllers/StoreController.php b/src/app/resource/controllers/StoreController.php
index 96b04d165d6..ef147d6a6ce 100755
--- a/src/app/resource/controllers/StoreController.php
+++ b/src/app/resource/controllers/StoreController.php
@@ -279,6 +279,8 @@ class StoreController
             'origin_id'             => $args['originId'] ?? null,
             'res_id_master'         => $args['resIdMaster'],
             'attachment_type'       => $args['type'],
+            'recipient_id'          => $args['recipientId'] ?? null,
+            'recipient_type'        => $args['recipientType'] ?? null,
             'validation_date'       => $args['validationDate'] ?? null,
             'effective_date'        => $args['effectiveDate'] ?? null,
             'in_signature_book'     => empty($args['inSignatureBook']) ? 'false' : 'true',
@@ -300,6 +302,8 @@ class StoreController
 
         $preparedData = [
             'title'                 => $args['title'] ?? null,
+            'recipient_id'          => $args['recipientId'] ?? null,
+            'recipient_type'        => $args['recipientType'] ?? null,
             'attachment_type'       => $args['type'],
             'validation_date'       => $args['validationDate'] ?? null,
             'effective_date'        => $args['effectiveDate'] ?? null,
diff --git a/src/app/user/models/UserModelAbstract.php b/src/app/user/models/UserModelAbstract.php
index d8954670a7a..553621f1bf2 100755
--- a/src/app/user/models/UserModelAbstract.php
+++ b/src/app/user/models/UserModelAbstract.php
@@ -18,8 +18,6 @@ use SrcCore\models\AuthenticationModel;
 use SrcCore\models\DatabaseModel;
 use SrcCore\models\ValidatorModel;
 
-require_once 'core/class/Url.php';
-
 abstract class UserModelAbstract
 {
     public static function get(array $aArgs)
@@ -39,23 +37,28 @@ abstract class UserModelAbstract
         return $aUsers;
     }
 
-    public static function getById(array $aArgs)
+    public static function getById(array $args)
     {
-        ValidatorModel::notEmpty($aArgs, ['id']);
-        ValidatorModel::intVal($aArgs, ['id']);
+        ValidatorModel::notEmpty($args, ['id']);
+        ValidatorModel::intVal($args, ['id']);
 
-        $aUser = DatabaseModel::select([
-            'select'    => empty($aArgs['select']) ? ['*'] : $aArgs['select'],
+        $where = ['id = ?'];
+        if (!empty($args['noDeleted'])) {
+            $where[] = "status != 'DEL'";
+        }
+
+        $user = DatabaseModel::select([
+            'select'    => empty($args['select']) ? ['*'] : $args['select'],
             'table'     => ['users'],
-            'where'     => ['id = ?'],
-            'data'      => [$aArgs['id']]
+            'where'     => $where,
+            'data'      => [$args['id']]
         ]);
 
-        if (empty($aUser)) {
+        if (empty($user[0])) {
             return [];
         }
 
-        return $aUser[0];
+        return $user[0];
     }
 
     public static function getByExternalId(array $aArgs)
-- 
GitLab