codecombat/scripts/mongodb/addExternalSubs.js

71 lines
1.5 KiB
JavaScript
Raw Normal View History

2015-02-26 11:44:53 -05:00
// Add externally purchased subscriptions
// Usage:
// Edit hard coded data below.
// mongo <address>:<port>/<database> <script file> -u <username> -p <password>
// Skips duplicate payments, but this might be incorrect depending on the scenario
// TODO: output emails not found
var emails = ['pam@fred.com', 'Bob@fred.com'];
var purchaserID = '54ed0ac0ca7f1c421c025b3d';
var endDate = '2015-06-01';
var gems = 10500;
var amount = 1750;
var service = 'invoice';
emails = emails.map(function(e) { return e.toLowerCase();});
log("Input Data");
log("service\t" + service);
log("purchaserID\t" + purchaserID);
log("end date\t" + endDate);
log("gems\t" + gems);
log("amount\t" + amount);
log("emails");
log(emails);
// 1. Set free = endDate, updated purchased.gems
db.users.update(
{emailLower: {$in: emails}, "stripe.free": {$ne: endDate}},
{
$set: {
"stripe.free": endDate
},
$inc: {
"purchased.gems": gems
}
},
{
multi: true
}
);
// 2. create Payment objects
var cursor = db.users.find({emailLower: {$in: emails}});
while (cursor.hasNext()) {
var doc = cursor.next();
var criteria = {
purchaser: ObjectId(purchaserID),
recipient: doc._id,
service: service,
gems: gems,
amount: amount
}
if (db.payments.findOne(criteria)) {
log("Already have a payment for " + doc.email);
}
else {
db.payments.insert(criteria);
log("Added payment for " + doc.email);
}
}
function log(str) {
print(new Date().toISOString() + " " + str);
}