Skip to content

Commit

Permalink
add proper response page
Browse files Browse the repository at this point in the history
  • Loading branch information
chaosarium committed Nov 9, 2019
1 parent ea00429 commit deed90a
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 23 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ A manual-management free signup system utilising crytography
- [x] admin.ejs
- [x] cancel.ejs
- [x] help.ejs
- [ ] message.ejs
- [ ] closed.ejs
- [x] message.ejs
- [x] announcement system


Expand Down
42 changes: 24 additions & 18 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const url = "mongodb://localhost:27017"; //url to MongoDB
const dbName = "PractiseRoomSignup"; //database name to use
const mainCollectionName = "StudentRecords" //collection name to use for student signups

const openHour = 07 //the hour when signup opens; 0 <= openHour <= 23
const openHour = 20 //the hour when signup opens; 0 <= openHour <= 23
const openMin = 00 //the minute when signup opens; 0 <= openMin <= 59
const closeHour = 23 //the hour when signup closes; 0 <= closeHour <= 23
const closeMin = 05 //the minute when signup closes; 0 <= closeMin <= 59
Expand Down Expand Up @@ -321,7 +321,7 @@ app.listen(port, function(){
app.get("/", function(req, res){

if (checkReadStatus() == false) {
res.send("signup closed page")
res.render("response", {responseTitle: "Not Yet", responseMessage: "Sign-up opens between " + (openHour < 10 ? "0" : "") + openHour + ":" + (openMin < 10 ? "0" : "") + openMin + " to " + (closeHour < 10 ? "0" : "") + closeHour + ":" + (closeMin < 10 ? "0" : "") + closeMin + ", please come back later.", linkStatus: false, linkLocation: "/", linkText: "Home", backStatus: false, redirectDuration: 0, debugStatus: true})
}
else {
MongoClient.connect(url, function (err, db) {
Expand Down Expand Up @@ -409,26 +409,26 @@ app.post("/signup-req", function(req, res){

if (checkOpenStatus() == false) {
// if signup is open
res.send("sorry, signup closed")
res.render("response", {responseTitle: "Not Yet", responseMessage: "Sign-up opens between " + (openHour < 10 ? "0" : "") + openHour + ":" + (openMin < 10 ? "0" : "") + openMin + " to " + (closeHour < 10 ? "0" : "") + closeHour + ":" + (closeMin < 10 ? "0" : "") + closeMin + ", please come back later.", linkStatus: false, linkLocation: "/", linkText: "Home", backStatus: false, redirectDuration: 0, debugStatus: true})
}
else {
dbCheckStudent(fullName, studentID, function(callBackResult){
if (callBackResult == true){
// If student exists
res.send("you already signed up")
res.render("response", {responseTitle: "ERROR", responseMessage: "You have already signed up, please do not sign-up more than once in the same day.", linkStatus: true, linkLocation: "/", linkText: "Home", backStatus: false, redirectDuration: 0, debugStatus: true,})
}
else {
// If student does not exist, check occupation
dbCheckOccupation(room, time, function (callBackResult) {
if (callBackResult == true) {
// If room occupied
res.send("Ooooops, someone signed up for this very room at this very time when you were filling in the form")
res.render("response", {responseTitle: "Ooooops", responseMessage: "Someone, somewhere signed up for this very room at this very time when you were filling in the form, please sign-up for another room or another time.", linkStatus: true, linkLocation: "/", linkText: "Home", backStatus: false, redirectDuration: 0, debugStatus: true})
}
else {
// If room available, check password
if (SHALL24(fullName + studentID) !== password.toLowerCase() && crypto.createHmac('sha256', password).digest('hex') !== operationPasswordHash) {
// Wrong password
res.send("wrong name or password, please double check and try again")
res.render("response", {responseTitle: "Wrong Information", responseMessage: "Your name, student ID and your password do not match, please double check and try again.", linkStatus: false, linkLocation: "/", linkText: "Home", backStatus: true, redirectDuration: 0, debugStatus: true})
} else {
// Correct password, add to database
var remarkStatus = false;
Expand All @@ -443,7 +443,7 @@ app.post("/signup-req", function(req, res){

dbInsert(room, time, fullName, grade, studentID, ensembleStatus, remarkStatus, remark)

res.send("signup successful!")
res.render("response", {responseTitle: "Signup Successful!", responseMessage: "You will now be able to see your name on the sign-up table, please remember to come to your practise session. \n\nRedirecting in 3 seconds.", linkStatus: true, linkLocation: "/", linkText: "Home", backStatus: false, redirectDuration: 3000, debugStatus: false})
}
}
})
Expand All @@ -461,7 +461,7 @@ app.get("/cancel", function(req, res){
res.render("cancel", {fullName: "", studentID: ""});
} else {
//respond with error
res.send("you can only submit cancel requests when signup is open")
res.render("response", {responseTitle: "Not Now", responseMessage: "You can only submit cancel requests when sign-up is open.", linkStatus: true, linkLocation: "/", linkText: "Home", backStatus: false, redirectDuration: 0, debugStatus: true})
}

});
Expand All @@ -479,7 +479,7 @@ app.post("/cancel", function(req, res){
res.render("cancel", {fullName: fullName, studentID: studentID});
} else {
//respond with error
res.send("you can only submit cancel requests when signup is open")
res.render("response", {responseTitle: "Not Now", responseMessage: "You can only submit cancel requests when sign-up is open.", linkStatus: true, linkLocation: "/", linkText: "Home", backStatus: false, redirectDuration: 0, debugStatus: true})
}

});
Expand All @@ -494,28 +494,28 @@ app.post("/cancel-req", function(req, res){

if (checkOpenStatus() == false) {
// if signup is closed
res.send("you can only submit cancel requests when signup is open")
res.render("response", {responseTitle: "Not Now", responseMessage: "You can only submit cancel requests when sign-up is open.", linkStatus: true, linkLocation: "/", linkText: "Home", backStatus: false, redirectDuration: 0, debugStatus: true})
}
else {
dbCheckStudent(fullName, studentID, function (callBackResult) {
if (callBackResult == true) {
// If student exists, check password
if (SHALL24(fullName + studentID) !== password.toLowerCase() && crypto.createHmac('sha256', password).digest('hex') !== operationPasswordHash) {
// Wrong password
res.send("wrong name, student ID or password, please double check and try again")
res.render("response", {responseTitle: "Wrong Information", responseMessage: "The name, student ID and your password do not match, please double check and try again.", linkStatus: false, linkLocation: "/", linkText: "Home", backStatus: true, redirectDuration: 0, debugStatus: true})
} else {
// Correct password, remove entry

console.log("[cancel-req]removing from database: " + "|" + fullName + "|" + studentID + "|" + password + "|");

dbRemove(fullName, studentID);

res.send("Cancel successful!")
res.render("response", {responseTitle: "Cancel Successful!", responseMessage: "Removing your name from the sign-up table.", linkStatus: true, linkLocation: "/", linkText: "Home", backStatus: false, redirectDuration: 0, debugStatus: false})
}
}
else {
// If student does not exists
res.send("this student have not signed up, check the name and password you entered")
res.render("response", {responseTitle: "ERROR", responseMessage: "This student has not signed up, please check the information you entered.", linkStatus: false, linkLocation: "/", linkText: "Home", backStatus: true, redirectDuration: 0, debugStatus: true})
}
})

Expand Down Expand Up @@ -545,10 +545,10 @@ app.post("/password-req", function(req, res){
var password = req.body.password

if (crypto.createHmac('sha256', password).digest('hex') !== operationPasswordHash) {
res.send("Wrong admin password, don't even try to break the system")
res.render("response", {responseTitle: "ERROR", responseMessage: "Wrong admin password, don't try to break into the system.", linkStatus: false, linkLocation: "/", linkText: "Home", backStatus: true, redirectDuration: 0, debugStatus: true})
} else {
var lookupPassword = SHALL24(fullName + studentID);
res.send("The student's password is " + lookupPassword + ", please stop using forgetting password as an excuse to not practise!")
res.render("response", {responseTitle: "Password Lookup", responseMessage: "The student's password is " + lookupPassword + ", please stop using FORGETTING PASSWORD as an excuse of NOT PRACTISING!", linkStatus: false, linkLocation: "/", linkText: "Home", backStatus: true, redirectDuration: 0, debugStatus: false})
}

});
Expand All @@ -561,12 +561,12 @@ app.post("/announcement-req", function(req, res){
var password = req.body.password

if (crypto.createHmac('sha256', password).digest('hex') !== operationPasswordHash) {
res.send("Wrong admin password, don't even try to break the system")
res.render("response", {responseTitle: "ERROR", responseMessage: "Wrong admin password, don't try to break into the system.", linkStatus: false, linkLocation: "/", linkText: "Home", backStatus: true, redirectDuration: 0, debugStatus: true})
} else {
fs.writeFile("announcement.txt", announcement, function (err) {
console.log("Announcement modified!");
});
res.send("Announcement midified successfully!")
res.render("response", {responseTitle: "Success", responseMessage: "The announcement has been modified successfully!", linkStatus: true, linkLocation: "/", linkText: "Home", backStatus: false, redirectDuration: 0, debugStatus: false})
}

});
Expand Down Expand Up @@ -688,4 +688,10 @@ app.post("/debug-req", function(req, res){

// fs.writeFile("announcement.txt", "(announcement)", function(err) {
// console.log("Announcement modified!");
// });
// });


//Response page test
// app.get("/response-test", function(req, res){
// res.render("response", {responseTitle: "title", responseMessage: "message \n2nd paragraph", linkStatus: true, linkLocation: "/help", linkText: "haha", backStatus: true, redirectDuration: 0})
// });
6 changes: 3 additions & 3 deletions views/admin.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,12 @@
</form>

<h4 class="mt-5">New announcement</h4>
<p>Type the announcement here, this will replace the old announcement on the home page.</p>
<p>Type the announcement here, this will replace the old announcement on the home page. To remove existing announcement, leave the "New Announcement" box blank.</p>

<form method="POST" action="/announcement-req">

<div class="form-group">
<textarea id="announcement" id="announcement" name="announcement" required style="height: 150px;"></textarea>
<textarea id="announcement" id="announcement" name="announcement" style="height: 150px;" required></textarea>
<label for="textarea" class="control-label">New Announcement</label><i class="bar"></i>
</div>

Expand All @@ -56,7 +56,7 @@
</div>

<a class="btn btn-primary" href="/">Back</a>
<input class="btn btn-primary" type="submit"></input>
<input class="btn btn-primary" onclick="document.getElementById('announcement').required = false" type="submit"></input>

</form>

Expand Down
63 changes: 63 additions & 0 deletions views/response.ejs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Practise Room Sign-up</title>
<link rel="icon" href="/favicons/Image002.png">
<link rel="stylesheet" href="/css/bootstrap.css">
<link rel="stylesheet" href="/css/style.css">
</head>
<body>

<div class="container">
<h1 class="mt-4">Practise Room Sign-up <sup>beta</sup></h1>
<h3 class="mt-5"><%= responseTitle %></h3>
<p class="" style="white-space: pre-wrap;"><%= responseMessage %></p>

<% if (debugStatus) { %>
<p>If you think this is a bug, please report to <a href="https://github.com/leonluleonlu/PractiseRoomSignup/issues">github.com/leonluleonlu/PractiseRoomSignup/issues</a> or email 2220056@ncpachina.org</p>
<% } %>

<% if (linkStatus) { %>
<a class="btn btn-primary" href="<%= linkLocation %>"><%= linkText %></a>
<% } %>

<% if (backStatus) { %>
<button class="btn btn-primary" onclick="window.history.back()">Back</button>
<% } %>

<h3 class="mt-5">Debug info:</h3>
<p><small class="text-muted">from response.ejs</small></p>
<p><small>Data = <%= responseTitle %></small></p>
<p><small>Data = <%= responseMessage %></small></p>
<p><small>Data = <%= linkStatus %></small></p>
<p><small>Data = <%= linkLocation %></small></p>
<p><small>Data = <%= linkText %></small></p>
<p><small>Data = <%= backStatus %></small></p>
<p><small>Data = <%= redirectDuration %></small></p>


<% if (redirectDuration > 0) { %>
<script type="text/javascript">
function goBack() {
window.history.back();
}
setTimeout(function(){ goBack() }, <%= redirectDuration %>);
</script>
<% } %>


</div>


<!-- JQuery -->
<script type="text/javascript" src="/js/jquery-3.4.0.min.js"></script>
<!-- Bootstrap tooltips -->
<script type="text/javascript" src="/js/popper.min.js"></script>
<!-- Bootstrap core JavaScript -->
<script type="text/javascript" src="/js/bootstrap.js"></script>
<script src="/js/main.js"></script>
</body>
</html>

0 comments on commit deed90a

Please sign in to comment.