Fixes and tickets

This commit is contained in:
Thomas Demirdjian
2025-10-28 15:43:18 +01:00
parent 2599a25d77
commit d1be3c19a2
31 changed files with 6663 additions and 795 deletions
+60 -23
View File
@@ -5,11 +5,18 @@ const {
const admin = require("firebase-admin");
const { refList, ALERT_TYPE } = require("../index");
const { Expo } = require("expo-server-sdk");
const { Resend } = require("resend");
const { basicTemplate } = require("../helpers/email");
const { RESEND_API_KEY } = require("../config/keys");
const resendInstance = new Resend(RESEND_API_KEY);
// Initialisation de Expo SDK
let expo = new Expo();
const db = admin.firestore();
const EMAIL_FROM = "MusicLand <musicland@minuit.app>";
exports.sendNotificationWhenDocIsCreated = onDocumentCreated(
{ region: "europe-west1", document: "notifications/{notificationId}" },
async (event) => {
@@ -26,14 +33,18 @@ exports.sendNotificationWhenDocIsCreated = onDocumentCreated(
throw new Error("Receiver and message are required");
}
const userSnap = await db.collection(receiverCollection).doc(receiver).get();
const { pushToken = null, pushTokens = [] } = userSnap?.data() || {};
const {
pushToken = null,
pushTokens = [],
email = "",
emailNotifications = false,
} = (await db.collection(receiverCollection).doc(receiver).get()).data();
const tokensSet = new Set(
[]
.concat(Array.isArray(pushTokens) ? pushTokens : [])
.concat(pushToken ? [pushToken] : [])
.filter(Boolean)
.filter(Boolean),
);
const tokens = Array.from(tokensSet);
@@ -50,11 +61,31 @@ exports.sendNotificationWhenDocIsCreated = onDocumentCreated(
message: message,
data: notifData || {},
});
if (emailNotifications && !!email) {
try {
if (!email) {
throw new Error("userMail is required");
}
if (!email?.subject || !email?.html) {
throw new Error("Email subject and html are required");
}
await resendInstance.emails.send({
from: EMAIL_FROM,
to: [email],
subject: title,
html: basicTemplate({ title, content: message }),
});
} catch (e) {
console.log("Error sending email:", e);
}
} else {
console.log(`Email disabled for user ${receiver} and type ${type}`);
}
} catch (e) {
console.log(e);
return e;
}
}
},
);
// Fonction pour envoyer la notification via Expo SDK
@@ -144,7 +175,11 @@ async function sendExpoNotification({
}
}
async function removeInvalidTokens({ tokens = [], receiverId, receiverCollection }) {
async function removeInvalidTokens({
tokens = [],
receiverId,
receiverCollection,
}) {
try {
if (!receiverId || !tokens.length) {
return;
@@ -170,7 +205,7 @@ async function removeInvalidTokens({ tokens = [], receiverId, receiverCollection
await docRef.set(updates, { merge: true });
console.log(
"Pruned invalid push tokens",
JSON.stringify({ receiverId, tokens: uniqueTokens }, null, 2)
JSON.stringify({ receiverId, tokens: uniqueTokens }, null, 2),
);
} catch (error) {
console.log("Failed to prune invalid push tokens:", error);
@@ -204,7 +239,7 @@ const sendNotification = async ({
const { id } = await refList.notifications.add(payload);
console.log(
"[sendNotification] Notification created",
JSON.stringify({ id, receiver, receiverCollection }, null, 2)
JSON.stringify({ id, receiver, receiverCollection }, null, 2),
);
return id;
@@ -224,7 +259,7 @@ exports.createProjectCommentNotification = onDocumentCreated(
try {
console.log(
"[createProjectCommentNotification] Trigger received",
JSON.stringify(event.params || {}, null, 2)
JSON.stringify(event.params || {}, null, 2),
);
const { data: snap } = event;
const { projectId, commentId } = event.params || {};
@@ -233,21 +268,21 @@ exports.createProjectCommentNotification = onDocumentCreated(
if (!projectId || !comment) {
console.log(
"[createProjectCommentNotification] Missing project/comment data",
{ hasProjectId: !!projectId, hasComment: !!comment }
{ hasProjectId: !!projectId, hasComment: !!comment },
);
return null;
}
console.log(
"[createProjectCommentNotification] Comment payload",
JSON.stringify(comment, null, 2)
JSON.stringify(comment, null, 2),
);
const projectSnap = await refList.projects.doc(projectId).get();
if (!projectSnap.exists) {
console.log(
"[createProjectCommentNotification] Project not found",
projectId
projectId,
);
return null;
}
@@ -258,7 +293,7 @@ exports.createProjectCommentNotification = onDocumentCreated(
if (!receiver || receiver === comment.userId) {
console.log(
"[createProjectCommentNotification] Invalid receiver",
JSON.stringify({ receiver, commentUserId: comment.userId })
JSON.stringify({ receiver, commentUserId: comment.userId }),
);
return null;
}
@@ -283,8 +318,8 @@ exports.createProjectCommentNotification = onDocumentCreated(
projectId,
},
null,
2
)
2,
),
);
await sendNotification({
@@ -308,7 +343,7 @@ exports.createProjectCommentNotification = onDocumentCreated(
});
console.log(
"[createProjectCommentNotification] Notification creation complete"
"[createProjectCommentNotification] Notification creation complete",
);
return null;
@@ -316,7 +351,7 @@ exports.createProjectCommentNotification = onDocumentCreated(
console.log("createProjectCommentNotification error:", error);
return error;
}
}
},
);
exports.createProjectLikeNotification = onDocumentWritten(
@@ -356,10 +391,10 @@ exports.createProjectLikeNotification = onDocumentWritten(
const beforePlaybackSet = new Set(beforePlaybackLikes);
const newSongLikers = afterSongLikes.filter(
(uid) => uid && !beforeSongSet.has(uid)
(uid) => uid && !beforeSongSet.has(uid),
);
const newPlaybackLikers = afterPlaybackLikes.filter(
(uid) => uid && !beforePlaybackSet.has(uid)
(uid) => uid && !beforePlaybackSet.has(uid),
);
const newLikers = [];
@@ -413,7 +448,7 @@ exports.createProjectLikeNotification = onDocumentWritten(
});
return null;
})
}),
);
return null;
@@ -421,7 +456,7 @@ exports.createProjectLikeNotification = onDocumentWritten(
console.log("[createProjectLikeNotification] error:", error);
return error;
}
}
},
);
exports.createNewFollowerNotification = onDocumentWritten(
@@ -451,7 +486,9 @@ exports.createNewFollowerNotification = onDocumentWritten(
}
const previousSet = new Set(beforeFollowers);
const newFollowers = afterFollowers.filter((uid) => !previousSet.has(uid));
const newFollowers = afterFollowers.filter(
(uid) => !previousSet.has(uid),
);
if (!newFollowers.length) {
return null;
@@ -487,7 +524,7 @@ exports.createNewFollowerNotification = onDocumentWritten(
});
return null;
})
}),
);
return null;
@@ -495,5 +532,5 @@ exports.createNewFollowerNotification = onDocumentWritten(
console.log("[createNewFollowerNotification] error:", error);
return error;
}
}
},
);