From 569c8cf05ef59e90d044df84b5205ae08ae9d08f Mon Sep 17 00:00:00 2001
From: Guillaume Heurtier <guillaume.heurtier@maarch.org>
Date: Fri, 25 Jun 2021 10:18:59 +0200
Subject: [PATCH] FEAT #17163 TIME 1:30 WIP scheduler log in history

---
 bin/scheduler.php | 50 +++++++++++++++++++++++++++++++++++++----------
 1 file changed, 40 insertions(+), 10 deletions(-)

diff --git a/bin/scheduler.php b/bin/scheduler.php
index 5ad1cda1e3..c1a8fa98c8 100644
--- a/bin/scheduler.php
+++ b/bin/scheduler.php
@@ -18,17 +18,10 @@ $configPath = $argv[1];
 DatabasePDO::reset();
 new DatabasePDO(['configPath' => $configPath]);
 
-$restUser = UserModel::get(['select' => ['id'], 'where' => ['"isRest" = ?'], 'data' => ['true']]);
-if (empty($restUser[0])) {
-    echo "User not found\n";
-    exit(1);
-}
-$restUser = $restUser[0];
-$GLOBALS['id'] = $restUser['id'];
-
 $configuration = ConfigurationModel::getByIdentifier(['identifier' => 'customization']);
 $configuration = $configuration[0];
 $configuration['value'] = json_decode($configuration['value'], true);
+
 $applicationUrl = $configuration['value']['applicationUrl'];
 
 if (empty($applicationUrl)) {
@@ -37,12 +30,27 @@ if (empty($applicationUrl)) {
         'objectType' => 'configurations',
         'objectId'   => $configuration['id'],
         'type'       => 'ERROR',
-        'message'    => "{schedulerError} : Cannot run scheduler : no applicationUrl defined"
+        'message'    => "{schedulerError} :  no applicationUrl defined"
     ]);
     echo "Cannot run scheduler : no applicationUrl defined\n";
     exit(1);
 }
 
+$restUser = UserModel::get(['select' => ['id'], 'where' => ['"isRest" = ?'], 'data' => ['true']]);
+if (empty($restUser[0])) {
+    HistoryController::add([
+        'code'       => 'KO',
+        'objectType' => 'configurations',
+        'objectId'   => $configuration['id'],
+        'type'       => 'ERROR',
+        'message'    => "{schedulerError} :  no rest user found"
+    ]);
+    echo "User not found\n";
+    exit(1);
+}
+$restUser = $restUser[0];
+$GLOBALS['id'] = $restUser['id'];
+
 $schedules = NotificationsScheduleModel::get();
 
 $currentDate = new DateTime();
@@ -148,6 +156,8 @@ function sendNotifications(array $args)
 
     $recipients = array_unique(array_column($notificationStack, 'recipient_id'));
 
+    $nbEmailSent = 0;
+
     foreach ($recipients as $recipientId) {
         $recipientNotifications = array_filter($notificationStack, function ($notification) use ($recipientId) {
             return $notification['recipient_id'] == $recipientId;
@@ -171,7 +181,7 @@ function sendNotifications(array $args)
 
         $emailBody .= str_replace('{{rows}}', $rows, $lang['notificationTemplateTable']);
 
-        EmailController::createEmail([
+        $isSent = EmailController::createEmail([
             'userId' => $restUser['id'],
             'data'   => [
                 'sender'     => 'Notification',
@@ -182,8 +192,28 @@ function sendNotifications(array $args)
                 'status'     => 'EXPRESS'
             ]
         ]);
+
+        if (!empty($isSent['errors'])) {
+            HistoryController::add([
+                'code'       => 'KO',
+                'objectType' => 'configurations',
+                'objectId'   => $args['configurationId'],
+                'type'       => 'ERROR',
+                'message'    => "{schedulerError} : error when sending summary : " . $isSent['errors']
+            ]);
+        } else {
+            $nbEmailSent++;
+        }
     }
 
+    HistoryController::add([
+        'code'       => 'KO',
+        'objectType' => 'configurations',
+        'objectId'   => $args['configurationId'],
+        'type'       => 'ERROR',
+        'message'    => "{schedulerError} : sent $nbEmailSent {$args['type']}summaries"
+    ]);
+
     $stackIds = array_column($notificationStack, 'id');
     NotificationStackModel::delete([
         'where' => ['id in (?)'],
-- 
GitLab