Created
January 25, 2016 17:39
-
-
Save jpopesculian/34489ef905719ad78dec to your computer and use it in GitHub Desktop.
Some scripts for Stripe and RocketChat diffs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const Stripe = require('stripe') | |
const MongoClient = require('mongodb').MongoClient | |
const diff = require('deep-diff').diff | |
const assert = require('assert') | |
const STRIPE_KEY = process.env.STRIPE_KEY | |
const MONGO_URL = process.env.MONGO_URL | |
MongoClient.connect(MONGO_URL, (err, db) => { | |
assert.equal(err, null) | |
db.collection('rocketchat_payment_subscriptions') | |
.find().toArray((err, subscriptions) => { | |
assert.equal(err, null) | |
// logDiffs(subscriptions) | |
updateStatuses(subscriptions) | |
}) | |
}) | |
function updateStatuses(subscriptions) { | |
var checked = new Array(subscriptions.length) | |
subscriptions.forEach((subscription, index) => { | |
const subscriptionName = `Subscription {_id: ${subscription._id}, user: ${subscription.user}}` | |
console.log(`Checking ${subscriptionName}`) | |
getStatusFromStripe(subscription, (err, status) => { | |
if (err) { | |
return console.error(`${subscriptionName} could not be found on stripe! (${err.type})`) | |
} | |
if (subscription.status != status && subscription.user) { | |
console.log(`Updating ${subscriptionName} from ${subscription.status} to ${status}`) | |
updateStatus(subscription, (err, result) => { | |
if (err) { | |
return console.error(`${subscriptionName} could not be updated!`) | |
} | |
checked[index] = true | |
// allTrue(checked, () => process.exit()) | |
}) | |
} | |
}) | |
}) | |
} | |
function updateStatus(subscription, callback) { | |
// @TODO: Implement this shit | |
callback(null, null) | |
} | |
function getStatusFromStripe(chat_sub, callback) { | |
const stripe = Stripe(STRIPE_KEY) | |
stripe.customers.retrieveSubscription( | |
chat_sub.customer, | |
chat_sub.subscriptionId, | |
(err, stripe_sub) => { | |
if (err) { | |
if (err.type == 'StripeInvalidRequestError') { | |
return callback(null, "canceled") | |
} | |
return callback(err) | |
} | |
return callback(null, stripe_sub.status) | |
} | |
) | |
} | |
function logDiffs(subscriptions) { | |
var checked = new Array(subscriptions.length) | |
for (var index in subscriptions) { | |
var subscription = subscriptions[index] | |
var onDiff = ((checkedIndex) => (err, diffs) => { | |
if (diffs) { | |
logDiffs(diffs, subscription) | |
} | |
checked[checkedIndex] = true | |
// allTrue(checked, () => process.exit()) | |
})(index) | |
diffWithStripe(subscription, onDiff) | |
} | |
} | |
function diffWithStripe(chat_sub, callback) { | |
const stripe = Stripe(STRIPE_KEY) | |
const diffFilter = (path, key) => { | |
if (path.length == 0) { | |
switch(key) { | |
case '_id': | |
case 'id': | |
case 'subscriptionId': | |
case 'user': | |
case 'createdAt': | |
case 'updatedAt': | |
return true | |
} | |
} else { | |
switch(path[0]) { | |
case 'discount': | |
return true | |
} | |
} | |
return false | |
} | |
stripe.customers.retrieveSubscription( | |
chat_sub.customer, | |
chat_sub.subscriptionId, | |
(err, stripe_sub) => { | |
if (err) { | |
callback(err) | |
} | |
var diffs = diff(chat_sub, stripe_sub, diffFilter) | |
callback(null, diffs) | |
} | |
) | |
} | |
function logDiffs(diffs, subscription) { | |
console.log("======= DIFFERENCES FOUND =========") | |
console.log(subscription) | |
console.log("-----------------------------------") | |
console.log(diffs) | |
console.log("-----------------------------------\n") | |
} | |
function allTrue(checked, onTrue, onFalse) { | |
for (check of checked) { | |
if (!check) return onFalse() | |
} | |
return onTrue() | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "rocketchat-diff", | |
"version": "1.0.0", | |
"description": "", | |
"main": "index.js", | |
"scripts": { | |
"test": "echo \"Error: no test specified\" && exit 1" | |
}, | |
"author": "", | |
"license": "ISC", | |
"dependencies": { | |
"deep-diff": "^0.3.3", | |
"mongodb": "^2.1.4", | |
"stripe": "^4.2.0" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment