Event filtering

This commit is contained in:
Milan Paunovic
2024-10-30 02:41:00 +01:00
parent 7f79a1c819
commit 01338e7c70
2 changed files with 26 additions and 18 deletions

View File

@ -18,7 +18,7 @@ exports.sendNotificationOnEventCreation = functions.firestore
.document('Events/{eventId}')
.onCreate(async (snapshot, context) => {
const eventData = snapshot.data();
const {familyId, creatorId, email} = eventData;
const { familyId, creatorId, email } = eventData;
if (email) {
console.log('Event has an email field. Skipping notification.');
@ -30,12 +30,11 @@ exports.sendNotificationOnEventCreation = functions.firestore
return;
}
let pushTokens = await getPushTokensForFamilyExcludingCreator(familyId, creatorId);
if (!pushTokens.length) {
pushTokens = await getPushTokensForFamilyExcludingCreator(familyId, creatorId);
if (!pushTokens.length) {
console.log('No push tokens available for the event.');
return;
}
console.log('No push tokens available for the event.');
return;
}
eventCount++;
@ -49,21 +48,20 @@ exports.sendNotificationOnEventCreation = functions.firestore
? `An event "${eventData.title}" has been added. Check it out!`
: `${eventCount} new events have been added.`;
let messages = [];
for (let pushToken of pushTokens) {
let messages = pushTokens.map(pushToken => {
if (!Expo.isExpoPushToken(pushToken)) {
console.error(`Push token ${pushToken} is not a valid Expo push token`);
continue;
return null;
}
messages.push({
return {
to: pushToken,
sound: 'default',
title: 'New Events Added!',
body: eventMessage,
data: {eventId: context.params.eventId},
});
}
data: { eventId: context.params.eventId },
};
}).filter(Boolean);
let chunks = expo.chunkPushNotifications(messages);
let tickets = [];
@ -78,7 +76,7 @@ exports.sendNotificationOnEventCreation = functions.firestore
console.log('Notification successfully sent:', ticket.id);
} else if (ticket.status === 'error') {
console.error(`Notification error: ${ticket.message}`);
if (ticket.details && ticket.details.error === 'DeviceNotRegistered') {
if (ticket.details?.error === 'DeviceNotRegistered') {
await removeInvalidPushToken(ticket.to);
}
}
@ -88,8 +86,8 @@ exports.sendNotificationOnEventCreation = functions.firestore
}
}
eventCount = 0; // Reset the event count after sending notification
pushTokens = []; // Reset push tokens for the next round
eventCount = 0;
pushTokens = [];
}, 5000);
});