syncAssignmentCreated()
syncAssignmentCreated
Function Name
syncAssignmentCreated
Trigger
This Cloud Function is triggered by the creation of a document within the Firestore path users/{roarUid}/assignments/{assignmentUid}
. It specifically listens for new assignments being added under individual user profiles.
Operation
Upon detecting the creation of an assignment document, the function executes several operations:
Data Retrieval: It retrieves the newly created assignment's data from the snapshot provided by the event trigger.
Database Transactions:
- User Document Update: Updates the user document to include the new assignment in the list of assignments. This is achieved by:
- Adding the assignment UID to an array field for tracking assigned assignments.
- Storing or updating the date when the assignment was assigned.
- Assignment Statistics Update: Manages statistics related to assignment completion across different organizational contexts within the
administrations
collection. It updates or increments the "assigned" status for the assignment across various organizational levels and the total count.
- User Document Update: Updates the user document to include the new assignment in the list of assignments. This is achieved by:
Logging: Outputs debug statements to trace the function's execution and provide insights into the processing stages and any issues encountered.
Detailed Workflow
Assignment Document Creation:
- Captures the event when a new assignment document is created.
- Extracts necessary details such as
roarUid
,assignmentUid
, and assignment data from the event context and document snapshot.
First Transaction:
- Constructs references to the user's document.
- Updates the user's document to reflect the newly assigned assignment, ensuring that the assignment's presence is recorded and its assigned date is noted.
Second Transaction:
- Accesses the completion stats document related to the assignment.
- Updates the "assigned" counts for the relevant organizational units and the overall total, ensuring that the assignment creation is fully integrated into organizational tracking and statistics.
Error Handling and Security Considerations
Error Handling:
- Implements robust transaction handling to manage potential failures in database operations.
- Uses retries and error logging to ensure that each step of the data synchronization is attempted reliably, with detailed logs for diagnosing issues.
Security:
- Ensures that the function operates under authenticated conditions, where only valid and authorized changes trigger data updates.
- Validates data integrity and consistency before applying updates to prevent corrupt or unintended changes from propagating.
Deployment and Maintenance
This function is deployed automatically using GitHub actions, whenever changes are merged into the main
branch.