mirror of
https://github.com/urosran/cally.git
synced 2025-07-10 15:17:17 +00:00
Added notifciations
This commit is contained in:
@ -1,6 +1,6 @@
|
||||
const {onRequest} = require("firebase-functions/v2/https");
|
||||
const {getAuth} = require("firebase-admin/auth");
|
||||
const {getFirestore} = require("firebase-admin/firestore");
|
||||
const {getFirestore, Timestamp} = require("firebase-admin/firestore");
|
||||
const logger = require("firebase-functions/logger");
|
||||
const functions = require('firebase-functions');
|
||||
const admin = require('firebase-admin');
|
||||
@ -22,7 +22,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, title } = eventData;
|
||||
|
||||
if (!familyId || !creatorId) {
|
||||
console.error('Missing familyId or creatorId in event data');
|
||||
@ -44,7 +44,7 @@ exports.sendNotificationOnEventCreation = functions.firestore
|
||||
|
||||
notificationTimeout = setTimeout(async () => {
|
||||
const eventMessage = eventCount === 1
|
||||
? `An event "${eventData.title}" has been added. Check it out!`
|
||||
? `An event "${title}" has been added. Check it out!`
|
||||
: `${eventCount} new events have been added.`;
|
||||
|
||||
let messages = pushTokens.map(pushToken => {
|
||||
@ -85,6 +85,22 @@ exports.sendNotificationOnEventCreation = functions.firestore
|
||||
}
|
||||
}
|
||||
|
||||
// Save the notification in Firestore for record-keeping
|
||||
const notificationData = {
|
||||
creatorId,
|
||||
familyId,
|
||||
content: eventMessage,
|
||||
eventId: context.params.eventId,
|
||||
timestamp: Timestamp.now(),
|
||||
};
|
||||
|
||||
try {
|
||||
await db.collection("Notifications").add(notificationData);
|
||||
console.log("Notification stored in Firestore:", notificationData);
|
||||
} catch (error) {
|
||||
console.error("Error saving notification to Firestore:", error);
|
||||
}
|
||||
|
||||
// Reset state variables after notifications are sent
|
||||
eventCount = 0;
|
||||
pushTokens = [];
|
||||
|
Reference in New Issue
Block a user