fix list and improve cloud functions

This commit is contained in:
Thomas Demirdjian
2025-10-01 15:29:24 +02:00
parent 2908afc259
commit 0bc186413b
18 changed files with 382 additions and 299 deletions
+21
View File
@@ -1,5 +1,8 @@
const admin = require("firebase-admin");
const { onDocumentWritten } = require("firebase-functions/firestore");
const { refList } = require("../index");
const { getSunoTimestamps } = require("./lyrics");
const { deleteFolder } = require("../helpers/firebase");
exports.onProjectUpdate = onDocumentWritten(
"projects/{projectId}",
@@ -13,6 +16,24 @@ exports.onProjectUpdate = onDocumentWritten(
if (!previousData?.songUrl && currentData?.songUrl) {
await getSunoTimestamps(projectId);
}
if (!currentData) {
const { userId } = previousData || {};
//delete folder
await deleteFolder(`users/${userId}/projects/${projectId}/`);
//delete tasks
const snapshot = await refList.tasks
.where("projectId", "==", projectId)
.get();
if (snapshot.empty) return null;
const batch = admin.firestore().batch();
snapshot.forEach((doc) => {
batch.delete(doc.ref);
});
await batch.commit();
}
} catch (e) {
console.log(e);
}