Spaces:
Paused
Paused
Download scripts/fixUserRoles.js from imkrish/campverse-backend: direct link, hf CLI and curl.
- Browser
- Download file 3.83 kB
-
https://huggingface.co/spaces/imkrish/campverse-backend/resolve/main/scripts/fixUserRoles.js
- Command line
-
hf download hf://spaces/imkrish/campverse-backend/scripts/fixUserRoles.js
-
curl -L -o fixUserRoles.js https://huggingface.co/spaces/imkrish/campverse-backend/resolve/main/scripts/fixUserRoles.js
3.83 kB
| /** | |
| * Migration Script: Fix User Roles | |
| * | |
| * This script fixes the critical bug where default roles were set as ['student,host'] | |
| * (a single string) instead of ['student'] (an array with one element). | |
| * | |
| * It will: | |
| * 1. Find all users with the malformed role 'student,host' | |
| * 2. Replace it with proper ['student'] array | |
| * 3. Preserve any additional roles (host, verifier, platformAdmin) | |
| * 4. Report on the changes made | |
| */ | |
| require('dotenv').config(); | |
| const mongoose = require('mongoose'); | |
| const User = require('../Models/User'); | |
| async function fixUserRoles() { | |
| try { | |
| console.log('π§ Starting user roles migration...'); | |
| console.log('Connecting to MongoDB...'); | |
| await mongoose.connect(process.env.MONGO_URI, { | |
| useNewUrlParser: true, | |
| useUnifiedTopology: true, | |
| }); | |
| console.log('β Connected to MongoDB\n'); | |
| // Find all users with the malformed role | |
| const usersWithBadRole = await User.find({ | |
| roles: 'student,host' | |
| }); | |
| console.log(`Found ${usersWithBadRole.length} users with malformed 'student,host' role\n`); | |
| if (usersWithBadRole.length === 0) { | |
| console.log('β No users need fixing. All roles are properly formatted.'); | |
| await mongoose.disconnect(); | |
| return; | |
| } | |
| // Process each user | |
| let fixed = 0; | |
| const skipped = 0; | |
| let errors = 0; | |
| for (const user of usersWithBadRole) { | |
| try { | |
| console.log(`Processing user: ${user.email} (${user._id})`); | |
| console.log(` Current roles: ${JSON.stringify(user.roles)}`); | |
| // Determine correct roles based on user's actual permissions | |
| const newRoles = ['student']; | |
| // Check if user should be a host | |
| if (user.canHost || user.hostEligibilityStatus?.status === 'approved') { | |
| newRoles.push('host'); | |
| console.log(' - User is an approved host, adding \'host\' role'); | |
| } | |
| // Check if user is a verifier | |
| if (user.verifierEligibilityStatus?.approvedBy) { | |
| newRoles.push('verifier'); | |
| console.log(' - User is a verifier, adding \'verifier\' role'); | |
| } | |
| // Note: platformAdmin needs manual verification | |
| // We don't automatically assign it | |
| // Update user | |
| user.roles = newRoles; | |
| await user.save(); | |
| console.log(` β Updated roles to: ${JSON.stringify(newRoles)}\n`); | |
| fixed++; | |
| } catch (error) { | |
| console.error(` β Error processing user ${user.email}:`, error.message); | |
| errors++; | |
| } | |
| } | |
| // Summary | |
| console.log(`\n${ '='.repeat(60)}`); | |
| console.log('π MIGRATION SUMMARY'); | |
| console.log('='.repeat(60)); | |
| console.log(`Total users processed: ${usersWithBadRole.length}`); | |
| console.log(`β Successfully fixed: ${fixed}`); | |
| console.log(`βοΈ Skipped: ${skipped}`); | |
| console.log(`β Errors: ${errors}`); | |
| console.log(`${'='.repeat(60) }\n`); | |
| // Verify the fix | |
| console.log('π Verifying fix...'); | |
| const stillBroken = await User.find({ | |
| roles: 'student,host' | |
| }); | |
| if (stillBroken.length === 0) { | |
| console.log('β Verification passed! No users have malformed roles.\n'); | |
| } else { | |
| console.log(`β οΈ Warning: ${stillBroken.length} users still have malformed roles.\n`); | |
| } | |
| await mongoose.disconnect(); | |
| console.log('β Migration completed successfully!'); | |
| } catch (error) { | |
| console.error('β Migration failed:', error); | |
| process.exit(1); | |
| } | |
| } | |
| // Run the migration | |
| if (require.main === module) { | |
| fixUserRoles() | |
| .then(() => process.exit(0)) | |
| .catch((error) => { | |
| console.error('Fatal error:', error); | |
| process.exit(1); | |
| }); | |
| } | |
| module.exports = fixUserRoles; | |