📚 CollectionTriggers.ts Documentation
Index
- Overview
- Purpose
- Key Concepts
- Main Exports & Event Triggers
- Core Helper Functions
- Data Flow & Architecture
- Sequence Diagram: Event/Brand Edit Flow
- API Documentation
- Summary Table of Triggers
- Dependencies
Overview
CollectionTriggers.ts is a Cloud Functions file for Firebase that provides automated workflows on Firestore collections—primarily for events, brands, and users. Its triggers handle notifications, data synchronization, and business logic updates when these documents are created, updated, or deleted.
Purpose
- Automate notifications to users and admins based on changes in Firestore collections.
- Sync and update event/brand/user data (e.g., follower counts, status, IDs).
- Enforce business rules around publishing, review, and deletion processes.
- Maintain backward compatibility for older app versions (some legacy code).
Key Concepts
- Firestore Triggers: Event-driven functions that react to create, update, or delete operations in specific collections/documents.
- Notification Dispatch: Sends messages to different roles (owner, admin, everyone) depending on the type and status of change.
- Business Logic Enforcement: Ensures that data like follower counts and publish status are correctly managed and updated.
Main Exports & Event Triggers
1. eventBrandUser_edited_notification
Trigger:
On update for any document under any collection (/{collectionId}/{docId}).
Purpose:
- Handles logic when an event, brand, or user is edited.
- Notifies the appropriate users (owner, admin, or everyone) depending on the changes.
- Updates follower counts if necessary, especially when a promotion is boosted.
- Manages publishing workflow (published, under review).
Key Operations:
- For events: Syncs
countFollowersandlistFollowersacross boosted events. - For events/brands: Notifies admins or owners based on changes in
publishStatus. - (Legacy) For users: Updates underground membership expiry dates (for old app versions).
2. eventBrand_created_notification
Trigger:
On create for any document under any collection (/{collectionId}/{docId}).
Purpose:
- Handles logic when a new event or brand is created.
- Notifies admins if the new document is under review.
- Updates document IDs for consistency.
3. eventBrand_deleted_notification
Trigger:
On delete for any document under any collection (/{collectionId}/{docId}).
Purpose:
- Notifies admins when an event or brand is deleted.
Core Helper Functions
getMaxLikeBoostedEvents
- Input:
beforeData,afterData(document snapshots) - Purpose: When an event is promoted, ensures that its follower count and list are correctly boosted in sync with the most popular published event at the same place.
- Returns:
{ maxcountFollowers, maxListFollowers }for updating the event document.
isEventOrBrand
- Checks if the collection is
eventsorbrands.
isEvent
- Checks if the collection is only
events.
Data Flow & Architecture
Firestore Trigger Architecture
flowchart TD
subgraph Firestore
A((Collection: events/brands/users))
end
A -- onUpdate --> B[eventBrandUser_edited_notification]
A -- onCreate --> C[eventBrand_created_notification]
A -- onDelete --> D[eventBrand_deleted_notification]
B -- Notifies / Updates --> E[Notify Helper / Update Firestore]
C -- Notifies / Updates --> E
D -- Notifies --> E
- A: Firestore collections where event/brand/user documents exist.
- B/C/D: Trigger functions reacting to changes.
- E: Notification helpers and Firestore updaters.
Sequence Diagram: Event/Brand Edit Flow
sequenceDiagram
participant Firestore
participant TriggerFn as eventBrandUser_edited_notification
participant Notify
participant DB as Firestore DB
Firestore->>TriggerFn: Document Updated (/{collectionId}/{docId})
TriggerFn->>TriggerFn: Parse before/after data, check collection type
alt If collection is event
TriggerFn->>DB: Query for maxLikeBoostedEvents
TriggerFn->>TriggerFn: Compare/update countFollowers, listFollowers
TriggerFn->>DB: Update document if necessary
end
alt If publishStatus changed
TriggerFn->>Notify: Notify owner or admin, depending on new status
end
TriggerFn-->>Firestore: Done
API Documentation
NOTE:
This file contains Firestore triggers, not traditional HTTP API endpoints.
No REST API blocks are generated.
Instead, it documents automated server-side logic.
Summary Table of Triggers
| Trigger Name | Firestore Event | Collection(s) | Action Type | Notification Recipients | Other Effects |
|---|---|---|---|---|---|
| eventBrandUser_edited_notification | onUpdate | events/brands/users | Edit/Update | Owner/Admin | Update followers, statuses |
| eventBrand_created_notification | onCreate | events/brands | Create | Admin | Update IDs, notify admin |
| eventBrand_deleted_notification | onDelete | events/brands | Delete | Admin | Notify admin |
Dependencies
{
"commands": {
"npm": "npm install @google-cloud/firestore",
"yarn": "yarn add @google-cloud/firestore",
"pnpm": "pnpm add @google-cloud/firestore",
"bun": "bun add @google-cloud/firestore"
}
}
Other internal dependencies:
../Helpers/Constants../Helpers/Notify../Helpers/Functions
Detailed Function Reference
eventBrandUser_edited_notification
On document update:
- Events:
- If event has a promotion boost, finds the event with the highest follower count at the same place.
- Updates the current event's follower count and list to match this max if needed.
- Events/Brands:
- If
publishStatuschanges:- Published: Notifies owner if not an editor.
- Under Review: Notifies admins.
- If
- Legacy (Users):
- (Commented out) Handles setting an expiry date for underground membership for compatibility with old app versions.
eventBrand_created_notification
On document create:
- If collection is event or brand:
- Updates the document with new IDs and edit date.
- If
publishStatusis "under review", notifies admins.
eventBrand_deleted_notification
On document delete:
- If collection is event or brand:
- Notifies admins of deletion.
Notable Patterns & Business Rules
-
Role-based Notification:
Owners, admins, and editors each receive different notifications based on the document status and action performed. -
Follower Count Synchronization:
Ensures that promotional boosts are reflected correctly across similar events at the same place. -
Legacy Support:
Contains commented code for users with old app versions (<3.5.0).
Example: Notification Flow for Event Publishing
When an event is updated and its publishStatus changes:
- If event is published (
publishStatus == 1):- Notify the owner only if the owner is not an editor/admin.
- If event is under review (
publishStatus == 0):- Notify the admins.
Conclusion
The CollectionTriggers.ts file is a crucial automation hub for managing events, brands, and users in Firestore. It ensures consistent business logic, real-time notifications, and correct data synchronization for key objects in your application—helping maintain robust workflows and user experiences.
Tip:
Always keep helper modules (Constants, Notify, Functions) up to date, as business rules and notification logic are abstracted there!