🔍 Code Extractor

function delete_notification

Maturity: 53

Deletes a notification node from a Neo4j graph database by its unique identifier (UID).

File:
/tf/active/vicechatdev/CDocs/utils/notifications.py
Lines:
1080 - 1102
Complexity:
simple

Purpose

This function removes a notification record from the database by matching its UID and performing a DETACH DELETE operation to remove the node and all its relationships. It's used in notification management systems to clean up notifications that are no longer needed, have been dismissed by users, or need to be removed as part of data maintenance operations. The function includes error handling and logging to track deletion failures.

Source Code

def delete_notification(notification_uid: str) -> bool:
    """
    Delete a notification.
    
    Args:
        notification_uid: UID of notification
        
    Returns:
        Boolean indicating success
    """
    try:
        db.run_query(
            """
            MATCH (n:Notification {UID: $notification_uid})
            DETACH DELETE n
            """,
            {"notification_uid": notification_uid}
        )
        return True
        
    except Exception as e:
        logger.error(f"Error deleting notification {notification_uid}: {e}")
        return False

Parameters

Name Type Default Kind
notification_uid str - positional_or_keyword

Parameter Details

notification_uid: A string representing the unique identifier (UID) of the notification to be deleted. This should be a valid UID that exists in the database as a property of a Notification node. The value is typically a UUID string format.

Return Value

Type: bool

Returns a boolean value: True if the notification was successfully deleted from the database, False if an exception occurred during the deletion process. Note that the function returns True even if no matching notification was found (the query executes successfully but affects zero nodes).

Dependencies

  • CDocs
  • logging

Required Imports

import logging
from CDocs import db

Usage Example

import logging
from CDocs import db

# Initialize logger
logger = logging.getLogger(__name__)

# Example 1: Delete a notification by UID
notification_uid = "550e8400-e29b-41d4-a716-446655440000"
success = delete_notification(notification_uid)

if success:
    print(f"Notification {notification_uid} deleted successfully")
else:
    print(f"Failed to delete notification {notification_uid}")

# Example 2: Delete multiple notifications
notification_uids = [
    "550e8400-e29b-41d4-a716-446655440000",
    "660e8400-e29b-41d4-a716-446655440001"
]

for uid in notification_uids:
    result = delete_notification(uid)
    print(f"Deletion of {uid}: {'Success' if result else 'Failed'}")

Best Practices

  • Always verify that the notification_uid exists before calling this function if you need to distinguish between 'not found' and 'deletion error' scenarios
  • Consider implementing a soft delete mechanism (marking as deleted rather than removing) if you need to maintain audit trails or allow recovery
  • Use this function within a transaction context if you need to ensure atomicity with other database operations
  • Log the notification_uid before deletion if you need to track what was deleted for audit purposes
  • Be aware that DETACH DELETE removes all relationships, so ensure this is the desired behavior for your use case
  • Consider implementing authorization checks before deletion to ensure the caller has permission to delete the notification
  • The function returns True even if no notification was found - implement additional checks if you need to verify the notification existed

Similar Components

AI-powered semantic similarity - components with related functionality:

  • function delete_all_notifications 77.6% similar

    Deletes all notifications or only read notifications for a specific user from a Neo4j graph database.

    From: /tf/active/vicechatdev/CDocs/utils/notifications.py
  • function delete_node 75.8% similar

    Deletes a node from a Neo4j graph database by its unique identifier (UID), with optional cascade deletion of connected nodes and relationships.

    From: /tf/active/vicechatdev/CDocs/db/db_operations.py
  • function mark_notification_read 68.2% similar

    Updates a notification's read status in a Neo4j graph database and sets a timestamp when marked as read.

    From: /tf/active/vicechatdev/CDocs/utils/notifications.py
  • function count_user_notifications 66.5% similar

    Counts the total number of notifications for a specific user in a Neo4j graph database, with an option to filter for only unread notifications.

    From: /tf/active/vicechatdev/CDocs/utils/notifications.py
  • function create_notification 64.6% similar

    Creates an in-app notification for a user in a graph database, linking it to the user and optionally to a related resource.

    From: /tf/active/vicechatdev/CDocs/utils/notifications.py
← Back to Browse