fix(event): dates no longer break when editing event

This commit is contained in:
ElPoyo
2026-06-15 15:28:52 +02:00
parent f32fe3e4e8
commit 3c49d6ed21
14 changed files with 105 additions and 29 deletions
+73
View File
@@ -0,0 +1,73 @@
const admin = require("firebase-admin");
const serviceAccount = require("./serviceAccountKey.json");
// Initialiser Firebase Admin
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
});
const db = admin.firestore();
async function migrateEventDates() {
console.log("🔧 Migration: Correction du fuseau horaire des événements");
console.log("================================================\n");
try {
const eventsSnapshot = await db.collection("events").get();
console.log(`📦 Total d'événements: ${eventsSnapshot.size}`);
let updatedCount = 0;
const batch = db.batch();
for (const doc of eventsSnapshot.docs) {
const data = doc.data();
let updated = false;
const updates = {};
const fixDate = (timestamp) => {
if (!timestamp) return null;
const oldDate = timestamp.toDate();
// On prend les composants UTC (qui étaient en fait l'heure locale mal enregistrée)
// et on crée une vraie date locale avec
const newDate = new Date(
oldDate.getUTCFullYear(),
oldDate.getUTCMonth(),
oldDate.getUTCDate(),
oldDate.getUTCHours(),
oldDate.getUTCMinutes(),
oldDate.getUTCSeconds()
);
return admin.firestore.Timestamp.fromDate(newDate);
};
if (data.StartDateTime) {
updates.StartDateTime = fixDate(data.StartDateTime);
updated = true;
}
if (data.EndDateTime) {
updates.EndDateTime = fixDate(data.EndDateTime);
updated = true;
}
if (updated) {
batch.update(doc.ref, updates);
updatedCount++;
}
}
if (updatedCount > 0) {
await batch.commit();
console.log(`${updatedCount} événements corrigés avec succès !`);
} else {
console.log("️ Aucun événement à corriger.");
}
} catch (error) {
console.error("❌ Erreur lors de la migration:", error);
process.exit(1);
}
}
migrateEventDates().then(() => process.exit(0));