refactor deleteNotification method

This commit is contained in:
isUnknown 2024-11-17 12:01:20 +01:00
parent 82f4aaf904
commit fe56312f20
6 changed files with 70 additions and 33 deletions

View file

@ -1,10 +1,11 @@
<?php
Kirby::plugin('adrienpayet/pdc-notifications', [
'routes' => [
require(__DIR__ . '/routes/mark-as-read.php'),
],
// 'routes' => [
// require(__DIR__ . '/routes/mark-as-read.php'),
// ],
'userMethods' => [
'sendNotification' => require(__DIR__ . '/user-methods/send.php')
'sendNotification' => require(__DIR__ . '/user-methods/send.php'),
'deleteNotification' => require(__DIR__ . '/user-methods/delete.php')
]
]);

View file

@ -0,0 +1,26 @@
<?php
return function($project, $notificationId) {
$projectManagers = $project->managers()->toUsers();
foreach ($projectManagers as $user) {
try {
$notifications = $user->notifications()->isNotEmpty()
? Yaml::decode($user->notifications()->value())
: [];
foreach ($notifications as $key => $notification) {
if ($notification['id'] === $notificationId) {
unset($notifications[$key]);
}
}
// Réindexation et ré-encodage en YAML avant la mise à jour
$user->update([
'notifications' => Yaml::encode(array_values($notifications))
]);
} catch (\Throwable $th) {
throw new Exception("Error updating notifications: " . $th->getMessage() . ' line ' . $th->getLine(), 1);
}
}
};