refactor deleteNotification method
This commit is contained in:
parent
82f4aaf904
commit
fe56312f20
6 changed files with 70 additions and 33 deletions
26
public/site/plugins/notifications/user-methods/delete.php
Normal file
26
public/site/plugins/notifications/user-methods/delete.php
Normal 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);
|
||||
}
|
||||
}
|
||||
};
|
||||
Loading…
Add table
Add a link
Reference in a new issue