syncAssignmentUpdated()
syncAssignmentUpdated
Function Name
syncAssignmentUpdated
Trigger
This Cloud Function is triggered by an update event on any document within the path users/{roarUid}/assignments/{assignmentUid}
. It listens for changes to assignment documents and executes data synchronization based on those changes.
Operation
The function performs the following operations:
Data Retrieval:
- Extracts previous (
prevData
) and current (currData
) data from the assignment document to determine changes. - Retrieves document paths and organizational data from the assignment.
- Extracts previous (
Database Transactions:
- Document Reference Setup: Establishes references to necessary documents within Firestore, including user documents and assignment statistics documents in the
administrations
collection. - Statistics Update: Analyzes changes between previous and current data to adjust counters in organizational and assignment-level statistics.
- Status Updates: Updates the user's document to reflect changes in the assignment's status (e.g., started, completed) based on the data comparison.
- Document Reference Setup: Establishes references to necessary documents within Firestore, including user documents and assignment statistics documents in the
Detailed Workflow
Initial Checks:
- Verifies that the document snapshot is valid and contains data to process.
Transaction Execution:
- Organizational Changes: Identifies added or removed organizations and adjusts assignment counters accordingly. This includes handling counters for assigned, started, and completed statuses.
- Task Status Changes: Checks each task within the assignment for changes in status (e.g., from not started to started or from started to completed) and updates counters and user document fields accordingly.
Error Handling and Security Considerations
Error Handling:
- Manages errors during database transactions, ensuring that any issues are caught and handled appropriately. Logs errors and attempts retries up to a specified limit.
- Provides robust handling of incomplete data or unexpected document structures to prevent erroneous data manipulation.
Security:
- Ensures that only authorized changes trigger the function, using Firestore security rules to restrict access based on user roles or document ownership.
- Validates changes to sensitive data fields to ensure integrity and prevent unauthorized data modification.
Deployment and Maintenance
This function is deployed automatically using GitHub actions, whenever changes are merged into the main
branch.