|
| 1 | +/* eslint-disable */ |
| 2 | +// Idempotent Firestore seed script. |
| 3 | +// Run from repo root: node scripts/seed-firestore.js |
| 4 | +// Requires Backend/serviceAccountKey.json (already gitignored). |
| 5 | + |
| 6 | +const path = require('path'); |
| 7 | +const fs = require('fs'); |
| 8 | +const Module = require('module'); |
| 9 | + |
| 10 | +// firebase-admin is installed in Backend/node_modules, not root |
| 11 | +Module.globalPaths.push(path.join(__dirname, '..', 'Backend', 'node_modules')); |
| 12 | +const admin = require(path.join(__dirname, '..', 'Backend', 'node_modules', 'firebase-admin')); |
| 13 | + |
| 14 | +const serviceAccountPath = path.join(__dirname, '..', 'Backend', 'serviceAccountKey.json'); |
| 15 | +if (!fs.existsSync(serviceAccountPath)) { |
| 16 | + console.error( |
| 17 | + `Missing ${serviceAccountPath}.\n` + |
| 18 | + 'Download a service-account key from Firebase Console > Project settings > Service accounts.\n' + |
| 19 | + 'See Backend/SETUP_FIREBASE.md.', |
| 20 | + ); |
| 21 | + process.exit(1); |
| 22 | +} |
| 23 | + |
| 24 | +const serviceAccount = require(serviceAccountPath); |
| 25 | +admin.initializeApp({ credential: admin.credential.cert(serviceAccount) }); |
| 26 | +const db = admin.firestore(); |
| 27 | + |
| 28 | +// Mock data sources (kept as the seed of truth) |
| 29 | +const studySpots = [ |
| 30 | + { |
| 31 | + id: '1', |
| 32 | + name: 'Doe Library', |
| 33 | + location: 'On campus', |
| 34 | + description: 'Convenient, beautiful library', |
| 35 | + distance: 0.3, |
| 36 | + hours: '9AM - 9PM', |
| 37 | + noiseLevel: 'Silent', |
| 38 | + outlets: true, |
| 39 | + lighting: 'Bright', |
| 40 | + crowded: 'Low', |
| 41 | + roomType: 'Library', |
| 42 | + open: true, |
| 43 | + rating: 4.7, |
| 44 | + image: '/cat.webp', |
| 45 | + }, |
| 46 | + { |
| 47 | + id: '2', |
| 48 | + name: 'MLK Student Union', |
| 49 | + location: 'On campus', |
| 50 | + description: 'Collaborative study environment', |
| 51 | + distance: 0.5, |
| 52 | + hours: '10AM - 11PM', |
| 53 | + noiseLevel: 'Medium', |
| 54 | + outlets: true, |
| 55 | + lighting: 'Medium', |
| 56 | + crowded: 'High', |
| 57 | + roomType: 'Student Center', |
| 58 | + open: true, |
| 59 | + rating: 4.1, |
| 60 | + image: '/anothercat.jpg', |
| 61 | + }, |
| 62 | + { |
| 63 | + id: '3', |
| 64 | + name: 'Cafe Strada', |
| 65 | + location: 'Off campus', |
| 66 | + description: 'Great outdoor seating', |
| 67 | + distance: 0.4, |
| 68 | + hours: '8AM - 6PM', |
| 69 | + noiseLevel: 'Loud', |
| 70 | + outlets: false, |
| 71 | + lighting: 'Dim', |
| 72 | + crowded: 'Medium', |
| 73 | + roomType: 'Cafe', |
| 74 | + open: true, |
| 75 | + rating: 3.2, |
| 76 | + image: '/yetanothercat.jpg', |
| 77 | + }, |
| 78 | +]; |
| 79 | + |
| 80 | +const studyGroups = [ |
| 81 | + { |
| 82 | + id: '1', |
| 83 | + course: 'CS 61A', |
| 84 | + name: 'cs warriors', |
| 85 | + pace: 'Fast', |
| 86 | + noiseLevel: 'Medium', |
| 87 | + groupSize: 4, |
| 88 | + availability: 'Evenings', |
| 89 | + vibe: 'Focused', |
| 90 | + method: 'Practice problems', |
| 91 | + description: 'Looking for 2 more members!', |
| 92 | + creator: 'Alex', |
| 93 | + meetingTime: 'Wed, April 10, 6:00PM', |
| 94 | + meetingPlace: 'Doe Library Room 123', |
| 95 | + members: ['Alex', 'Taylor', 'Jordan'], |
| 96 | + image: '/cat.webp', |
| 97 | + }, |
| 98 | + { |
| 99 | + id: '2', |
| 100 | + course: 'MATH 54', |
| 101 | + name: 'we love arun sharma', |
| 102 | + pace: 'Medium', |
| 103 | + noiseLevel: 'Quiet', |
| 104 | + groupSize: 3, |
| 105 | + availability: 'Afternoons', |
| 106 | + vibe: 'Chill', |
| 107 | + method: 'Concept discussion', |
| 108 | + description: 'Hoping to study collaboratively and meet new people!', |
| 109 | + creator: 'Jamie', |
| 110 | + meetingTime: 'Thu, April 11, 3:00PM', |
| 111 | + meetingPlace: 'Evans Hall Room 210', |
| 112 | + members: ['Jamie', 'Sam'], |
| 113 | + image: '/anothercat.jpg', |
| 114 | + }, |
| 115 | +]; |
| 116 | + |
| 117 | +async function seedSpots() { |
| 118 | + let created = 0; |
| 119 | + let skipped = 0; |
| 120 | + for (const { id, ...data } of studySpots) { |
| 121 | + const ref = db.collection('spots').doc(id); |
| 122 | + const snap = await ref.get(); |
| 123 | + if (snap.exists) { |
| 124 | + skipped++; |
| 125 | + continue; |
| 126 | + } |
| 127 | + await ref.set({ |
| 128 | + ...data, |
| 129 | + ratingSum: 0, |
| 130 | + ratingCount: 0, |
| 131 | + createdAt: admin.firestore.FieldValue.serverTimestamp(), |
| 132 | + }); |
| 133 | + created++; |
| 134 | + } |
| 135 | + console.log(`spots: ${created} created, ${skipped} already existed`); |
| 136 | +} |
| 137 | + |
| 138 | +async function seedGroups() { |
| 139 | + let created = 0; |
| 140 | + let skipped = 0; |
| 141 | + for (const { id, ...data } of studyGroups) { |
| 142 | + const ref = db.collection('groups').doc(id); |
| 143 | + const snap = await ref.get(); |
| 144 | + if (snap.exists) { |
| 145 | + skipped++; |
| 146 | + continue; |
| 147 | + } |
| 148 | + await ref.set({ |
| 149 | + ...data, |
| 150 | + ownerId: '', |
| 151 | + memberIds: [], |
| 152 | + createdAt: admin.firestore.FieldValue.serverTimestamp(), |
| 153 | + }); |
| 154 | + created++; |
| 155 | + } |
| 156 | + console.log(`groups: ${created} created, ${skipped} already existed`); |
| 157 | +} |
| 158 | + |
| 159 | +(async () => { |
| 160 | + try { |
| 161 | + await seedSpots(); |
| 162 | + await seedGroups(); |
| 163 | + console.log('Seed complete.'); |
| 164 | + process.exit(0); |
| 165 | + } catch (e) { |
| 166 | + console.error('Seed failed:', e); |
| 167 | + process.exit(1); |
| 168 | + } |
| 169 | +})(); |
0 commit comments