Repository navigation
Expand file tree
/
Copy pathshift_dates.js
More file actions
73 lines (60 loc) · 1.91 KB
/
Copy pathshift_dates.js
File metadata and controls
73 lines (60 loc) · 1.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
require('dotenv').config();
const { initializeApp, getApps, cert } = require('firebase-admin/app');
const { getFirestore } = require('firebase-admin/firestore');
if (getApps().length === 0) {
let serviceAccount = {};
try {
serviceAccount = JSON.parse(process.env.FIREBASE_SERVICE_ACCOUNT_KEY || '{}');
} catch(e) {
console.error('Error parsing FIREBASE_SERVICE_ACCOUNT_KEY:', e.message);
process.exit(1);
}
initializeApp({
credential: cert(serviceAccount)
});
}
const db = getFirestore();
async function run() {
try {
console.log('Connecting to Firebase Firestore...');
const snapshot = await db.collection('tasks').get();
console.log(`Found ${snapshot.docs.length} tasks in Firestore.`);
if (snapshot.empty) {
console.log('No tasks found in Firestore.');
process.exit(0);
}
let batch = db.batch();
let count = 0;
let totalUpdated = 0;
for (const doc of snapshot.docs) {
const data = doc.data();
let day;
const status = data.status || 'Assigned';
if (status === 'Overdue') {
day = Math.floor(Math.random() * 14) + 1; // Aug 1 - 14
} else if (status === 'Assigned' || status === 'In Progress') {
day = Math.floor(Math.random() * 16) + 15; // Aug 15 - 30
} else {
day = Math.floor(Math.random() * 30) + 1; // Aug 1 - 30
}
const newDueDate = `2026-08-${day.toString().padStart(2, '0')}`;
batch.update(doc.ref, { due_date: newDueDate });
count++;
totalUpdated++;
if (count >= 400) {
await batch.commit();
batch = db.batch();
count = 0;
}
}
if (count > 0) {
await batch.commit();
}
console.log(`Successfully updated due_date for all ${totalUpdated} tasks in Firestore to August 2026!`);
process.exit(0);
} catch (err) {
console.error('Error updating tasks in Firestore:', err);
process.exit(1);
}
}
run();