codecombat/scripts/mongodb/migrations/2016-03-18-init-school-roles.js

52 lines
2 KiB
JavaScript
Raw Normal View History

2016-03-09 17:40:52 -05:00
// Usage: copy and paste into mongo
// Set all users with trial requests to a teacher or teacher-like role, depending on trial request.
var hasTrialRequest = {};
db.trial.requests.find().forEach(function(trialRequest) {
var role = trialRequest.properties.role || 'teacher';
var user = db.users.findOne({_id: trialRequest.applicant}, {role:1, name:1, email:1});
print(JSON.stringify(user), JSON.stringify(trialRequest.properties), role);
if (!user.role) {
print(db.users.update({_id: trialRequest.applicant}, {$set: {role: role}}));
}
hasTrialRequest[user._id.str] = true;
});
2016-03-09 17:40:52 -05:00
var teacherRoles = ['teacher', 'technology coordinator', 'advisor', 'principal', 'superintendent'];
// 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-03-09 17:40:52 -05:00
db.users.find({'role': {$in: teacherRoles}}, {_id: 1, name: 1, email: 1, role: 1}).forEach(function(user) {
print('Updating user', JSON.stringify(user));
if (!hasTrialRequest.user._id.str) {
print('\tunset role');
//db.users.update({_id: user._id}, {$unset: {role: ''}});
}
else {
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
});
// 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;
}
for (var i in classroom.members) {
var memberID = classroom.members[i];
print('updating member', memberID);
print(db.users.update({_id: memberID, role: {$exists: false}}, {$set: {role: 'student'}}));
}
});