notifications
This commit is contained in:
@@ -0,0 +1,213 @@
|
||||
const { onDocumentCreated } = require("firebase-functions/v2/firestore");
|
||||
const admin = require("firebase-admin");
|
||||
const { refList, ALERT_TYPE } = require("../index");
|
||||
const { Expo } = require("expo-server-sdk");
|
||||
|
||||
// Initialisation de Expo SDK
|
||||
let expo = new Expo();
|
||||
const db = admin.firestore();
|
||||
|
||||
exports.sendNotificationWhenDocIsCreated = onDocumentCreated(
|
||||
{ region: "europe-west1", document: "notifications/{notificationId}" },
|
||||
async (event) => {
|
||||
try {
|
||||
const {
|
||||
receiver = null,
|
||||
title = "MusicLand",
|
||||
receiverCollection = "users",
|
||||
message = "",
|
||||
data: notifData = {},
|
||||
} = event.data.data();
|
||||
|
||||
const { pushToken = null } =
|
||||
(await db.collection(receiverCollection).doc(receiver).get())?.data() ||
|
||||
{};
|
||||
|
||||
if (!receiver || !message) {
|
||||
throw new Error("Receiver and message are required");
|
||||
}
|
||||
|
||||
if (pushToken) {
|
||||
await sendExpoNotification(pushToken, {
|
||||
title: title || "MusicLand",
|
||||
message: message,
|
||||
data: notifData || {},
|
||||
});
|
||||
} else {
|
||||
console.warn("User push token not found");
|
||||
}
|
||||
} catch (e) {
|
||||
console.log(e);
|
||||
return e;
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
// Fonction pour envoyer la notification via Expo SDK
|
||||
async function sendExpoNotification(
|
||||
pushToken,
|
||||
{ title = "", message = "", data = {} }
|
||||
) {
|
||||
try {
|
||||
if (!Expo.isExpoPushToken(pushToken)) {
|
||||
throw new Error(`Invalid push token: ${pushToken}`);
|
||||
} else {
|
||||
console.log("Valid push token:", pushToken);
|
||||
}
|
||||
|
||||
const receipts = await expo.sendPushNotificationsAsync([
|
||||
{
|
||||
to: pushToken,
|
||||
sound: "default",
|
||||
title: title,
|
||||
body: message,
|
||||
data: data || {},
|
||||
priority: "high",
|
||||
badge: 1,
|
||||
channelId: "default",
|
||||
},
|
||||
]);
|
||||
console.log("Sent push notifications:", receipts);
|
||||
|
||||
return { sent: true };
|
||||
} catch (e) {
|
||||
console.log("Error sending notification:", e);
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
// Fonction pour ajouter une notification à la base de données
|
||||
const sendNotification = async ({
|
||||
sender = "SYSTEM",
|
||||
receiver = null,
|
||||
receiverCollection = "users",
|
||||
title = "",
|
||||
message = null,
|
||||
data = {},
|
||||
}) => {
|
||||
try {
|
||||
if (!receiver || !message) {
|
||||
throw new Error("Receiver and message are required");
|
||||
}
|
||||
const { id } = await refList.notifications.add({
|
||||
sender,
|
||||
receiver,
|
||||
receiverCollection,
|
||||
title,
|
||||
message,
|
||||
time: new Date(),
|
||||
data,
|
||||
});
|
||||
console.log(`Notification ${id} created`);
|
||||
|
||||
return id;
|
||||
} catch (e) {
|
||||
console.log(e);
|
||||
}
|
||||
};
|
||||
|
||||
exports.sendNotification = sendNotification;
|
||||
|
||||
exports.createProjectCommentNotification = onDocumentCreated(
|
||||
{
|
||||
region: "europe-west1",
|
||||
document: "projects/{projectId}/comments/{commentId}",
|
||||
},
|
||||
async (event) => {
|
||||
try {
|
||||
console.log(
|
||||
"[createProjectCommentNotification] Trigger received",
|
||||
JSON.stringify(event.params || {}, null, 2)
|
||||
);
|
||||
const { data: snap } = event;
|
||||
const { projectId, commentId } = event.params || {};
|
||||
const comment = snap?.data();
|
||||
|
||||
if (!projectId || !comment) {
|
||||
console.log(
|
||||
"[createProjectCommentNotification] Missing project/comment data",
|
||||
{ hasProjectId: !!projectId, hasComment: !!comment }
|
||||
);
|
||||
return null;
|
||||
}
|
||||
|
||||
console.log(
|
||||
"[createProjectCommentNotification] Comment payload",
|
||||
JSON.stringify(comment, null, 2)
|
||||
);
|
||||
|
||||
const projectSnap = await refList.projects.doc(projectId).get();
|
||||
if (!projectSnap.exists) {
|
||||
console.log(
|
||||
"[createProjectCommentNotification] Project not found",
|
||||
projectId
|
||||
);
|
||||
return null;
|
||||
}
|
||||
|
||||
const project = projectSnap.data() || {};
|
||||
const receiver = project.userId || null;
|
||||
|
||||
if (!receiver || receiver === comment.userId) {
|
||||
console.log(
|
||||
"[createProjectCommentNotification] Invalid receiver",
|
||||
JSON.stringify({ receiver, commentUserId: comment.userId })
|
||||
);
|
||||
return null;
|
||||
}
|
||||
|
||||
const commenterName =
|
||||
typeof comment?.userName === "string" && comment.userName.trim()
|
||||
? comment.userName.trim()
|
||||
: "Un utilisateur";
|
||||
const projectTitle =
|
||||
typeof project.title === "string" && project.title.trim()
|
||||
? project.title.trim()
|
||||
: "ton projet";
|
||||
const message = `${commenterName} a commenté ton projet "${projectTitle}"`;
|
||||
|
||||
console.log(
|
||||
"[createProjectCommentNotification] Creating notification",
|
||||
JSON.stringify(
|
||||
{
|
||||
receiver,
|
||||
message,
|
||||
commentId,
|
||||
projectId,
|
||||
},
|
||||
null,
|
||||
2
|
||||
)
|
||||
);
|
||||
|
||||
await sendNotification({
|
||||
sender: comment.userId || "SYSTEM",
|
||||
receiver,
|
||||
receiverCollection: "users",
|
||||
title: "Nouveau commentaire",
|
||||
message,
|
||||
data: {
|
||||
type: ALERT_TYPE?.NEW_COMMENT,
|
||||
projectId,
|
||||
commentId,
|
||||
commenterId: comment.userId || null,
|
||||
commenterName: commenterName,
|
||||
commenterProfilePicture: comment?.profilePicture || "",
|
||||
text:
|
||||
typeof comment?.text === "string" && comment.text.trim()
|
||||
? comment.text.trim()
|
||||
: "",
|
||||
},
|
||||
});
|
||||
|
||||
console.log(
|
||||
"[createProjectCommentNotification] Notification creation complete"
|
||||
);
|
||||
|
||||
return null;
|
||||
} catch (error) {
|
||||
console.log("createProjectCommentNotification error:", error);
|
||||
return error;
|
||||
}
|
||||
}
|
||||
);
|
||||
Reference in New Issue
Block a user