-
Notifications
You must be signed in to change notification settings - Fork 0
/
invitations.js
81 lines (77 loc) · 2.12 KB
/
invitations.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
var db = require('./db'),
constants = require('./constants'),
util = require('./util'),
mkrespcb = util.mkrespcb;
var m = module.exports = {};
var isActive;
setTimeout(function() {
db.SystemParam.findOne({
key: constants.SystemParamKeys.globalInvitations,
'value.active': true
}, function(err, value) {
isActive = (!err || err === '') && value;
});
}, 1000);
/**
* Returns true iff the system is currently limiting the number of invitations allowed.
* Updates only with server restart.
*/
m.isLimitActive = function isLimitActive() {
return isActive;
};
/**
* Calls the given callback(err, res) with an object containing the global invitation limit,
* and the remaining number of invitations.
* Example:
* {
* limit: 32,
* remaining: 13
* }
*/
m.getInvitationStatus = function getInvitationStatus(callback) {
db.SystemParam.findOne({
key: constants.SystemParamKeys.globalInvitations
}, callback);
};
m.getInvitationStatusResource = function getInvitationStatusResource(req, res) {
return m.getInvitationStatus(mkrespcb(res, 400, function(param) {
res.json(param.value);
}));
};
/**
* Verifies that there are enough remaining invitations, and if so decrements the remaining
* invitations by the given value and calls the given callback(bool) with true value.
* Otherwise, calls the given callback with false value.
*/
m.verifyAndDecrement = function verifyAndDecrement(valueToDecrement, callback) {
db.SystemParam.findAndModify({
key: constants.SystemParamKeys.globalInvitations,
'value.remaining': {
$gte: valueToDecrement
}
}, null, {
$inc: {
'value.remaining': -valueToDecrement
}
}, function(err, invitationStatus) {
callback((!err || err === '') && invitationStatus);
});
};
m.updateInvitationLimit = function updateInvitationLimit(req, res) {
var newLimit = parseInt(req.param('limit'));
db.SystemParam.update({
key: constants.SystemParamKeys.globalInvitations
}, {
key: constants.SystemParamKeys.globalInvitations,
value: {
limit: newLimit,
remaining: newLimit,
active: true
}
}, {
upsert: true
}, mkrespcb(res, 400, function() {
isActive = true;
res.json('success');
}));
};