From 00aa9f97d60d2496a1ff484b4ee7f4ebd0ffb5fc Mon Sep 17 00:00:00 2001
From: Damien <damien.burel@maarch.org>
Date: Mon, 17 Feb 2020 12:22:15 +0100
Subject: [PATCH] FEAT #12071 TIME 0:35 Get field by res id

---
 rest/index.php                                |  9 +++++---
 .../contact/controllers/ContactController.php | 12 ++++------
 .../resource/controllers/ResController.php    | 22 +++++++++++++++++++
 3 files changed, 32 insertions(+), 11 deletions(-)

diff --git a/rest/index.php b/rest/index.php
index b8f967d7a68..2e43fcf6c5f 100755
--- a/rest/index.php
+++ b/rest/index.php
@@ -311,6 +311,9 @@ $app->get('/roles', \Entity\controllers\ListTemplateController::class . ':getRol
 $app->get('/availableCircuits', \Entity\controllers\ListTemplateController::class . ':getAvailableCircuits');
 $app->put('/circuits/{type}', \Entity\controllers\ListInstanceController::class . ':updateCircuits');
 
+//MessageExchanges
+$app->get('/messageExchanges/{id}', \MessageExchange\controllers\MessageExchangeController::class . ':getById');
+
 //Notes
 $app->post('/notes', \Note\controllers\NoteController::class . ':create');
 $app->get('/notes/{id}', \Note\controllers\NoteController::class . ':getById');
@@ -365,17 +368,17 @@ $app->get('/resources/{resId}/visaCircuit', \Entity\controllers\ListInstanceCont
 $app->get('/resources/{resId}/opinionCircuit', \Entity\controllers\ListInstanceController::class . ':getOpinionCircuitByResId');
 $app->get('/resources/{resId}/parallelOpinion', \Entity\controllers\ListInstanceController::class . ':getParallelOpinionByResId');
 $app->get('/resources/{resId}/defaultCircuit', \Entity\controllers\ListTemplateController::class . ':getDefaultCircuitByResId');
-$app->delete('/resources/{resId}/circuits/{type}', \Entity\controllers\ListInstanceController::class . ':deleteCircuit');
 $app->get('/resources/{resId}/linkedResources', \Resource\controllers\LinkController::class . ':getLinkedResources');
 $app->post('/resources/{resId}/linkedResources', \Resource\controllers\LinkController::class . ':linkResources');
-$app->delete('/resources/{resId}/linkedResources/{id}', \Resource\controllers\LinkController::class . ':unlinkResources');
 $app->put('/resources/{resId}/sign', \SignatureBook\controllers\SignatureBookController::class . ':signResource');
 $app->put('/resources/{resId}/unsign', \SignatureBook\controllers\SignatureBookController::class . ':unsignResource');
 $app->get('/resources/{resId}/acknowledgementReceipts', \AcknowledgementReceipt\controllers\AcknowledgementReceiptController::class . ':getByResId');
 $app->get('/resources/{resId}/shippings', \Shipping\controllers\ShippingController::class . ':getByResId');
 $app->get('/resources/{resId}/messageExchanges', \MessageExchange\controllers\MessageExchangeController::class . ':getByResId');
 $app->get('/resources/{resId}/emailsInitialization', \Email\controllers\EmailController::class . ':getInitializationByResId');
-$app->get('/messageExchanges/{id}', \MessageExchange\controllers\MessageExchangeController::class . ':getById');
+$app->get('/resources/{resId}/fields/{fieldId}', \Resource\controllers\ResController::class . ':getField');
+$app->delete('/resources/{resId}/linkedResources/{id}', \Resource\controllers\LinkController::class . ':unlinkResources');
+$app->delete('/resources/{resId}/circuits/{type}', \Entity\controllers\ListInstanceController::class . ':deleteCircuit');
 
 $app->put('/res/resource/status', \Resource\controllers\ResController::class . ':updateStatus');
 $app->post('/res/list', \Resource\controllers\ResController::class . ':getList');
diff --git a/src/app/contact/controllers/ContactController.php b/src/app/contact/controllers/ContactController.php
index 912e4889f65..9fa3b054c84 100755
--- a/src/app/contact/controllers/ContactController.php
+++ b/src/app/contact/controllers/ContactController.php
@@ -438,9 +438,7 @@ class ContactController
         if (!empty($queryParams['redirect'])) {
             if (!Validator::intVal()->validate($queryParams['redirect'])) {
                 return $response->withStatus(400)->withJson(['errors' => 'Query param redirect is not an integer']);
-            }
-
-            if ($queryParams['redirect'] == $args['id']) {
+            } elseif ($queryParams['redirect'] == $args['id']) {
                 return $response->withStatus(400)->withJson(['errors' => 'Cannot redirect to contact you are deleting']);
             }
 
@@ -449,8 +447,6 @@ class ContactController
                 return $response->withStatus(400)->withJson(['errors' => 'Contact does not exist']);
             }
 
-            // Replace contact with redirect
-            // get all res_id linked to contact args['id']
             $resourcesContacts = ResourceContactModel::get([
                 'select' => ['res_id', 'mode'],
                 'where'  => ['item_id = ?', "type = 'contact'"],
@@ -459,8 +455,8 @@ class ContactController
 
             ResourceContactModel::update([
                 'set'   => ['item_id' => $queryParams['redirect']],
-                'where' => ['item_id = ?', "type = 'contact'"],
-                'data'  => [$args['id']]
+                'where' => ['item_id = ?', 'type = ?'],
+                'data'  => [$args['id'], 'contact']
             ]);
 
             // Delete duplicates if needed
@@ -502,7 +498,7 @@ class ContactController
 
         ResourceContactModel::delete([
             'where' => ['item_id = ?', "type = 'contact'"],
-            'data' => [$args['id']]
+            'data'  => [$args['id']]
         ]);
 
         ContactModel::delete([
diff --git a/src/app/resource/controllers/ResController.php b/src/app/resource/controllers/ResController.php
index b88d73418c4..b6ce8c3c4c0 100755
--- a/src/app/resource/controllers/ResController.php
+++ b/src/app/resource/controllers/ResController.php
@@ -814,6 +814,28 @@ class ResController extends ResourceControlController
         return $response->withStatus(204);
     }
 
+    public function getField(Request $request, Response $response, array $args)
+    {
+        if (!ResController::hasRightByResId(['resId' => [$args['resId']], 'userId' => $GLOBALS['id']])) {
+            return $response->withStatus(403)->withJson(['errors' => 'Document out of perimeter']);
+        }
+
+        $authorizedFields = ['destination', 'status'];
+        if (!in_array($args['fieldId'], $authorizedFields)) {
+            return $response->withStatus(403)->withJson(['errors' => 'Field out of perimeter']);
+        }
+
+        $resource = ResModel::getById([
+            'select'    => [$args['fieldId']],
+            'resId'     => $args['resId']
+        ]);
+        if (empty($resource)) {
+            return $response->withStatus(400)->withJson(['errors' => 'Document does not exist']);
+        }
+
+        return $response->withJson(['field' => $resource[$args['fieldId']]]);
+    }
+
     public static function getEncodedDocument(array $aArgs)
     {
         ValidatorModel::notEmpty($aArgs, ['resId']);
-- 
GitLab