time based on periods rather than timestamps

This commit is contained in:
2026-03-19 17:43:00 +00:00
parent 6d5807befc
commit d15980bb52
7 changed files with 102 additions and 35 deletions

View File

@@ -5,8 +5,38 @@ import bcrypt from "bcrypt";
import Database from "better-sqlite3";
Date.prototype.addHours= function(h){
this.setTime(this.getTime() + (h*60*60*1000));
return this;
this.setTime(this.getTime() + (h*60*60*1000));
return this;
}
function getPeriod() {
const now = new Date();
let hours = now.getHours();
let minutes = now.getMinutes();
if ((hours == 8) || (hours == 9 && minutes < 45)) {
// P1
return 1;
}
if ((hours == 9 && minutes >= 45) || (hours < 11 && minutes < 5)) {
// P2 / Break
return 2;
}
if ((hours == 11 && minutes >= 5) || (hours == 12 && minutes < 5)) {
// P3
return 3;
}
if ((hours == 12 && minutes >= 5) || (hours == 13 && minutes < 5)) {
// P4
return 4;
}
if ((hours == 13 && minutes >= 5) || (hours == 15 && minutes < 15)) {
// Lunch to P5
return 5;
} else {
// too early / late
return -1;
}
}
const app = e();
@@ -21,24 +51,35 @@ app.get('/', (req, res) => {
res.send("Nothing Here");
});
app.get('/currentRooms', (req, res) => {
// req has no data
app.get('/getPeriod', (req, res) => {
// req has nothing
// res has current period
res.status(200).json({period: getPeriod()});
});
app.post('/currentRooms', (req, res) => {
// req has day
// res has all room at current time
console.log("currentRooms");
const today = new Date();
let stmt = db.prepare(`SELECT * FROM TimeSlots INNER JOIN Rooms ON Rooms.Id=TimeSlots.Room WHERE TimeEnd BETWEEN '${today.toISOString()}' AND '${today.addHours(1).toISOString()}';`);
const currentPeriod = getPeriod();
if (currentPeriod == -1) {
res.status(418).send("Not in school");
return;
}
const today = req.body.day;
let stmt = db.prepare(`SELECT * FROM TimeSlots INNER JOIN Rooms ON Rooms.Id=TimeSlots.Room WHERE Period BETWEEN ${currentPeriod - 1} AND ${currentPeriod + 1} AND Day=${today};`);
let records = stmt.all();
res.status(200).json({records: records});
});
app.post('/addTimeSlot', (req, res) => {
// req has roomid userid starttime and end time
// req has roomid userid period and day
// res has success or faliure
console.log("addTimeSlot");
const body = req.body;
let roomid = parseInt(body.roomid);
let userid = parseInt(body.userid);
let stmt = db.prepare(`INSERT INTO TimeSlots (TimeStart, TimeEnd, Room) VALUES ('${body.startTime}', '${body.endTime}', ${roomid});`);
let stmt = db.prepare(`INSERT INTO TimeSlots (Period, Day, Room) VALUES (${body.period}, ${body.day}, ${roomid});`);
stmt.run();
stmt = db.prepare(`UPDATE Users SET Submissions = Submissions + 1 WHERE Id=${userid};`);
stmt.run();
@@ -54,7 +95,7 @@ app.post('/addRoom', (req, res) => {
let stmt = db.prepare(`SELECT * FROM Rooms WHERE RoomName='${name}'`);
let storedRecord = stmt.get();
if (storedRecord) {
res.status(400).send("room already exists");
res.status(418).send("room already exists");
return;
}
stmt = db.prepare(`INSERT INTO Rooms (RoomName) VALUES (${name})`);
@@ -81,7 +122,7 @@ app.post('/createUser', async (req, res) => {
let stmt = db.prepare(`SELECT * FROM Users WHERE Email='${body.email}';`);
let storedRecord = stmt.get();
if (storedRecord) {
res.status(400).send("account with that email already exists");
res.status(418).send("account with that email already exists");
return;
}
const generatedHash = await bcrypt.hash(body.pass, 10);
@@ -100,7 +141,7 @@ app.post('/login', async (req, res) => {
let stmt = db.prepare(`SELECT Pass FROM Users WHERE Email='${body.email}';`);
let storedHash = stmt.get();
if (!storedHash) {
res.status(400).send("problem with email");
res.status(418).send("problem with email");
return;
}
if (await bcrypt.compare(body.pass, storedHash.Pass)){
@@ -108,7 +149,7 @@ app.post('/login', async (req, res) => {
const uid = stmt.get();
res.status(200).send({uid: uid});
} else {
res.status(400).send("incorrect password");
res.status(418).send("incorrect password");
}
});