2016-05-09 18:16:54 -04:00
|
|
|
// Migrate users from coursePrepaidID to coursePrepaid
|
|
|
|
|
2016-05-24 19:25:19 -04:00
|
|
|
startDate = new Date(Date.UTC(2016,4,15)).toISOString(); // NOTE: Month is 0 indexed...
|
|
|
|
endDate = new Date(Date.UTC(2017,5,1)).toISOString();
|
2016-05-09 18:16:54 -04:00
|
|
|
|
2016-05-24 19:25:19 -04:00
|
|
|
cutoffDate = new Date(2015,11,11);
|
|
|
|
cutoffID = ObjectId(Math.floor(cutoffDate/1000).toString(16)+'0000000000000000');
|
|
|
|
|
|
|
|
print('Setting start/end', startDate, endDate, cutoffID);
|
|
|
|
|
|
|
|
var cursor = db.prepaids.find({type: 'course', _id: { $gt: cutoffID }})
|
|
|
|
|
|
|
|
cursor.forEach(function (prepaid) {
|
2016-05-09 18:16:54 -04:00
|
|
|
var properties = prepaid.properties || {};
|
|
|
|
if (!(prepaid.endDate && prepaid.startDate)) {
|
|
|
|
if (!prepaid.endDate) {
|
|
|
|
if(properties.endDate) {
|
|
|
|
print('Updating from existing end date', properties.endDate);
|
|
|
|
prepaid.endDate = properties.endDate.toISOString();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
prepaid.endDate = endDate;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!prepaid.startDate) {
|
|
|
|
prepaid.startDate = startDate;
|
|
|
|
}
|
2016-05-24 19:25:19 -04:00
|
|
|
print('updating prepaid', prepaid._id, 'creator', prepaid.creator, 'start/end', prepaid.startDate, prepaid.endDate);
|
|
|
|
print(' -', db.prepaids.save(prepaid));
|
2016-05-09 18:16:54 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
var redeemers = prepaid.redeemers || [];
|
|
|
|
for (var index in redeemers) {
|
|
|
|
var redeemer = redeemers[index];
|
2016-05-24 19:25:19 -04:00
|
|
|
var user = db.users.findOne({ _id: redeemer.userID }, { coursePrepaid: 1, coursePrepaidID: 1, email:1, name:1, permissions: 1 });
|
2016-05-09 18:16:54 -04:00
|
|
|
if (user.coursePrepaidID && !user.coursePrepaid) {
|
|
|
|
var update = {
|
|
|
|
$set: { coursePrepaid: { _id: user.coursePrepaidID, startDate: prepaid.startDate, endDate: prepaid.endDate } },
|
|
|
|
$unset: { coursePrepaidID: '' }
|
|
|
|
}
|
2016-05-24 19:25:19 -04:00
|
|
|
print('\t updating user', user._id, user.name, user.email, user.permissions, JSON.stringify(update));
|
|
|
|
print('\t', db.users.update({_id: user._id}, update));
|
2016-05-09 18:16:54 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|