From ed72ee3212c58ad846cbb3c7c12809b348a18343 Mon Sep 17 00:00:00 2001 From: Ruslan Baidan Date: Wed, 2 Oct 2024 12:27:02 +0200 Subject: [PATCH] Added the system messages functionality. --- config/module.config.php | 19 +++ ...0230901112005_fix_positions_cleanup_db.php | 26 ++++ .../ApiSystemMessagesController.php | 41 +++++++ src/Entity/SystemMessage.php | 115 ++++++++++++++++++ src/Service/SystemMessageService.php | 51 ++++++++ src/Table/SystemMessageTable.php | 51 ++++++++ view/layout/layout.phtml | 1 + 7 files changed, 304 insertions(+) create mode 100755 src/Controller/ApiSystemMessagesController.php create mode 100755 src/Entity/SystemMessage.php create mode 100644 src/Service/SystemMessageService.php create mode 100755 src/Table/SystemMessageTable.php diff --git a/config/module.config.php b/config/module.config.php index af062c33..d793b50b 100755 --- a/config/module.config.php +++ b/config/module.config.php @@ -98,6 +98,19 @@ ], ], + 'monarc_api_system_messages' => [ + 'type' => 'segment', + 'options' => [ + 'route' => '/api/system-messages[/:id]', + 'constraints' => [ + 'id' => '[0-9]+', + ], + 'defaults' => [ + 'controller' => Controller\ApiSystemMessagesController::class, + ], + ], + ], + 'monarc_api_models' => [ 'type' => 'segment', 'options' => [ @@ -1413,6 +1426,7 @@ Controller\ApiAnrInstancesMetadataFieldsController::class => AutowireFactory::class, Controller\ApiInstanceMetadataController::class => AutowireFactory::class, Controller\ApiSoaScaleCommentController::class => AutowireFactory::class, + Controller\ApiSystemMessagesController::class => AutowireFactory::class, Export\Controller\ApiAnrExportController::class => AutowireFactory::class, Export\Controller\ApiAnrInstancesExportController::class => AutowireFactory::class, Export\Controller\ApiAnrObjectsExportController::class => AutowireFactory::class, @@ -1495,6 +1509,7 @@ Table\SoaTable::class => ClientEntityManagerFactory::class, Table\SnapshotTable::class => ClientEntityManagerFactory::class, Table\SoaScaleCommentTable::class => ClientEntityManagerFactory::class, + Table\SystemMessageTable::class => ClientEntityManagerFactory::class, Table\ThemeTable::class => ClientEntityManagerFactory::class, Table\ThreatTable::class => ClientEntityManagerFactory::class, Table\UserTable::class => ClientEntityManagerFactory::class, @@ -1566,6 +1581,7 @@ Service\InstanceMetadataService::class => AutowireFactory::class, Service\SoaService::class => AutowireFactory::class, Service\SoaScaleCommentService::class => AutowireFactory::class, + Service\SystemMessageService::class => AutowireFactory::class, Stats\Service\StatsAnrService::class => ReflectionBasedAbstractFactory::class, Stats\Service\StatsSettingsService::class => AutowireFactory::class, CronTask\Service\CronTaskService::class => AutowireFactory::class, @@ -1802,6 +1818,7 @@ 'monarc_api_stats_global/processed', 'monarc_api_stats_global/general_settings', 'monarc_api_stats_global/validate-stats-availability', + 'monarc_api_system_messages', ], // User : RWD access per analysis Entity\UserRole::USER_FO => [ @@ -1909,6 +1926,7 @@ 'monarc_api_stats_global/processed', 'monarc_api_stats_global/general_settings', 'monarc_api_stats_global/validate-stats-availability', + 'monarc_api_system_messages', ], Entity\UserRole::USER_ROLE_CEO => [ 'monarc_api_admin_users_roles', @@ -1923,6 +1941,7 @@ 'monarc_api_stats_global/anrs_settings', 'monarc_api_stats_global/general_settings', 'monarc_api_stats_global/validate-stats-availability', + 'monarc_api_system_messages', ], ], 'activeLanguages' => ['fr'], diff --git a/migrations/db/20230901112005_fix_positions_cleanup_db.php b/migrations/db/20230901112005_fix_positions_cleanup_db.php index 652ddf65..599f5bc1 100644 --- a/migrations/db/20230901112005_fix_positions_cleanup_db.php +++ b/migrations/db/20230901112005_fix_positions_cleanup_db.php @@ -456,6 +456,32 @@ public function change() ->removeColumn('updated_at') ->update(); + $this->execute( + 'CREATE TABLE IF NOT EXISTS `system_messages` ( + `id` int(11) unsigned NOT NULL AUTO_INCREMENT, + `user_id` int(11) unsigned NOT NULL, + `title` varchar(255) NOT NULL, + `description` TEXT, + `status` smallint(3) unsigned NOT NULL DEFAULT 1, + `creator` varchar(255) NOT NULL, + `created_at` datetime DEFAULT CURRENT_TIMESTAMP, + `updater` varchar(255) DEFAULT NULL, + `updated_at` datetime DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP, + PRIMARY KEY (`id`), + CONSTRAINT `system_messages_user_id_id_fk1` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE ON UPDATE CASCADE + );' + ); + $usersQuery = $this->query('SELECT id FROM users WHERE status = 1;'); + foreach ($usersQuery->fetchAll() as $userData) { + $this->table('system_messages')->insert([ + 'user_id' => $userData['id'], + 'title' => 'Monarc version is updated to v2.13.1', + 'description' => 'Please, read the release notes available https://www.monarc.lu/news. In case if you encounter any issues with your analysis, please let us know by sending us an email to info-monarc@nc3.lu.', + 'status' => 1, + 'creator' => 'System', + ])->saveData(); + } + /* TODO: Should be added to the next release migration, to perform this release in a safe mode. $this->table('anr_instance_metadata_fields')->removeColumn('label_translation_key')->update(); $this->table('instances_metadata')->removeColumn('comment_translation_key')->update(); diff --git a/src/Controller/ApiSystemMessagesController.php b/src/Controller/ApiSystemMessagesController.php new file mode 100755 index 00000000..d661f498 --- /dev/null +++ b/src/Controller/ApiSystemMessagesController.php @@ -0,0 +1,41 @@ +systemMessageService->getListOfActiveMessages(); + + return $this->getPreparedJsonResponse([ + 'count' => \count($messages), + 'messages' => $messages, + ]); + } + + /** + * @param int $id + */ + public function delete($id) + { + $this->systemMessageService->inactivateMessage((int)$id); + + return $this->getSuccessfulJsonResponse(); + } +} diff --git a/src/Entity/SystemMessage.php b/src/Entity/SystemMessage.php new file mode 100755 index 00000000..284e7c2a --- /dev/null +++ b/src/Entity/SystemMessage.php @@ -0,0 +1,115 @@ +id; + } + + public function getUser(): User + { + return $this->user; + } + + public function setUser(User $user): self + { + $this->user = $user; + + return $this; + } + + public function getTitle(): string + { + return $this->title; + } + + public function setTitle(string $title): self + { + $this->title = $title; + + return $this; + } + + public function getDescription(): string + { + return (string)$this->description; + } + + public function setDescription(string $description): self + { + $this->description = $description; + + return $this; + } + + public function getStatus(): int + { + return $this->status; + } + + public function setStatus(int $status): self + { + $this->status = $status; + + return $this; + } +} diff --git a/src/Service/SystemMessageService.php b/src/Service/SystemMessageService.php new file mode 100644 index 00000000..89b854db --- /dev/null +++ b/src/Service/SystemMessageService.php @@ -0,0 +1,51 @@ +getConnectedUser(); + $this->connectedUser = $connectedUser; + } + + public function getListOfActiveMessages(): array + { + $result = []; + foreach ($this->systemMessageTable->findAllActiveByUser($this->connectedUser) as $systemMessage) { + $result[] = [ + 'id' => $systemMessage->getId(), + 'title' => $systemMessage->getTitle(), + 'description' => $systemMessage->getDescription(), + ]; + } + + return $result; + } + + public function inactivateMessage(int $id): void + { + $systemMessage = $this->systemMessageTable->findByIdAndUser($id, $this->connectedUser); + if ($systemMessage->getStatus() === SystemMessage::STATUS_ACTIVE) { + $this->systemMessageTable->save( + $systemMessage->setStatus(SystemMessage::STATUS_INACTIVE)->setUpdater($this->connectedUser->getEmail()) + ); + } + } +} diff --git a/src/Table/SystemMessageTable.php b/src/Table/SystemMessageTable.php new file mode 100755 index 00000000..55460b69 --- /dev/null +++ b/src/Table/SystemMessageTable.php @@ -0,0 +1,51 @@ +getRepository()->createQueryBuilder('sm') + ->where('sm.user = :user') + ->andWhere('sm.status = ' . SystemMessage::STATUS_ACTIVE) + ->setParameter('user', $user) + ->getQuery() + ->getResult(); + } + + public function findByIdAndUser(int $id, User $user): SystemMessage + { + $systemMessage = $this->getRepository()->createQueryBuilder('sm') + ->where('sm.id = :id') + ->andWhere('sm.user = :user') + ->setParameter('id', $id) + ->setParameter('user', $user) + ->getQuery() + ->getOneOrNullResult(); + if ($systemMessage === null) { + throw EntityNotFoundException::fromClassNameAndIdentifier(SystemMessage::class, [$id]); + } + + return $systemMessage; + } +} diff --git a/view/layout/layout.phtml b/view/layout/layout.phtml index 567faeb7..86c3287b 100755 --- a/view/layout/layout.phtml +++ b/view/layout/layout.phtml @@ -92,6 +92,7 @@ ->appendFile('js/ClientSoaService.js') ->appendFile('js/StatsService.js') ->appendFile('js/ClientSettingsCtrl.js') + ->appendFile('js/SystemMessageService.js') // ANR, but client-only ->appendFile('js/anr/AnrKbMgmtCtrl.js')