Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

clean up db and no duplicate ids #30

Merged
merged 1 commit into from
Apr 17, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading