continue flow integartion
This commit is contained in:
+68
-17
@@ -365,13 +365,15 @@ exports.sunoCallback = onRequest(
|
||||
const overallTaskId = callbackData?.taskId || callbackData?.task_id || null;
|
||||
|
||||
// Déterminer les éléments piste(s) à traiter
|
||||
const items = Array.isArray(callbackData?.response?.data)
|
||||
? callbackData.response.data
|
||||
: Array.isArray(callbackData?.sunoData)
|
||||
? callbackData.sunoData
|
||||
: id || audioUrl || imageUrl || title || tags || duration
|
||||
? [callbackData]
|
||||
: [];
|
||||
const items = Array.isArray(callbackData?.data)
|
||||
? callbackData.data
|
||||
: Array.isArray(callbackData?.response?.data)
|
||||
? callbackData.response.data
|
||||
: Array.isArray(callbackData?.sunoData)
|
||||
? callbackData.sunoData
|
||||
: id || audioUrl || imageUrl || title || tags || duration
|
||||
? [callbackData]
|
||||
: [];
|
||||
|
||||
if (!items.length) {
|
||||
// Aucun contenu piste à mettre à jour: acquitter le callback sans erreur
|
||||
@@ -383,10 +385,28 @@ exports.sunoCallback = onRequest(
|
||||
});
|
||||
}
|
||||
|
||||
// Retrouver l'association taskId -> projectId via le champ sunoTaskId du projet
|
||||
let mappedProjectId = null;
|
||||
try {
|
||||
if (overallTaskId) {
|
||||
const projSnap = await db
|
||||
.collection("projects")
|
||||
.where("sunoTaskId", "==", overallTaskId)
|
||||
.limit(1)
|
||||
.get();
|
||||
if (!projSnap.empty) {
|
||||
mappedProjectId = projSnap.docs[0].id;
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
// ignore mapping error, we'll proceed without projectId
|
||||
}
|
||||
|
||||
const updates = [];
|
||||
|
||||
for (const item of items) {
|
||||
const trackId = item.id || item.musicId || item.audioId || item.audio_id;
|
||||
|
||||
if (!trackId) {
|
||||
continue;
|
||||
}
|
||||
@@ -394,10 +414,6 @@ exports.sunoCallback = onRequest(
|
||||
// Chercher le document correspondant dans Firestore
|
||||
const musicRef = db.collection("music").doc(trackId);
|
||||
const musicDoc = await musicRef.get();
|
||||
if (!musicDoc.exists) {
|
||||
// Si le doc n'existe pas, ignorer silencieusement cet item
|
||||
continue;
|
||||
}
|
||||
|
||||
const updateData = {
|
||||
status: item.status || overallStatus,
|
||||
@@ -421,18 +437,53 @@ exports.sunoCallback = onRequest(
|
||||
if (tagsText) updateData.style = tagsText;
|
||||
if (durationVal) updateData.duration = durationVal;
|
||||
if (errMsg) updateData.errorMessage = errMsg;
|
||||
if (overallTaskId) updateData.taskId = overallTaskId;
|
||||
if (mappedProjectId) updateData.projectId = mappedProjectId;
|
||||
|
||||
updates.push(
|
||||
musicRef
|
||||
.update(updateData)
|
||||
.then(() => ({ id: trackId, ok: true }))
|
||||
.catch(() => ({ id: trackId, ok: false })),
|
||||
);
|
||||
if (!musicDoc.exists) {
|
||||
updates.push(
|
||||
musicRef
|
||||
.set(
|
||||
{
|
||||
...updateData,
|
||||
createdAt: admin.firestore.FieldValue.serverTimestamp(),
|
||||
},
|
||||
{ merge: true },
|
||||
)
|
||||
.then(() => ({ id: trackId, ok: true }))
|
||||
.catch(() => ({ id: trackId, ok: false })),
|
||||
);
|
||||
} else {
|
||||
updates.push(
|
||||
musicRef
|
||||
.update(updateData)
|
||||
.then(() => ({ id: trackId, ok: true }))
|
||||
.catch(() => ({ id: trackId, ok: false })),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
const results = await Promise.all(updates);
|
||||
const updated = results.filter((r) => r.ok).map((r) => r.id);
|
||||
|
||||
// Mettre à jour le statut du projet si on connaît le projectId
|
||||
if (
|
||||
mappedProjectId &&
|
||||
(overallStatus === 200 ||
|
||||
overallStatus === "SUCCESS" ||
|
||||
callbackData?.callbackType === "complete")
|
||||
) {
|
||||
try {
|
||||
await db.collection("projects").doc(mappedProjectId).set(
|
||||
{
|
||||
musicStatus: "GENERATED",
|
||||
updatedAt: admin.firestore.FieldValue.serverTimestamp(),
|
||||
},
|
||||
{ merge: true },
|
||||
);
|
||||
} catch (_) {}
|
||||
}
|
||||
|
||||
return res.status(200).json({
|
||||
success: true,
|
||||
message: "Callback traité avec succès",
|
||||
|
||||
Reference in New Issue
Block a user