Spaces:
Running
Running
Upload functions/index.js
Browse files- functions/index.js +35 -0
functions/index.js
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
const functions = require('firebase-functions');
|
| 2 |
+
const admin = require('firebase-admin');
|
| 3 |
+
|
| 4 |
+
admin.initializeApp();
|
| 5 |
+
|
| 6 |
+
exports.resetCoinsEveryFiveDays = functions.pubsub
|
| 7 |
+
.schedule('0 0 */5 * *')
|
| 8 |
+
.timeZone('America/Mexico_City')
|
| 9 |
+
.onRun(async (context) => {
|
| 10 |
+
const db = admin.firestore();
|
| 11 |
+
const adminEmail = "dimensionalpulsar@gmail.com";
|
| 12 |
+
|
| 13 |
+
try {
|
| 14 |
+
// Buscamos solo usuarios que NO sean el administrador
|
| 15 |
+
const snapshot = await db.collection('users')
|
| 16 |
+
.where('email', '!=', adminEmail)
|
| 17 |
+
.get();
|
| 18 |
+
|
| 19 |
+
if (snapshot.empty) return null;
|
| 20 |
+
|
| 21 |
+
const batch = db.batch();
|
| 22 |
+
snapshot.docs.forEach((userDoc) => {
|
| 23 |
+
batch.update(userDoc.ref, {
|
| 24 |
+
coins: 0,
|
| 25 |
+
lastReset: admin.firestore.FieldValue.serverTimestamp()
|
| 26 |
+
});
|
| 27 |
+
});
|
| 28 |
+
|
| 29 |
+
await batch.commit();
|
| 30 |
+
return null;
|
| 31 |
+
} catch (error) {
|
| 32 |
+
console.error("Error en el reseteo:", error);
|
| 33 |
+
return null;
|
| 34 |
+
}
|
| 35 |
+
});
|