forked from sclorg/nodejs-ex
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheckinModule.js
More file actions
149 lines (137 loc) · 4.46 KB
/
Copy pathcheckinModule.js
File metadata and controls
149 lines (137 loc) · 4.46 KB
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
function checkinModule(){
var that = this;
var database = require('./database');
/**
* Retrieves a user-venue pair checkin defined by the venue id and user id from the database and executes the callback function on it
*/
that.findCheckin = function(venue, user, callback){
database.connection.query("SELECT *,UNIX_TIMESTAMP(timestamp) AS time FROM user_checkin_venue WHERE venueID=? AND userID=?", [venue, user], function(err, rows, fields){
if(!err){
for(var i=0; i<rows.length; i++){
var checkin = {
user: rows[i].userID,
venue: rows[i].venueID,
checkins: rows[i].checkin_count,
time: rows[i].time * 1000
}
callback(checkin);
return;
}
} else {
console.log("Error querying DB for checkin");
}
callback(null);
});
};
/**
* Retrieves all user-venue pair checkins for a venue defined by its id from the database, ordered descending by the checkin counters, and executes the callback function on them
*/
that.findCheckinsByVenue = function(venueId, callback){
database.connection.query("SELECT * FROM user_checkin_venue LEFT JOIN users ON (userID = id) WHERE venueID=? ORDER BY checkin_count DESC", [venueId], function(err, rows, fields){
if(!err){
var checkins = [];
for(var i=0; i<rows.length; i++){
var checkin = {
user: rows[i].userID,
venue: rows[i].venueID,
name: rows[i].username,
visits: rows[i].checkin_count
}
checkins.push(checkin);
}
callback(checkins);
return;
} else {
console.log("Error querying DB for venue checkins");
}
callback(null);
});
};
/**
* Retrieves all user-venue pair checkins and the sum of the checkin counters of a user from the database and executes the callback function on them
*/
that.findCheckinsByUser = function(userId, callback){
database.connection.query("SELECT *,SUM(checkin_count) AS count FROM user_checkin_venue WHERE userID=?", [userId], function(err, rows, fields){
if(!err){
var checkins = [];
for(var i=0; i<rows.length; i++){
var checkin = {
user: rows[i].userID,
venue: rows[i].venueID,
checkins: rows[i].checkin_count,
count: rows[i].count
}
checkins.push(checkin);
}
callback(checkins);
return;
} else {
console.log("Error querying DB for user checkins");
}
callback(null);
});
};
/**
* Inserts a checkin for a user-venue pair into the database and executes the callback function with status "OK" if successful
*/
that.createCheckin = function(venue, user, callback){
database.connection.query("INSERT INTO user_checkin_venue SET userID=?, venueID=?, checkin_count=?",
[user, venue, 1], function(err, result){
if(!err){
console.log("Inserted checkin into DB");
callback("OK");
} else {
console.log("Error trying to insert checkin into database");
callback(null);
}
});
};
/**
* Updates (increments) the checkin counter for a user-venue pair and executes the callback function with status "OK" if successful
*/
that.updateCheckinCounter = function(venue, user, callback){
database.connection.query("UPDATE user_checkin_venue SET checkin_count=checkin_count+1 WHERE venueID=? AND userID=?", [venue, user], function(err, result){
if(!err){
console.log("Updated checkin count for user "+user);
callback("OK");
} else {
console.log("Error updating checkin count");
callback(null);
}
});
};
/**
* Removes a user-venue pair checkin from the database and executes the callback function with status "OK" if successful
*/
that.removeCheckin = function(venue, user, callback){
database.connection.query("DELETE FROM user_checkin_venue WHERE id=?", [venue, user], function(err, rows, fields){
if(!err){
console.log("Deleted checkin of user" + user);
callback("OK");
return;
} else {
console.log("Error deleting checkin from DB");
}
callback(null);
});
};
/**
* Attempts a user checkin into a venue
* Successful, if the user has not checked in into the venue so far or the last checkin is more than 4 hours ago
*/
that.checkIn = function(venue, user, callback){
that.findCheckin(venue, user, function(checkin){
if(!checkin){
that.createCheckin(venue, user, callback);
}else{
var d = new Date();
var t = d.getTime();
if(t - checkin.time >= 1000 * 60 * 60 * 4) // 4 hours between checkins
that.updateCheckinCounter(venue, user, callback);
else
callback(null);
}
});
};
}
module.exports = new checkinModule();