Skip to content

Commit

Permalink
clean up db
Browse files Browse the repository at this point in the history
cleaned up db from hour long sessions, stopped duplication of session ids
  • Loading branch information
Devlin Leonard committed Apr 17, 2024
1 parent fa878fa commit 1fe9a6b
Showing 1 changed file with 38 additions and 4 deletions.
42 changes: 38 additions & 4 deletions receiptifyv1/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,25 @@ var generateSessionID = function () {
for (var i = 0; i < 6; i++) {
text += possible.charAt(Math.floor(Math.random() * possible.length));
}
const fileStream = fs.createReadStream("users.csv");
const rl = readline.createInterface({
input: fileStream,
crlfDelay: Infinity,
});
rl.on('line', (line) => {
const row = line.split(',');
if (row[2] == text) {
text = generateSessionID();
}
});
/*
for await (const line of rl) {
const row = line.split(',');
if (row[2] == text) {
text = generateSessionID();
}
}
*/
return text;
};

Expand Down Expand Up @@ -210,12 +229,29 @@ async function processFile(filePath, sessionID) {
crlfDelay: Infinity,
});
const users = [];
var newDB = "";
var first = true;
for await (const line of rl) {
if(first){
newDB += line + "\n";
first = false;
continue;
}
const row = line.split(',');
if (row[2] == sessionID) {
users.push(row[0]);
}
if((row[3] - 1 + 3600000 > Date.now())){
newDB += line + "\n";
};
}
fs.writeFile("users.csv", newDB, err => {
if (err) {
console.log(err.message);

throw err;
}
});
await rl.close();

return users;
Expand Down Expand Up @@ -281,10 +317,8 @@ app.get('/callback', function (req, res) {
);

// Gets time (year-month-day hour:min:sec)
var currentDate = new Date();
var access_time = currentDate.getFullYear() + '-' + (currentDate.getMonth()+1) + '-' + currentDate.getDate() + ' ' +
currentDate.getHours() + ':' + currentDate.getMinutes() + ':' + currentDate.getSeconds();
console.log("Token Access Time: ", access_time);
console.log(Date.now());
var access_time = Date.now();

// Writing to users.csv (Database)
fs.appendFile('users.csv', ('\n'+ profile.display_name + ',' + access_token +',' + sessionID + ',' + access_time + ','), (err) => {
Expand Down

0 comments on commit 1fe9a6b

Please sign in to comment.