-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathservices.js
62 lines (53 loc) · 1.45 KB
/
services.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
var Db = require('mongodb').Db,
Connection = require('mongodb').Connection,
Server = require('mongodb').Server;
module.exports = function() {
var isAuthed = false;
var db = new Db('knockoutradio', new Server('staff.mongohq.com', 10002, {auto_reconnect: true}), {native_parser:false});
function auth(cb){
if(isAuthed) {
cb();
return;
}
db.open(function(err, db) {
db.authenticate('demandnode', 'demandNode2011', cb);
isAuthed = true;
});
}
return {
getPoints: function(user_id, cb) {
var self = this;
auth( function() {
db.collection('points', function(err, col) {
if(err) console.log(err);
var cursor = col.find({
user_id: user_id
});
cursor.toArray(function(err, docs) {
if(docs.length == 0 || docs[0].points <= 0) {
self.adjustPoints(user_id, 100);
cb(100);
} else {
cb(docs[0].points);
}
});
})
})
},
adjustPoints: function(user_id, points, cb) {
if(typeof cb !== "function") var cb = function(){};
auth(function(){
db.collection('points', function(err, col) {
if(err) console.log(err);
col.update({
user_id: user_id
}, {
$inc: {
points: points
}
}, {upsert: true}, cb);
})
})
}
};
}