functions
This commit is contained in:
+86
-28
@@ -1,21 +1,17 @@
|
||||
const { onDocumentCreated } = require("firebase-functions/v2/firestore");
|
||||
const admin = require("firebase-admin");
|
||||
const logger = require("firebase-functions/logger");
|
||||
const {
|
||||
generateImageV2,
|
||||
} = require("../helpers/gemini");
|
||||
const { generateImageV2 } = require("../helpers/gemini");
|
||||
const { generatePicturePrompt } = require("../helpers/prompts");
|
||||
const path = require("path");
|
||||
const fs = require("fs");
|
||||
const axios = require("axios");
|
||||
const sharp = require("sharp");
|
||||
const { ALERT_TYPE, refList } = require("../index");
|
||||
const { sendNotification } = require("./notifications");
|
||||
|
||||
const db = admin.firestore();
|
||||
const bucket = admin.storage().bucket();
|
||||
const LOGO_PATH = path.resolve(
|
||||
__dirname,
|
||||
"../assets/musicLandProduction.png",
|
||||
);
|
||||
const LOGO_PATH = path.resolve(__dirname, "../assets/musicLandProduction.png");
|
||||
|
||||
async function buildCoverWithLogo(backgroundUrl, targetPath) {
|
||||
logger.info("🖼️ [Cover] Adding logo to generated background");
|
||||
@@ -124,22 +120,19 @@ async function performCoverGeneration(project) {
|
||||
|
||||
const [firstOption] = options;
|
||||
|
||||
await db
|
||||
.collection("projects")
|
||||
.doc(project.id)
|
||||
.set(
|
||||
{
|
||||
cover: {
|
||||
generatedBackground: firstOption?.generatedUrl || null,
|
||||
result: firstOption?.finalUrl || null,
|
||||
selectedOptionId: firstOption?.id || null,
|
||||
options,
|
||||
},
|
||||
coverStatus: "GENERATED",
|
||||
updatedAt: admin.firestore.FieldValue.serverTimestamp(),
|
||||
await refList.projects.doc(project.id).set(
|
||||
{
|
||||
cover: {
|
||||
generatedBackground: firstOption?.generatedUrl || null,
|
||||
result: firstOption?.finalUrl || null,
|
||||
selectedOptionId: firstOption?.id || null,
|
||||
options,
|
||||
},
|
||||
{ merge: true },
|
||||
);
|
||||
coverStatus: "GENERATED",
|
||||
updatedAt: admin.firestore.FieldValue.serverTimestamp(),
|
||||
},
|
||||
{ merge: true },
|
||||
);
|
||||
|
||||
logger.info("✅ [Cover] Saved", {
|
||||
projectId: project.id,
|
||||
@@ -162,6 +155,7 @@ exports.onTaskCreateGenerateCover = onDocumentCreated(
|
||||
const type = data?.type || "";
|
||||
const projectId = data?.projectId || null;
|
||||
let coverUrl = null;
|
||||
let project = null;
|
||||
try {
|
||||
if (!projectId) {
|
||||
throw new Error("projectId manquant dans la task");
|
||||
@@ -189,13 +183,12 @@ exports.onTaskCreateGenerateCover = onDocumentCreated(
|
||||
return;
|
||||
}
|
||||
|
||||
await db.collection("projects").doc(projectId).update({
|
||||
await refList.projects.doc(projectId).update({
|
||||
coverStatus: "GENERATING",
|
||||
updatedAt: admin.firestore.FieldValue.serverTimestamp(),
|
||||
});
|
||||
|
||||
const project =
|
||||
(await db.collection("projects").doc(projectId).get())?.data() || null;
|
||||
project = (await refList.projects.doc(projectId).get())?.data() || null;
|
||||
if (!project) {
|
||||
throw new Error("Projet non trouvé");
|
||||
}
|
||||
@@ -217,7 +210,7 @@ exports.onTaskCreateGenerateCover = onDocumentCreated(
|
||||
projectId,
|
||||
existingOptionsCount,
|
||||
});
|
||||
await db.collection("projects").doc(projectId).update({
|
||||
await refList.projects.doc(projectId).update({
|
||||
coverStatus: "GENERATED",
|
||||
updatedAt: admin.firestore.FieldValue.serverTimestamp(),
|
||||
});
|
||||
@@ -244,6 +237,39 @@ exports.onTaskCreateGenerateCover = onDocumentCreated(
|
||||
taskId: event?.params?.taskId,
|
||||
projectId,
|
||||
});
|
||||
|
||||
if (
|
||||
project?.userId &&
|
||||
typeof project.userId === "string" &&
|
||||
project.userId.trim()
|
||||
) {
|
||||
const receiverId = project.userId.trim();
|
||||
const projectTitle =
|
||||
typeof project?.title === "string" && project.title.trim()
|
||||
? project.title.trim()
|
||||
: "ton projet";
|
||||
const message = `Ta nouvelle pochette pour "${projectTitle}" est prête.`;
|
||||
try {
|
||||
await sendNotification({
|
||||
sender: "SYSTEM",
|
||||
receiver: receiverId,
|
||||
receiverCollection: "users",
|
||||
title: "Pochette générée",
|
||||
message,
|
||||
data: {
|
||||
type: ALERT_TYPE?.COVER_GENERATION_SUCCESS,
|
||||
projectId,
|
||||
projectTitle,
|
||||
coverUrl,
|
||||
},
|
||||
});
|
||||
} catch (notifError) {
|
||||
logger.error("❌ [Cover] Failed to send success notification", {
|
||||
projectId,
|
||||
error: notifError?.message || String(notifError),
|
||||
});
|
||||
}
|
||||
}
|
||||
return;
|
||||
} catch (error) {
|
||||
logger.error("❌ [Task] Cover generation error", {
|
||||
@@ -256,9 +282,41 @@ exports.onTaskCreateGenerateCover = onDocumentCreated(
|
||||
{ status: "ERROR", error: error?.message || "Erreur" },
|
||||
{ merge: true },
|
||||
);
|
||||
await db.collection("projects").doc(projectId).update({
|
||||
await refList.projects.doc(projectId).update({
|
||||
coverStatus: "ERROR",
|
||||
});
|
||||
if (
|
||||
project?.userId &&
|
||||
typeof project.userId === "string" &&
|
||||
project.userId.trim()
|
||||
) {
|
||||
const receiverId = project.userId.trim();
|
||||
const projectTitle =
|
||||
typeof project?.title === "string" && project.title.trim()
|
||||
? project.title.trim()
|
||||
: "ton projet";
|
||||
const message = `La génération de la pochette pour "${projectTitle}" a échoué.`;
|
||||
try {
|
||||
await sendNotification({
|
||||
sender: "SYSTEM",
|
||||
receiver: receiverId,
|
||||
receiverCollection: "users",
|
||||
title: "Pochette indisponible",
|
||||
message,
|
||||
data: {
|
||||
type: ALERT_TYPE?.COVER_GENERATION_FAILED,
|
||||
projectId,
|
||||
projectTitle,
|
||||
error: error?.message || String(error),
|
||||
},
|
||||
});
|
||||
} catch (notifError) {
|
||||
logger.error("❌ [Cover] Failed to send error notification", {
|
||||
projectId,
|
||||
error: notifError?.message || String(notifError),
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user