mirror of
https://github.com/codeninjasllc/codecombat.git
synced 2024-11-23 15:48:11 -05:00
Add updatePrepaid.js stored mongodb script
This commit is contained in:
parent
6b78ab3fe8
commit
4bc9ea77c6
1 changed files with 73 additions and 0 deletions
73
scripts/mongodb/stored/updatePrepaid.js
Normal file
73
scripts/mongodb/stored/updatePrepaid.js
Normal file
|
@ -0,0 +1,73 @@
|
|||
|
||||
// Update a prepaid document, and the denormalized data on user documents
|
||||
// Limits the properties allowed to be set, but does not perform validation on them. Use carefully!
|
||||
|
||||
// Usage
|
||||
// ---------------
|
||||
// In mongo shell
|
||||
//
|
||||
// > db.loadServerScripts();
|
||||
// > updatePrepaid('<prepaid id string>', { endDate: "2017-07-01T00:00:00.000Z", maxRedeemers: 10 });
|
||||
|
||||
var updatePrepaid = function updatePrepaid(stringID, originalUpdate) {
|
||||
try {
|
||||
load('./bower_components/lodash/dist/lodash.js')
|
||||
}
|
||||
catch (e) {
|
||||
print('Lodash could not be loaded, ensure you are in codecombat project directory.')
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
var id = ObjectId(stringID);
|
||||
}
|
||||
catch (e) {
|
||||
print('Invalid ObjectId given:', stringID);
|
||||
return;
|
||||
}
|
||||
|
||||
var prepaid = db.prepaids.findOne({_id: id});
|
||||
if (!prepaid) {
|
||||
print('Prepaid not found');
|
||||
return;
|
||||
}
|
||||
|
||||
print('Found prepaid', _.omit(prepaid, 'redeemers'));
|
||||
print('Has', prepaid.redeemers.length, 'redeemers.');
|
||||
|
||||
var prepaidUpdate = _.pick(originalUpdate, 'maxRedeemers', 'startDate', 'endDate' );
|
||||
if (_.isEmpty(prepaidUpdate)) {
|
||||
print('Skipping prepaid update, nothing to update.')
|
||||
}
|
||||
else {
|
||||
print('Update prepaid',
|
||||
JSON.stringify(prepaidUpdate, null, ' '),
|
||||
db.prepaids.update(
|
||||
{_id: id},
|
||||
{ $set: prepaidUpdate }
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
var userUpdate = _.pick(originalUpdate, 'startDate', 'endDate' );
|
||||
if (_.isEmpty(userUpdate)) {
|
||||
print('Skipping user update, nothing to update.')
|
||||
}
|
||||
else {
|
||||
print('Update users',
|
||||
JSON.stringify(userUpdate, null, ' '),
|
||||
db.users.update(
|
||||
{'coursePrepaid._id': id},
|
||||
{$set: userUpdate},
|
||||
{multi: true}
|
||||
)
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
db.system.js.save(
|
||||
{
|
||||
_id: 'updatePrepaid',
|
||||
value: updatePrepaid
|
||||
}
|
||||
);
|
Loading…
Reference in a new issue