2024-10-29 16:13:07 +01:00
|
|
|
<?php
|
|
|
|
|
|
2024-11-11 13:54:54 +01:00
|
|
|
/**
|
|
|
|
|
* Send a notification to all users who manage a specific project, excluding the user sending the notification.
|
|
|
|
|
*
|
|
|
|
|
* This function retrieves all users managing the project specified by `$projectUri` (excluding the current user)
|
|
|
|
|
* and adds a new notification to their `notifications` field, stored as a YAML-encoded array.
|
|
|
|
|
* If the `notifications` field is empty, an empty array is initialized before appending the new notification.
|
|
|
|
|
* In case of an error during update, an exception is thrown with the error message and line number.
|
|
|
|
|
*
|
|
|
|
|
* @param array $notificationData An associative array containing the notification data to be added.
|
|
|
|
|
* @param string $projectUri The URI of the project associated with this notification.
|
|
|
|
|
* @throws Exception If an error occurs while updating a user's notifications.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
return function ($projectUri, $notificationData) {
|
2024-11-17 12:23:28 +01:00
|
|
|
$notificationData['isRead'] = false;
|
2024-11-11 13:54:54 +01:00
|
|
|
$recipients = page($projectUri)->managers()->toUsers()->not($this);
|
|
|
|
|
|
|
|
|
|
if (!$recipients) return;
|
|
|
|
|
|
|
|
|
|
foreach ($recipients as $otherUser) {
|
2024-10-29 16:13:07 +01:00
|
|
|
try {
|
|
|
|
|
$notifications = $otherUser->notifications()->isNotEmpty()
|
|
|
|
|
? Yaml::decode($otherUser->notifications()->value())
|
|
|
|
|
: [];
|
|
|
|
|
|
2024-11-11 13:54:54 +01:00
|
|
|
$notifications[] = $notificationData;
|
2024-10-29 16:13:07 +01:00
|
|
|
|
|
|
|
|
$otherUser->update([
|
|
|
|
|
'notifications' => $notifications
|
|
|
|
|
]);
|
|
|
|
|
} catch (\Throwable $th) {
|
|
|
|
|
throw new Exception("Error updating notifications: " . $th->getMessage() . ' line ' . $th->getLine(), 1);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|