syncAssignmentDeleted()
syncAssignmentDeleted
Function Name
syncAssignmentDeleted
Trigger
This Cloud Function is triggered by the deletion of a document within the Firestore path users/{roarUid}/assignments/{assignmentUid}
. It monitors for document deletions to manage related data clean-up and synchronization tasks effectively.
Operation
Upon detecting the deletion of an assignment document, the function performs several key operations using Firestore transactions:
Retrieval of Deleted Document Data:
- Captures data from the deleted assignment document for use in updating related metrics.
Update of Assignment Metrics:
- Accesses the
administrations
document related to the deleted assignment to update assignment-related statistics such as "assigned", "started", and "completed" counters. - Decrements the respective counters based on the data from the deleted assignment:
- Assigned Counter: Decremented for each organization listed in the assignment's data and for the total.
- Started Counter: Only decremented for tasks that were marked as started.
- Completed Counter: Only decremented for tasks that were marked as completed, considering whether the entire assignment was previously marked as completed.
- Accesses the
Update of User Document:
- Updates the user's document to remove references to the deleted assignment, specifically deleting dates related to the assignment and removing the assignment ID from arrays tracking assignments' statuses.
Detailed Workflow
Document Deletion Event: Captures the event where an assignment document under a user profile is deleted.
Transaction Execution:
- Retrieval of Previous Data: Fetches data from the deleted document to guide updates.
- Metric Adjustments: Utilizes helper functions to decrement status counters in the
administrations/stats
collection based on the data from the deleted document. - User Document Update: Adjusts the user's document to reflect the removal of the assignment, ensuring that any reference to the deleted assignment is cleared from tracking fields.
Error Handling and Security Considerations
Error Handling:
- Implements robust handling of transaction failures and ensures that all operations are retried up to a limit if they fail initially.
- Manages potential inconsistencies that may arise if the deletion event does not provide complete data.
Security:
- Ensures that operations are only triggered by authorized deletions, with checks to prevent unauthorized data manipulation.
- Validates that only existing and accurate data is used to update counters and user profiles, ensuring integrity in data processing.
Deployment and Maintenance
This function is deployed automatically using GitHub actions, whenever changes are merged into the main
branch.