-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathadd_school_code.js
More file actions
90 lines (72 loc) · 2.31 KB
/
Copy pathadd_school_code.js
File metadata and controls
90 lines (72 loc) · 2.31 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/**
* add_school_code.js
*
* Adds schoolCode to all existing students that don't have one.
* Also adds parentIds array (replacing old parentUids/parentId fields).
*
* Run: node add_school_code.js
*
* Place serviceAccountKey.json in same folder before running.
*/
const admin = require('firebase-admin');
const { getFirestore } = require('firebase-admin/firestore');
const serviceAccount = require('./serviceAccountKey.json');
admin.initializeApp({
credential: admin.cert(serviceAccount),
});
const db = getFirestore();
// ← CHANGE THIS to match your school code
const SCHOOL_CODE = 'DSS2025';
async function migrate() {
console.log(`Adding schoolCode "${SCHOOL_CODE}" to students without it...`);
const snap = await db.collection('students').get();
let updated = 0;
let skipped = 0;
const batch = db.batch();
let batchCount = 0;
for (const doc of snap.docs) {
const data = doc.data();
const updates = {};
// Add schoolCode if missing
if (!data.schoolCode) {
updates.schoolCode = SCHOOL_CODE;
}
// Normalize parentIds — merge from parentUids and parentId
if (data.parentIds === undefined) {
const ids = new Set();
// From old parentUids array
if (Array.isArray(data.parentUids)) {
data.parentUids.forEach(id => { if (id) ids.add(id); });
}
// From old parentId string
if (data.parentId && typeof data.parentId === 'string' && data.parentId.trim()) {
ids.add(data.parentId.trim());
}
updates.parentIds = Array.from(ids);
}
if (Object.keys(updates).length > 0) {
batch.update(doc.ref, updates);
batchCount++;
updated++;
} else {
skipped++;
}
// Commit every 499
if (batchCount >= 499) {
await batch.commit();
console.log(` Committed ${updated} so far...`);
batchCount = 0;
}
}
if (batchCount > 0) await batch.commit();
console.log(`\nDone!`);
console.log(` Updated: ${updated} students`);
console.log(` Skipped: ${skipped} (already had schoolCode)`);
console.log(`\nAll students now have schoolCode="${SCHOOL_CODE}" and parentIds array.`);
console.log('View Students in the admin app should now show all students.');
process.exit(0);
}
migrate().catch(e => {
console.error('Error:', e);
process.exit(1);
});