-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathdatabase_operations.js
executable file
·198 lines (189 loc) · 5.1 KB
/
database_operations.js
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
const express = require('express'),
mongoose = require('mongoose'),
localStrategy = require('passport-local'),
passportLocalMongoose = require('passport-local-mongoose'),
Class = require('./models/class'),
Student = require('./models/students'),
Teacher = require('./models/teachers'),
csv = require('csv-parser'),
fs = require('fs'),
asyn = require('async');
// const createTeacher = (username, password, email, classes) => {
// var classes_array = []
// asyn.series([
// function findclasses(createTeacher) {
// console.log(classes)
// classes.forEach((clas,index)=>{
// Class.findOne({year:clas.year,department:clas.department,batch_name:clas.batch_name}, (err, c)=>{
// if(!err) {
// // console.log(c._id.toString(), "Classes ")
// classes_array.push(c._id.toString())
// if (index==classes.length-1) {
// createTeacher()
// }
// } else {
// // Error
// }
// })
// })
// },
// function createTeacher() {
// const teacher = new Teacher({
// username: username,
// email: email,
// designation: 'Teacher',
// classes: classes_array
// });
// Teacher.register(teacher, password, (err, teacher) => {
// if (!err) {
// console.log(teacher);
// console.log('Teacher created successfully');
// } else {
// console.log(err);
// }
// })
// }
// ],()=>{
// console.log("Everything executed")
// })
function createClasses() {
console.log('Creating classes...');
var clas = new Class({
year: 'FE',
department: 'EXTC',
batch_name: 'H1',
});
clas.save();
clas = new Class({
year: 'FE',
department: 'EXTC',
batch_name: 'H2',
});
clas.save();
clas = new Class({
year: 'FE',
department: 'EXTC',
batch_name: 'H3',
});
clas.save();
// clas = new Class({
// year: 'SE',
// department: 'IT',
// batch_name: 'D1'
// });
// clas.save();
// clas = new Class({
// year: 'SE',
// department: 'IT',
// batch_name: 'D2'
// });
// clas.save();
// clas = new Class({
// year: 'SE',
// department: 'IT',
// batch_name: 'D3'
// });
// clas.save();
// clas = new Class({
// year: 'SE',
// department: 'IT',
// batch_name: 'D4'
// });
// clas.save();
// clas = new Class({
// year: 'TE',
// department: 'IT',
// batch_name: 'D1'
// });
// clas.save();
// clas = new Class({
// year: 'TE',
// department: 'IT',
// batch_name: 'D2'
// });
// clas.save();
// clas = new Class({
// year: 'TE',
// department: 'IT',
// batch_name: 'D3'
// });
// clas.save();
// clas = new Class({
// year: 'TE',
// department: 'IT',
// batch_name: 'D4'
// });
clas.save(() => {
console.log('Saved');
});
}
function registerStudent(fileName) {
fs.createReadStream(fileName)
.pipe(csv())
.on('data', (row) => {
let classid;
console.log(row.year);
Class.findOne(
{
batch_name: row.batch.toUpperCase(),
year: row.year,
department: row.department,
},
(err, clas) => {
console.log(clas);
if (!err) {
classid = clas._id.toString();
const newStudent = new Student({
username: row.username,
name: capitalize(row.name),
email: row.email,
classId: classid,
designation: 'Student',
});
Student.register(newStudent, 'init@123', (err, student) => {
if (!err) {
console.log(`${student.name} added successfully`);
Student.authenticate('student')(() => {
console.log('ok');
});
} else {
console.log(err);
}
});
} else {
console.log('There was an error!');
}
}
);
})
.on('end', () => {
console.log('CSV file successfully processed');
});
}
// To convert uppercase names in sheets and to take only first and last names
function capitalize(name) {
const arr = name.trim().split(' ');
let capitalizedName = '';
if (arr.length >= 2) {
const firstName = arr[0];
const lastName = arr[arr.length - 1];
console.log(firstName);
capitalizedName =
firstName[0].toUpperCase() +
firstName.substring(1).toLowerCase() +
' ' +
lastName[0].toUpperCase() +
lastName.substring(1).toLowerCase();
} else if (arr.length == 1) {
const firstName = arr[0];
if (firstName)
capitalizedName =
firstName[0].toUpperCase() + firstName.substring(1).toLowerCase();
} else {
// Error
}
return capitalizedName;
}
registerStudent('csv/feextc2.csv');
// // module.exports.createClasses = createClasses;
// //module.exports.createTeacher = createTeacher;