|
| 1 | +/** |
| 2 | + * One-time migration for the teacher-approval feature. |
| 3 | + * |
| 4 | + * Approach for existing accounts: the founder account(s) are set approved + admin so they |
| 5 | + * can sign in and reach the admin page; EVERY other existing teacher is set to 'pending' so |
| 6 | + * they surface on the admin approval page for manual approve/reject (this also lets the admin |
| 7 | + * flow be tested against real accounts, and the junk/attack accounts get rejected there). |
| 8 | + * |
| 9 | + * The field must be PERSISTED (not left to the Mongoose default) so that the admin page's |
| 10 | + * `find({ teacherApprovalStatus: 'pending' })` query actually returns these old accounts. |
| 11 | + * |
| 12 | + * SAFE BY DEFAULT: dry-run (prints the plan, writes nothing). Pass --apply to write. |
| 13 | + * |
| 14 | + * node backend/scripts/migrate_teacher_approval.js # dry-run |
| 15 | + * node backend/scripts/migrate_teacher_approval.js --apply # perform updates |
| 16 | + * |
| 17 | + * Uses MONGODB_URI (falls back to mongodb://127.0.0.1:27017/spandan). Run OFF the live |
| 18 | + * session, and review the printed plan before using --apply. |
| 19 | + */ |
| 20 | +import mongoose from 'mongoose' |
| 21 | +import User from '../src/models/User.js' |
| 22 | + |
| 23 | +const APPLY = process.argv.includes('--apply') |
| 24 | +const URI = process.env.MONGODB_URI || 'mongodb://127.0.0.1:27017/spandan' |
| 25 | + |
| 26 | +// Founder account(s): set approved + admin so they can sign in and reach the admin page. |
| 27 | +// Override with FOUNDER_ADMIN_EMAILS (comma-separated). EVERY other existing teacher is set |
| 28 | +// to 'pending' so it appears on the admin approval page for manual approve/reject. |
| 29 | +const FOUNDER_ADMINS = (process.env.FOUNDER_ADMIN_EMAILS || 'imrohitvk@gmail.com') |
| 30 | + .split(',').map(e => e.trim().toLowerCase()).filter(Boolean) |
| 31 | + |
| 32 | +const isFounder = (email = '') => FOUNDER_ADMINS.includes(email.toLowerCase()) |
| 33 | + |
| 34 | +async function main() { |
| 35 | + await mongoose.connect(URI) |
| 36 | + console.log(`Connected: ${URI} | mode: ${APPLY ? 'APPLY (writing)' : 'DRY-RUN (no writes)'}`) |
| 37 | + |
| 38 | + const teachers = await User.find({ role: 'teacher' }).select('name email teacherApprovalStatus isActive isAdmin') |
| 39 | + const founders = [], pending = [] |
| 40 | + for (const t of teachers) { |
| 41 | + if (isFounder(t.email)) founders.push(t) |
| 42 | + else pending.push(t) |
| 43 | + } |
| 44 | + |
| 45 | + console.log(`\nTeachers: ${teachers.length} -> founder(approved+admin) ${founders.length}, set-pending ${pending.length}`) |
| 46 | + console.log('\n-- FOUNDER -> approved + isAdmin=true (can sign in and administer) --') |
| 47 | + founders.forEach(t => console.log(` ${t.email} (${t.name})`)) |
| 48 | + console.log('\n-- SET PENDING -> will appear on the admin page for approve/reject --') |
| 49 | + pending.forEach(t => console.log(` ${t.email} (${t.name})`)) |
| 50 | + |
| 51 | + if (!APPLY) { |
| 52 | + console.log('\nDRY-RUN complete. Re-run with --apply to write these changes.') |
| 53 | + await mongoose.disconnect(); return |
| 54 | + } |
| 55 | + |
| 56 | + const founderIds = founders.map(t => t._id) |
| 57 | + const pendingIds = pending.map(t => t._id) |
| 58 | + // Approve+admin the founders FIRST so the admin is never locked out. |
| 59 | + const r1 = founderIds.length ? await User.updateMany({ _id: { $in: founderIds } }, { $set: { teacherApprovalStatus: 'approved', isAdmin: true } }) : { modifiedCount: 0 } |
| 60 | + const r2 = pendingIds.length ? await User.updateMany({ _id: { $in: pendingIds } }, { $set: { teacherApprovalStatus: 'pending' } }) : { modifiedCount: 0 } |
| 61 | + console.log(`\nApplied: founders(approved+admin)=${r1.modifiedCount}, set-pending=${r2.modifiedCount}`) |
| 62 | + await mongoose.disconnect() |
| 63 | +} |
| 64 | + |
| 65 | +main().catch(e => { console.error(e); process.exit(1) }) |
0 commit comments