147 lines
4.6 KiB
JavaScript
147 lines
4.6 KiB
JavaScript
const functions = require("firebase-functions/v1");
|
|
const admin = require("firebase-admin");
|
|
|
|
admin.initializeApp();
|
|
|
|
// Helper function to send notifications to a list of users
|
|
async function sendNotificationToUsers(userIds, title, body, excludeUserId, data = {}) {
|
|
try {
|
|
const tokens = [];
|
|
|
|
for (const userId of userIds) {
|
|
if (userId === excludeUserId) continue;
|
|
|
|
const userDoc = await admin.firestore().collection("users").doc(userId).get();
|
|
if (userDoc.exists) {
|
|
const userData = userDoc.data();
|
|
if (userData.fcmToken) {
|
|
tokens.push(userData.fcmToken);
|
|
}
|
|
}
|
|
}
|
|
|
|
if (tokens.length > 0) {
|
|
const message = {
|
|
notification: {
|
|
title: title,
|
|
body: body,
|
|
},
|
|
tokens: tokens,
|
|
data: {
|
|
click_action: "FLUTTER_NOTIFICATION_CLICK",
|
|
...data
|
|
},
|
|
};
|
|
|
|
const response = await admin.messaging().sendMulticast(message);
|
|
console.log(`${response.successCount} messages were sent successfully`);
|
|
}
|
|
} catch (error) {
|
|
console.error("Error sending notification:", error);
|
|
}
|
|
}
|
|
|
|
exports.onActivityCreated = functions.firestore
|
|
.document("activities/{activityId}")
|
|
.onCreate(async (snapshot, context) => {
|
|
const activity = snapshot.data();
|
|
const tripId = activity.tripId;
|
|
const createdBy = activity.createdBy || "Unknown";
|
|
|
|
if (!tripId) {
|
|
console.log("No tripId found in activity");
|
|
return;
|
|
}
|
|
|
|
// Fetch trip to get participants
|
|
const tripDoc = await admin.firestore().collection("trips").doc(tripId).get();
|
|
if (!tripDoc.exists) {
|
|
console.log(`Trip ${tripId} not found`);
|
|
return;
|
|
}
|
|
|
|
const trip = tripDoc.data();
|
|
const participants = trip.participants || [];
|
|
if (trip.createdBy && !participants.includes(trip.createdBy)) {
|
|
participants.push(trip.createdBy);
|
|
}
|
|
|
|
// Fetch creator name
|
|
let creatorName = "Quelqu'un";
|
|
if (createdBy !== "Unknown") {
|
|
const userDoc = await admin.firestore().collection("users").doc(createdBy).get();
|
|
if (userDoc.exists) {
|
|
creatorName = userDoc.data().prenom || "Quelqu'un";
|
|
}
|
|
}
|
|
|
|
await sendNotificationToUsers(
|
|
participants,
|
|
"Nouvelle activité !",
|
|
`${creatorName} a ajouté une nouvelle activité : ${activity.name || activity.title}`,
|
|
createdBy,
|
|
{ tripId: tripId }
|
|
);
|
|
});
|
|
|
|
exports.onMessageCreated = functions.firestore
|
|
.document("groups/{groupId}/messages/{messageId}")
|
|
.onCreate(async (snapshot, context) => {
|
|
const message = snapshot.data();
|
|
const groupId = context.params.groupId;
|
|
const senderId = message.senderId;
|
|
|
|
// Fetch group to get members
|
|
const groupDoc = await admin.firestore().collection("groups").doc(groupId).get();
|
|
if (!groupDoc.exists) {
|
|
console.log(`Group ${groupId} not found`);
|
|
return;
|
|
}
|
|
|
|
const group = groupDoc.data();
|
|
const memberIds = group.memberIds || [];
|
|
|
|
let senderName = message.senderName || "Quelqu'un";
|
|
|
|
await sendNotificationToUsers(
|
|
memberIds,
|
|
"Nouveau message",
|
|
`${senderName} : ${message.text}`,
|
|
senderId,
|
|
{ groupId: groupId }
|
|
);
|
|
});
|
|
|
|
exports.onExpenseCreated = functions.firestore
|
|
.document("expenses/{expenseId}")
|
|
.onCreate(async (snapshot, context) => {
|
|
const expense = snapshot.data();
|
|
const groupId = expense.groupId;
|
|
const paidBy = expense.paidById || expense.paidBy;
|
|
|
|
if (!groupId) {
|
|
console.log("No groupId found in expense");
|
|
return;
|
|
}
|
|
|
|
// Fetch group to get members
|
|
const groupDoc = await admin.firestore().collection("groups").doc(groupId).get();
|
|
if (!groupDoc.exists) {
|
|
console.log(`Group ${groupId} not found`);
|
|
return;
|
|
}
|
|
|
|
const group = groupDoc.data();
|
|
const memberIds = group.memberIds || [];
|
|
|
|
let payerName = expense.paidByName || "Quelqu'un";
|
|
|
|
await sendNotificationToUsers(
|
|
memberIds,
|
|
"Nouvelle dépense",
|
|
`${payerName} a ajouté une dépense : ${expense.amount} ${expense.currency || '€'}`,
|
|
paidBy,
|
|
{ groupId: groupId }
|
|
);
|
|
});
|