Files
EM2_ERP/em2rp/functions/migrate_event_dates.js
T

74 lines
2.0 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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));