send notifications only if the event has no email address

This commit is contained in:
Milan Paunovic
2024-10-22 12:04:06 +02:00
parent 2be5e7c4e4
commit 1f45f5d21f

View File

@ -18,7 +18,12 @@ exports.sendNotificationOnEventCreation = functions.firestore
.document('Events/{eventId}')
.onCreate(async (snapshot, context) => {
const eventData = snapshot.data();
const {familyId, creatorId} = eventData;
const {familyId, creatorId, email} = eventData;
if (email) {
console.log('Event has an email field. Skipping notification.');
return;
}
if (!familyId || !creatorId) {
console.error('Missing familyId or creatorId in event data');
@ -33,14 +38,12 @@ exports.sendNotificationOnEventCreation = functions.firestore
}
}
// Increment event count for debouncing
eventCount++;
if (notificationTimeout) {
clearTimeout(notificationTimeout); // Reset the timer if events keep coming
clearTimeout(notificationTimeout);
}
// Set a debounce time (e.g., 5 seconds)
notificationTimeout = setTimeout(async () => {
const eventMessage = eventCount === 1
? `An event "${eventData.title}" has been added. Check it out!`
@ -88,7 +91,7 @@ exports.sendNotificationOnEventCreation = functions.firestore
eventCount = 0; // Reset the event count after sending notification
pushTokens = []; // Reset push tokens for the next round
}, 5000); // Debounce time (5 seconds)
}, 5000);
});