2016-03-09 17:40:52 -05:00
|
|
|
// Usage: copy and paste into mongo
|
|
|
|
|
2016-03-30 19:20:37 -04:00
|
|
|
|
|
|
|
// Set all users with trial requests to a teacher or teacher-like role, depending on trial request.
|
|
|
|
|
2016-04-13 12:43:05 -04:00
|
|
|
var project = {role:1, name:1, email:1, permissions: 1};
|
2016-03-30 19:20:37 -04:00
|
|
|
|
|
|
|
db.trial.requests.find().forEach(function(trialRequest) {
|
2016-04-13 12:43:05 -04:00
|
|
|
print('Inspecting trial request', trialRequest._id);
|
2016-03-30 19:20:37 -04:00
|
|
|
var role = trialRequest.properties.role || 'teacher';
|
2016-04-13 12:43:05 -04:00
|
|
|
var user = null;
|
|
|
|
if(!trialRequest.applicant) {
|
|
|
|
print('\tNO APPLICANT INCLUDED', JSON.stringify(trialRequest));
|
|
|
|
if(!trialRequest.properties.email) {
|
|
|
|
print('\tNO EMAIL EITHER');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
user = db.users.findOne({emailLower: trialRequest.properties.email.toLowerCase()}, project);
|
|
|
|
if(!user) {
|
|
|
|
print('\tUSER WITH EMAIL NOT FOUND, CONTINUE');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
print("\tOKAY GOT USER, UPDATE TRIAL REQUEST", JSON.stringify(user));
|
|
|
|
db.trial.requests.update({_id: trialRequest._id}, {$set: {applicant: user._id}});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
user = db.users.findOne({_id: trialRequest.applicant}, project);
|
|
|
|
}
|
|
|
|
if (!user.role && (user.permissions||[]).indexOf('admin') === -1) {
|
|
|
|
print('\tUpdating', JSON.stringify(user), 'to', role);
|
|
|
|
print(db.users.update({_id: user._id}, {$set: {role: role}}));
|
2016-03-30 19:20:37 -04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// Unset all teacher-like roles for users without a trial request.
|
|
|
|
// AND removes all remaining users with a teacher-like role from classroom membership (after conversion period)
|
|
|
|
|
2016-04-13 12:43:05 -04:00
|
|
|
var hasTrialRequest = {};
|
|
|
|
var teacherRoles = ['teacher', 'technology coordinator', 'advisor', 'principal', 'superintendent'];
|
|
|
|
|
|
|
|
db.trial.requests.find().forEach(function(trialRequest) {
|
|
|
|
if(!trialRequest.applicant) { return; }
|
|
|
|
hasTrialRequest[trialRequest.applicant.str] = true;
|
|
|
|
});
|
|
|
|
print(Object.keys(hasTrialRequest).length);
|
|
|
|
|
2016-03-09 17:40:52 -05:00
|
|
|
db.users.find({'role': {$in: teacherRoles}}, {_id: 1, name: 1, email: 1, role: 1}).forEach(function(user) {
|
2016-04-13 12:43:05 -04:00
|
|
|
print('Got user with teacher role', user._id);
|
|
|
|
if (!hasTrialRequest[user._id.str]) {
|
|
|
|
print('\tUnset role', JSON.stringify(user));
|
|
|
|
db.users.update({_id: user._id}, {$unset: {role: ''}});
|
2016-03-30 19:20:37 -04:00
|
|
|
}
|
|
|
|
else {
|
2016-04-13 12:43:05 -04:00
|
|
|
return; // TODO: Run when we've moved completely to separate user roles
|
2016-03-30 19:20:37 -04:00
|
|
|
var count = db.classrooms.count({members: user._id}, {name: 1});
|
|
|
|
if (count) {
|
|
|
|
print('\tWill remove from classrooms');
|
|
|
|
//print(db.classrooms.update({members: user._id}, {$pull: {members: user._id}}, {multi: true}));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
print('\tRole correct, in no classrooms. No action')
|
|
|
|
}
|
|
|
|
}
|
2016-03-09 17:40:52 -05:00
|
|
|
});
|
|
|
|
|
2016-03-30 19:20:37 -04:00
|
|
|
// Find all members of classrooms, set their role to 'student' if they do not already have a role
|
2016-03-09 17:40:52 -05:00
|
|
|
|
|
|
|
db.classrooms.find({}, {members: 1}).forEach(function(classroom) {
|
|
|
|
if(!classroom.members) {
|
|
|
|
return;
|
|
|
|
}
|
2016-04-13 12:43:05 -04:00
|
|
|
print('Updating for classroom', classroom._id, 'with members', classroom.members.length);
|
2016-03-09 17:40:52 -05:00
|
|
|
for (var i in classroom.members) {
|
|
|
|
var memberID = classroom.members[i];
|
2016-04-13 12:43:05 -04:00
|
|
|
print('\tupdating member', memberID);
|
2016-03-09 17:40:52 -05:00
|
|
|
print(db.users.update({_id: memberID, role: {$exists: false}}, {$set: {role: 'student'}}));
|
2016-04-13 12:43:05 -04:00
|
|
|
}
|
|
|
|
});
|