@@ -3,6 +3,7 @@ const mongoose = require('mongoose');
33const bcrypt = require ( 'bcryptjs' ) ;
44const fs = require ( 'fs' ) ;
55const csv = require ( 'csv-parser' ) ;
6+ const path = require ( 'path' ) ;
67
78async function creatementorsFromCSV ( ) {
89 try {
@@ -15,8 +16,12 @@ async function creatementorsFromCSV() {
1516 // Read and parse CSV file
1617 const mentors = [ ] ;
1718
19+ // Get CSV path from environment variable or use default path
20+ const csvPath = process . env . MENTORS_CSV_PATH || path . join ( __dirname , 'mentors.csv' ) ;
21+ console . log ( `Reading mentors from: ${ csvPath } ` ) ;
22+
1823 await new Promise ( ( resolve , reject ) => {
19- fs . createReadStream ( 'mentors.csv' )
24+ fs . createReadStream ( csvPath )
2025 . pipe ( csv ( ) )
2126 . on ( 'data' , ( row ) => {
2227 // Extract mentor data from CSV row
@@ -36,48 +41,50 @@ async function creatementorsFromCSV() {
3641
3742 console . log ( `Found ${ mentors . length } mentors in CSV file` ) ;
3843
39- // Clear existing mentors (optional - remove this if you want to keep existing mentors)
40- // await mongoose.connection.collection('mentors').deleteMany({});
41- // console.log('Cleared existing mentors');
44+ // We are updating existing mentors with new passwords
45+ console . log ( 'Updating existing mentors with new passwords' ) ;
4246
4347
44- // Create mentors in database
45- const createdMentors = [ ] ;
48+ // Update mentors in database
49+ const updatedMentors = [ ] ;
4650
4751 for ( const mentor of mentors ) {
4852 try {
49- // Generate a unique 5-character password for each mentor
53+ // Generate a unique password hash for each mentor
5054 const salt = await bcrypt . genSalt ( 10 ) ;
5155 const hashedPassword = await bcrypt . hash ( mentor . mentorPassword , salt ) ;
5256
53- const mentorDoc = {
54- teamName : mentor . mentorName ,
55- password : hashedPassword ,
56- role : 'mentor' , // Default role for mentors
57- isActive : true ,
58- createdAt : new Date ( ) ,
59- updatedAt : new Date ( )
60- } ;
61-
62- const result = await mongoose . connection . collection ( 'teams' ) . insertOne ( mentorDoc ) ;
57+ // Update existing mentor with new password
58+ const result = await mongoose . connection . collection ( 'teams' ) . updateOne (
59+ { teamName : mentor . mentorName } ,
60+ {
61+ $set : {
62+ password : hashedPassword ,
63+ updatedAt : new Date ( )
64+ }
65+ }
66+ ) ;
6367
64- createdMentors . push ( {
65- id : result . insertedId ,
66- mentorName : mentor . mentorName ,
67- password : mentor . mentorPassword // Store the plain password for display
68- } ) ;
68+ if ( result . matchedCount > 0 ) {
69+ updatedMentors . push ( {
70+ mentorName : mentor . mentorName ,
71+ password : mentor . mentorPassword // Store the plain password for display
72+ } ) ;
73+ } else {
74+ console . log ( `No mentor found with username: ${ mentor . mentorName } ` ) ;
75+ }
6976 } catch ( error ) {
7077 console . error ( `Error creating mentor ${ mentor . mentorName } :` , error . message ) ;
7178 }
7279 }
7380
7481 console . log ( '\n=== SUMMARY ===' ) ;
75- console . log ( `Successfully created ${ createdMentors . length } mentors:` ) ;
76- createdMentors . forEach ( mentor => {
82+ console . log ( `Successfully updated ${ updatedMentors . length } mentors:` ) ;
83+ updatedMentors . forEach ( mentor => {
7784 console . log ( `${ mentor . mentorName } ,${ mentor . password } ` ) ;
7885 } ) ;
7986
80- console . log ( '\nIMPORTANT: Save these passwords! Each mentor has a unique 5-character password .' ) ;
87+ console . log ( '\nIMPORTANT: Save these new passwords! Make sure to distribute them to the mentors .' ) ;
8188
8289 } catch ( error ) {
8390 console . error ( 'Error creating mentors from CSV:' , error ) ;
0 commit comments