-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
71 lines (67 loc) · 1.88 KB
/
Copy pathserver.js
File metadata and controls
71 lines (67 loc) · 1.88 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
require("dotenv").config();
const express = require("express");
const { redirect } = require("express/lib/response");
const notion = require("./notion.js");
const md5 = require("md5");
const app = express();
app.set("view engine", "ejs");
app.use(express.static("./public"));
app.use(express.urlencoded({extended: true}));
let status = [], tasks = [], courses = {};
function init(){
notion.getStatus().then((data) => (status = data));
notion.getTasks().then((data) => (tasks = data));
notion.getCoursesID_Name().then((data) => (courses = data));
}
async function init_async() {
await Promise.all([
notion.getStatus(),
notion.getTasks(),
notion.getCoursesID_Name(),
]).then(values => {
status = values[0];
tasks = values[1];
courses = values[2];
});
}
init();
setInterval(() => {
init();
}, 1000 * 60 * 15);
app.get("/", (req, res) => {
res.render('index', {status, tasks, courses});
})
app.get("/sync", async (req, res) => {
await init_async();
res.redirect("/");
})
app.post("/create", async (req, res) => {
const data = req.body;
let flag1 = false;
status.forEach(_status => {
if(_status.name == data.status) flag1 = true;
})
if(md5(data.password) === process.env.PASSWORD && flag1 && courses.hasOwnProperty(data.courseID) && data.dateStart != ''){
if (data.title == '') data.title = 'Untitled';
if (data.dateEnd == '') data.dateEnd = null;
await notion.createTask({
name: data.title,
course: data.courseID,
status: data.status,
dateStart: data.dateStart,
dateEnd: data.dateEnd,
});
await init_async();
res.redirect("/");
}
else if(!courses.hasOwnProperty(data.courseID)){
res.status(403).send("Missing course");
}
else if(!data.dateStart){
res.status(403).send("Missing start date");
}
else{
res.status(403).send("Xin đừng hack");
}
});
app.listen(process.env.PORT || 5000);