From c1c1b09f514e01c4ec7049205739d624b30257c0 Mon Sep 17 00:00:00 2001
From: Guillaume Heurtier <guillaume.heurtier@maarch.org>
Date: Tue, 22 Jun 2021 09:42:23 +0200
Subject: [PATCH] FEAT #17163 TIME 1:30 add is writable check when creating
 scheduler script + fix fill stack + delete stack after doc is processed + WIP
 scheduler log

---
 bin/scheduler.php                                  | 14 +++++++++++---
 bin/scheduler.sh                                   |  4 ----
 lang/fr.json                                       |  5 +++--
 .../document/controllers/DocumentController.php    |  6 ++++++
 src/app/email/controllers/EmailController.php      |  4 ++--
 .../NotificationsScheduleController.php            |  7 +++++++
 6 files changed, 29 insertions(+), 11 deletions(-)
 delete mode 100644 bin/scheduler.sh

diff --git a/bin/scheduler.php b/bin/scheduler.php
index 2b14ea7ebe..5ad1cda1e3 100644
--- a/bin/scheduler.php
+++ b/bin/scheduler.php
@@ -3,6 +3,7 @@
 use Configuration\models\ConfigurationModel;
 use Document\models\DocumentModel;
 use Email\controllers\EmailController;
+use History\controllers\HistoryController;
 use Notification\models\NotificationsScheduleModel;
 use Notification\models\NotificationStackModel;
 use SrcCore\controllers\LanguageController;
@@ -27,10 +28,17 @@ $GLOBALS['id'] = $restUser['id'];
 
 $configuration = ConfigurationModel::getByIdentifier(['identifier' => 'customization']);
 $configuration = $configuration[0];
-$configuration = json_decode($configuration['value'], true);
-$applicationUrl = $configuration['applicationUrl'];
+$configuration['value'] = json_decode($configuration['value'], true);
+$applicationUrl = $configuration['value']['applicationUrl'];
 
 if (empty($applicationUrl)) {
+    HistoryController::add([
+        'code'       => 'KO',
+        'objectType' => 'configurations',
+        'objectId'   => $configuration['id'],
+        'type'       => 'ERROR',
+        'message'    => "{schedulerError} : Cannot run scheduler : no applicationUrl defined"
+    ]);
     echo "Cannot run scheduler : no applicationUrl defined\n";
     exit(1);
 }
@@ -182,7 +190,7 @@ function sendNotifications(array $args)
         'data'  => [$stackIds]
     ]);
 
-    return true;
+    return $stackIds;
 }
 
 
diff --git a/bin/scheduler.sh b/bin/scheduler.sh
deleted file mode 100644
index 82c5e8093d..0000000000
--- a/bin/scheduler.sh
+++ /dev/null
@@ -1,4 +0,0 @@
-#!/bin/sh
-cd /var/www/html/MaarchParapheur/
-filePath='/var/www/html/MaarchParapheur/bin/scheduler.php'
-php $filePath /var/www/html/MaarchParapheur/config/
diff --git a/lang/fr.json b/lang/fr.json
index 45e8c15809..5cc8a243ac 100755
--- a/lang/fr.json
+++ b/lang/fr.json
@@ -572,7 +572,7 @@
 		"scheduleUpdated": "Planification modifiée",
 		"frequency": "Fréquence",
 		"notificationDocumentsAddedBody": "Bonjour,<br/><br/>Des documents vous ont été transmis sur Maarch Parapheur.<br/><br/>",
-		"notificationTemplateTable": "<table style=\"border: 1pt solid #000000; width: 1582px; height: 77px;\" border=\"1\" frame=\"box\" cellspacing=\"1\" cellpadding=\"5\">\n<tbody>\n<tr>\n<td><span style=\"font-family: arial,helvetica,sans-serif; font-size: small;\"><strong>Objet</strong></span></td>\n<td><span style=\"font-family: arial,helvetica,sans-serif; font-size: small;\"><strong>Liens</strong></span></td>\n</tr>\n{{rows}}\n</tbody>\n</table>",
+		"notificationTemplateTable": "<table style=\"border: 1pt solid #000000; width: 582px; height: 77px;\" border=\"1\" frame=\"box\" cellspacing=\"1\" cellpadding=\"5\">\n<tbody>\n<tr>\n<td><span style=\"font-family: arial,helvetica,sans-serif; font-size: small;\"><strong>Objet</strong></span></td>\n<td><span style=\"font-family: arial,helvetica,sans-serif; font-size: small;\"><strong>Liens</strong></span></td>\n</tr>\n{{rows}}\n</tbody>\n</table>",
 		"notificationTemplateRow": "<tr>\n<td><span style=\"font-family: arial,helvetica,sans-serif; font-size: small;\">{{object}}</span></td>\n<td><span style=\"font-family: arial,helvetica,sans-serif;\"><a href=\"{{link}}\">Voir</a></span></td>\n</tr>",
 		"notificationDocumentsAddedSubject": "[Maarch Parapheur] Nouveaux documents à traiter",
 		"applicationUrl": "URL de l'application",
@@ -599,6 +599,7 @@
 		"schedulerInitialized": "Planificateur initialisé",
 		"scheduleDeleted": "Planification supprimée",
 		"manage_notificationsDesc": "Planifier les récapitulatifs d'évènements",
-		"notifications_schedule": "Planification de notifications"
+		"notifications_schedule": "Planification de notifications",
+		"schedulerError": "Erreur planificateur  "
 	}
 }
diff --git a/src/app/document/controllers/DocumentController.php b/src/app/document/controllers/DocumentController.php
index 508468180c..68bd43e020 100755
--- a/src/app/document/controllers/DocumentController.php
+++ b/src/app/document/controllers/DocumentController.php
@@ -18,6 +18,7 @@ use Attachment\controllers\AttachmentController;
 use Docserver\models\AdrModel;
 use Email\controllers\EmailController;
 use Group\controllers\PrivilegeController;
+use Notification\models\NotificationStackModel;
 use Respect\Validation\Validator;
 use setasign\Fpdi\Tcpdf\Fpdi;
 use SrcCore\controllers\UrlController;
@@ -969,6 +970,11 @@ class DocumentController
             $historyMessagePart = "{actionDoneInPlaceOf} {$user} : ";
         }
 
+        NotificationStackModel::delete([
+            'where' => ['main_document_id = ?', 'type = ?', 'recipient_id = ?'],
+            'data'  => [$args['id'], 'next_user', $GLOBALS['id']]
+        ]);
+
         HistoryController::add([
             'code'          => 'OK',
             'objectType'    => 'main_documents',
diff --git a/src/app/email/controllers/EmailController.php b/src/app/email/controllers/EmailController.php
index 5eca8813a3..b8e576559b 100644
--- a/src/app/email/controllers/EmailController.php
+++ b/src/app/email/controllers/EmailController.php
@@ -298,7 +298,7 @@ class EmailController
         $schedule = NotificationsScheduleModel::get([
             'select' => ['id'],
             'where'  => ['type = ?'],
-            'data'   => ['typist_' . $args['mode']]
+            'data'   => ['next_user']
         ]);
         $schedule = array_column($schedule, 'id');
         if (!empty($nextUser['preferences']['notifications']['summaries'])) {
@@ -323,7 +323,7 @@ class EmailController
             $schedule = NotificationsScheduleModel::get([
                 'select' => ['id'],
                 'where'  => ['type = ?'],
-                'data'   => ['typist_' . $args['mode']]
+                'data'   => ['next_user']
             ]);
             $schedule = array_column($schedule, 'id');
             if (!empty($nextSubstituteUser['preferences']['notifications']['summaries'])) {
diff --git a/src/app/notifications/controllers/NotificationsScheduleController.php b/src/app/notifications/controllers/NotificationsScheduleController.php
index d964e8c42f..bb6075aec6 100644
--- a/src/app/notifications/controllers/NotificationsScheduleController.php
+++ b/src/app/notifications/controllers/NotificationsScheduleController.php
@@ -227,6 +227,9 @@ class NotificationsScheduleController
 
         if (empty($configuration['value']['schedulerPath']) || !is_file($configuration['value']['schedulerPath'])) {
             $schedulerScript = NotificationsScheduleController::createSchedulerScript();
+            if (!empty($schedulerScript['errors'])) {
+                return $response->withStatus(500)->withJson(['errors' => $schedulerScript['errors']]);
+            }
             $configuration['value']['schedulerPath'] = $schedulerScript['path'];
         }
 
@@ -324,6 +327,10 @@ class NotificationsScheduleController
 
         $fullPath = $corePath . 'bin/' . $filename;
 
+        if (!is_writable($corePath . 'bin/')) {
+            return ['errors' => 'Cannot create scheduler script in ' . $corePath . 'bin/'];
+        }
+
         $file_open = fopen($fullPath, 'w+');
 
         fwrite($file_open, '#!/bin/sh');
-- 
GitLab