Better management of the (un)subscription process

This commit is contained in:
quenenni 2013-04-03 00:30:11 +02:00
parent 335a0b065f
commit 787b9157be

View file

@ -15,19 +15,22 @@ exports.registerRoute = function (hook_name, args, callback) {
var action = param[0]; var action = param[0];
var actionId = param[1]; var actionId = param[1];
var padURL = req.protocol + "://" + req.get('host') + "/p/" +padId; var padURL = req.protocol + "://" + req.get('host') + "/p/" +padId;
var resultDb = {};
async.series( async.waterfall(
[ [
function(cb) { function(cb) {
// Is the (un)subscription valid (exists & not older than 24h) // Is the (un)subscription valid (exists & not older than 24h)
db.get("emailSubscription:"+padId, function(err, userIds){ db.get("emailSubscription:"+padId, function(err, userIds){
var foundInDb = false; var foundInDb = false;
var timeDiffGood = false; var timeDiffGood = false;
var email = "your email"; var email = "your email";
var resultDb = {
"foundInDb": foundInDb,
"timeDiffGood": timeDiffGood,
"email": email
}
if(userIds && userIds['pending'] && userIds['pending'].length > 0){ if(userIds && userIds['pending']){
async.forEach(Object.keys(userIds['pending']), function(user){ async.forEach(Object.keys(userIds['pending']), function(user){
var userInfo = userIds['pending'][user]; var userInfo = userIds['pending'][user];
@ -68,42 +71,42 @@ exports.registerRoute = function (hook_name, args, callback) {
padId padId
); );
} }
}
},
function(err, msg){
// There should be something in here!
console.error("Error in emailSubscription async in first function", err, " -> ", msg);
}); // end async for each
}
resultDb = { resultDb = {
"foundInDb": foundInDb, "foundInDb": foundInDb,
"timeDiffGood": timeDiffGood, "timeDiffGood": timeDiffGood,
"email": email "email": user
} }
}
},
cb(null, 1); function(err, msg){
if (err != null) {
console.error("Error in async.forEach", err, " -> ", msg);
}
}); // end async for each
}
cb(null, resultDb);
}); });
}, },
function(cb) { function(resultDb, cb) {
// Create and send the output message // Create and send the output message
sendContent(res, args, action, padId, padURL, resultDb); sendContent(res, args, action, padId, padURL, resultDb);
cb(null, resultDb);
cb(null, 2);
}, },
function(cb) { function(resultDb, cb) {
// Take a moment to clean all obsolete pending data // Take a moment to clean all obsolete pending data
cleanPendingData(padId); cleanPendingData(padId);
cb(null, resultDb);
cb(null, 3);
} }
], ],
function(err, results){ function(err, results){
if (err != null) {
console.error("Callback async.series: Err -> ", err, " / results -> ", results); console.error("Callback async.series: Err -> ", err, " / results -> ", results);
} }
}
); );
}); });