Skip to content

Instantly share code, notes, and snippets.

@feltnerm
Created October 29, 2014 15:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save feltnerm/1fb28e6d26bbdeaf1703 to your computer and use it in GitHub Desktop.
Save feltnerm/1fb28e6d26bbdeaf1703 to your computer and use it in GitHub Desktop.
// third-party
var mysql_escape = require('mysql').escape;
// local
var Database = require('./db');
var db = Database();
// Here we select the most recent transaction for the licenseholder
// later on we just check to see if their purchase date was within the
// past year (365) days
module.exports.get_most_recent_transaction = function(email) {
var query = "SELECT *, " +
"Datediff(CURRENT_DATE, tr.date) AS time_period " +
"FROM licenseholders AS lh " +
"LEFT OUTER JOIN transactions AS tr " +
"ON lh.email = tr.licenseholder " +
"WHERE lh.email = " + mysql_escape(email) + " " +
"ORDER BY TIME_period ASC " +
"LIMIT 1; ";
return new Promise(function(resolve, reject) {
db.query(query).then(function(licenseholders) {
if (licenseholders && licenseholders.length >= 1) {
var row = licenseholders.shift();
if (row && ((row.time_period !== null && 0 <= row.time_period < 365) || row.TRIAL === 1)) {
resolve(row);
}
reject('renew', email);
}
reject('email', email);
});
})
};
module.exports.insert_custom_build = function(build_data){
return new Promise(function(resolve, reject) {
db.insert('custom_builds', build_data).then(function (data) {
resolve(data);
}, function(err){
reject('db')
});
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment