diff --git a/bin/scheduler.php b/bin/scheduler.php index 5ad1cda1e39811e54e8a92791fdfd6dc81895453..c1a8fa98c8dfc00c03b8acc865cd7aacb202ce10 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 (?)'],